-
Notifications
You must be signed in to change notification settings - Fork 88
/
test.py
133 lines (124 loc) · 5.09 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
from cerberus import Validator
import pytest
import crunchbase
import pprint
pp = pprint.PrettyPrinter(indent=2)
# enable cache?
crunchbase.BASE_CONFIG["cache"] = True
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}")
@pytest.mark.asyncio
@pytest.mark.flaky(reruns=3, reruns_delay=30)
async def test_company_scraping():
url = "https://www.crunchbase.com/organization/tesla-motors/people"
result = await crunchbase.scrape_company(url)
schema = {
"employees": {
"type": "list",
"schema": {
"type": "dict",
"schema": {
"name": {"type": "string"},
"linkedin": {"type": "string"},
"job_departments": {"type": "list", "schema": {"type": "string"}},
"job_levels": {"type": "list", "schema": {"type": "string"}},
},
},
},
"organization": {
"type": "dict",
"schema": {
"id": {"type": "string"},
"name": {"type": "string"},
"description": {"type": "string"},
"linkedin": {"type": "string", "nullable": True, "regex": r"https*://www.linkedin.com/company/.+?"},
"twitter": {"type": "string", "nullable": True, "regex": r"https://(?:www\.)?twitter.com/.+?"},
"facebook": {"type": "string", "nullable": True, "regex": r"https://www.facebook.com/.+?"},
"email": {"type": "string", "nullable": True, "regex": r".+?@.+?"},
"phone": {"type": "string", "nullable": True, "regex": r"[\d-]+?"},
"website": {"type": "string", "nullable": True, "regex": r"https*://.+?"},
"categories": {"type": "list", "schema": {"type": "string"}},
"investments": {
"type": "list",
"schema": {
"type": "dict",
"schema": {
"name": {"type": "string"},
"raised": {"type": "integer"},
"organization": {"type": "string"},
},
},
},
},
},
}
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_person_scraping():
url = "https://www.crunchbase.com/person/danny-hayes-8e1b"
result = await crunchbase.scrape_person(url)
schema = {
"name": {"type": "string"},
"title": {"type": "string"},
"type": {"type": "string"},
"gender": {"type": "string"},
"linkedin": {"type": "string", "nullable": True, "regex": r"https*://www.linkedin.com/in/.+?"},
"twitter": {"type": "string", "nullable": True, "regex": r"https://twitter.com/.+?"},
"facebook": {"type": "string", "nullable": True, "regex": r"https://www.facebook.com/.+?"},
"description": {"type": "string"},
"location_groups": {"type": "list", "schema": {"type": "string"}},
"location": {"type": "list", "schema": {"type": "string"}},
"education": {
"type": "list",
"schema": {
"type": "dict",
"schema": {
"school": {"type": "string"},
"started_on": {"type": "string"},
"completed_on": {"type": "string"},
"type": {"type": "string", "nullable": True},
},
},
},
"timeline": {
"type": "list",
"schema": {
"type": "dict",
"schema": {
"title": {"type": "string", "nullable": True},
"author": {"type": "string", "nullable": True},
"publisher": {"type": "string", "nullable": True},
"url": {"type": "string", "nullable": True},
"date": {"type": "string", "nullable": True},
"type": {"type": "string", "nullable": True},
},
},
},
"investments": {
"type": "list",
"schema": {
"type": "dict",
"schema": {
"name": {"type": "string"},
"raised": {"type": "integer"},
"organization": {"type": "string"},
},
},
},
"investing_overview": {
"type": "dict",
"schema": {
"num_current_advisor_jobs": {"type": "integer"},
"num_founded_organizations": {"type": "integer"},
"num_portfolio_organizations": {"type": "integer"},
"rank_principal_investor": {"type": "integer"},
"num_exits": {"type": "integer"},
},
},
}
validator = Validator(schema, allow_unknown=True)
validate_or_fail(result, validator)