-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathtest.py
173 lines (156 loc) · 6.45 KB
/
test.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
from cerberus import Validator
import pytest
import glassdoor
import pprint
pp = pprint.PrettyPrinter(indent=4)
# enable cache?
glassdoor.BASE_CONFIG["cache"] = True
def test_glassdoor_url():
expected = "https://www.glassdoor.com/Overview/Working-at-eBay-Motors-Group-EI_IE4189745.11,28.htm"
glassdoor.Url.overview("eBay-Motors-Group", "4189745") == expected
expected = "https://www.glassdoor.com/Jobs/eBay-Motors-Group-Jobs-E4189745.htm"
glassdoor.Url.jobs("eBay-Motors-Group", "4189745") == expected
expected = "https://www.glassdoor.com/Jobs/eBay-Motors-Group-Jobs-E4189745.htm?filter.countryId=1"
glassdoor.Url.jobs("eBay-Motors-Group", "4189745", glassdoor.Region.UNITED_STATES) == expected
expected = "https://www.glassdoor.com/Reviews/eBay-Motors-Group-Reviews-E4189745.htm"
glassdoor.Url.reviews("eBay-Motors-Group", "4189745") == expected
expected = "https://www.glassdoor.com/Salaries/eBay-Motors-Group-Salaries-E4189745.htm"
glassdoor.Url.salaries("eBay-Motors-Group", "4189745") == expected
def validate_or_fail(item, validator):
if not validator.validate(item):
pp.pformat(item)
pytest.fail(f"Validation failed for item: {pp.pformat(item)}\nErrors: {validator.errors}")
def find_errors(errors, prefix=""):
for key, value in errors.items():
if isinstance(value, dict):
yield from find_errors(value, f"{prefix}{key}.")
else:
yield f"{prefix}{key}"
@pytest.mark.asyncio
@pytest.mark.flaky(reruns=3, reruns_delay=30)
async def test_find_companies():
schema = {
"name": {"type": "string"},
"id": {"type": "integer"},
"logoURL": {"type": "string", "nullable": True},
"employerId": {"type": "integer", "nullable": True},
"employerName": {"type": "string", "nullable": True},
}
results = await glassdoor.find_companies("Ebay")
validator = Validator(schema, allow_unknown=True)
for item in results:
validate_or_fail(item, validator)
assert len(results) > 5
@pytest.mark.asyncio
@pytest.mark.flaky(reruns=3, reruns_delay=30)
async def test_job_scraping():
url = "https://www.glassdoor.com/Jobs/eBay-Jobs-E7853.htm?filter.countryId=1"
result = await glassdoor.scrape_jobs(url, max_pages=2)
schema = {
"jobTitleText": {"type": "string"},
"jobLink": {"type": "string"},
"payCurrency": {"type": "string"},
"locationName": {"type": "string"},
"jobCountryId": {"type": "integer"},
"ageInDays": {"type": "integer"},
"employer": {
"type": "dict",
"schema": {
"id": {"type": "integer"},
"shortName": {"type": "string"},
},
},
}
validator = Validator(schema, allow_unknown=True)
for item in result:
validate_or_fail(item, validator)
@pytest.mark.asyncio
@pytest.mark.flaky(reruns=3, reruns_delay=30)
async def test_salary_scraping():
url = "https://www.glassdoor.com/Salary/eBay-Salaries-E7853.htm"
result = await glassdoor.scrape_salaries(url, max_pages=3)
schema = {
"salaryCount": {"type": "integer"},
"jobTitleCount": {"type": "integer"},
"numPages": {"type": "integer"},
"results": {
"type": "list",
"schema": {
"type": "dict",
"schema": {
"currency": {
"type": "dict",
"schema": {
"__typename": {"type": "string"},
"code": {"type": "string"},
}
},
"jobTitle": {
"type": "dict",
"schema": {
"id": {"type": "integer"},
"text": {"type": "string"},
}
},
"salaryCount": {"type": "integer"},
"basePayStatistics": {
"type": "dict",
"schema": {
"percentiles": {
"type": "list", # This ensures the validator expects a list
"schema": {
"type": "dict", # Each item in the list is a dictionary
"schema": {
"ident": {"type": "string"},
"value": {"type": "float"},
}
}
}
}
},
},
},
},
}
validator = Validator(schema, allow_unknown=True)
validate_or_fail(result, validator)
@pytest.mark.asyncio
@pytest.mark.flaky(reruns=3, reruns_delay=30)
async def test_review_scraping():
url = "https://www.glassdoor.com/Reviews/eBay-Reviews-E7853.htm"
result = await glassdoor.scrape_reviews(url, max_pages=3)
schema = {
"allReviewsCount": {"type": "integer"},
"ratedReviewsCount": {"type": "integer"},
"filteredReviewsCount": {"type": "integer"},
"reviews": {
"type": "list",
"schema": {
"type": "dict",
"schema": {
"isLegal": {"type": "boolean"},
"featured": {"type": "boolean"},
"languageId": {"type": "string"},
"reviewId": {"type": "integer"},
"countHelpful": {"type": "integer"},
"countNotHelpful": {"type": "integer"},
"ratingOverall": {"type": "integer"},
"ratingCeo": {"nullable": True, "type": "string"},
"pros": {"nullable": True, "type": "string"},
"cons": {"nullable": True, "type": "string"},
"summary": {"nullable": True, "type": "string"},
"advice": {"nullable": True, "type": "string"},
"jobTitle": {
"type": "dict",
"nullable": True,
"schema": {
"text": {"type": "string"},
"id": {"type": "integer"},
},
},
},
},
},
}
validator = Validator(schema, allow_unknown=True)
validate_or_fail(result, validator)