From 8494eeca000ded545cdc7f6b3f8204514e5dea7a Mon Sep 17 00:00:00 2001 From: suaaa7 Date: Sat, 20 Mar 2021 14:40:24 +0900 Subject: [PATCH 1/2] add: add test with callFUT --- python_ci/requirements-dev.txt | 1 + python_ci/src/converter.py | 11 ++++++ python_ci/src/tests/test_converter.py | 50 +++++++++++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 python_ci/src/converter.py create mode 100644 python_ci/src/tests/test_converter.py diff --git a/python_ci/requirements-dev.txt b/python_ci/requirements-dev.txt index 679ffe8..728bc17 100644 --- a/python_ci/requirements-dev.txt +++ b/python_ci/requirements-dev.txt @@ -1,2 +1,3 @@ flake8==3.8.3 mypy==0.782 +fsspec=0.8.7 diff --git a/python_ci/src/converter.py b/python_ci/src/converter.py new file mode 100644 index 0000000..d894b04 --- /dev/null +++ b/python_ci/src/converter.py @@ -0,0 +1,11 @@ +from io import StringIO +from typing import Optional + +import pandas as pd + + +def json_str_to_df(json_str: str) -> Optional[pd.DataFrame]: + try: + return pd.read_json(path_or_buf=StringIO(json_str), orient='records') + except ValueError: + return None diff --git a/python_ci/src/tests/test_converter.py b/python_ci/src/tests/test_converter.py new file mode 100644 index 0000000..0562ca4 --- /dev/null +++ b/python_ci/src/tests/test_converter.py @@ -0,0 +1,50 @@ +from typing import Any +from unittest import TestCase +from unittest.mock import MagicMock, patch + +import pandas as pd +from pandas.testing import assert_frame_equal + + +class TestJsonStrToDF(TestCase): + def _call_fut(self, *args) -> Any: + from converter import json_str_to_df + + return json_str_to_df(*args) + + def test_for_s3_image(self) -> None: + test_json_str = '[{"name": "test", "image_path": ["s3://image.jpg"]}]' + + expected_df = pd.DataFrame({ + 'name': ['test'], + 'image_path': [['s3://image.jpg']] + }) + actual_df = self._call_fut(test_json_str) + + assert_frame_equal(actual_df, expected_df) + + def test_for_https_image(self) -> None: + test_json_str = '[{"name": "test", "image_path": ["https://image.jpg"]}]' + + expected_df = pd.DataFrame({ + 'name': ['test'], + 'image_path': [['https://image.jpg']] + }) + actual_df = self._call_fut(test_json_str) + + assert_frame_equal(actual_df, expected_df) + + @patch('converter.StringIO') + def test_return_None_when_without_StringIO(self, StringI0: MagicMock) -> None: + test_json_str = '[{"name": "test", "image_path": ["https://image.jpg"]}]' + + StringI0.return_value = test_json_str + actual_df = self._call_fut(test_json_str) + + self.assertEqual(actual_df, None) + + def test_return_None_when_unexpected_char(self) -> None: + test_json_str = '[{"name": "test"' + actual_df = self._call_fut(test_json_str) + + self.assertEqual(actual_df, None) From 9a4782083613faeaf3da817611a9e94fc92c7d63 Mon Sep 17 00:00:00 2001 From: suaaa7 Date: Sat, 20 Mar 2021 14:42:50 +0900 Subject: [PATCH 2/2] fix: = => == --- python_ci/requirements-dev.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python_ci/requirements-dev.txt b/python_ci/requirements-dev.txt index 728bc17..3d14d99 100644 --- a/python_ci/requirements-dev.txt +++ b/python_ci/requirements-dev.txt @@ -1,3 +1,3 @@ flake8==3.8.3 mypy==0.782 -fsspec=0.8.7 +fsspec==0.8.7