fix: レビュー指摘点の反映、int型とstring型の判定追加
This commit is contained in:
parent
a40bad2033
commit
63cdbd6c03
@ -15,7 +15,8 @@ class ConvertStrategyFactory:
|
||||
self.__float_convert_strategy = FloatConvertStrategy()
|
||||
self.__boolean_convert_strategy = BooleanConvertStrategy()
|
||||
self.__datetime_convert_strategy = DatetimeConvertStrategy()
|
||||
self.__non_convert_strategy = NonConvertStrategy()
|
||||
self.__int_convert_strategy = IntConvertStrategy()
|
||||
self.__string_convert_strategy = StringConvertStrategy()
|
||||
|
||||
def create(self, value):
|
||||
|
||||
@ -31,33 +32,49 @@ class ConvertStrategyFactory:
|
||||
elif type(value) == str and re.fullmatch(DATE_PATTERN_YYYYMMDDHHMMSSFFF_UTC, value):
|
||||
convert_strategy = self.__datetime_convert_strategy
|
||||
|
||||
elif type(value) == int:
|
||||
convert_strategy = self.__int_convert_strategy
|
||||
|
||||
else:
|
||||
convert_strategy = self.__non_convert_strategy
|
||||
convert_strategy = self.__string_convert_strategy
|
||||
|
||||
return convert_strategy
|
||||
|
||||
|
||||
class NoneValueConvertStrategy:
|
||||
def convert_value(self, convert_value: None) -> str:
|
||||
"""Noneを''空文字に変換する処理"""
|
||||
return ''
|
||||
|
||||
|
||||
class BooleanConvertStrategy:
|
||||
def convert_value(self, convert_value: str) -> bool:
|
||||
"""booleanを数値に変換する処理"""
|
||||
return CSV_TRUE_VALUE if convert_value is True else CSV_FALSE_VALUE
|
||||
|
||||
|
||||
class DatetimeConvertStrategy:
|
||||
def convert_value(self, convert_value: str) -> str:
|
||||
"""UTCのdatetime文字列をJSTの日時文字列に変換する処理"""
|
||||
# データ登録処理がJSTとして登録するため、変換処理内で事前にJSTの日時文字列に変換する
|
||||
return datetime.strptime(convert_value, CRM_DATETIME_FORMAT).astimezone(gettz(CONVERT_TZ)).strftime(YYYYMMDDHHMMSS)
|
||||
|
||||
|
||||
class FloatConvertStrategy:
|
||||
def convert_value(self, convert_value: str) -> int:
|
||||
"""float型をint型に変換する処理"""
|
||||
return int(convert_value)
|
||||
|
||||
|
||||
class NonConvertStrategy:
|
||||
def convert_value(self, convert_value):
|
||||
class IntConvertStrategy:
|
||||
def convert_value(self, convert_value: int):
|
||||
"""int型を変換せずに返す処理"""
|
||||
# ConvertStrategyFactoryにて型チェックを行っているため値を変換せずに返す
|
||||
return convert_value
|
||||
|
||||
|
||||
class StringConvertStrategy:
|
||||
def convert_value(self, convert_value: str):
|
||||
"""string型を変換せずに返す処理"""
|
||||
# ConvertStrategyFactoryにて型チェックを行っているため値を変換せずに返す
|
||||
return convert_value
|
||||
|
||||
@ -2,8 +2,9 @@ from src.converter.convert_strategy import (BooleanConvertStrategy,
|
||||
ConvertStrategyFactory,
|
||||
DatetimeConvertStrategy,
|
||||
FloatConvertStrategy,
|
||||
NonConvertStrategy,
|
||||
NoneValueConvertStrategy)
|
||||
IntConvertStrategy,
|
||||
NoneValueConvertStrategy,
|
||||
StringConvertStrategy)
|
||||
|
||||
|
||||
class TestConvertStrategyFactory:
|
||||
@ -23,7 +24,7 @@ class TestConvertStrategyFactory:
|
||||
actual = sut.create(None)
|
||||
|
||||
# Expects
|
||||
assert type(actual).__name__ == "NoneValueConvertStrategy"
|
||||
assert type(actual) == NoneValueConvertStrategy
|
||||
|
||||
def test_create_float(self):
|
||||
"""
|
||||
@ -40,7 +41,7 @@ class TestConvertStrategyFactory:
|
||||
actual = sut.create(1.2345678E7)
|
||||
|
||||
# Expects
|
||||
assert type(actual).__name__ == "FloatConvertStrategy"
|
||||
assert type(actual) == FloatConvertStrategy
|
||||
|
||||
def test_create_bool_true(self):
|
||||
"""
|
||||
@ -57,7 +58,7 @@ class TestConvertStrategyFactory:
|
||||
actual = sut.create(True)
|
||||
|
||||
# Expects
|
||||
assert type(actual).__name__ == "BooleanConvertStrategy"
|
||||
assert type(actual) == BooleanConvertStrategy
|
||||
|
||||
def test_create_bool_false(self):
|
||||
"""
|
||||
@ -74,7 +75,7 @@ class TestConvertStrategyFactory:
|
||||
actual = sut.create(False)
|
||||
|
||||
# Expects
|
||||
assert type(actual).__name__ == "BooleanConvertStrategy"
|
||||
assert type(actual) == BooleanConvertStrategy
|
||||
|
||||
def test_create_datetime(self):
|
||||
"""
|
||||
@ -91,7 +92,7 @@ class TestConvertStrategyFactory:
|
||||
actual = sut.create('2022-06-13T10:15:32.000+0000')
|
||||
|
||||
# Expects
|
||||
assert type(actual).__name__ == "DatetimeConvertStrategy"
|
||||
assert type(actual) == DatetimeConvertStrategy
|
||||
|
||||
def test_create_other_str(self):
|
||||
"""
|
||||
@ -108,7 +109,7 @@ class TestConvertStrategyFactory:
|
||||
actual = sut.create('test_string')
|
||||
|
||||
# Expects
|
||||
assert type(actual).__name__ == "NonConvertStrategy"
|
||||
assert type(actual) == StringConvertStrategy
|
||||
|
||||
def test_create_other_int(self):
|
||||
"""
|
||||
@ -125,7 +126,7 @@ class TestConvertStrategyFactory:
|
||||
actual = sut.create(100)
|
||||
|
||||
# Expects
|
||||
assert type(actual).__name__ == "NonConvertStrategy"
|
||||
assert type(actual) == IntConvertStrategy
|
||||
|
||||
|
||||
class TestNoneValueConvertStrategy:
|
||||
@ -225,26 +226,9 @@ class TestFloatConvertStrategy:
|
||||
assert actual == 12345678
|
||||
|
||||
|
||||
class TestNonConvertStrategy:
|
||||
class TestIntConvertStrategy:
|
||||
|
||||
def test_convert_value_str(self):
|
||||
"""
|
||||
Cases:
|
||||
引数に文字列を指定した場合、加工されず文字列が返ってくること
|
||||
Arranges:
|
||||
- なし
|
||||
Expects:
|
||||
- 戻り値が、期待値と一致する
|
||||
"""
|
||||
|
||||
# Act
|
||||
sut = NonConvertStrategy()
|
||||
actual = sut.convert_value('テストデータ')
|
||||
|
||||
# Expects
|
||||
assert actual == 'テストデータ'
|
||||
|
||||
def test_convert_value_int(self):
|
||||
def test_convert_value(self):
|
||||
"""
|
||||
Cases:
|
||||
引数に整数を指定した場合、加工されず整数が返ってくること
|
||||
@ -255,8 +239,28 @@ class TestNonConvertStrategy:
|
||||
"""
|
||||
|
||||
# Act
|
||||
sut = NonConvertStrategy()
|
||||
sut = IntConvertStrategy()
|
||||
actual = sut.convert_value(100)
|
||||
|
||||
# Expects
|
||||
assert actual == 100
|
||||
|
||||
|
||||
class TestStringConvertStrategy:
|
||||
|
||||
def test_convert_value(self):
|
||||
"""
|
||||
Cases:
|
||||
引数に文字列を指定した場合、加工されず文字列が返ってくること
|
||||
Arranges:
|
||||
- なし
|
||||
Expects:
|
||||
- 戻り値が、期待値と一致する
|
||||
"""
|
||||
|
||||
# Act
|
||||
sut = StringConvertStrategy()
|
||||
actual = sut.convert_value('テストデータ')
|
||||
|
||||
# Expects
|
||||
assert actual == 'テストデータ'
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user