-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathtest.py
162 lines (135 loc) · 4.73 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
from cerberus import Validator as _Validator
import pytest
import linkedin
import pprint
pp = pprint.PrettyPrinter(indent=4)
linkedin.BASE_CONFIG["cache"] = False
class Validator(_Validator):
def _validate_min_presence(self, min_presence, field, value):
pass # required for adding non-standard keys to schema
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 require_min_presence(items, key, min_perc=0.1):
"""check whether dataset contains items with some amount of non-null values for a given key"""
count = sum(1 for item in items if item.get(key))
if count < len(items) * min_perc:
pytest.fail(
f'inadequate presence of "{key}" field in dataset, only {count} out of {len(items)} items have it (expected {min_perc*100}%)'
)
profile_schema = {
"profile": {
"type": "dict",
"schema": {
"address": {
"type": "dict",
"schema": {
"addressLocality": {"type": "string"},
"addressCountry": {"type": "string"},
}
}
}
},
"posts": {
"type": "list",
"schema": {
"type": "dict",
"schema": {
"name": {"type": "string"},
"articleBody": {"type": "string"},
"url": {"type": "string"},
}
}
}
}
company_schema = {
"overview": {
"type": "dict",
"schema": {
"name": {"type": "string"},
"url": {"type": "string"},
"description": {"type": "string"},
"numberOfEmployees": {"type": "integer"},
"Industry": {"type": "string"},
"Headquarters": {"type": "string"},
"Founded": {"type": "string"},
"Specialties": {"type": "string"},
}
}
}
job_search_schema = {
"title": {"type": "string"},
"company": {"type": "string"},
"address": {"type": "string"},
"timeAdded": {"type": "string"},
"jobUrl": {"type": "string"},
"companyUrl": {"type": "string"},
}
job_page_schema = {
"datePosted": {"type": "string"},
"employmentType": {"type": "string"},
"industry": {"type": "string"},
"title": {"type": "string"},
"validThrough": {"type": "string"},
}
@pytest.mark.asyncio
@pytest.mark.flaky(reruns=3, reruns_delay=30)
async def test_profile_scraping():
profile_data = await linkedin.scrape_profile(
urls=[
"https://www.linkedin.com/in/williamhgates"
]
)
validator = Validator(profile_schema, allow_unknown=True)
for item in profile_data:
validate_or_fail(item, validator)
assert len(profile_data) == 1
@pytest.mark.asyncio
@pytest.mark.flaky(reruns=3, reruns_delay=30)
async def test_company_scraping():
company_data = await linkedin.scrape_company(
urls=[
"https://linkedin.com/company/microsoft",
"https://linkedin.com/company/google",
"https://linkedin.com/company/apple"
]
)
validator = Validator(company_schema, allow_unknown=True)
for item in company_data:
validate_or_fail(item, validator)
for k in company_schema:
require_min_presence(company_data, k, min_perc=company_schema[k].get("min_presence", 0.1))
assert len(company_data) == 3
@pytest.mark.asyncio
@pytest.mark.flaky(reruns=3, reruns_delay=30)
async def test_job_search_scraping():
job_search_data = await linkedin.scrape_job_search(
# it include other search parameters, refer to the search pages on browser for more details
keyword="Python Developer",
location="United States",
max_pages=3
)
validator = Validator(job_search_schema, allow_unknown=True)
for item in job_search_data:
validate_or_fail(item, validator)
for k in job_search_schema:
require_min_presence(job_search_data, k, min_perc=job_search_schema[k].get("min_presence", 0.1))
assert len(job_search_data) > 20
@pytest.mark.asyncio
@pytest.mark.flaky(reruns=3, reruns_delay=30)
async def test_job_page_scraping():
job_search_data = await linkedin.scrape_job_search(
# it include other search parameters, refer to the search pages on browser for more details
keyword="Python Developer",
location="United States",
max_pages=1
)
job_urls = [i["jobUrl"] for i in job_search_data]
job_data = await linkedin.scrape_jobs(
urls=job_urls[:4]
)
validator = Validator(job_page_schema, allow_unknown=True)
for item in job_data:
validate_or_fail(item, validator)
assert len(job_data) >= 1