Skip to content

Commit

Permalink
♻️ Refactor the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
garlontas committed Dec 30, 2023
1 parent 7402296 commit 151434d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 24 deletions.
38 changes: 20 additions & 18 deletions tests/test_csv_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@
from unittest import TestCase
from unittest.mock import patch, mock_open

from file_test import OPEN, PATH_EXISTS, PATH_ISFILE
from pystreamapi.loaders import csv

file_content = """
attr1,attr2
1,2.0
a,b
"""
file_path = 'path/to/data.csv'


class TestCSVLoader(TestCase):

def test_csv_loader(self):
with (patch('builtins.open', mock_open(read_data=file_content)),
patch('os.path.exists', return_value=True),
patch('os.path.isfile', return_value=True)):
data = csv('path/to/data.csv')
with (patch(OPEN, mock_open(read_data=file_content)),
patch(PATH_EXISTS, return_value=True),
patch(PATH_ISFILE, return_value=True)):
data = csv(file_path)
self.assertEqual(len(data), 2)
self.assertEqual(data[0].attr1, 1)
self.assertIsInstance(data[0].attr1, int)
Expand All @@ -27,9 +29,9 @@ def test_csv_loader(self):
self.assertIsInstance(data[1].attr1, str)

def test_csv_loader_with_casting_disabled(self):
with (patch('builtins.open', mock_open(read_data=file_content)),
patch('os.path.exists', return_value=True),
patch('os.path.isfile', return_value=True)):
with (patch(OPEN, mock_open(read_data=file_content)),
patch(PATH_EXISTS, return_value=True),
patch(PATH_ISFILE, return_value=True)):
data = csv('path/to/data.csv', cast_types=False)
self.assertEqual(len(data), 2)
self.assertEqual(data[0].attr1, '1')
Expand All @@ -40,26 +42,26 @@ def test_csv_loader_with_casting_disabled(self):
self.assertIsInstance(data[1].attr1, str)

def test_csv_loader_is_iterable(self):
with (patch('builtins.open', mock_open(read_data=file_content)),
patch('os.path.exists', return_value=True),
patch('os.path.isfile', return_value=True)):
data = csv('path/to/data.csv')
with (patch(OPEN, mock_open(read_data=file_content)),
patch(PATH_EXISTS, return_value=True),
patch(PATH_ISFILE, return_value=True)):
data = csv(file_path)
self.assertEqual(len(list(iter(data))), 2)

def test_csv_loader_with_custom_delimiter(self):
with (patch('builtins.open', mock_open(read_data=file_content.replace(",", ";"))),
patch('os.path.exists', return_value=True),
patch('os.path.isfile', return_value=True)):
with (patch(OPEN, mock_open(read_data=file_content.replace(",", ";"))),
patch(PATH_EXISTS, return_value=True),
patch(PATH_ISFILE, return_value=True)):
data = csv('path/to/data.csv', delimiter=';')
self.assertEqual(len(data), 2)
self.assertEqual(data[0].attr1, 1)
self.assertIsInstance(data[0].attr1, int)

def test_csv_loader_with_empty_file(self):
with (patch('builtins.open', mock_open(read_data="")),
patch('os.path.exists', return_value=True),
patch('os.path.isfile', return_value=True)):
data = csv('path/to/data.csv')
with (patch(OPEN, mock_open(read_data="")),
patch(PATH_EXISTS, return_value=True),
patch(PATH_ISFILE, return_value=True)):
data = csv(file_path)
self.assertEqual(len(data), 0)

def test_csv_loader_with_invalid_path(self):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_error_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def test_remove_sentinels_no_sentinels(self):
def test_sentinel_eq(self):
s1 = Sentinel()
s2 = Sentinel()
self.assertTrue(s1 == s2)
self.assertEqual(s1, s2)

def test_sentinel_eq_false(self):
s1 = Sentinel()
Expand All @@ -118,7 +118,7 @@ def test_sentinel_eq_false(self):
def test_sentinel_ne(self):
s1 = Sentinel()
s2 = object()
self.assertTrue(s1 != s2)
self.assertNotEqual(s1, s2)

def test_sentinel_ne_false(self):
s1 = Sentinel()
Expand Down
8 changes: 4 additions & 4 deletions tests/test_json_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
}
]
"""

file_path = 'path/to/data.json'

class TestJsonLoader(TestCase):

def test_json_loader_from_file(self):
with (patch(OPEN, mock_open(read_data=file_content)),
patch(PATH_EXISTS, return_value=True),
patch(PATH_ISFILE, return_value=True)):
data = json('path/to/data.json')
data = json(file_path)
self.assertEqual(len(data), 2)
self.assertEqual(data[0].attr1, 1)
self.assertIsInstance(data[0].attr1, int)
Expand All @@ -39,14 +39,14 @@ def test_json_loader_is_iterable(self):
with (patch(OPEN, mock_open(read_data=file_content)),
patch(PATH_EXISTS, return_value=True),
patch(PATH_ISFILE, return_value=True)):
data = json('path/to/data.json')
data = json(file_path)
self.assertEqual(len(list(iter(data))), 2)

def test_json_loader_with_empty_file(self):
with (patch(OPEN, mock_open(read_data="")),
patch(PATH_EXISTS, return_value=True),
patch(PATH_ISFILE, return_value=True)):
data = json('path/to/data.json')
data = json(file_path)
self.assertEqual(len(data), 0)

def test_json_loader_with_invalid_path(self):
Expand Down

0 comments on commit 151434d

Please sign in to comment.