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
|
# Copyright (c) 2017-2019 David Steele <dsteele@gmail.com>
#
# SPDX-License-Identifier: GPL-2.0-or-later
# License-Filename: LICENSE
import os
import shutil
import tempfile
import pytest
from comitup.persist import persist
@pytest.fixture(scope="module")
def dir_fxt(request):
dir = tempfile.mkdtemp()
def fin():
shutil.rmtree(dir)
request.addfinalizer(fin)
return dir
count = 0
@pytest.fixture()
def jsonpath(request, dir_fxt):
global count
path = os.path.join(dir_fxt, "persist%d.json" % count)
count += 1
return path
def test_persist_is_dict(jsonpath):
mydict = persist(jsonpath)
mydict["a"] = "b"
assert mydict["a"] == "b"
def test_persist_default(jsonpath):
mydict = persist(jsonpath, {"a": "b"})
assert mydict["a"] == "b"
def test_persist_default_persists(jsonpath):
persist(jsonpath, {"a": "b"})
new = persist(jsonpath)
assert new["a"] == "b"
def test_persist_override_default(jsonpath):
persist(jsonpath, {"a": "b"})
new = persist(jsonpath, {"a": "c"})
assert new["a"] == "b"
def test_persist_override_default2(jsonpath):
mydict = persist(jsonpath, {"a": "a"})
mydict["a"] = "b"
new = persist(jsonpath, {"a": "c"})
assert new["a"] == "b"
def test_persist_update(jsonpath):
mydict = persist(jsonpath, {"a": "a"})
mydict.update({"a": "b"})
new = persist(jsonpath, {"a": "c"})
assert new["a"] == "b"
def test_persist_setdefault(jsonpath):
mydict = persist(jsonpath)
mydict.setdefault("a", "b")
new = persist(jsonpath, {"a": "c"})
assert new["a"] == "b"
def test_persist_setattr(jsonpath):
mydict = persist(jsonpath)
mydict.a = "b"
assert mydict["a"] == "b"
def test_persist_getattr(jsonpath):
mydict = persist(jsonpath)
mydict["a"] = "b"
assert mydict.a == "b"
def test_persist_persist_setattr(jsonpath):
mydict = persist(jsonpath)
mydict.a = "b"
new = persist(jsonpath)
assert new["a"] == "b"
def test_persist_persist_getattr(jsonpath):
mydict = persist(jsonpath)
mydict["a"] = "b"
new = persist(jsonpath)
assert new.a == "b"
def test_persist_file_format(jsonpath):
mydict = persist(jsonpath)
mydict["a"] = "b"
expected = '{\n "a": "b"\n}'
assert open(jsonpath, "r").read() == expected
def test_persist_get_attr_dict(jsonpath):
mydict = persist(jsonpath)
assert mydict._path == jsonpath
assert mydict.__getattr__("_path") == jsonpath
def test_persist_set_attr_dict(jsonpath):
mydict = persist(jsonpath)
mydict["_path"] = "foo"
mydict["a"] = "b"
newdict = persist(jsonpath)
assert newdict.a == "b"
|