1
|
|
|
# |
2
|
|
|
# Copyright (C) 2011 - 2016 Satoru SATOH <ssato at redhat.com> |
3
|
|
|
# |
4
|
|
|
# pylint: disable=missing-docstring |
5
|
|
|
import os.path |
6
|
|
|
import tempfile |
7
|
|
|
import unittest |
8
|
|
|
|
9
|
|
|
import anyconfig.compat |
10
|
|
|
|
11
|
|
|
from anyconfig.compat import OrderedDict |
12
|
|
|
from anyconfig.utils import is_dict_like |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
CNF_0 = dict(name="a", a=1, b=dict(b=[1, 2], c="C")) |
16
|
|
|
SCM_0 = {"type": "object", |
17
|
|
|
"properties": { |
18
|
|
|
"name": {"type": "string"}, |
19
|
|
|
"a": {"type": "integer"}, |
20
|
|
|
"b": {"type": "object", |
21
|
|
|
"properties": { |
22
|
|
|
"b": {"type": "array", |
23
|
|
|
"items": {"type": "integer"}}}}}} |
24
|
|
|
# :seealso: tests/00-cnf.json |
25
|
|
|
CNF_1 = {"a": 1, "b": {"b": [1, 2], "c": "C"}, "name": "aaa"} |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
def selfdir(): |
29
|
|
|
""" |
30
|
|
|
>>> os.path.exists(selfdir()) |
31
|
|
|
True |
32
|
|
|
""" |
33
|
|
|
return os.path.dirname(__file__) |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
def setup_workdir(): |
37
|
|
|
""" |
38
|
|
|
>>> workdir = setup_workdir() |
39
|
|
|
>>> assert workdir != '.' |
40
|
|
|
>>> assert workdir != '/' |
41
|
|
|
>>> os.path.exists(workdir) |
42
|
|
|
True |
43
|
|
|
>>> os.rmdir(workdir) |
44
|
|
|
""" |
45
|
|
|
return tempfile.mkdtemp(dir="/tmp", prefix="python-anyconfig-tests-") |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
def cleanup_workdir(workdir): |
49
|
|
|
""" |
50
|
|
|
FIXME: Danger! |
51
|
|
|
|
52
|
|
|
>>> from os import linesep as lsep |
53
|
|
|
>>> workdir = setup_workdir() |
54
|
|
|
>>> os.path.exists(workdir) |
55
|
|
|
True |
56
|
|
|
>>> open(os.path.join(workdir, "workdir.stamp"), 'w').write("OK!" + lsep) |
57
|
|
|
>>> cleanup_workdir(workdir) |
58
|
|
|
>>> os.path.exists(workdir) |
59
|
|
|
False |
60
|
|
|
""" |
61
|
|
|
assert workdir != '/' |
62
|
|
|
assert workdir != '.' |
63
|
|
|
|
64
|
|
|
os.system("rm -rf " + workdir) |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
def dicts_equal(dic, ref, ordered=False): |
68
|
|
|
"""Compare (maybe nested) dicts. |
69
|
|
|
""" |
70
|
|
|
if not is_dict_like(dic) or not is_dict_like(ref): |
71
|
|
|
return dic == ref |
72
|
|
|
|
73
|
|
|
fnc = list if ordered else sorted |
74
|
|
|
if fnc(dic.keys()) != fnc(ref.keys()): |
75
|
|
|
return False |
76
|
|
|
|
77
|
|
|
for key in ref.keys(): |
78
|
|
|
if key not in dic or not dicts_equal(dic[key], ref[key]): |
79
|
|
|
return False |
80
|
|
|
|
81
|
|
|
return True |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
def to_bytes(astr): |
85
|
|
|
""" |
86
|
|
|
Convert a string to bytes. Do nothing in python 2.6. |
87
|
|
|
""" |
88
|
|
|
return bytes(astr, 'utf-8') if anyconfig.compat.IS_PYTHON_3 else astr |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
class Test(unittest.TestCase): |
92
|
|
|
|
93
|
|
|
def test_dicts_equal(self): |
94
|
|
|
dic0 = {'a': 1} |
95
|
|
|
dic1 = OrderedDict((('a', [1, 2, 3]), |
96
|
|
|
('b', OrderedDict((('c', "CCC"), ))))) |
97
|
|
|
dic2 = dic1.copy() |
98
|
|
|
dic2["b"] = None |
99
|
|
|
|
100
|
|
|
dic3 = OrderedDict((('b', OrderedDict((('c', "CCC"), ))), |
101
|
|
|
('a', [1, 2, 3]))) |
102
|
|
|
|
103
|
|
|
self.assertTrue(dicts_equal({}, {})) |
104
|
|
|
self.assertTrue(dicts_equal(dic0, dic0)) |
105
|
|
|
self.assertTrue(dicts_equal(dic1, dic1)) |
106
|
|
|
self.assertTrue(dicts_equal(dic2, dic2)) |
107
|
|
|
self.assertTrue(dicts_equal(dic1, dic3)) |
108
|
|
|
|
109
|
|
|
self.assertFalse(dicts_equal(dic0, {})) |
110
|
|
|
self.assertFalse(dicts_equal(dic0, dic1)) |
111
|
|
|
self.assertFalse(dicts_equal(dic1, dic2)) |
112
|
|
|
self.assertFalse(dicts_equal(dic1, dic3, ordered=True)) |
113
|
|
|
|
114
|
|
|
# vim:sw=4:ts=4:et: |
115
|
|
|
|