File: test_toml_tests.py

package info (click to toggle)
python-tomlkit 0.6.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,296 kB
  • sloc: python: 3,280; sh: 5; makefile: 4
file content (52 lines) | stat: -rw-r--r-- 1,291 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import json
import pytest

from tomlkit import parse
from tomlkit._compat import decode
from tomlkit._compat import unicode
from tomlkit._utils import parse_rfc3339
from tomlkit.exceptions import TOMLKitError


def to_bool(s):
    assert s in ["true", "false"]

    return s == "true"


stypes = {
    "string": unicode,
    "bool": to_bool,
    "integer": int,
    "float": float,
    "datetime": parse_rfc3339,
}


def untag(value):
    if isinstance(value, list):
        return [untag(i) for i in value]
    elif "type" in value and "value" in value and len(value) == 2:
        if value["type"] in stypes:
            val = decode(value["value"])

            return stypes[value["type"]](val)
        elif value["type"] == "array":
            return [untag(i) for i in value["value"]]
        else:
            raise Exception("Unsupported type {}".format(value["type"]))
    else:
        return {k: untag(v) for k, v in value.items()}


def test_valid_decode(valid_case):
    json_val = untag(json.loads(valid_case["json"]))
    toml_val = parse(valid_case["toml"])

    assert toml_val == json_val
    assert toml_val.as_string() == valid_case["toml"]


def test_invalid_decode(invalid_decode_case):
    with pytest.raises(TOMLKitError):
        parse(invalid_decode_case["toml"])