From 2e24ce85f2079b6abeadf38b16721961148df3b0 Mon Sep 17 00:00:00 2001 From: "shimoda.m@nds-tyo.co.jp" Date: Fri, 19 Aug 2022 16:11:33 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20dict=E5=9E=8B=E3=81=AE=E5=A4=89?= =?UTF-8?q?=E6=8F=9B=E5=99=A8=E7=94=9F=E6=88=90=E5=87=A6=E7=90=86=E3=82=92?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/converter/convert_strategy.py | 10 +++++ .../tests/converter/test_convert_strategy.py | 45 +++++++++++++++++-- 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/ecs/crm-datafetch/src/converter/convert_strategy.py b/ecs/crm-datafetch/src/converter/convert_strategy.py index 22fca8fc..3e092315 100644 --- a/ecs/crm-datafetch/src/converter/convert_strategy.py +++ b/ecs/crm-datafetch/src/converter/convert_strategy.py @@ -1,4 +1,5 @@ import re +from collections import OrderedDict from datetime import datetime from dateutil.tz import gettz @@ -17,6 +18,7 @@ class ConvertStrategyFactory: self.__datetime_convert_strategy = DatetimeConvertStrategy() self.__int_convert_strategy = IntConvertStrategy() self.__string_convert_strategy = StringConvertStrategy() + self.__dict_convert_strategy = DictConvertStrategy() def create(self, value): @@ -35,6 +37,8 @@ class ConvertStrategyFactory: elif type(value) == int: convert_strategy = self.__int_convert_strategy + elif type(value) == dict or type(value) == OrderedDict: + convert_strategy = self.__dict_convert_strategy else: convert_strategy = self.__string_convert_strategy @@ -78,3 +82,9 @@ class StringConvertStrategy: """string型を変換せずに返す処理""" # ConvertStrategyFactoryにて型チェックを行っているため値を変換せずに返す return convert_value + + +class DictConvertStrategy: + def convert_value(self, convert_value: str): + """dict型の項目を文字列に変換して返す処理""" + return convert_value diff --git a/ecs/crm-datafetch/tests/converter/test_convert_strategy.py b/ecs/crm-datafetch/tests/converter/test_convert_strategy.py index 0e268fc4..77ecfea2 100644 --- a/ecs/crm-datafetch/tests/converter/test_convert_strategy.py +++ b/ecs/crm-datafetch/tests/converter/test_convert_strategy.py @@ -1,6 +1,9 @@ +from typing import OrderedDict + from src.converter.convert_strategy import (BooleanConvertStrategy, ConvertStrategyFactory, DatetimeConvertStrategy, + DictConvertStrategy, FloatConvertStrategy, IntConvertStrategy, NoneValueConvertStrategy, @@ -94,10 +97,10 @@ class TestConvertStrategyFactory: # Expects assert type(actual) == DatetimeConvertStrategy - def test_create_other_str(self): + def test_create_str(self): """ Cases: - 引数にSalesforce日付型以外の文字列を指定した場合、NonConvertStrategyインスタンスが返ってくること + 引数にSalesforce日付型以外の文字列を指定した場合、StringConvertStrategyインスタンスが返ってくること Arranges: - なし Expects: @@ -111,10 +114,10 @@ class TestConvertStrategyFactory: # Expects assert type(actual) == StringConvertStrategy - def test_create_other_int(self): + def test_create_int(self): """ Cases: - 引数に整数を指定した場合、NonConvertStrategyインスタンスが返ってくること + 引数に整数を指定した場合、IntConvertStrategyインスタンスが返ってくること Arranges: - なし Expects: @@ -128,6 +131,40 @@ class TestConvertStrategyFactory: # Expects assert type(actual) == IntConvertStrategy + def test_create_dict(self): + """ + Cases: + 引数に辞書型の値を指定した場合、IntConvertStrategyインスタンスが返ってくること + Arranges: + - なし + Expects: + - 戻り値が、期待値と一致する + """ + + # Act + sut = ConvertStrategyFactory() + actual = sut.create({'key': 'value'}) + + # Expects + assert type(actual) == DictConvertStrategy + + def test_create_ordered_dict_dict(self): + """ + Cases: + 引数に辞書型の値を指定した場合、IntConvertStrategyインスタンスが返ってくること + Arranges: + - なし + Expects: + - 戻り値が、期待値と一致する + """ + + # Act + sut = ConvertStrategyFactory() + actual = sut.create(OrderedDict([('key', 'value')])) + + # Expects + assert type(actual) == DictConvertStrategy + class TestNoneValueConvertStrategy: