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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
|
# From https://github.com/uiri/toml
#
# The MIT License
#
# Copyright 2013-2019 William Pearson
# Copyright 2015-2016 Julien Enselme
# Copyright 2016 Google Inc.
# Copyright 2017 Samuel Vasko
# Copyright 2017 Nate Prewitt
# Copyright 2017 Jack Evans
# Copyright 2019 Filippo Broggini
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# stdlib
import copy
import datetime
import os
import pathlib
import sys
from decimal import Decimal
from typing import Any, Dict
# 3rd party
import pytest
# this package
import dom_toml
from dom_toml import dump, dumps, load, loads
from dom_toml.decoder import InlineTableDict
from dom_toml.encoder import TomlArraySeparatorEncoder, TomlEncoder, TomlNumpyEncoder, TomlPathlibEncoder
test_toml = os.path.abspath(os.path.join(__file__, "..", "test.toml"))
TEST_STR = """
[a]\r
b = 1\r
c = 2
"""
TEST_DICT: Dict[str, Any] = {'a': {'b': 1, 'c': 2}}
def test_bug_196():
d = datetime.datetime.now()
bug_dict = {'x': d}
round_trip_bug_dict: Dict[str, Any] = loads(dumps(bug_dict))
assert round_trip_bug_dict == bug_dict
assert round_trip_bug_dict['x'] == bug_dict['x']
# def test__dict():
def test_inline_dict():
class TestDict(InlineTableDict):
pass
t = copy.deepcopy(TEST_DICT)
t['d'] = TestDict()
t['d']['x'] = "abc"
o: Dict[str, Any] = loads(dumps(t, encoder=TomlEncoder(preserve=True)))
assert o == loads(dumps(o, encoder=TomlEncoder(preserve=True)))
@pytest.mark.parametrize(
"encoder_cls",
[
pytest.param(TomlArraySeparatorEncoder, id="type"),
pytest.param(TomlArraySeparatorEncoder(), id="instance"),
pytest.param(TomlArraySeparatorEncoder(separator=",\t"), id="instance_tab"),
]
)
def test_array_sep(encoder_cls):
d = {'a': [1, 2, 3]}
o: Dict[str, Any] = loads(dumps(d, encoder=encoder_cls))
assert o == loads(dumps(o, encoder=encoder_cls))
def test_numpy_floats():
np = pytest.importorskip("numpy")
encoder = TomlNumpyEncoder()
d = {'a': np.array([1, .3], dtype=np.float64)}
o: Dict[str, Any] = loads(dumps(d, encoder=encoder))
assert o == loads(dumps(o, encoder=encoder))
d = {'a': np.array([1, .3], dtype=np.float32)}
o = loads(dumps(d, encoder=encoder))
assert o == loads(dumps(o, encoder=encoder))
d = {'a': np.array([1, .3], dtype=np.float16)}
o = loads(dumps(d, encoder=encoder))
assert o == loads(dumps(o, encoder=encoder))
def test_numpy_ints():
np = pytest.importorskip("numpy")
encoder = TomlNumpyEncoder()
d = {'a': np.array([1, 3], dtype=np.int64)}
o: Dict[str, Any] = loads(dumps(d, encoder=encoder))
assert o == loads(dumps(o, encoder=encoder))
d = {'a': np.array([1, 3], dtype=np.int32)}
o = loads(dumps(d, encoder=encoder))
assert o == loads(dumps(o, encoder=encoder))
d = {'a': np.array([1, 3], dtype=np.int16)}
o = loads(dumps(d, encoder=encoder))
assert o == loads(dumps(o, encoder=encoder))
# @pytest.mark.parametrize(
# "encoder_cls",
# [
# pytest.param(toml_ordered.TomlOrderedEncoder, id="type"),
# pytest.param(toml_ordered.TomlOrderedEncoder(), id="instance"),
# ]
# )
# @pytest.mark.parametrize(
# "decoder_cls",
# [
# pytest.param(toml_ordered.TomlOrderedDecoder, id="type"),
# pytest.param(toml_ordered.TomlOrderedDecoder(), id="instance"),
# ]
# )
# def test_ordered(encoder_cls, decoder_cls):
# o: Dict[str, Any] = loads(dumps(TEST_DICT, encoder=encoder_cls), decoder=decoder_cls)
# assert o == loads(dumps(TEST_DICT, encoder=encoder_cls), decoder=decoder_cls)
def test_tuple():
d = {'a': (3, 4)}
o: Dict[str, Any] = loads(dumps(d))
assert o == loads(dumps(o))
@pytest.mark.skip(reason="unknown error")
def test_decimal():
PLACES = Decimal(10)**-4
d = {'a': Decimal("0.1")}
o: Dict[str, Any] = loads(dumps(d))
assert o == loads(dumps(o))
assert Decimal(o['a']).quantize(PLACES) == d['a'].quantize(PLACES)
with pytest.raises(TypeError):
loads(2) # type: ignore[arg-type]
if sys.version_info < (3, 12):
error_msg = "expected str, bytes or os.PathLike object, not int"
else:
error_msg = "argument should be a str or an os.PathLike object where __fspath__ returns a str, not 'int'"
with pytest.raises(TypeError, match=error_msg):
load(2) # type: ignore[arg-type]
if sys.version_info < (3, 12):
error_msg = "expected str, bytes or os.PathLike object, not list"
else:
error_msg = "argument should be a str or an os.PathLike object where __fspath__ returns a str, not 'list'"
with pytest.raises(TypeError, match=error_msg):
load([]) # type: ignore[arg-type]
if sys.version_info < (3, 12):
error_msg = "argument should be a str object or an os.PathLike object returning str, not <class 'bytes'>"
else:
error_msg = "argument should be a str or an os.PathLike object where __fspath__ returns a str, not 'bytes"
with pytest.raises(TypeError, match=error_msg):
load(b"test.toml") # type: ignore[arg-type]
class FakeFile:
def __init__(self):
self.written = ''
def write(self, s):
self.written += s
def read(self):
return self.written
def test_dump(tmp_pathplus):
dump(TEST_DICT, tmp_pathplus / "file.toml")
dump(load(tmp_pathplus / "file.toml"), tmp_pathplus / "file2.toml")
dump(load(tmp_pathplus / "file2.toml"), tmp_pathplus / "file3.toml")
assert (tmp_pathplus / "file2.toml").read_text() == (tmp_pathplus / "file3.toml").read_text()
def test_paths():
load(test_toml)
load(pathlib.Path(test_toml))
def test_nonexistent():
load(test_toml)
with pytest.raises(FileNotFoundError, match=r"No such file or directory: .*'nonexist.toml'\)?"):
load("nonexist.toml")
def test_commutativity():
o: Dict[str, Any] = loads(dumps(TEST_DICT))
assert o == loads(dumps(o))
@pytest.mark.parametrize(
"encoder_cls", [
pytest.param(TomlPathlibEncoder, id="type"),
pytest.param(TomlPathlibEncoder(), id="instance"),
]
)
def test_pathlib(encoder_cls):
o = {"root": {"path": pathlib.Path("/home/edgy")}}
sep = "\\\\" if os.sep == '\\' else '/'
test_str = f"""[root]
path = "{sep}home{sep}edgy"
"""
assert test_str == dumps(o, encoder=encoder_cls)
dom_toml.loads(test_str)
def test_deepcopy_timezone():
o: Dict[str, Any] = loads("dob = 1979-05-24T07:32:00-08:00")
o2: Dict[str, Any] = copy.deepcopy(o)
assert o2["dob"] == o["dob"]
assert o2["dob"] is not o["dob"]
|