summaryrefslogtreecommitdiff
path: root/tests/test_engine.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test_engine.py')
-rw-r--r--tests/test_engine.py48
1 files changed, 35 insertions, 13 deletions
diff --git a/tests/test_engine.py b/tests/test_engine.py
index 28e25e6..a3d2898 100644
--- a/tests/test_engine.py
+++ b/tests/test_engine.py
@@ -1,40 +1,62 @@
1import adblock 1import adblock
2import pytest 2import pytest
3 3
4SMALL_FILTER_LIST = """
5||wikipedia.org^
6||old.reddit.com^
7||lobste.rs^
8"""
4 9
5def test_engine_arguments(): 10
6 # None of these should panic 11def empty_engine():
7 adblock.Engine() 12 return adblock.Engine(adblock.FilterSet())
8 adblock.Engine([]) 13
9 adblock.Engine(network_filters=None) 14
10 adblock.Engine(network_filters=[]) 15def test_engine_creation_and_blocking():
11 adblock.Engine(load_network=False, load_cosmetic=True, debug=False) 16 filter_set = adblock.FilterSet(debug=True)
12 adblock.Engine(debug=True) 17 filter_set.add_filter_list(SMALL_FILTER_LIST)
18 engine = adblock.Engine(filter_set=filter_set)
19
20 blocker_result_wikipedia = engine.check_network_urls(
21 url="https://wikipedia.org/img.png",
22 source_url="https://google.com/",
23 request_type="image",
24 )
25 assert isinstance(blocker_result_wikipedia, adblock.BlockerResult)
26 assert blocker_result_wikipedia.matched
27
28 blocker_result_facebook = engine.check_network_urls(
29 "https://facebook.com/directory/img.png",
30 "https://old.reddit.com/r/all",
31 "image",
32 )
33 assert isinstance(blocker_result_facebook, adblock.BlockerResult)
34 assert not blocker_result_facebook.matched
13 35
14 36
15def test_serde_file(tmpdir): 37def test_serde_file(tmpdir):
16 path = str(tmpdir / "cache.dat") 38 path = str(tmpdir / "cache.dat")
17 39
18 engine0 = adblock.Engine() 40 engine0 = empty_engine()
19 with pytest.raises(FileNotFoundError): 41 with pytest.raises(FileNotFoundError):
20 # We haven't created the cache.dat file, so we should get an exception 42 # We haven't created the cache.dat file, so we should get an exception
21 # when attempting to deserialize. 43 # when attempting to deserialize.
22 engine0.deserialize_from_file(path) 44 engine0.deserialize_from_file(path)
23 45
24 engine1 = adblock.Engine() 46 engine1 = empty_engine()
25 serialization_result = engine1.serialize_to_file(path) 47 serialization_result = engine1.serialize_to_file(path)
26 assert serialization_result is None 48 assert serialization_result is None
27 49
28 engine2 = adblock.Engine() 50 engine2 = empty_engine()
29 deserialization_result = engine2.deserialize_from_file(path) 51 deserialization_result = engine2.deserialize_from_file(path)
30 assert deserialization_result is None 52 assert deserialization_result is None
31 53
32 54
33def test_serde(): 55def test_serde():
34 engine = adblock.Engine() 56 engine = empty_engine()
35 serialization_result = engine.serialize() 57 serialization_result = engine.serialize()
36 assert isinstance(serialization_result, bytes) 58 assert isinstance(serialization_result, bytes)
37 59
38 engine2 = adblock.Engine() 60 engine2 = empty_engine()
39 deserialization_result = engine2.deserialize(serialization_result) 61 deserialization_result = engine2.deserialize(serialization_result)
40 assert deserialization_result is None 62 assert deserialization_result is None