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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import unittest
from dotty_dict import Dotty, dotty
class TestDottyValueAccess(unittest.TestCase):
def setUp(self):
self.dot = dotty({
'flat_key': 'flat value',
'deep': {
'nested': 12,
'deeper': {
'secret': 'abcd',
'ridiculous': {
'hell': 'is here',
},
},
},
})
def test_access_flat_value(self):
self.assertEqual(self.dot['flat_key'], 'flat value')
# noinspection PyUnusedLocal
def test_raise_key_error_if_key_does_not_exist(self):
with self.assertRaises(KeyError):
val = self.dot['not_existing'] # noqa
def test_access_deep_nested_value(self):
self.assertEqual(self.dot['deep.nested'], 12)
def test_access_middle_nested_value(self):
self.assertDictEqual(self.dot['deep.deeper.ridiculous'],
{'hell': 'is here'})
def test_set_flat_value(self):
self.dot['new_flat'] = 'super flat'
self.assertIn('new_flat', self.dot)
def test_set_deep_nested_value(self):
self.dot['deep.new_key'] = 'new value'
self.assertIn('new_key', self.dot['deep'])
def test_set_new_deeply_nested_value(self):
self.dot['other.chain.of.keys'] = True
self.assertDictEqual(self.dot._data, {
'flat_key': 'flat value',
'deep': {
'nested': 12,
'deeper': {
'secret': 'abcd',
'ridiculous': {
'hell': 'is here',
},
},
},
'other': {
'chain': {
'of': {
'keys': True,
},
},
},
})
def test_dotty_has_flat_key(self):
self.assertIn('flat_key', self.dot)
def test_dotty_has_deeply_nested_key(self):
self.assertIn('deep.nested', self.dot)
def test_dotty_has_not_flat_key(self):
self.assertNotIn('some_key', self.dot)
def test_dotty_has_not_deeply_nested_key(self):
self.assertNotIn('deep.other.chain', self.dot)
def test_has_in(self):
result = 'deep.deeper.secret' in self.dot
self.assertTrue(result)
def test_has_not_in(self):
result = 'deep.other' in self.dot
self.assertFalse(result)
def test_delete_flat_key(self):
del self.dot['flat_key']
self.assertDictEqual(self.dot._data, {
'deep': {
'nested': 12,
'deeper': {
'secret': 'abcd',
'ridiculous': {
'hell': 'is here',
},
},
},
})
def test_delete_nested_key(self):
del self.dot['deep.deeper.secret']
self.assertDictEqual(self.dot._data, {
'flat_key': 'flat value',
'deep': {
'nested': 12,
'deeper': {
'ridiculous': {
'hell': 'is here',
},
},
},
})
def test_raise_key_error_on_delete_not_existing_key(self):
with self.assertRaises(KeyError):
del self.dot['deep.deeper.key']
def test_set_value_with_escaped_separator(self):
self.dot[r'deep.deeper.escaped\.dot_key'] = 'it works!'
self.assertDictEqual(self.dot._data, {
'flat_key': 'flat value',
'deep': {
'nested': 12,
'deeper': {
'escaped.dot_key': 'it works!',
'secret': 'abcd',
'ridiculous': {
'hell': 'is here',
},
},
},
})
def test_get_value_with_escaped_separator(self):
dot = dotty({
'flat_key': 'flat value',
'deep': {
'nested': 12,
'deeper': {
'escaped.dot_key': 'it works!',
'secret': 'abcd',
'ridiculous': {
'hell': 'is here',
},
},
},
})
result = dot[r'deep.deeper.escaped\.dot_key']
self.assertEqual(result, 'it works!')
def test_get_value_with_escaped_escape_separator(self):
dot = dotty({
'flat_key': 'flat value',
'deep': {
'nested': 12,
'deeper': {
'escaped\\': {
'dot_key': 'it works!',
},
'secret': 'abcd',
'ridiculous': {
'hell': 'is here',
},
},
},
})
result = dot[r'deep.deeper.escaped\\.dot_key']
self.assertEqual(result, 'it works!')
def test_use_custom_separator_and_custom_escape_char(self):
sep = ','
esc = '$'
dot = Dotty({}, separator=sep, esc_char=esc)
dot['abcd,efg,hij'] = 'test'
dot['abcd,efg$,hij'] = 'test2'
dot[r'abcd,efg\$,hij'] = 'test3'
self.assertDictEqual(dot._data, {
'abcd': {
'efg': {
'hij': 'test',
},
'efg,hij': 'test2',
'efg$': {
'hij': 'test3',
},
},
})
def test_string_digit_key(self):
dot = dotty({'field': {
'1': 'one',
'5': 'five'
}})
dict_one = dot['field.1']
dict_five = dot['field.5']
self.assertEqual(dict_one, 'one')
self.assertEqual(dict_five, 'five')
def test_integer_keys(self):
dot = dotty({'field': {
1: 'one',
5: 'five'
}})
dict_one = dot['field.1']
dict_five = dot['field.5']
self.assertEqual(dict_one, 'one')
self.assertEqual(dict_five, 'five')
def test_data_gathering_with_int(self):
dot = dotty({
'2': 'string_value',
2: 'int_value',
'nested': {
'2': 'nested_string_value',
3: 'nested_int_value'
}
})
dict_string = dot['2']
dict_int = dot[2]
nested_dict_string = dot['nested.2']
nested_dict_int = dot['nested.3']
self.assertEqual(dict_string, 'string_value')
self.assertEqual(dict_int, 'int_value')
self.assertEqual(nested_dict_string, 'nested_string_value')
self.assertEqual(nested_dict_int, 'nested_int_value')
def test_non_standard_key_types(self):
dot = Dotty({
3.3: 'float',
True: 'bool',
None: 'None',
'nested': {
4.4: 'nested_float'
}
}, separator=',')
dict_float = dot[3.3]
dict_bool = dot[True]
dict_none = dot[None]
nested_dict_float = dot['nested,4.4']
self.assertEqual(dict_float, 'float')
self.assertEqual(dict_bool, 'bool')
self.assertEqual(dict_none, 'None')
self.assertEqual(nested_dict_float, 'nested_float')
|