Skip to content

Commit 7553282

Browse files
committed
test: add Python tests and example
1 parent a597300 commit 7553282

File tree

3 files changed

+61
-16
lines changed

3 files changed

+61
-16
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,9 +318,10 @@ See the file
318318
[`LICENSE`](LICENSE).
319319

320320
`example.toml` from <https://github.com/toml-lang/toml>.
321+
`example.cbor`,
321322
`example.json`,
322323
`example.msgpack`,
323-
`example.cbor`,
324+
`example.py`,
324325
`example.yml`,
325326
`tests/bin.msgpack`,
326327
and `tests/bin.yml`

example.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{'title': 'TOML Example',
2+
'owner': {'name': 'Tom Preston-Werner',
3+
'organization': 'GitHub',
4+
'bio': 'GitHub Cofounder & CEO\nLikes tater tots and beer.',
5+
'dob': datetime.datetime(1979, 5, 27, 7, 32, tzinfo=datetime.timezone.utc)},
6+
'database': {'server': '192.168.1.1',
7+
'ports': [8001, 8001, 8002],
8+
'connection_max': 5000,
9+
'enabled': True},
10+
'servers': {'alpha': {'ip': '10.0.0.1', 'dc': 'eqdc10'},
11+
'beta': {'ip': '10.0.0.2', 'dc': 'eqdc10', 'country': '中国'}},
12+
'clients': {'data': [['gamma', 'delta'], [1, 2]], 'hosts': ['alpha', 'omega']},
13+
'products': [{'name': 'Hammer', 'sku': 738594937},
14+
{'name': 'Nail', 'sku': 284758393, 'color': 'gray'}]}

tests/test_remarshal.py

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
def data_file_path(filename: str) -> str:
3737
path_list = []
38-
if re.match(r"example\.(json|msgpack|toml|yaml|cbor)$", filename):
38+
if re.match(r"example\.(json|msgpack|py|toml|yaml|cbor)$", filename):
3939
path_list.append("..")
4040
path_list.append(filename)
4141
return str(TEST_PATH.joinpath(*path_list))
@@ -140,6 +140,14 @@ def convert_and_read(tmp_path):
140140
)
141141

142142

143+
def patch_date(x: Any) -> Any:
144+
x["owner"]["dob"] = datetime.datetime(
145+
1979, 5, 27, 7, 32, 0, 0, datetime.timezone.utc
146+
)
147+
148+
return x
149+
150+
143151
class TestRemarshal:
144152
def test_cbor2cbor(self, convert_and_read) -> None:
145153
output = convert_and_read("example.cbor", "cbor", "cbor")
@@ -177,23 +185,16 @@ def test_yaml2yaml(self, convert_and_read) -> None:
177185
assert output == reference
178186

179187
def test_json2cbor(self, convert_and_read) -> None:
180-
def patch(x: Any) -> Any:
181-
x["owner"]["dob"] = datetime.datetime(
182-
1979, 5, 27, 7, 32, 0, 0, datetime.timezone.utc
183-
)
184-
return x
185-
186188
output = convert_and_read(
187189
"example.json",
188190
"json",
189-
"cbor",
190-
transform=patch,
191+
"msgpack",
192+
transform=patch_date,
191193
)
194+
reference = read_file("example.msgpack")
195+
assert output == reference
192196

193-
reference = read_file("example.cbor")
194-
assert_cbor_same(output, reference)
195-
196-
def test_json2msgpack(self, convert_and_read) -> None:
197+
def test_json2python(self, convert_and_read) -> None:
197198
def patch(x: Any) -> Any:
198199
x["owner"]["dob"] = datetime.datetime(
199200
1979, 5, 27, 7, 32, tzinfo=datetime.timezone.utc
@@ -203,10 +204,11 @@ def patch(x: Any) -> Any:
203204
output = convert_and_read(
204205
"example.json",
205206
"json",
206-
"msgpack",
207+
"python",
208+
indent=Defaults.PYTHON_INDENT,
207209
transform=patch,
208210
)
209-
reference = read_file("example.msgpack")
211+
reference = read_file("example.py")
210212
assert output == reference
211213

212214
def test_json2toml(self, convert_and_read) -> None:
@@ -248,6 +250,13 @@ def test_msgpack2json(self, convert_and_read) -> None:
248250
reference = read_file("example.json")
249251
assert output == reference
250252

253+
def test_msgpack2python(self, convert_and_read) -> None:
254+
output = convert_and_read(
255+
"example.msgpack", "msgpack", "python", indent=Defaults.PYTHON_INDENT
256+
)
257+
reference = read_file("example.py")
258+
assert output == reference
259+
251260
def test_msgpack2toml(self, convert_and_read) -> None:
252261
output = convert_and_read("example.msgpack", "msgpack", "toml")
253262
reference = read_file("example.toml")
@@ -287,6 +296,16 @@ def test_toml2msgpack(self, convert_and_read) -> None:
287296
reference = read_file("example.msgpack")
288297
assert output == reference
289298

299+
def test_toml2python(self, convert_and_read) -> None:
300+
output = convert_and_read(
301+
"example.toml",
302+
"toml",
303+
"python",
304+
indent=Defaults.PYTHON_INDENT,
305+
)
306+
reference = read_file("example.py")
307+
assert output == reference
308+
290309
def test_toml2yaml(self, convert_and_read) -> None:
291310
output = convert_and_read("example.toml", "toml", "yaml")
292311
reference = read_file("example.yaml")
@@ -321,6 +340,17 @@ def test_yaml2msgpack(self, convert_and_read) -> None:
321340
reference = read_file("example.msgpack")
322341
assert output == reference
323342

343+
def test_yaml2python(self, convert_and_read) -> None:
344+
output = convert_and_read(
345+
"example.yaml",
346+
"yaml",
347+
"python",
348+
indent=Defaults.PYTHON_INDENT,
349+
transform=patch_date,
350+
)
351+
reference = read_file("example.py")
352+
assert output == reference
353+
324354
def test_yaml2toml(self, convert_and_read) -> None:
325355
output = convert_and_read("example.yaml", "yaml", "toml")
326356
reference = read_file("example.toml")

0 commit comments

Comments
 (0)