|
| 1 | +from unittest import TestCase, main |
| 2 | +from unittest.mock import patch |
| 3 | + |
| 4 | +import pandas as pd |
| 5 | +from pandas.testing import assert_frame_equal |
| 6 | + |
| 7 | +from src.df_service import DFService |
| 8 | + |
| 9 | + |
| 10 | +class TestDFService(TestCase): |
| 11 | + def setUp(self) -> None: |
| 12 | + self.patcher = patch('src.df_service.ImageDownloadService.get_local_path') |
| 13 | + self.m_get_local_path = self.patcher.start() |
| 14 | + |
| 15 | + self.service = DFService() |
| 16 | + self.test_df = pd.DataFrame({ |
| 17 | + 'num': [1, 2], |
| 18 | + 'image_hash': [ |
| 19 | + ['aaaaa'], |
| 20 | + ['aaaaa', 'aaaaa'] |
| 21 | + ], |
| 22 | + 'image_path': [ |
| 23 | + ['s3://test_image.jpg'], |
| 24 | + ['s3://test_image.jpg', 's3://test_image.jpg'] |
| 25 | + ] |
| 26 | + }) |
| 27 | + self.test_base_dir = '/home/test-user' |
| 28 | + |
| 29 | + def tearDown(self) -> None: |
| 30 | + self.patcher.stop() |
| 31 | + |
| 32 | + def test_return_empty_df(self) -> None: |
| 33 | + self.m_get_local_path.return_value = None |
| 34 | + |
| 35 | + actual_df = self.service.swap_and_remove_path( |
| 36 | + self.test_df, |
| 37 | + self.test_base_dir |
| 38 | + ) |
| 39 | + |
| 40 | + self.assertTrue(actual_df.empty) |
| 41 | + self.assertEqual(self.m_get_local_path.call_count, 3) |
| 42 | + self.m_get_local_path.assert_called_with( |
| 43 | + image_hash='aaaaa', |
| 44 | + base_dir=self.test_base_dir |
| 45 | + ) |
| 46 | + |
| 47 | + def test_return_empty_df_when_base_dir_is_none(self) -> None: |
| 48 | + self.m_get_local_path.return_value = None |
| 49 | + |
| 50 | + actual_df = self.service.swap_and_remove_path(self.test_df) |
| 51 | + |
| 52 | + self.assertTrue(actual_df.empty) |
| 53 | + self.assertEqual(self.m_get_local_path.call_count, 3) |
| 54 | + self.m_get_local_path.assert_called_with( |
| 55 | + image_hash='aaaaa', |
| 56 | + base_dir=None |
| 57 | + ) |
| 58 | + |
| 59 | + def test_return_expected_df(self) -> None: |
| 60 | + self.m_get_local_path.return_value = '/opt/program/test_image.jpg' |
| 61 | + |
| 62 | + expected_df = pd.DataFrame({ |
| 63 | + 'num': [1, 2], |
| 64 | + 'image_hash': [ |
| 65 | + ['aaaaa'], |
| 66 | + ['aaaaa', 'aaaaa'] |
| 67 | + ], |
| 68 | + 'image_path': [ |
| 69 | + ['/opt/program/test_image.jpg'], |
| 70 | + ['/opt/program/test_image.jpg', '/opt/program/test_image.jpg'] |
| 71 | + ] |
| 72 | + }) |
| 73 | + actual_df = self.service.swap_and_remove_path(self.test_df) |
| 74 | + |
| 75 | + assert_frame_equal(actual_df, expected_df) |
| 76 | + self.assertEqual(self.m_get_local_path.call_count, 3) |
| 77 | + |
| 78 | + def test_return_expected_df_when_get_local_path_return_none(self) -> None: |
| 79 | + self.m_get_local_path.side_effect = [ |
| 80 | + '/opt/program/test_image.jpg', |
| 81 | + None, |
| 82 | + '/opt/program/test_image.jpg' |
| 83 | + ] |
| 84 | + |
| 85 | + expected_df = pd.DataFrame({ |
| 86 | + 'num': [1], |
| 87 | + 'image_hash': [ |
| 88 | + ['aaaaa'] |
| 89 | + ], |
| 90 | + 'image_path': [ |
| 91 | + ['/opt/program/test_image.jpg'] |
| 92 | + ] |
| 93 | + }) |
| 94 | + actual_df = self.service.swap_and_remove_path(self.test_df) |
| 95 | + |
| 96 | + assert_frame_equal(actual_df, expected_df) |
| 97 | + self.assertEqual(self.m_get_local_path.call_count, 3) |
| 98 | + |
| 99 | + |
| 100 | +if __name__ == '__main__': |
| 101 | + main() |
0 commit comments