feat: CSV変換モジュールの最終的な出力をCSVのリストに変更。テストコードの修正内容はファイル書き込みモジュールにも横展開

This commit is contained in:
shimoda.m@nds-tyo.co.jp 2022-10-12 16:06:14 +09:00
parent a74e0ee8f3
commit fed4883680
3 changed files with 13 additions and 133 deletions

View File

@ -1,6 +1,3 @@
import csv
import io
from src.config.objects import TargetObject
from src.converter.convert_strategy import ConvertStrategyFactory
@ -14,8 +11,7 @@ class CSVStringConverter:
def convert(self) -> str:
extracted_sf_object_jsons = self.__extract_sf_object_jsons()
csv_data = self.__convert_to_csv(extracted_sf_object_jsons)
csv_string = self.__write_csv_string(csv_data)
return csv_string
return csv_data
def __extract_sf_object_jsons(self) -> list:
try:
@ -42,7 +38,7 @@ class CSVStringConverter:
def __convert_to_csv(self, extracted_sf_object_jsons) -> list:
try:
columns = self.__target_object.columns
csv_data = []
csv_data = [columns]
for i, json_object in enumerate(extracted_sf_object_jsons, 1):
csv_row = []
for column in columns:
@ -59,18 +55,3 @@ class CSVStringConverter:
except Exception as e:
raise Exception(
f'CSV変換に失敗しました カラム名:[{column}] 行番号: [{i}] エラー内容:[{e}]')
def __write_csv_string(self, csv_data) -> str:
# TODO: CsvWriterに移管
try:
with io.StringIO(newline='') as string_stream:
writer = csv.writer(string_stream, delimiter=',', lineterminator='\r\n',
doublequote=True, quotechar='"', quoting=csv.QUOTE_ALL, strict=True)
writer.writerow(self.__target_object.columns)
writer.writerows(csv_data)
csv_value = string_stream.getvalue()
return csv_value
except Exception as e:
raise Exception('CSVデータの出力に失敗しました', e)

View File

