8000 Add df_service · suaaa7/samplecode-for-qiita@f6193dd · GitHub
[go: up one dir, main page]

Skip to content

Commit f6193dd

Browse files
committed
Add df_service
1 parent 2e30521 commit f6193dd

File tree

3 files changed

+138
-0
lines changed

3 files changed

+138
-0
lines changed

python_ci/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
requests==2.24.0
2+
pandas==1.1.3

python_ci/src/df_service.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from typing import Optional
2+
3+
import pandas as pd
4+
5+
6+
class DFService:
7+
def swap_and_remove_path(
8+
self,
9+
df: pd.DataFrame,
10+
base_dir: Optional[str] = None
11+
) -> pd.DataFrame:
12+
service = ImageDownloadService()
13+
14+
for i, image_hashes in enumerate(df.image_hash):
15+
image_paths = []
16+
for image_hash in image_hashes:
17+
maybe_local_path = service.get_local_path(
18+
image_hash=image_hash,
19+
base_dir=base_dir
20+
)
21+
if maybe_local_path is None:
22+
print("Skipped this data.")
23+
else:
24+
image_paths.append(maybe_local_path)
25+
df.at[i, 'image_path'] = image_paths
26+
27+
return df[df['image_path'].map(len) == df['num']]
28+
29+
30+
class ImageDownloadService:
31+
def get_local_path(
32+
self,
33+
image_hash: str,
34+
base_dir: Optional[str]
35+
) -> Optional[str]:
36+
return None
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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

Comments
 (0)
0