|
a |
|
b/tests/test_preferences.py |
|
|
1 |
"""Test defaults and preferences |
|
|
2 |
Files tested: defaults.py |
|
|
3 |
""" |
|
|
4 |
|
|
|
5 |
import collections |
|
|
6 |
import os |
|
|
7 |
import unittest |
|
|
8 |
from shutil import copyfile |
|
|
9 |
|
|
|
10 |
from dosma.defaults import _Preferences |
|
|
11 |
|
|
|
12 |
import nested_lookup |
|
|
13 |
|
|
|
14 |
# Duplicate the resources file |
|
|
15 |
_test_preferences_sample_filepath = os.path.join( |
|
|
16 |
os.path.dirname(os.path.abspath(__file__)), "resources/preferences.yml" |
|
|
17 |
) |
|
|
18 |
_test_preferences_duplicate_filepath = os.path.join( |
|
|
19 |
os.path.dirname(os.path.abspath(__file__)), "resources/.test.preferences.yml" |
|
|
20 |
) |
|
|
21 |
|
|
|
22 |
|
|
|
23 |
class PreferencesMock(_Preferences): |
|
|
24 |
_preferences_filepath = _test_preferences_duplicate_filepath |
|
|
25 |
|
|
|
26 |
|
|
|
27 |
class TestPreferences(unittest.TestCase): |
|
|
28 |
@classmethod |
|
|
29 |
def setUpClass(cls): |
|
|
30 |
copyfile(_test_preferences_sample_filepath, _test_preferences_duplicate_filepath) |
|
|
31 |
|
|
|
32 |
@classmethod |
|
|
33 |
def tearDownClass(cls): |
|
|
34 |
if os.path.isfile(_test_preferences_duplicate_filepath): |
|
|
35 |
os.remove(_test_preferences_duplicate_filepath) |
|
|
36 |
|
|
|
37 |
def test_duplicate(self): |
|
|
38 |
"""Test duplicate instances share same config.""" |
|
|
39 |
a = PreferencesMock() |
|
|
40 |
b = PreferencesMock() |
|
|
41 |
|
|
|
42 |
assert a.config == b.config, "Configs must be the same dictionary." |
|
|
43 |
|
|
|
44 |
# Test with _Preferences to be certain |
|
|
45 |
a = _Preferences() |
|
|
46 |
b = _Preferences() |
|
|
47 |
|
|
|
48 |
assert a.config == b.config, "Configs must be the same dictionary." |
|
|
49 |
|
|
|
50 |
def test_get(self): |
|
|
51 |
a = PreferencesMock() |
|
|
52 |
|
|
|
53 |
# Raise error when key doesn't exist. |
|
|
54 |
with self.assertRaises(KeyError): |
|
|
55 |
a.get("sample-key") |
|
|
56 |
|
|
|
57 |
# Raise error when key is not specific. |
|
|
58 |
assert nested_lookup.get_occurrence_of_key(a.config, "foo") > 1 |
|
|
59 |
with self.assertRaises(KeyError): |
|
|
60 |
a.get("foo") |
|
|
61 |
|
|
|
62 |
# No error when lookup is specific. |
|
|
63 |
a.get("testing1/foo") |
|
|
64 |
|
|
|
65 |
def test_set(self): |
|
|
66 |
a = PreferencesMock() |
|
|
67 |
|
|
|
68 |
# Raise error when key doesn't exist. |
|
|
69 |
with self.assertRaises(KeyError): |
|
|
70 |
a.set("sample-key", "bar") |
|
|
71 |
|
|
|
72 |
# Raise error when key is not specific. |
|
|
73 |
assert nested_lookup.get_occurrence_of_key(a.config, "foo") > 1, "%s." % a.config |
|
|
74 |
with self.assertRaises(KeyError): |
|
|
75 |
a.set("foo", 100) |
|
|
76 |
|
|
|
77 |
# Raise error when value is not the same |
|
|
78 |
with self.assertRaises(TypeError): |
|
|
79 |
a.set("testing1/foo", "bar") |
|
|
80 |
|
|
|
81 |
# Success when using full path or prefix kwarg |
|
|
82 |
a.set("testing1/foo", 50) |
|
|
83 |
assert a.get("testing1/foo") == 50, "Value mismatch: got %s, expected %s" % ( |
|
|
84 |
a.get("testing1/foo"), |
|
|
85 |
50, |
|
|
86 |
) |
|
|
87 |
|
|
|
88 |
a.set("foo", 100, "testing1") |
|
|
89 |
assert a.get("testing1/foo") == 100, "Value mismatch: got %s, expected %s" % ( |
|
|
90 |
a.get("testing1/foo"), |
|
|
91 |
100, |
|
|
92 |
) |
|
|
93 |
|
|
|
94 |
def test_write(self): |
|
|
95 |
a = PreferencesMock() |
|
|
96 |
|
|
|
97 |
a.set("testing1/foo", 250) |
|
|
98 |
a.save() |
|
|
99 |
|
|
|
100 |
b = PreferencesMock() |
|
|
101 |
|
|
|
102 |
assert a.config == b.config, "Configs must be the same dictionary." |
|
|
103 |
assert a.get("testing1/foo") == b.get("testing1/foo") |
|
|
104 |
|
|
|
105 |
|
|
|
106 |
class TestPreferencesSchema(unittest.TestCase): |
|
|
107 |
@classmethod |
|
|
108 |
def setUpClass(cls): |
|
|
109 |
copyfile(_test_preferences_sample_filepath, _test_preferences_duplicate_filepath) |
|
|
110 |
|
|
|
111 |
@classmethod |
|
|
112 |
def tearDownClass(cls): |
|
|
113 |
if os.path.isfile(_test_preferences_duplicate_filepath): |
|
|
114 |
os.remove(_test_preferences_duplicate_filepath) |
|
|
115 |
|
|
|
116 |
def test_cmd_line_schema(self): |
|
|
117 |
"""Test that the command line schema for preferences is valid. |
|
|
118 |
|
|
|
119 |
Checks: |
|
|
120 |
- No overlapping aliases |
|
|
121 |
- Fields ['aliases', 'type', 'nargs', 'help'] present |
|
|
122 |
- All aliases are list and begin with '--' |
|
|
123 |
""" |
|
|
124 |
a = PreferencesMock() |
|
|
125 |
config_dict = a.cmd_line_flags() |
|
|
126 |
|
|
|
127 |
# Check to see not duplicates in aliases |
|
|
128 |
aliases = [] |
|
|
129 |
for k in config_dict.keys(): |
|
|
130 |
aliases.extend(config_dict[k]["aliases"]) |
|
|
131 |
|
|
|
132 |
alias_duplicates = [ |
|
|
133 |
item for item, count in collections.Counter(aliases).items() if count > 1 |
|
|
134 |
] |
|
|
135 |
assert len(alias_duplicates) == 0, "Duplicate aliases: %s" % alias_duplicates |
|
|
136 |
|
|
|
137 |
for k in config_dict.keys(): |
|
|
138 |
arg = config_dict[k] |
|
|
139 |
# Check to see each field has at least 4 primary keys |
|
|
140 |
for field in ["name", "aliases", "type", "nargs", "help"]: |
|
|
141 |
assert field in arg.keys(), "`%s` missing from %s" % (field, k) |
|
|
142 |
|
|
|
143 |
# Check type(aliases) is list |
|
|
144 |
assert type(arg["aliases"]) is list, "Aliases must be list - k" % k |
|
|
145 |
|
|
|
146 |
# Check to see each field has at least 4 primary keys |
|
|
147 |
for alias in arg["aliases"]: |
|
|
148 |
assert alias.startswith("--"), "Alias '%s' in %s must start with '--'" % (alias, k) |