@ -1,4 +1,3 @@
import textwrap
from collections import OrderedDict
import pytest
@ -98,15 +97,15 @@ class TestCSVStringConverter:
actual = csv_string_converter.convert()
# Expects
expected_value = '''\
"Id","AccountId","UserOrGroupId","AccountAccessLevel","OpportunityAccessLevel","CaseAccessLevel","ContactAccessLevel","RowCause","LastModifiedDate","LastModifiedById","IsDeleted"\r\n\
"TEST001","test001","","1","2","3","4","テストのため1","2022-06-01 09:00:00","1234567.0","0"\r\n\
"TEST002","test002","","5","6","7","8","テストのため2","2022-06-03 01:30:30","2.23","1"\r\n\
"TEST003","test003","","9","10","11","12","テストのため3","2022-06-04 08:50:50","3.234567","0"\r\n\
'''
expect = [
["Id", "AccountId", "UserOrGroupId", "AccountAccessLevel", "OpportunityAccessLevel", "CaseAccessLevel",
"ContactAccessLevel", "RowCause", "LastModifiedDate", "LastModifiedById", "IsDeleted"],
["TEST001", "test001", "", 1, 2, 3, 4, "テストのため1", "2022-06-01 09:00:00", 1234567.0, 0],
["TEST002", "test002", "", 5, 6, 7, 8, "テストのため2", "2022-06-03 01:30:30", 2.23, 1],
["TEST003", "test003", "", 9, 10, 11, 12, "テストのため3", "2022-06-04 08:50:50", 3.234567, 0]
]
# expected_valueのインデントが半角スペースと認識されてしまうため、`textwrap.dedent`にて補正
assert actual == textwrap.dedent(expected_value)
assert actual == expect
def test_raise_convert_extract_jsons(self) -> str:
"""
@ -291,103 +290,3 @@ class TestCSVStringConverter:
# Expects
assert 'CSV変換に失敗しました カラム名:[Id] 行番号: [1]' in str(e.value)
def test_raise_convert_write_csv_string(self, monkeypatch) -> str:
"""
Cases:
csvデータ出力のCSVデータ取得で例外が発生すること
Arranges:
- オブジェクト情報の作成
- データの作成
- 実行日時取得インスタンスの生成
- オブジェクト情報インスタンスの生成
- csvデータ出力のエラーを発生させるためのモックを準備
Expects:
例外が期待値と一致すること
"""
# Arranges
object_info = {
"object_name": "AccountShare",
"columns": [
"Id",
"AccountId",
"UserOrGroupId",
"AccountAccessLevel",
"OpportunityAccessLevel",
"CaseAccessLevel",
"ContactAccessLevel",
"RowCause",
"LastModifiedDate",
"LastModifiedById",
"IsDeleted"
],
"is_skip": False,
"is_update_last_fetch_datetime": False,
"last_fetch_datetime_file_name": "AccountShare.json",
"upload_file_name": "CRM_AccountShare_{execute_datetime}",
"datetime_column": "LastModifiedDate"
}
data = [
OrderedDict([
('attributes', OrderedDict([('type', 'AccountShare'), ('url', '/services/data/v1.0/sobjects/AccountShare/test1')])),
('Id', 'TEST001'),
('AccountId', 'test001'),
('UserOrGroupId', None),
('AccountAccessLevel', 1),
('OpportunityAccessLevel', 2),
('CaseAccessLevel', 3),
('ContactAccessLevel', 4),
('RowCause', 'テストのため1'),
('LastModifiedDate', '2022-06-01T00:00:00.000+0000'),
('LastModifiedById', 1.234567E+6),
('IsDeleted', False)
]),
OrderedDict([
('attributes', OrderedDict([('type', 'AccountShare'), ('url', '/services/data/v1.0/sobjects/AccountShare/test1')])),
('Id', 'TEST002'),
('AccountId', 'test002'),
('UserOrGroupId', None),
('AccountAccessLevel', 5),
('OpportunityAccessLevel', 6),
('CaseAccessLevel', 7),
('ContactAccessLevel', 8),
('RowCause', 'テストのため2'),
('LastModifiedDate', '2022-06-02T16:30:30.000+0000'),
('LastModifiedById', 2.234567E+6),
('IsDeleted', True)
]),
OrderedDict([
('attributes', OrderedDict([('type', 'AccountShare'), ('url', '/services/data/v1.0/sobjects/AccountShare/test1')])),
('Id', 'TEST003'),
('AccountId', 'test003'),
('UserOrGroupId', None),
('AccountAccessLevel', 9),
('OpportunityAccessLevel', 10),
('CaseAccessLevel', 11),
('ContactAccessLevel', 12),
('RowCause', 'テストのため3'),
('LastModifiedDate', '2022-06-03T23:50:50.000+0000'),
('LastModifiedById', 3.234567E+6),
('IsDeleted', False)
])
]
execute_datetime = ExecuteDateTime()
target_object = TargetObject(object_info, execute_datetime)
def dummy_method(arg):
raise Exception(e)
# データ加工のみだと事前の処理によりエラーとなるため、csv出力モジュールをモック化する
monkeypatch.setattr("csv.writer", dummy_method)
# Act
csv_string_converter = CSVStringConverter(target_object, data)
with pytest.raises(Exception) as e:
csv_string_converter.convert()
# Expects
assert 'CSVデータの出力に失敗しました' in str(e.value)

View File

@ -68,9 +68,9 @@ class TestCsvFileWriter:
content = [
["Id", "AccountId", "UserOrGroupId", "AccountAccessLevel", "OpportunityAccessLevel", "CaseAccessLevel",
"ContactAccessLevel", "RowCause", "LastModifiedDate", "LastModifiedById", "IsDeleted"],
["TEST001", "test001", "", "1", "2", "3", "4", "テストのため1", "2022-06-01 09:00:00", "1234567.0", "0"],
["TEST002", "test002", "", "5", "6", "7", "8", "テストのため2", "2022-06-03 01:30:30", "2.23", "1"],
["TEST003", "test003", "", "9", "10", "11", "12", "テストのため3", "2022-06-04 08:50:50", "3.234567", "0"]
["TEST001", "test001", "", 1, 2, 3, 4, "テストのため1", "2022-06-01 09:00:00", 1234567.0, 0],
["TEST002", "test002", "", 5, 6, 7, 8, "テストのため2", "2022-06-03 01:30:30", 2.23, 1],
["TEST003", "test003", "", 9, 10, 11, 12, "テストのため3", "2022-06-04 08:50:50", 3.234567, 0]
]
# Act