Skip to content

Commit

Permalink
add: toml format.
Browse files Browse the repository at this point in the history
  • Loading branch information
wf001 committed Nov 22, 2022
1 parent c911bdb commit ab81694
Show file tree
Hide file tree
Showing 7 changed files with 701 additions and 175 deletions.
3 changes: 2 additions & 1 deletion fconv/formats/__init__.py
Original file line number Diff line number Diff line change
@@ -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():
Expand Down
20 changes: 20 additions & 0 deletions fconv/formats/toml.py
Original file line number Diff line number Diff line change
@@ -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)
1 change: 1 addition & 0 deletions tests/fixtures/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
11 changes: 11 additions & 0 deletions tests/fixtures/test.toml
Original file line number Diff line number Diff line change
@@ -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
4 changes: 3 additions & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
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(
"argv",
[
[__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],
],
)
Expand Down
48 changes: 48 additions & 0 deletions tests/test_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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
Loading

0 comments on commit ab81694

Please sign in to comment.