8000 Add test with callFUT by suaaa7 · Pull Request #24 · suaaa7/samplecode-for-qiita · GitHub
[go: up one dir, main page]

Skip to content

Add test with callFUT #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions python_ci/requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
flake8==3.8.3
mypy==0.782
fsspec==0.8.7
11 changes: 11 additions & 0 deletions python_ci/src/converter.py
Original file line number Diff line number Diff line change
@@ -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
50 changes: 50 additions & 0 deletions python_ci/src/tests/test_converter.py
Original file line number Diff line number Diff line change
@@ -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)
0