Merge pull request #65 feature-NEWDWH2021-643-convert_strategy into develop-6crm
This commit is contained in:
commit
193f30a95b
@ -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: str):
|
||||
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
|
||||
|
||||
@ -73,8 +73,8 @@ UPD_JP_NAME = '前回取得日時ファイル更新'
|
||||
END_JP_NAME = '取得処理実施結果アップロード処理'
|
||||
|
||||
# CSVチェック
|
||||
CSV_TRUE_VALUE = '1'
|
||||
CSV_FALSE_VALUE = '0'
|
||||
CSV_TRUE_VALUE = 1
|
||||
CSV_FALSE_VALUE = 0
|
||||
|
||||
# オブジェクト変数
|
||||
OBJECTS_KEY = 'objects'
|
||||
|
||||
266
ecs/crm-datafetch/tests/converter/test_convert_strategy.py
Normal file
266
ecs/crm-datafetch/tests/converter/test_convert_strategy.py
Normal file
@ -0,0 +1,266 @@
|
||||
from src.converter.convert_strategy import (BooleanConvertStrategy,
|
||||
ConvertStrategyFactory,
|
||||
DatetimeConvertStrategy,
|
||||
FloatConvertStrategy,
|
||||
IntConvertStrategy,
|
||||
NoneValueConvertStrategy,
|
||||
StringConvertStrategy)
|
||||
|
||||
|
||||
class TestConvertStrategyFactory:
|
||||
|
||||
def test_create_none(self):
|
||||
"""
|
||||
Cases:
|
||||
引数にnull(None)を指定した場合、NoneValueConvertStrategyインスタンスが返ってくること
|
||||
Arranges:
|
||||
- なし
|
||||
Expects:
|
||||
- 戻り値が、期待値と一致する
|
||||
"""
|
||||
|
||||
# Act
|
||||
sut = ConvertStrategyFactory()
|
||||
actual = sut.create(None)
|
||||
|
||||
# Expects
|
||||
assert type(actual) == NoneValueConvertStrategy
|
||||
|
||||
def test_create_float(self):
|
||||
"""
|
||||
Cases:
|
||||
引数に指数表記を指定した場合、FloatConvertStrategyインスタンスが返ってくること
|
||||
Arranges:
|
||||
- なし
|
||||
Expects:
|
||||
- 戻り値が、期待値と一致する
|
||||
"""
|
||||
|
||||
# Act
|
||||
sut = ConvertStrategyFactory()
|
||||
actual = sut.create(1.2345678E7)
|
||||
|
||||
# Expects
|
||||
assert type(actual) == FloatConvertStrategy
|
||||
|
||||
def test_create_bool_true(self):
|
||||
"""
|
||||
Cases:
|
||||
引数に論理値(True)を指定した場合、BooleanConvertStrategyインスタンスが返ってくること
|
||||
Arranges:
|
||||
- なし
|
||||
Expects:
|
||||
- 戻り値が、期待値と一致する
|
||||
"""
|
||||
|
||||
# Act
|
||||
sut = ConvertStrategyFactory()
|
||||
actual = sut.create(True)
|
||||
|
||||
# Expects
|
||||
assert type(actual) == BooleanConvertStrategy
|
||||
|
||||
def test_create_bool_false(self):
|
||||
"""
|
||||
Cases:
|
||||
引数に論理値(False)を指定した場合、BooleanConvertStrategyインスタンスが返ってくること
|
||||
Arranges:
|
||||
- なし
|
||||
Expects:
|
||||
- 戻り値が、期待値と一致する
|
||||
"""
|
||||
|
||||
# Act
|
||||
sut = ConvertStrategyFactory()
|
||||
actual = sut.create(False)
|
||||
|
||||
# Expects
|
||||
assert type(actual) == BooleanConvertStrategy
|
||||
|
||||
def test_create_datetime(self):
|
||||
"""
|
||||
Cases:
|
||||
引数に文字列かつ日付文字列を指定した場合、DatetimeConvertStrategyインスタンスが返ってくること
|
||||
Arranges:
|
||||
- なし
|
||||
Expects:
|
||||
- 戻り値が、期待値と一致する
|
||||
"""
|
||||
|
||||
# Act
|
||||
sut = ConvertStrategyFactory()
|
||||
actual = sut.create('2022-06-13T10:15:32.000+0000')
|
||||
|
||||
# Expects
|
||||
assert type(actual) == DatetimeConvertStrategy
|
||||
|
||||
def test_create_other_str(self):
|
||||
"""
|
||||
Cases:
|
||||
引数にSalesforce日付型以外の文字列を指定した場合、NonConvertStrategyインスタンスが返ってくること
|
||||
Arranges:
|
||||
- なし
|
||||
Expects:
|
||||
- 戻り値が、期待値と一致する
|
||||
"""
|
||||
|
||||
# Act
|
||||
sut = ConvertStrategyFactory()
|
||||
actual = sut.create('test_string')
|
||||
|
||||
# Expects
|
||||
assert type(actual) == StringConvertStrategy
|
||||
|
||||
def test_create_other_int(self):
|
||||
"""
|
||||
Cases:
|
||||
引数に整数を指定した場合、NonConvertStrategyインスタンスが返ってくること
|
||||
Arranges:
|
||||
- なし
|
||||
Expects:
|
||||
- 戻り値が、期待値と一致する
|
||||
"""
|
||||
|
||||
# Act
|
||||
sut = ConvertStrategyFactory()
|
||||
actual = sut.create(100)
|
||||
|
||||
# Expects
|
||||
assert type(actual) == IntConvertStrategy
|
||||
|
||||
|
||||
class TestNoneValueConvertStrategy:
|
||||
|
||||
def test_convert_value(self) -> str:
|
||||
"""
|
||||
Cases:
|
||||
引数にnull(None)を指定した場合、''(空文字)が返ってくること
|
||||
Arranges:
|
||||
- なし
|
||||
Expects:
|
||||
- 戻り値が、期待値と一致する
|
||||
"""
|
||||
|
||||
# Act
|
||||
sut = NoneValueConvertStrategy()
|
||||
actual = sut.convert_value(None)
|
||||
|
||||
# Expects
|
||||
assert actual == ""
|
||||
|
||||
|
||||
class TestBooleanConvertStrategy:
|
||||
|
||||
def test_convert_value_true(self) -> bool:
|
||||
"""
|
||||
Cases:
|
||||
引数に論理値(True)を指定した場合、数値(1)が返ってくること
|
||||
Arranges:
|
||||
- なし
|
||||
Expects:
|
||||
- 戻り値が、期待値と一致する
|
||||
"""
|
||||
|
||||
# Act
|
||||
sut = BooleanConvertStrategy()
|
||||
actual = sut.convert_value(True)
|
||||
|
||||
# Expects
|
||||
assert actual == 1
|
||||
|
||||
def test_convert_value_false(self) -> bool:
|
||||
"""
|
||||
Cases:
|
||||
引数に論理値(False)を指定した場合、数値(0)が返ってくること
|
||||
Arranges:
|
||||
- なし
|
||||
Expects:
|
||||
- 戻り値が、期待値と一致する
|
||||
"""
|
||||
|
||||
# Act
|
||||
sut = BooleanConvertStrategy()
|
||||
actual = sut.convert_value(False)
|
||||
|
||||
# Expects
|
||||
assert actual == 0
|
||||
|
||||
|
||||
class TestDatetimeConvertStrategy:
|
||||
|
||||
def test_convert_value(self) -> str:
|
||||
"""
|
||||
Cases:
|
||||
引数に日付文字列を指定した場合、YYYY-MM-DD HH:MM:SSがJSTで返ってくること
|
||||
Arranges:
|
||||
- なし
|
||||
Expects:
|
||||
- 戻り値が、期待値と一致する
|
||||
"""
|
||||
|
||||
# Act
|
||||
sut = DatetimeConvertStrategy()
|
||||
actual = sut.convert_value('2022-06-13T20:15:32.000+0000')
|
||||
|
||||
# Expects
|
||||
assert actual == "2022-06-14 05:15:32"
|
||||
|
||||
|
||||
class TestFloatConvertStrategy:
|
||||
|
||||
def test_convert_value(self) -> int:
|
||||
"""
|
||||
Cases:
|
||||
引数に指数表記を指定した場合、整数で返ってくること
|
||||
Arranges:
|
||||
- なし
|
||||
Expects:
|
||||
- 戻り値が、期待値と一致する
|
||||
"""
|
||||
|
||||
# Act
|
||||
sut = FloatConvertStrategy()
|
||||
actual = sut.convert_value(1.2345678E7)
|
||||
|
||||
# Expects
|
||||
assert actual == 12345678
|
||||
|
||||
|
||||
class TestIntConvertStrategy:
|
||||
|
||||
def test_convert_value(self):
|
||||
"""
|
||||
Cases:
|
||||
引数に整数を指定した場合、加工されず整数が返ってくること
|
||||
Arranges:
|
||||
- なし
|
||||
Expects:
|
||||
- 戻り値が、期待値と一致する
|
||||
"""
|
||||
|
||||
# Act
|
||||
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