-
Notifications
You must be signed in to change notification settings - Fork 203
Expand file tree
/
Copy pathtest.py
More file actions
129 lines (113 loc) · 4.58 KB
/
Copy pathtest.py
File metadata and controls
129 lines (113 loc) · 4.58 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
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
from cerberus import Validator as _Validator
import pytest
import realtorcom
import pprint
pp = pprint.PrettyPrinter(indent=4)
# enable cache?
realtorcom.BASE_CONFIG["cache"] = True
class Validator(_Validator):
def _validate_min_presence(self, min_presence, field, value):
pass
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, getter, key, min_perc):
"""fail when fewer than min_perc of items have a non-null value at `key`"""
count = sum(1 for item in items if getter(item, key) is not None)
if count < len(items) * min_perc:
pytest.fail(
f'inadequate presence of "{key}" field: only {count}/{len(items)} items have it '
f'(expected at least {min_perc * 100:.1f}%)'
)
@pytest.mark.asyncio
@pytest.mark.flaky(reruns=3, reruns_delay=30)
async def test_property_scraping():
url = "https://www.realtor.com/realestateandhomes-detail/12355-Attlee-Dr_Houston_TX_77077_M70330-35605"
result = await realtorcom.scrape_property(url)
schema = {
"id": {"type": "string"},
"href": {"type": "string", "regex": "https://www.realtor.com/realestateandhomes-detail/.+?"},
"status": {"type": "string"},
"sold_date": {"type": "string", "regex": "\d+-\d+-\d+", "nullable": True},
"tags": {"type": "list", "schema": {"type": "string"}, "nullable": True},
"list_price": {"type": "integer"},
"list_price_last_change": {"type": "integer"},
"details": {
"type": "dict",
"schema": {
"beds": {"type": "integer"},
"baths": {"type": "integer"},
},
},
"flags": {"type": "dict"},
"phones": {"type": "list", "schema": {"type": "dict", "schema": {"number": {"type": "string"}}}},
"location": {
"type": "dict",
"schema": {
"address": {
"type": "dict",
"schema": {
"city": {"type": "string"},
"line": {"type": "string"},
"state": {"type": "string"},
"postal_code": {"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_search_scraping():
result = await realtorcom.scrape_search("CA", "San-Francisco", max_pages=2)
schema = {
"property_id": {"type": "string"},
"permalink": {"type": "string"},
"list_price": {"type": "integer", "nullable": True}, # some propreties can have no price
"list_date": {"type": "string"},
"photos": {
"type": "list",
"nullable": True,
"schema": {
"type": "dict",
"schema": {
"href": {"type": "string"}
}
}
},
"description": {
"type": "dict",
"required": True,
"schema": {
"beds": {"type": "integer", "required": True, "nullable": True, "min_presence": 0.05},
"baths": {"type": "integer", "required": True, "nullable": True, "min_presence": 0.05},
"sqft": {"type": "integer", "required": True, "nullable": True, "min_presence": 0.05},
"lot_size": {"type": "integer", "required": True, "nullable": True, "min_presence": 0.05},
"type": {"type": "string", "required": True, "min_presence": 0.5},
},
},
}
validator = Validator(schema, allow_unknown=True)
for item in result:
validate_or_fail(item, validator)
desc_schema = schema["description"]["schema"]
for field, rules in desc_schema.items():
if "min_presence" in rules:
require_min_presence(
result,
lambda item, k: item.get("description", {}).get(k),
field,
rules["min_presence"],
)
@pytest.mark.asyncio
@pytest.mark.flaky(reruns=3, reruns_delay=30)
async def test_feed_scraping():
url = "https://cdn.realtor.ca/sitemap/realtorsitemap/sitemap.xml"
result_feed = await realtorcom.scrape_feed(url)
assert len(result_feed) > 0
for url, lastmod in result_feed.items():
assert url.startswith("https://www.realtor.ca/")
assert lastmod is not None