|
| 1 | +# pylint: disable=not-context-manager |
| 2 | +from unittest import TestCase |
| 3 | +from unittest.mock import patch, mock_open |
| 4 | + |
| 5 | +from pystreamapi.loaders import csv |
| 6 | + |
| 7 | +file_content = """ |
| 8 | +attr1,attr2 |
| 9 | +1,2.0 |
| 10 | +a,b |
| 11 | +""" |
| 12 | + |
| 13 | + |
| 14 | +class TestCSVLoader(TestCase): |
| 15 | + |
| 16 | + def test_csv_loader(self): |
| 17 | + with (patch('builtins.open', mock_open(read_data=file_content)), |
| 18 | + patch('os.path.exists', return_value=True), |
| 19 | + patch('os.path.isfile', return_value=True)): |
| 20 | + data = csv('path/to/data.csv') |
| 21 | + self.assertEqual(len(data), 2) |
| 22 | + self.assertEqual(data[0].attr1, 1) |
| 23 | + self.assertIsInstance(data[0].attr1, int) |
| 24 | + self.assertEqual(data[0].attr2, 2.0) |
| 25 | + self.assertIsInstance(data[0].attr2, float) |
| 26 | + self.assertEqual(data[1].attr1, 'a') |
| 27 | + self.assertIsInstance(data[1].attr1, str) |
| 28 | + |
| 29 | + def test_csv_loader_with_casting_disabled(self): |
| 30 | + with (patch('builtins.open', mock_open(read_data=file_content)), |
| 31 | + patch('os.path.exists', return_value=True), |
| 32 | + patch('os.path.isfile', return_value=True)): |
| 33 | + data = csv('path/to/data.csv', cast_types=False) |
| 34 | + self.assertEqual(len(data), 2) |
| 35 | + self.assertEqual(data[0].attr1, '1') |
| 36 | + self.assertIsInstance(data[0].attr1, str) |
| 37 | + self.assertEqual(data[0].attr2, '2.0') |
| 38 | + self.assertIsInstance(data[0].attr2, str) |
| 39 | + self.assertEqual(data[1].attr1, 'a') |
| 40 | + self.assertIsInstance(data[1].attr1, str) |
| 41 | + |
| 42 | + def test_csv_loader_is_iterable(self): |
| 43 | + with (patch('builtins.open', mock_open(read_data=file_content)), |
| 44 | + patch('os.path.exists', return_value=True), |
| 45 | + patch('os.path.isfile', return_value=True)): |
| 46 | + data = csv('path/to/data.csv') |
| 47 | + self.assertEqual(len(list(iter(data))), 2) |
| 48 | + |
| 49 | + def test_csv_loader_with_custom_delimiter(self): |
| 50 | + with (patch('builtins.open', mock_open(read_data=file_content.replace(",", ";"))), |
| 51 | + patch('os.path.exists', return_value=True), |
| 52 | + patch('os.path.isfile', return_value=True)): |
| 53 | + data = csv('path/to/data.csv', delimiter=';') |
| 54 | + self.assertEqual(len(data), 2) |
| 55 | + self.assertEqual(data[0].attr1, 1) |
| 56 | + self.assertIsInstance(data[0].attr1, int) |
| 57 | + |
| 58 | + def test_csv_loader_with_empty_file(self): |
| 59 | + with (patch('builtins.open', mock_open(read_data="")), |
| 60 | + patch('os.path.exists', return_value=True), |
| 61 | + patch('os.path.isfile', return_value=True)): |
| 62 | + data = csv('path/to/data.csv') |
| 63 | + self.assertEqual(len(data), 0) |
| 64 | + |
| 65 | + def test_csv_loader_with_invalid_path(self): |
| 66 | + with self.assertRaises(FileNotFoundError): |
| 67 | + csv('path/to/invalid.csv') |
| 68 | + |
| 69 | + def test_csv_loader_with_no_file(self): |
| 70 | + with self.assertRaises(ValueError): |
| 71 | + csv('./') |
0 commit comments