test: add read/write tests for util.toml
Add two unit tests for our toml util module. - Write an object with util.toml, read it with util.toml, and compare written and read objects. - Write an object directly as a string, read it with util.toml, comparing with an expected object. A test that writes with util.toml, reads as string, and verifies the read string is difficult to do in a general way, because each toml module we support writes files in a slightly different way.
This commit is contained in:
parent
b496732a02
commit
fe1e310f2e
1 changed files with 43 additions and 0 deletions
43
test/mod/test_util_toml.py
Normal file
43
test/mod/test_util_toml.py
Normal file
|
|
@ -0,0 +1,43 @@
|
||||||
|
#
|
||||||
|
# Tests for the 'osbuild.util.toml' module
|
||||||
|
#
|
||||||
|
import os
|
||||||
|
from tempfile import TemporaryDirectory
|
||||||
|
|
||||||
|
from osbuild.util import toml
|
||||||
|
|
||||||
|
data_obj = {
|
||||||
|
"top": {
|
||||||
|
"t2-1": {
|
||||||
|
"list": ["a", "b", "c"]
|
||||||
|
},
|
||||||
|
"t2-2": {
|
||||||
|
"str": "test"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
data_str = """
|
||||||
|
[top.t2-1]
|
||||||
|
list = ["a", "b", "c"]
|
||||||
|
|
||||||
|
[top.t2-2]
|
||||||
|
str = "test"
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def test_write_read():
|
||||||
|
with TemporaryDirectory() as tmpdir:
|
||||||
|
path = os.path.join(tmpdir, "test.toml")
|
||||||
|
toml.dump_to_file(data_obj, path)
|
||||||
|
rdata = toml.load_from_file(path)
|
||||||
|
assert data_obj == rdata
|
||||||
|
|
||||||
|
|
||||||
|
def test_read():
|
||||||
|
with TemporaryDirectory() as tmpdir:
|
||||||
|
path = os.path.join(tmpdir, "test.toml")
|
||||||
|
with open(path, "w", encoding="utf-8") as test_file:
|
||||||
|
test_file.write(data_str)
|
||||||
|
rdata = toml.load_from_file(path)
|
||||||
|
assert rdata == data_obj
|
||||||
Loading…
Add table
Add a link
Reference in a new issue