def test_trie_dict_copy(): t = Trie() t.put(['x'], 'value') t.put(['x', 'y'], 'value') clone = t.clone() assert t.flatten() == clone.flatten() assert t.to_json() == clone.to_json() assert _join_keys(t.keys([])) == _join_keys(clone.keys([])) assert _join_items(t.items([])) == _join_items(clone.items([]))
def test_trie_dict_hypothesis(): random.seed(0) trials = 10000 size = 8 key_len = 8 for _ in range(trials): t = Trie() normal_dict = {} # insert num_keys = random.randint(0, size) for _ in range(num_keys): key = ''.join( random.choices('a0!///', k=random.randint(1, key_len))) value = str(random.randint(1, key_len)) clean_key = '/'.join(split(key)) t.put(split(key), value) normal_dict[clean_key] = value assert split(key) in t assert t.flatten() == normal_dict assert set(_join_items(t.items([]))) == set(normal_dict.items()) assert set(_join_keys(t.keys([]))) == set(normal_dict.keys()) assert len(t) == len(normal_dict) # clone clone = t.clone() assert t.flatten() == clone.flatten() assert t.to_json() == clone.to_json() assert _join_keys(t.keys([])) == _join_keys(clone.keys([])) assert _join_items(t.items([])) == _join_items(clone.items([])) # delete for key in list(normal_dict.keys()): del normal_dict[key] t.discard(split(key)) assert split(key) not in t assert t.flatten() == normal_dict assert set(_join_items(t.items([]))) == set(normal_dict.items()) assert set(_join_keys(t.keys([]))) == set(normal_dict.keys()) assert len(t) == len(normal_dict)
def test_to_json_single(): t = Trie() t.put(['x'], 'value') assert t.to_json() == {'x': 'value'}
def test_to_json_multiple(): t = Trie() t.put(['x'], 'value1') t.put(['x', 'y'], 'value2') t.put(['z'], 'value3') assert t.to_json() == {'x': ('value1', {'y': 'value2'}), 'z': 'value3'}
def test_to_json_empty(): t = Trie() assert t.to_json() == {}