From ab816949124e97f7905d51bd58e6638eb66280e0 Mon Sep 17 00:00:00 2001 From: wf001 Date: Tue, 22 Nov 2022 17:04:41 +0000 Subject: [PATCH] add: toml format. --- fconv/formats/__init__.py | 3 +- fconv/formats/toml.py | 20 + tests/fixtures/__init__.py | 1 + tests/fixtures/test.toml | 11 + tests/test_cli.py | 4 +- tests/test_formats.py | 48 +++ tests/test_former.py | 789 +++++++++++++++++++++++++++++-------- 7 files changed, 701 insertions(+), 175 deletions(-) create mode 100644 tests/fixtures/test.toml diff --git a/fconv/formats/__init__.py b/fconv/formats/__init__.py index 45b1079..0080fb3 100644 --- a/fconv/formats/__init__.py +++ b/fconv/formats/__init__.py @@ -1,7 +1,8 @@ from fconv.formats.json import Json +from fconv.formats.toml import Toml from fconv.formats.yaml import Yaml -SUPPORTED_FORMATS = {"json": Json, "yaml": Yaml} +SUPPORTED_FORMATS = {"json": Json, "yaml": Yaml, "toml": Toml} def get_supported_formats(): diff --git a/fconv/formats/toml.py b/fconv/formats/toml.py index e69de29..57a05c5 100644 --- a/fconv/formats/toml.py +++ b/fconv/formats/toml.py @@ -0,0 +1,20 @@ +from typing import Any, Dict + +from .base import BaseDictionalizeFormat + + +class Toml(BaseDictionalizeFormat): + def __init__(self): + super().__init__(load_data_key="s", dump_data_key="o") + + @staticmethod + def load(src_ctx: Dict[str, Any]) -> Dict[str, Any]: + import toml + + return toml.loads(**src_ctx) + + @staticmethod + def dump(internal: Dict[str, Any]) -> str: + import toml + + return toml.dumps(**internal) diff --git a/tests/fixtures/__init__.py b/tests/fixtures/__init__.py index 460ad60..87dd018 100644 --- a/tests/fixtures/__init__.py +++ b/tests/fixtures/__init__.py @@ -6,6 +6,7 @@ FIXTURES_ROOT = Path(__file__).parent JSON_FILE_PATH = str(FIXTURES_ROOT / "test.json") YAML_FILE_PATH = str(FIXTURES_ROOT / "test.yaml") +TOML_FILE_PATH = str(FIXTURES_ROOT / "test.toml") class FakeValidFormat(BaseDictionalizeFormat): diff --git a/tests/fixtures/test.toml b/tests/fixtures/test.toml new file mode 100644 index 0000000..367eb8e --- /dev/null +++ b/tests/fixtures/test.toml @@ -0,0 +1,11 @@ +country = "Japan" + +[[user]] +name = "Taro" +phone = ["111-222-3333", "222-333-4444"] +age = 10 + +[[user]] +name = "Hanako" +phone = ["555-666-777"] +age = 20 diff --git a/tests/test_cli.py b/tests/test_cli.py index bc10301..41b7c17 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -6,7 +6,7 @@ from fconv import __prog__ from fconv.__main__ import main -from .fixtures import JSON_FILE_PATH, YAML_FILE_PATH +from .fixtures import JSON_FILE_PATH, TOML_FILE_PATH, YAML_FILE_PATH @pytest.mark.parametrize( @@ -14,6 +14,8 @@ [ [__prog__, "json", "yaml", "-i", JSON_FILE_PATH], [__prog__, "yaml", "json", "-i", YAML_FILE_PATH], + [__prog__, "toml", "json", "-i", TOML_FILE_PATH], + [__prog__, "yaml", "toml", "-i", YAML_FILE_PATH], [__prog__, "json", "yaml", "-i", JSON_FILE_PATH, "-o", YAML_FILE_PATH], ], ) diff --git a/tests/test_formats.py b/tests/test_formats.py index edcfd1c..861027e 100644 --- a/tests/test_formats.py +++ b/tests/test_formats.py @@ -4,6 +4,7 @@ from fconv.formats.base import BaseDictionalizeFormat from fconv.formats.json import Json +from fconv.formats.toml import Toml from fconv.formats.yaml import Yaml @@ -101,3 +102,50 @@ def test_get_dump_kwargs(self, mocker): ) Yaml().get_dump_kwargs({}, {}) assert m_super_gen_input.call_count == 1 + + class TestToml: + def test_load(self, mocker): + m_loads = mocker.patch("toml.loads") + src_ctx = {"a": "b"} + Toml().load(src_ctx) + + _, act_called_kwargs = m_loads.call_args + assert m_loads.called + assert act_called_kwargs == {"a": "b"} + + def test_load_handle_error(self, mocker): + mocker.patch("toml.loads").side_effect = Exception() + src_ctx = {"a": "b"} + + with pytest.raises(Exception): + Toml().load(src_ctx) + + def test_dump(self, mocker): + m_loads = mocker.patch("toml.dumps") + src_ctx = {"a": "b"} + Toml().dump(src_ctx) + + _, act_called_kwargs = m_loads.call_args + assert m_loads.called + assert act_called_kwargs == {"a": "b"} + + def test_dump_handle_error(self, mocker): + mocker.patch("toml.dumps").side_effect = Exception() + src_ctx = {"a": "b"} + + with pytest.raises(Exception): + Toml().dump(src_ctx) + + def test_get_load_kwargs(self, mocker): + m_super_gen_input = mocker.patch.object( + BaseDictionalizeFormat, "get_load_kwargs", MagicMock() + ) + Yaml().get_load_kwargs("ctx", {}) + assert m_super_gen_input.call_count == 1 + + def test_get_dump_kwargs(self, mocker): + m_super_gen_input = mocker.patch.object( + BaseDictionalizeFormat, "get_dump_kwargs", MagicMock() + ) + Toml().get_dump_kwargs({}, {}) + assert m_super_gen_input.call_count == 1 diff --git a/tests/test_former.py b/tests/test_former.py index 8cf1313..c5067d8 100644 --- a/tests/test_former.py +++ b/tests/test_former.py @@ -2,243 +2,686 @@ import json import os +import pytest +import toml import yaml from fconv.core import Former from fconv.formats.json import Json +from fconv.formats.toml import Toml from fconv.formats.yaml import Yaml -from .fixtures import JSON_FILE_PATH, YAML_FILE_PATH +from .fixtures import JSON_FILE_PATH, TOML_FILE_PATH, YAML_FILE_PATH ################ # JSON -> YAML ################ -def test_form_json_file_to_yaml_file(): - """ - Confirm converting json file to yaml file - """ - dt = datetime.datetime.now().strftime("%Y%m%d-%H:%M:%S") - out_name = f"{dt}.yaml" - - Former( - src_format=Json, - target_format=Yaml, - src_path=JSON_FILE_PATH, - target_path=out_name, - ).form() +class TestJsonToYamlIntegration: + def test_form_json_file_to_yaml_file(self): + """ + Confirm converting json file to yaml file + """ + dt = datetime.datetime.now().strftime("%Y%m%d-%H:%M:%S") + out_name = f"{dt}.yaml" + + Former( + src_format=Json, + target_format=Yaml, + src_path=JSON_FILE_PATH, + target_path=out_name, + ).form() + + # testing + with open(out_name) as f: + act = yaml.safe_load(f.read()) + assert act["country"] == "Japan" + assert act["user"][0]["age"] == 10 + assert act["user"][1]["name"] == "Hanako" + assert act["user"][1]["phone"][0] == "555-666-777" + os.remove(out_name) + + def test_form_json_file_to_yaml_str(self): + """ + Confirm converting json file to yaml string + """ + + r = Former( + src_format=Json, + target_format=Yaml, + src_path=JSON_FILE_PATH, + ).form() + + # testing + assert type(r) is str + act = yaml.safe_load(r) + assert act["country"] == "Japan" + assert act["user"][0]["age"] == 10 + assert act["user"][1]["name"] == "Hanako" + assert act["user"][1]["phone"][0] == "555-666-777" - # testing - with open(out_name) as f: - act = yaml.safe_load(f.read()) + def test_form_json_str_to_yaml_file(self): + """ + Confirm converting json string to yaml file + """ + dt = datetime.datetime.now().strftime("%Y%m%d-%H:%M:%S") + out_name = f"{dt}.yaml" + + src_ctx = None + with open(JSON_FILE_PATH) as f: + src_ctx = f.read() + + Former( + src_format=Json, + target_format=Yaml, + target_path=out_name, + ).form(src_ctx) + + # testing + with open(out_name) as f: + act = yaml.safe_load(f.read()) + assert act["country"] == "Japan" + assert act["user"][0]["age"] == 10 + assert act["user"][1]["name"] == "Hanako" + assert act["user"][1]["phone"][0] == "555-666-777" + os.remove(out_name) + + def test_form_json_str_to_yaml_str(self): + """ + Confirm converting json string to yaml file + """ + src_ctx = None + with open(JSON_FILE_PATH) as f: + src_ctx = f.read() + + r = Former( + src_format=Json, + target_format=Yaml, + ).form(src_ctx) + + assert type(r) is str + act = yaml.safe_load(r) + # testing assert act["country"] == "Japan" assert act["user"][0]["age"] == 10 assert act["user"][1]["name"] == "Hanako" assert act["user"][1]["phone"][0] == "555-666-777" - os.remove(out_name) - - -def test_form_json_file_to_yaml_str(): - """ - Confirm converting json file to yaml string - """ - - r = Former( - src_format=Json, - target_format=Yaml, - src_path=JSON_FILE_PATH, - ).form() - - # testing - assert type(r) is str - act = yaml.safe_load(r) - assert act["country"] == "Japan" - assert act["user"][0]["age"] == 10 - assert act["user"][1]["name"] == "Hanako" - assert act["user"][1]["phone"][0] == "555-666-777" - - -def test_form_json_str_to_yaml_file(): - """ - Confirm converting json string to yaml file - """ - dt = datetime.datetime.now().strftime("%Y%m%d-%H:%M:%S") - out_name = f"{dt}.yaml" - - src_ctx = None - with open(JSON_FILE_PATH) as f: - src_ctx = f.read() - - Former( - src_format=Json, - target_format=Yaml, - target_path=out_name, - ).form(src_ctx) - - # testing - with open(out_name) as f: - act = yaml.safe_load(f.read()) + + def test_form_json_file_to_yaml_file_with_opt(self): + """ + json to yaml + """ + dt = datetime.datetime.now().strftime("%Y%m%d-%H:%M:%S") + out_name = f"{dt}.yaml" + + Former( + src_format=Json, + target_format=Yaml, + src_path=JSON_FILE_PATH, + target_path=out_name, + in_opt={"parse_int": float}, + out_opt={"indent": 2}, + ).form() + + # testing + with open(out_name) as f: + act = yaml.safe_load(f.read()) + assert act["country"] == "Japan" + assert act["user"][0]["age"] == 10 + assert act["user"][1]["name"] == "Hanako" + assert act["user"][1]["phone"][0] == "555-666-777" + os.remove(out_name) + + +################ +# YAML -> JSON +################ + + +class TestYamlToJsonIntegration: + def test_form_yaml_file_to_json_file(self): + """ + yaml to json + """ + dt = datetime.datetime.now().strftime("%Y%m%d-%H:%M:%S") + out_name = f"{dt}.json" + + Former(Yaml, Json, src_path=YAML_FILE_PATH, target_path=out_name).form() + + # testing + with open(out_name) as f: + act = json.loads(f.read()) + assert act["country"] == "Japan" + assert act["user"][0]["age"] == 10 + assert act["user"][1]["name"] == "Hanako" + assert act["user"][1]["phone"][0] == "555-666-777" + os.remove(out_name) + + def test_form_yaml_file_to_json_str(self): + """ + Confirm converting yaml file to json string + """ + + r = Former(Yaml, Json, src_path=YAML_FILE_PATH).form() + + act = json.loads(r) + + # testing assert act["country"] == "Japan" assert act["user"][0]["age"] == 10 assert act["user"][1]["name"] == "Hanako" assert act["user"][1]["phone"][0] == "555-666-777" - os.remove(out_name) - - -def test_form_json_str_to_yaml_str(): - """ - Confirm converting json string to yaml file - """ - src_ctx = None - with open(JSON_FILE_PATH) as f: - src_ctx = f.read() - - r = Former( - src_format=Json, - target_format=Yaml, - ).form(src_ctx) - - assert type(r) is str - act = yaml.safe_load(r) - # testing - assert act["country"] == "Japan" - assert act["user"][0]["age"] == 10 - assert act["user"][1]["name"] == "Hanako" - assert act["user"][1]["phone"][0] == "555-666-777" - - -def test_form_json_file_to_yaml_file_with_opt(): - """ - json to yaml - """ - dt = datetime.datetime.now().strftime("%Y%m%d-%H:%M:%S") - out_name = f"{dt}.yaml" - - Former( - src_format=Json, - target_format=Yaml, - src_path=JSON_FILE_PATH, - target_path=out_name, - in_opt={"parse_int": float}, - out_opt={"indent": 2}, - ).form() - - # testing - with open(out_name) as f: - act = yaml.safe_load(f.read()) + + def test_form_yaml_str_to_json_file(self): + """ + Confirm converting yaml string to json file + """ + + dt = datetime.datetime.now().strftime("%Y%m%d-%H:%M:%S") + out_name = f"{dt}.json" + src_ctx = None + + with open(YAML_FILE_PATH) as f: + src_ctx = f.read() + + Former(Yaml, Json, target_path=out_name).form(src_ctx) + + # testing + with open(out_name) as f: + act = json.loads(f.read()) + assert act["country"] == "Japan" + assert act["user"][0]["age"] == 10 + assert act["user"][1]["name"] == "Hanako" + assert act["user"][1]["phone"][0] == "555-666-777" + os.remove(out_name) + + def test_form_yaml_str_to_json_str(self): + """ + Confirm converting yaml string to json string + """ + + src_ctx = None + + with open(YAML_FILE_PATH) as f: + src_ctx = f.read() + + r = Former(Yaml, Json).form(src_ctx) + + act = json.loads(r) + + # testing assert act["country"] == "Japan" assert act["user"][0]["age"] == 10 assert act["user"][1]["name"] == "Hanako" assert act["user"][1]["phone"][0] == "555-666-777" - os.remove(out_name) + + def test_form_yaml_file_to_json_file_with_opt(self): + """ + yaml to json + """ + dt = datetime.datetime.now().strftime("%Y%m%d-%H:%M:%S") + out_name = f"{dt}.json" + + Former( + Yaml, + Json, + src_path=YAML_FILE_PATH, + target_path=out_name, + out_opt={"indent": 3}, + ).form() + + # testing + with open(out_name) as f: + act = json.loads(f.read()) + assert act["country"] == "Japan" + assert act["user"][0]["age"] == 10 + assert act["user"][1]["name"] == "Hanako" + assert act["user"][1]["phone"][0] == "555-666-777" + os.remove(out_name) ################ -# YAML -> JSON +# JSON -> TOML ################ -def test_form_yaml_file_to_json_file(): - """ - yaml to json - """ - dt = datetime.datetime.now().strftime("%Y%m%d-%H:%M:%S") - out_name = f"{dt}.json" +class TestJsonToTomlIntegration: + def test_form_json_file_to_toml_file(self): + """ + Confirm converting json file to toml file + """ + dt = datetime.datetime.now().strftime("%Y%m%d-%H:%M:%S") + out_name = f"{dt}.toml" + + Former( + src_format=Json, + target_format=Toml, + src_path=JSON_FILE_PATH, + target_path=out_name, + ).form() + + # testing + with open(out_name) as f: + act = toml.loads(f.read()) + assert act["country"] == "Japan" + assert act["user"][0]["age"] == 10 + assert act["user"][1]["name"] == "Hanako" + assert act["user"][1]["phone"][0] == "555-666-777" + os.remove(out_name) + + def test_form_json_file_to_toml_str(self): + """ + Confirm converting json file to toml string + """ + + r = Former( + src_format=Json, + target_format=Toml, + src_path=JSON_FILE_PATH, + ).form() + + # testing + assert type(r) is str + act = toml.loads(r) + assert act["country"] == "Japan" + assert act["user"][0]["age"] == 10 + assert act["user"][1]["name"] == "Hanako" + assert act["user"][1]["phone"][0] == "555-666-777" + + def test_form_json_str_to_toml_file(self): + """ + Confirm converting json string to toml file + """ + dt = datetime.datetime.now().strftime("%Y%m%d-%H:%M:%S") + out_name = f"{dt}.toml" + + src_ctx = None + with open(JSON_FILE_PATH) as f: + src_ctx = f.read() + + Former( + src_format=Json, + target_format=Toml, + target_path=out_name, + ).form(src_ctx) + + # testing + with open(out_name) as f: + act = toml.loads(f.read()) + assert act["country"] == "Japan" + assert act["user"][0]["age"] == 10 + assert act["user"][1]["name"] == "Hanako" + assert act["user"][1]["phone"][0] == "555-666-777" + os.remove(out_name) + + def test_form_json_str_to_toml_str(self): + """ + Confirm converting json string to toml file + """ + src_ctx = None + with open(JSON_FILE_PATH) as f: + src_ctx = f.read() + + r = Former( + src_format=Json, + target_format=Toml, + ).form(src_ctx) + + assert type(r) is str + act = toml.loads(r) + # testing + assert act["country"] == "Japan" + assert act["user"][0]["age"] == 10 + assert act["user"][1]["name"] == "Hanako" + assert act["user"][1]["phone"][0] == "555-666-777" + + @pytest.mark.skip(reason="needless") + def test_form_json_file_to_toml_file_with_opt(self): + """ + json to toml + """ + dt = datetime.datetime.now().strftime("%Y%m%d-%H:%M:%S") + out_name = f"{dt}.toml" + + Former( + src_format=Json, + target_format=Toml, + src_path=JSON_FILE_PATH, + target_path=out_name, + in_opt={"parse_int": float}, + ).form() + + # testing + with open(out_name) as f: + act = toml.loads(f.read()) + assert act["country"] == "Japan" + assert act["user"][0]["age"] == 10 + assert act["user"][1]["name"] == "Hanako" + assert act["user"][1]["phone"][0] == "555-666-777" + os.remove(out_name) - Former(Yaml, Json, src_path=YAML_FILE_PATH, target_path=out_name).form() - # testing - with open(out_name) as f: - act = json.loads(f.read()) +################ +# TOML -> JSON +################ +class TestTomlToJsonIntegration: + def test_form_toml_file_to_json_file(self): + """ + toml to json + """ + dt = datetime.datetime.now().strftime("%Y%m%d-%H:%M:%S") + out_name = f"{dt}.json" + + Former(Toml, Json, src_path=TOML_FILE_PATH, target_path=out_name).form() + + # testing + with open(out_name) as f: + act = json.loads(f.read()) + assert act["country"] == "Japan" + assert act["user"][0]["age"] == 10 + assert act["user"][1]["name"] == "Hanako" + assert act["user"][1]["phone"][0] == "555-666-777" + os.remove(out_name) + + def test_form_toml_file_to_json_str(self): + """ + Confirm converting toml file to json string + """ + + r = Former(Toml, Json, src_path=TOML_FILE_PATH).form() + + act = json.loads(r) + + # testing assert act["country"] == "Japan" assert act["user"][0]["age"] == 10 assert act["user"][1]["name"] == "Hanako" assert act["user"][1]["phone"][0] == "555-666-777" - os.remove(out_name) + def test_form_toml_str_to_json_file(self): + """ + Confirm converting toml string to json file + """ + + dt = datetime.datetime.now().strftime("%Y%m%d-%H:%M:%S") + out_name = f"{dt}.json" + src_ctx = None + + with open(TOML_FILE_PATH) as f: + src_ctx = f.read() + + Former(Toml, Json, target_path=out_name).form(src_ctx) + + # testing + with open(out_name) as f: + act = json.loads(f.read()) + assert act["country"] == "Japan" + assert act["user"][0]["age"] == 10 + assert act["user"][1]["name"] == "Hanako" + assert act["user"][1]["phone"][0] == "555-666-777" + os.remove(out_name) -def test_form_yaml_file_to_json_str(): - """ - Confirm converting yaml file to json string - """ + def test_form_toml_str_to_json_str(self): + """ + Confirm converting toml string to json string + """ - r = Former(Yaml, Json, src_path=YAML_FILE_PATH).form() + src_ctx = None - act = json.loads(r) + with open(TOML_FILE_PATH) as f: + src_ctx = f.read() - # testing - assert act["country"] == "Japan" - assert act["user"][0]["age"] == 10 - assert act["user"][1]["name"] == "Hanako" - assert act["user"][1]["phone"][0] == "555-666-777" + r = Former(Toml, Json).form(src_ctx) + act = json.loads(r) -def test_form_yaml_str_to_json_file(): - """ - Confirm converting yaml string to json file - """ + # testing + assert act["country"] == "Japan" + assert act["user"][0]["age"] == 10 + assert act["user"][1]["name"] == "Hanako" + assert act["user"][1]["phone"][0] == "555-666-777" - dt = datetime.datetime.now().strftime("%Y%m%d-%H:%M:%S") - out_name = f"{dt}.json" - src_ctx = None + def test_form_toml_file_to_json_file_with_opt(self): + """ + toml to json + """ + dt = datetime.datetime.now().strftime("%Y%m%d-%H:%M:%S") + out_name = f"{dt}.json" + + Former( + Toml, + Json, + src_path=TOML_FILE_PATH, + target_path=out_name, + out_opt={"indent": 3}, + ).form() + + # testing + with open(out_name) as f: + act = json.loads(f.read()) + assert act["country"] == "Japan" + assert act["user"][0]["age"] == 10 + assert act["user"][1]["name"] == "Hanako" + assert act["user"][1]["phone"][0] == "555-666-777" + os.remove(out_name) - with open(YAML_FILE_PATH) as f: - src_ctx = f.read() - Former(Yaml, Json, target_path=out_name).form(src_ctx) +################ +# YAML -> TOML +################ +class TestYamlToTomlIntegration: + def test_form_yaml_file_to_toml_file(self): + """ + Confirm converting yaml file to toml file + """ + dt = datetime.datetime.now().strftime("%Y%m%d-%H:%M:%S") + out_name = f"{dt}.toml" + + Former( + src_format=Yaml, + target_format=Toml, + src_path=YAML_FILE_PATH, + target_path=out_name, + ).form() + + # testing + with open(out_name) as f: + act = toml.loads(f.read()) + assert act["country"] == "Japan" + assert act["user"][0]["age"] == 10 + assert act["user"][1]["name"] == "Hanako" + assert act["user"][1]["phone"][0] == "555-666-777" + os.remove(out_name) + + def test_form_yaml_file_to_toml_str(self): + """ + Confirm converting yaml file to toml string + """ + + r = Former( + src_format=Yaml, + target_format=Toml, + src_path=YAML_FILE_PATH, + ).form() + + # testing + assert type(r) is str + act = toml.loads(r) + assert act["country"] == "Japan" + assert act["user"][0]["age"] == 10 + assert act["user"][1]["name"] == "Hanako" + assert act["user"][1]["phone"][0] == "555-666-777" + + def test_form_yaml_str_to_toml_file(self): + """ + Confirm converting yaml string to toml file + """ + dt = datetime.datetime.now().strftime("%Y%m%d-%H:%M:%S") + out_name = f"{dt}.toml" + + src_ctx = None + with open(YAML_FILE_PATH) as f: + src_ctx = f.read() + + Former( + src_format=Yaml, + target_format=Toml, + target_path=out_name, + ).form(src_ctx) + + # testing + with open(out_name) as f: + act = toml.loads(f.read()) + assert act["country"] == "Japan" + assert act["user"][0]["age"] == 10 + assert act["user"][1]["name"] == "Hanako" + assert act["user"][1]["phone"][0] == "555-666-777" + os.remove(out_name) + + def test_form_yaml_str_to_toml_str(self): + """ + Confirm converting yaml string to toml file + """ + src_ctx = None + with open(YAML_FILE_PATH) as f: + src_ctx = f.read() + + r = Former( + src_format=Yaml, + target_format=Toml, + ).form(src_ctx) + + assert type(r) is str + act = toml.loads(r) + # testing + assert act["country"] == "Japan" + assert act["user"][0]["age"] == 10 + assert act["user"][1]["name"] == "Hanako" + assert act["user"][1]["phone"][0] == "555-666-777" - # testing - with open(out_name) as f: - act = json.loads(f.read()) + @pytest.mark.skip(reason="needless") + def test_form_yaml_file_to_toml_file_with_opt(self): + """ + yaml to toml + """ + dt = datetime.datetime.now().strftime("%Y%m%d-%H:%M:%S") + out_name = f"{dt}.toml" + + Former( + src_format=Yaml, + target_format=Toml, + src_path=YAML_FILE_PATH, + target_path=out_name, + ).form() + + # testing + with open(out_name) as f: + act = toml.loads(f.read()) + assert act["country"] == "Japan" + assert act["user"][0]["age"] == 10 + assert act["user"][1]["name"] == "Hanako" + assert act["user"][1]["phone"][0] == "555-666-777" + os.remove(out_name) + + +class TestTomlToYamlIntegration: + def test_form_toml_file_to_yaml_file(self): + """ + toml to yaml + """ + dt = datetime.datetime.now().strftime("%Y%m%d-%H:%M:%S") + out_name = f"{dt}.yaml" + + Former(Toml, Yaml, src_path=TOML_FILE_PATH, target_path=out_name).form() + + # testing + with open(out_name) as f: + act = yaml.safe_load(f.read()) + assert act["country"] == "Japan" + assert act["user"][0]["age"] == 10 + assert act["user"][1]["name"] == "Hanako" + assert act["user"][1]["phone"][0] == "555-666-777" + os.remove(out_name) + + def test_form_toml_file_to_yaml_str(self): + """ + Confirm converting toml file to yaml string + """ + + r = Former(Toml, Yaml, src_path=TOML_FILE_PATH).form() + + act = yaml.safe_load(r) + + # testing assert act["country"] == "Japan" assert act["user"][0]["age"] == 10 assert act["user"][1]["name"] == "Hanako" assert act["user"][1]["phone"][0] == "555-666-777" - os.remove(out_name) + def test_form_toml_str_to_yaml_file(self): + """ + Confirm converting toml string to yaml file + """ -def test_form_yaml_str_to_json_str(): - """ - Confirm converting yaml string to json string - """ + dt = datetime.datetime.now().strftime("%Y%m%d-%H:%M:%S") + out_name = f"{dt}.yaml" + src_ctx = None - src_ctx = None + with open(TOML_FILE_PATH) as f: + src_ctx = f.read() - with open(YAML_FILE_PATH) as f: - src_ctx = f.read() + Former(Toml, Yaml, target_path=out_name).form(src_ctx) - r = Former(Yaml, Json).form(src_ctx) + # testing + with open(out_name) as f: + act = yaml.safe_load(f.read()) + assert act["country"] == "Japan" + assert act["user"][0]["age"] == 10 + assert act["user"][1]["name"] == "Hanako" + assert act["user"][1]["phone"][0] == "555-666-777" + os.remove(out_name) - act = json.loads(r) + def test_form_toml_str_to_yaml_str(self): + """ + Confirm converting toml string to yaml string + """ - # testing - assert act["country"] == "Japan" - assert act["user"][0]["age"] == 10 - assert act["user"][1]["name"] == "Hanako" - assert act["user"][1]["phone"][0] == "555-666-777" + src_ctx = None + with open(TOML_FILE_PATH) as f: + src_ctx = f.read() -def test_form_yaml_file_to_json_file_with_opt(): - """ - yaml to json - """ - dt = datetime.datetime.now().strftime("%Y%m%d-%H:%M:%S") - out_name = f"{dt}.json" + r = Former(Toml, Yaml).form(src_ctx) - Former( - Yaml, - Json, - src_path=YAML_FILE_PATH, - target_path=out_name, - out_opt={"indent": 3}, - ).form() + act = yaml.safe_load(r) - # testing - with open(out_name) as f: - act = json.loads(f.read()) + # testing assert act["country"] == "Japan" assert act["user"][0]["age"] == 10 assert act["user"][1]["name"] == "Hanako" assert act["user"][1]["phone"][0] == "555-666-777" - os.remove(out_name) + + def test_form_toml_file_to_yaml_file_with_opt(self): + """ + toml to yaml + """ + dt = datetime.datetime.now().strftime("%Y%m%d-%H:%M:%S") + out_name = f"{dt}.yaml" + + Former( + Toml, + Yaml, + src_path=TOML_FILE_PATH, + target_path=out_name, + out_opt={"indent": 3}, + ).form() + + # testing + with open(out_name) as f: + act = yaml.safe_load(f.read()) + assert act["country"] == "Japan" + assert act["user"][0]["age"] == 10 + assert act["user"][1]["name"] == "Hanako" + assert act["user"][1]["phone"][0] == "555-666-777" + os.remove(out_name)