-
-
Notifications
You must be signed in to change notification settings - Fork 253
/
test_validation.py
188 lines (161 loc) · 7.06 KB
/
test_validation.py
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
"""
Break crawler-user-agents.json in ways that validate.py should detect
Usage:
$ pytest test_validation.py
"""
from __future__ import print_function
import json
import subprocess
from copy import deepcopy
import pytest
def update_json_file(data):
with open('crawler-user-agents.json', 'w') as f:
json.dump(data, f, indent=2)
@pytest.fixture
def restore_original_json():
# Load original version of crawler-user-agents.json
with open('crawler-user-agents.json') as f:
original_json = json.load(f)
# By using a yield statement instead of return, all the code after
# the yield statement serves as the teardown code:
yield None
# tear down code: restore original version of crawler-user-agents.json
update_json_file(original_json)
def assert_validate_failed():
assert subprocess.call(['python', 'validate.py']) != 0
def assert_validate_passed():
assert subprocess.call(['python', 'validate.py']) == 0
def test_simple_pass(restore_original_json):
# the json must be an array of objects containing "pattern"
# there must be more than 10 instances to pass
user_agent_list = [{'pattern': 'foo',
'instances': ['foo',
'afoo',
'foob',
'cfood',
'/foo/',
'\\foo\\',
':foo:',
'foo.',
'!foo',
'/foo',
'foo\\',
'FoofooFoo',
'foot']}]
update_json_file(user_agent_list)
assert_validate_passed()
def test_simplest_pass(restore_original_json):
# the simplest crawler file passes
user_agent_list = [{'pattern': 'foo', 'instances': []}]
update_json_file(user_agent_list)
assert_validate_passed()
def test_schema_violation_dict1(restore_original_json):
# contract: the json must be an array
user_agent_list = {'foo':None}
update_json_file(user_agent_list)
assert_validate_failed()
def test_schema_violation_dict2(restore_original_json):
# contract: the json must be an array of objects containing "pattern"
user_agent_list = [{'foo':None}]
update_json_file(user_agent_list)
assert_validate_failed()
def test_schema_violation_dict3(restore_original_json):
# contract: the json must be an array of objects containing "pattern" and valid properties
user_agent_list = [{'pattern':'foo', 'foo':3}]
update_json_file(user_agent_list)
assert_validate_failed()
def test_simple_duplicate_detection(restore_original_json):
# contract: if we have the same pattern twice, it fails
user_agent_list = [{'pattern': 'foo',
'instances': ['foo',
'afoo',
'foob',
'cfood',
'/foo/',
'\\foo\\',
':foO:',
'foo.',
'!foo',
'/foo',
'foo\\',
'FoofooFoo',
'foot']},
{'pattern': 'foo'}]
update_json_file(user_agent_list)
assert_validate_failed()
def test_simple_duplicate_detection2(restore_original_json):
# contract: if we have the same pattern twice, it fails (even w/o instances)
user_agent_list = [{'pattern': 'foo',
'instances': ['foo',
'afoo',
'foob',
'cfood',
'/foo/',
'\\foo\\',
':foo:',
'foo.',
'!foo',
'/foo',
'foo\\',
'FoofooFoo',
'foot']},
{'pattern': 'bar'},
{'pattern': 'bar'}]
update_json_file(user_agent_list)
assert_validate_failed()
def test_subset_duplicate_detection(restore_original_json):
# contract: if a pattern matches another pattern, it fails
user_agent_list = [{'pattern': 'foo',
'instances': ['foo',
'afoo',
'foob',
'cfood',
'/foo/',
'\\foo\\',
':foo:',
'foo.',
'!foo',
'/foo',
'foo\\',
'FoofooFoo',
'foot']},
{'pattern': 'afoot'}]
update_json_file(user_agent_list)
assert_validate_failed()
def test_case_sensitivity(restore_original_json):
# contract: the patterns are case sensitive
user_agent_list = [{'pattern': 'foo',
'instances': ['FOO',
'aFoo',
'foob',
'cfood',
'/foo/',
'\\foo\\',
':foo:',
'fOo.',
'!FOO',
'/foo',
'foo\\',
'FoofooFoo',
'foot']}]
update_json_file(user_agent_list)
assert_validate_failed()
def test_duplicate_case_insensitive_detection(restore_original_json):
# contract: fail if we have patterns that differ only in capitailization
user_agent_list = [{'pattern': 'foo',
'instances': ['foo',
'afoo',
'foob',
'cfood',
'/foo/',
'\\foo\\',
':foo:',
'foo.',
'!foo',
'/foo',
'foo\\',
'FoofooFoo',
'foot']},
{'pattern': 'fOo'}]
update_json_file(user_agent_list)
assert_validate_failed()