forked from cdgriffith/Box
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_converters.py
More file actions
92 lines (78 loc) · 2.82 KB
/
test_converters.py
File metadata and controls
92 lines (78 loc) · 2.82 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
import os
import shutil
from pathlib import Path
from test.common import movie_data, tmp_dir
import msgpack
import pytest
import ruamel.yaml as yaml
from box import BoxError
from box.converters import _from_toml, _to_json, _to_msgpack, _to_toml, _to_yaml
toml_string = """[movies.Spaceballs]
imdb_stars = 7.1
rating = "PG"
length = 96
Director = "Mel Brooks"
[[movies.Spaceballs.Stars]]
name = "Mel Brooks"
imdb = "nm0000316"
role = "President Skroob"
[[movies.Spaceballs.Stars]]
name = "John Candy"
imdb = "nm0001006"
role = "Barf"
"""
class TestConverters:
@pytest.fixture(autouse=True)
def temp_dir_cleanup(self):
shutil.rmtree(str(tmp_dir), ignore_errors=True)
try:
os.mkdir(str(tmp_dir))
except OSError:
pass
yield
shutil.rmtree(str(tmp_dir), ignore_errors=True)
def test_to_toml(self):
formatted = _to_toml(movie_data)
assert formatted.startswith("[movies.Spaceballs]")
def test_to_toml_file(self):
out_file = Path(tmp_dir, "toml_test.tml")
assert not out_file.exists()
_to_toml(movie_data, filename=out_file)
assert out_file.exists()
assert out_file.read_text().startswith("[movies.Spaceballs]")
def test_from_toml(self):
result = _from_toml(toml_string)
assert result["movies"]["Spaceballs"]["length"] == 96
def test_from_toml_file(self):
out_file = Path(tmp_dir, "toml_test.tml")
assert not out_file.exists()
out_file.write_text(toml_string)
result = _from_toml(filename=out_file)
assert result["movies"]["Spaceballs"]["length"] == 96
def test_bad_from_toml(self):
with pytest.raises(BoxError):
_from_toml()
def test_to_json(self):
m_file = os.path.join(tmp_dir, "movie_data")
movie_string = _to_json(movie_data)
assert "Rick Moranis" in movie_string
_to_json(movie_data, filename=m_file)
assert "Rick Moranis" in open(m_file).read()
assert json.load(open(m_file)) == json.loads(movie_string)
def test_to_yaml(self):
m_file = os.path.join(tmp_dir, "movie_data")
movie_string = _to_yaml(movie_data)
assert "Rick Moranis" in movie_string
_to_yaml(movie_data, filename=m_file)
assert "Rick Moranis" in open(m_file).read()
assert yaml.load(open(m_file), Loader=yaml.SafeLoader) == yaml.load(movie_string, Loader=yaml.SafeLoader)
def test_to_msgpack(self):
m_file = os.path.join(tmp_dir, "movie_data")
msg_data = _to_msgpack(movie_data)
assert b"Rick Moranis" in msg_data
_to_msgpack(movie_data, filename=m_file)
assert b"Rick Moranis" in open(m_file, "rb").read()
assert msgpack.unpack(open(m_file, "rb")) == msgpack.unpackb(msg_data)