From 151434de3442b5599d78ee033237c68b3ffa24b7 Mon Sep 17 00:00:00 2001 From: Stefan Garlonta Date: Sat, 30 Dec 2023 13:10:43 +0100 Subject: [PATCH] :recycle: Refactor the tests --- tests/test_csv_loader.py | 38 +++++++++++++++++++------------------ tests/test_error_handler.py | 4 ++-- tests/test_json_loader.py | 8 ++++---- 3 files changed, 26 insertions(+), 24 deletions(-) diff --git a/tests/test_csv_loader.py b/tests/test_csv_loader.py index 41d983c..0f11ed6 100644 --- a/tests/test_csv_loader.py +++ b/tests/test_csv_loader.py @@ -2,6 +2,7 @@ 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 = """ @@ -9,15 +10,16 @@ 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) @@ -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') @@ -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): diff --git a/tests/test_error_handler.py b/tests/test_error_handler.py index 80aaef8..22e4bc4 100644 --- a/tests/test_error_handler.py +++ b/tests/test_error_handler.py @@ -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() @@ -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() diff --git a/tests/test_json_loader.py b/tests/test_json_loader.py index 7532af4..7f0a164 100644 --- a/tests/test_json_loader.py +++ b/tests/test_json_loader.py @@ -18,7 +18,7 @@ } ] """ - +file_path = 'path/to/data.json' class TestJsonLoader(TestCase): @@ -26,7 +26,7 @@ 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) @@ -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):