-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtest_validation.py
More file actions
73 lines (53 loc) · 2.47 KB
/
Copy pathtest_validation.py
File metadata and controls
73 lines (53 loc) · 2.47 KB
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
#!/usr/bin/env python3
"""
Test the validation system for AutoGlossarySpider.
"""
def test_missing_name():
"""Test that missing name attribute raises TypeError at class definition."""
try:
from public_law.shared.spiders.enhanced_base import AutoGlossarySpider
class MissingNameSpider(AutoGlossarySpider): # type: ignore
start_urls = ["https://example.com/glossary"]
assert False, "Should have raised TypeError for missing name"
except TypeError as e:
print(f"✓ Caught missing name error: {e}")
assert "must define a 'name' class attribute" in str(e)
def test_missing_urls():
"""Test that missing start_urls raises TypeError at class definition."""
try:
from public_law.shared.spiders.enhanced_base import AutoGlossarySpider
class MissingUrlsSpider(AutoGlossarySpider): # type: ignore
name = "aus_missing_urls_glossary"
assert False, "Should have raised TypeError for missing start_urls"
except TypeError as e:
print(f"✓ Caught missing start_urls error: {e}")
assert "must define a 'start_urls' class attribute" in str(e)
def test_invalid_name_format():
"""Test that invalid name format raises ValueError."""
try:
from public_law.shared.spiders.enhanced_base import AutoGlossarySpider
class InvalidNameSpider(AutoGlossarySpider): # type: ignore
name = "invalid-name-format"
start_urls = ["https://example.com/glossary"]
assert False, "Should have raised ValueError for invalid name format"
except ValueError as e:
print(f"✓ Caught invalid name format error: {e}")
assert "must follow pattern" in str(e)
def test_valid_spider():
"""Test that valid spider creates successfully."""
from public_law.shared.spiders.enhanced_base import AutoGlossarySpider
class ValidSpider(AutoGlossarySpider):
name = "aus_valid_glossary"
start_urls = ["https://example.com/glossary"]
print("✓ Valid spider created successfully")
assert ValidSpider.name == "aus_valid_glossary"
assert ValidSpider.start_urls == ["https://example.com/glossary"]
if __name__ == "__main__":
print("Testing AutoGlossarySpider validation...\n")
test_missing_name()
test_missing_urls()
test_invalid_name_format()
test_valid_spider()
print("\n" + "="*50)
print("All validation tests passed! 🎉")
print("Invalid spider configurations are caught at class definition time.")