Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️ Refactor the tests #81

Merged
merged 3 commits into from
Dec 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 22 additions & 20 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,10 +29,10 @@ 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)):
data = csv('path/to/data.csv', cast_types=False)
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, cast_types=False)
self.assertEqual(len(data), 2)
self.assertEqual(data[0].attr1, '1')
self.assertIsInstance(data[0].attr1, str)
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)):
data = csv('path/to/data.csv', delimiter=';')
with (patch(OPEN, mock_open(read_data=file_content.replace(",", ";"))),
patch(PATH_EXISTS, return_value=True),
patch(PATH_ISFILE, return_value=True)):
data = csv(file_path, 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
10 changes: 5 additions & 5 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 All @@ -69,4 +69,4 @@ def test_json_loader_from_string(self):

def test_json_loader_from_empty_string(self):
with self.assertRaises(JSONDecodeError):
self.assertEqual(len(json('', read_from_src=True)), 0)
len(json('', read_from_src=True))
Loading