Revision c8294257
Added by koszko over 1 year ago
test/haketilo_test/unit/test_patterns_query_tree.py | ||
---|---|---|
6 | 6 |
|
7 | 7 |
# This file is part of Haketilo |
8 | 8 |
# |
9 |
# Copyright (C) 2021, Wojtek Kosior
|
|
9 |
# Copyright (C) 2021, 2022 Wojtek Kosior <koszko@koszko.org>
|
|
10 | 10 |
# |
11 | 11 |
# This program is free software: you can redistribute it and/or modify |
12 | 12 |
# it under the terms of the CC0 1.0 Universal License as published by |
... | ... | |
70 | 70 |
returnval(branch); |
71 | 71 |
}''') |
72 | 72 |
assert branch == { |
73 |
'literal_match': None, |
|
74 |
'wildcard_matches': [None, None, None], |
|
75 |
'children': { |
|
73 |
'c': { |
|
76 | 74 |
'com': { |
77 |
'literal_match': None, |
|
78 |
'wildcard_matches': [None, None, None], |
|
79 |
'children': { |
|
75 |
'c': { |
|
80 | 76 |
'example': { |
81 |
'literal_match': ['some_item'], |
|
82 |
'wildcard_matches': [None, None, None], |
|
83 |
'children': { |
|
84 |
} |
|
77 |
'l': ['some_item'] |
|
85 | 78 |
} |
86 | 79 |
} |
87 | 80 |
} |
... | ... | |
95 | 88 |
returnval([branch, items_added]); |
96 | 89 |
}''', branch) |
97 | 90 |
assert items_added == 1 |
98 |
assert branch['children']['com']['children']['example']['literal_match'] \
|
|
99 |
== ['some_item', 'other_item']
|
|
91 |
assert branch['c']['com']['c']['example']['l'] \
|
|
92 |
== ['some_item', 'other_item'] |
|
100 | 93 |
|
101 | 94 |
for i in range(3): |
102 | 95 |
for expected_array in [['third_item'], ['third_item', '4th_item']]: |
... | ... | |
110 | 103 |
}''', |
111 | 104 |
branch, wildcard, expected_array[-1]) |
112 | 105 |
assert items_added == 2 |
113 |
sample = branch['children']['com']['children']['sample'] |
|
114 |
assert sample['wildcard_matches'][i] == expected_array |
|
115 |
assert sample['children'][wildcard]['literal_match'] \ |
|
116 |
== expected_array |
|
106 |
sample = branch['c']['com']['c']['sample'] |
|
107 |
assert sample[wildcard] == expected_array |
|
108 |
assert sample['c'][wildcard]['l'] == expected_array |
|
117 | 109 |
|
118 | 110 |
branch, items_added = execute_in_page( |
119 | 111 |
'''{ |
... | ... | |
124 | 116 |
}''', |
125 | 117 |
branch) |
126 | 118 |
assert items_added == 1 |
127 |
assert branch['children']['org']['children']['koszko']['children']['***']\
|
|
128 |
['children']['123']['literal_match'] == ['5th_item']
|
|
119 |
assert branch['c']['org']['c']['koszko']['c']['***']['c']['123']['l'] \
|
|
120 |
== ['5th_item'] |
|
129 | 121 |
|
130 | 122 |
# Let's verify that removing a nonexistent element doesn't modify the tree. |
131 | 123 |
branch2, items_removed = execute_in_page( |
... | ... | |
150 | 142 |
}''', |
151 | 143 |
branch) |
152 | 144 |
assert items_removed == 1 |
153 |
assert 'org' not in branch['children']
|
|
145 |
assert 'org' not in branch['c'] |
|
154 | 146 |
|
155 | 147 |
for i in range(3): |
156 | 148 |
for expected_array in [['third_item'], None]: |
... | ... | |
166 | 158 |
assert items_removed == 2 |
167 | 159 |
if i == 2 and expected_array == []: |
168 | 160 |
break |
169 |
sample = branch['children']['com']['children'].get('sample', {}) |
|
170 |
assert sample.get('wildcard_matches', [None, None, None])[i] \ |
|
161 |
sample = branch['c']['com']['c'].get('sample', {}) |
|
162 |
assert sample.get(wildcard) == expected_array |
|
163 |
assert sample.get('c', {}).get(wildcard, {}).get('l') \ |
|
171 | 164 |
== expected_array |
172 |
assert sample.get('children', {}).get(wildcard, {})\ |
|
173 |
.get('literal_match') == expected_array |
|
174 | 165 |
|
175 | 166 |
for i in range(2): |
176 | 167 |
branch, items_removed = execute_in_page( |
... | ... | |
182 | 173 |
branch) |
183 | 174 |
assert items_removed == 1 |
184 | 175 |
if i == 0: |
185 |
assert branch['children']['com']['children']['example']\ |
|
186 |
['literal_match'] == ['some_item'] |
|
187 |
else: |
|
188 |
assert branch == { |
|
189 |
'literal_match': None, |
|
190 |
'wildcard_matches': [None, None, None], |
|
191 |
'children': { |
|
192 |
} |
|
193 |
} |
|
176 |
assert branch['c']['com']['c']['example']['l'] == ['some_item'] |
|
177 |
|
|
178 |
assert branch == {} |
|
194 | 179 |
|
195 | 180 |
@pytest.mark.get_page('https://gotmyowndoma.in') |
196 | 181 |
def test_search_branch(execute_in_page): |
Also available in: Unified diff
optimize Pattern Query Tree for size of its JSON.stringify()'ed representation