-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_fil.py
82 lines (64 loc) 路 1.93 KB
/
test_fil.py
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
import itertools
import pytest
import tdir
try:
import tomlkit
TOMLKIT = bool(tomlkit)
except ImportError:
TOMLKIT = False
import fil
DATA = (
None,
False,
{},
[],
23,
32.5,
"five",
{"hello": "world"},
{"a": {"very": {"deep": {"one": "two"}}}},
[{"a": 1, "b": 2, "c": 3}, {"a": 9, "b": 7, "c": 9}, {}],
itertools,
)
PAIRS = itertools.product(fil.SUFFIX_TO_CLASS, DATA)
@pytest.mark.parametrize("suffix, data", list(PAIRS))
@tdir
def test_fil(suffix, data):
file_name = "data" + suffix
is_jl = "j" in suffix and "l" in suffix
if False and suffix == ".toml" and isinstance(data, dict):
with open(file_name, "wb") as fp:
if "hello" in data:
fp.write(b'hello = "world"\n')
else:
fp.write(b'[a.very.deep]\none = "two"\n')
round_trip = fil.read(file_name)
assert round_trip == data
expect_error = (
(suffix == ".txt" and not isinstance(data, str))
or (suffix == ".toml" and not (isinstance(data, dict) and TOMLKIT))
or (is_jl and not isinstance(data, list))
or (data is itertools)
)
if expect_error:
with pytest.raises(Exception) as e:
fil.write(data, file_name)
msg = e.value.args[0]
if is_jl:
expected = "JSON Line data must be iterable and not dict or str"
assert msg == expected
elif data is itertools:
assert msg == "cannot represent an object" or "module" in msg
else:
assert (
msg == "Install module `tomlkit` to use .toml files"
or "files only accept" in msg
)
else:
fil.write(data, file_name)
round_trip = fil.read(file_name)
if is_jl:
round_trip = list(round_trip)
assert data == round_trip
def test_default():
assert fil.read("non-existent.json", "none") == "none"