Revision e1282a63
Added by koszko almost 2 years ago
test/unit/test_patterns_query_tree.py | ||
---|---|---|
27 | 27 |
|
28 | 28 |
def test_modify_branch(execute_in_page, patterns_tree_code): |
29 | 29 |
""" |
30 |
patterns_query_tree.js contains Patterns Tree data structure that allows
|
|
30 |
patterns_query_tree.js contains Pattern Tree data structure that allows |
|
31 | 31 |
arrays of string labels to be mapped to items. |
32 | 32 |
Verify operations modifying a single branch of such tree work properly. |
33 | 33 |
""" |
... | ... | |
68 | 68 |
# the right result. |
69 | 69 |
branch = execute_in_page( |
70 | 70 |
'''{ |
71 |
const branch = make_tree_node();
|
|
71 |
const branch = empty_node();
|
|
72 | 72 |
modify_sequence(branch, ['com', 'example'], item_adder('some_item')); |
73 | 73 |
returnval(branch); |
74 | 74 |
}''') |
... | ... | |
197 | 197 |
|
198 | 198 |
def test_search_branch(execute_in_page, patterns_tree_code): |
199 | 199 |
""" |
200 |
patterns_query_tree.js contains Patterns Tree data structure that allows
|
|
200 |
patterns_query_tree.js contains Pattern Tree data structure that allows |
|
201 | 201 |
arrays of string labels to be mapped to items. |
202 | 202 |
Verify searching a single branch of such tree work properly. |
203 | 203 |
""" |
... | ... | |
210 | 210 |
# Let's construct some tree branch to test on. |
211 | 211 |
execute_in_page( |
212 | 212 |
''' |
213 |
var branch = make_tree_node();
|
|
213 |
var branch = empty_node();
|
|
214 | 214 |
|
215 | 215 |
for (const [item, sequence] of [ |
216 | 216 |
['(root)', []], |
... | ... | |
281 | 281 |
print('sequence:', sequence, '\nexpected:', expected, |
282 | 282 |
'\nresult:', result, file=sys.stderr) |
283 | 283 |
raise e from None |
284 |
|
|
285 |
def test_pattern_tree(execute_in_page, patterns_tree_code): |
|
286 |
""" |
|
287 |
patterns_query_tree.js contains Pattern Tree data structure that allows |
|
288 |
arrays of string labels to be mapped to items. |
|
289 |
Verify operations on entire such tree work properly. |
|
290 |
""" |
|
291 |
execute_in_page(patterns_tree_code, page='https://gotmyowndoma.in') |
|
292 |
|
|
293 |
# Perform tests with all possible patterns for a simple URL. |
|
294 |
url = 'https://example.com' |
|
295 |
patterns = [ |
|
296 |
'https://example.com', |
|
297 |
'https://example.com/***', |
|
298 |
'https://***.example.com', |
|
299 |
'https://***.example.com/***' |
|
300 |
] |
|
301 |
bad_patterns = [ |
|
302 |
'http://example.com', |
|
303 |
'https://a.example.com', |
|
304 |
'https://*.example.com', |
|
305 |
'https://**.example.com', |
|
306 |
'https://example.com/a', |
|
307 |
'https://example.com/*', |
|
308 |
'https://example.com/**', |
|
309 |
] |
|
310 |
|
|
311 |
expected = [{'key': p} for p in patterns] |
|
312 |
|
|
313 |
tree, result = execute_in_page( |
|
314 |
'''{ |
|
315 |
const tree = pattern_tree.make(); |
|
316 |
for (const pattern of arguments[0].concat(arguments[1])) { |
|
317 |
pattern_tree.register(tree, pattern, 'key', pattern); |
|
318 |
pattern_tree.register(tree, pattern + '/', 'key', pattern + '/'); |
|
319 |
} |
|
320 |
returnval([tree, [...pattern_tree.search(tree, arguments[2])]]); |
|
321 |
}''', |
|
322 |
patterns, bad_patterns, url) |
|
323 |
assert expected == result |
|
324 |
|
|
325 |
# Also verify that deregistering half of the good patterns works correctly. |
|
326 |
patterns_removed = [pattern for i, pattern in enumerate(patterns) if i % 2] |
|
327 |
patterns = [pattern for i, pattern in enumerate(patterns) if not (i % 2)] |
|
328 |
expected = [{'key': p} for p in patterns] |
|
329 |
tree, result = execute_in_page( |
|
330 |
'''{ |
|
331 |
const tree = arguments[0]; |
|
332 |
for (const pattern of arguments[1]) { |
|
333 |
pattern_tree.deregister(tree, pattern, 'key'); |
|
334 |
pattern_tree.deregister(tree, pattern + '/', 'key'); |
|
335 |
} |
|
336 |
returnval([tree, [...pattern_tree.search(tree, arguments[2])]]); |
|
337 |
}''', |
|
338 |
tree, patterns_removed, url) |
|
339 |
assert expected == result |
|
340 |
|
|
341 |
# Also verify that deregistering all the patterns works correctly. |
|
342 |
tree = execute_in_page( |
|
343 |
'''{ |
|
344 |
const tree = arguments[0]; |
|
345 |
for (const pattern of arguments[1].concat(arguments[2])) { |
|
346 |
pattern_tree.deregister(tree, pattern, 'key'); |
|
347 |
pattern_tree.deregister(tree, pattern + '/', 'key'); |
|
348 |
} |
|
349 |
returnval(tree); |
|
350 |
}''', |
|
351 |
tree, patterns, bad_patterns) |
|
352 |
assert tree == {} |
|
353 |
|
|
354 |
# Perform tests with all possible patterns for a complex URL. |
|
355 |
url = 'http://settings.query.example.com/google/tries/destroy/adblockers//' |
|
356 |
patterns = [ |
|
357 |
'http://settings.query.example.com/google/tries/destroy/adblockers', |
|
358 |
'http://settings.query.example.com/google/tries/destroy/adblockers/***', |
|
359 |
'http://settings.query.example.com/google/tries/destroy/*', |
|
360 |
'http://settings.query.example.com/google/tries/destroy/***', |
|
361 |
'http://settings.query.example.com/google/tries/**', |
|
362 |
'http://settings.query.example.com/google/tries/***', |
|
363 |
'http://settings.query.example.com/google/**', |
|
364 |
'http://settings.query.example.com/google/***', |
|
365 |
'http://settings.query.example.com/**', |
|
366 |
'http://settings.query.example.com/***', |
|
367 |
|
|
368 |
'http://***.settings.query.example.com/google/tries/destroy/adblockers', |
|
369 |
'http://***.settings.query.example.com/google/tries/destroy/adblockers/***', |
|
370 |
'http://***.settings.query.example.com/google/tries/destroy/*', |
|
371 |
'http://***.settings.query.example.com/google/tries/destroy/***', |
|
372 |
'http://***.settings.query.example.com/google/tries/**', |
|
373 |
'http://***.settings.query.example.com/google/tries/***', |
|
374 |
'http://***.settings.query.example.com/google/**', |
|
375 |
'http://***.settings.query.example.com/google/***', |
|
376 |
'http://***.settings.query.example.com/**', |
|
377 |
'http://***.settings.query.example.com/***', |
|
378 |
'http://*.query.example.com/google/tries/destroy/adblockers', |
|
379 |
'http://*.query.example.com/google/tries/destroy/adblockers/***', |
|
380 |
'http://*.query.example.com/google/tries/destroy/*', |
|
381 |
'http://*.query.example.com/google/tries/destroy/***', |
|
382 |
'http://*.query.example.com/google/tries/**', |
|
383 |
'http://*.query.example.com/google/tries/***', |
|
384 |
'http://*.query.example.com/google/**', |
|
385 |
'http://*.query.example.com/google/***', |
|
386 |
'http://*.query.example.com/**', |
|
387 |
'http://*.query.example.com/***', |
|
388 |
'http://***.query.example.com/google/tries/destroy/adblockers', |
|
389 |
'http://***.query.example.com/google/tries/destroy/adblockers/***', |
|
390 |
'http://***.query.example.com/google/tries/destroy/*', |
|
391 |
'http://***.query.example.com/google/tries/destroy/***', |
|
392 |
'http://***.query.example.com/google/tries/**', |
|
393 |
'http://***.query.example.com/google/tries/***', |
|
394 |
'http://***.query.example.com/google/**', |
|
395 |
'http://***.query.example.com/google/***', |
|
396 |
'http://***.query.example.com/**', |
|
397 |
'http://***.query.example.com/***', |
|
398 |
'http://**.example.com/google/tries/destroy/adblockers', |
|
399 |
'http://**.example.com/google/tries/destroy/adblockers/***', |
|
400 |
'http://**.example.com/google/tries/destroy/*', |
|
401 |
'http://**.example.com/google/tries/destroy/***', |
|
402 |
'http://**.example.com/google/tries/**', |
|
403 |
'http://**.example.com/google/tries/***', |
|
404 |
'http://**.example.com/google/**', |
|
405 |
'http://**.example.com/google/***', |
|
406 |
'http://**.example.com/**', |
|
407 |
'http://**.example.com/***', |
|
408 |
'http://***.example.com/google/tries/destroy/adblockers', |
|
409 |
'http://***.example.com/google/tries/destroy/adblockers/***', |
|
410 |
'http://***.example.com/google/tries/destroy/*', |
|
411 |
'http://***.example.com/google/tries/destroy/***', |
|
412 |
'http://***.example.com/google/tries/**', |
|
413 |
'http://***.example.com/google/tries/***', |
|
414 |
'http://***.example.com/google/**', |
|
415 |
'http://***.example.com/google/***', |
|
416 |
'http://***.example.com/**', |
|
417 |
'http://***.example.com/***' |
|
418 |
] |
|
419 |
bad_patterns = [ |
|
420 |
'https://settings.query.example.com/google/tries/destroy/adblockers', |
|
421 |
'http://settings.query.example.com/google/tries/destroy/adblockers/a', |
|
422 |
'http://settings.query.example.com/google/tries/destroy/adblockers/*', |
|
423 |
'http://settings.query.example.com/google/tries/destroy/adblockers/**', |
|
424 |
'http://settings.query.example.com/google/tries/destroy/a', |
|
425 |
'http://settings.query.example.com/google/tries/destroy/**', |
|
426 |
'http://settings.query.example.com/google/tries/*', |
|
427 |
'http://a.settings.query.example.com/google/tries/destroy/adblockers', |
|
428 |
'http://*.settings.query.example.com/google/tries/destroy/adblockers', |
|
429 |
'http://**.settings.query.example.com/google/tries/destroy/adblockers', |
|
430 |
'http://a.query.example.com/google/tries/destroy/adblockers', |
|
431 |
'http://**.query.example.com/google/tries/destroy/adblockers', |
|
432 |
'http://*.example.com/google/tries/destroy/adblockers' |
|
433 |
] |
|
434 |
|
|
435 |
expected = [{'key': p + s} for p in patterns for s in ['/', '']] |
|
436 |
|
|
437 |
tree, result = execute_in_page( |
|
438 |
'''{ |
|
439 |
const tree = pattern_tree.make(); |
|
440 |
for (const pattern of arguments[0].concat(arguments[1])) { |
|
441 |
pattern_tree.register(tree, pattern, 'key', pattern); |
|
442 |
pattern_tree.register(tree, pattern + '/', 'key', pattern + '/'); |
|
443 |
} |
|
444 |
returnval([tree, [...pattern_tree.search(tree, arguments[2])]]); |
|
445 |
}''', |
|
446 |
patterns, bad_patterns, url) |
|
447 |
assert expected == result |
|
448 |
|
|
449 |
# Also verify that deregistering all patterns with trailing slash works |
|
450 |
# correctly. |
|
451 |
expected = [{'key': p} for p in patterns] |
|
452 |
tree, result = execute_in_page( |
|
453 |
'''{ |
|
454 |
const tree = arguments[0]; |
|
455 |
for (const pattern of arguments[1]) |
|
456 |
pattern_tree.deregister(tree, pattern + '/', 'key'); |
|
457 |
returnval([tree, [...pattern_tree.search(tree, arguments[2])]]); |
|
458 |
}''', |
|
459 |
tree, patterns, url) |
|
460 |
assert expected == result |
|
461 |
|
|
462 |
# Also verify that deregistering all the patterns works correctly. |
|
463 |
tree = execute_in_page( |
|
464 |
'''{ |
|
465 |
const tree = arguments[0]; |
|
466 |
for (const pattern of arguments[1]) |
|
467 |
pattern_tree.deregister(tree, pattern, 'key'); |
|
468 |
for (const pattern of arguments[2]) { |
|
469 |
pattern_tree.deregister(tree, pattern, 'key'); |
|
470 |
pattern_tree.deregister(tree, pattern + '/', 'key'); |
|
471 |
} |
|
472 |
returnval(tree); |
|
473 |
}''', |
|
474 |
tree, patterns, bad_patterns) |
|
475 |
assert tree == {} |
Also available in: Unified diff
finish implementing more efficient querying of URL patterns
The algorithm is implemented and tested. However, it is yet to be hooked into the actual extension.