forked from toby-p/rightmove_webscraper.py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
64 lines (54 loc) · 2.66 KB
/
tests.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
#!/usr/bin/env python3
from rightmove_webscraper import rightmove_data
from pandas import DataFrame
def test_valid_urls(urls):
for u in urls:
print("Testing URL: {} ...".format(u))
try:
data = rightmove_data(u)
if len(data.get_results) > 0:
print("> Passed - collected results.")
print("Testing object attributes ...")
check_all_attrs(data)
print("Testing object methods ...")
if isinstance(data.summary(), DataFrame):
print("method: summary()\n> Passed - returned type {}".format(DataFrame))
else:
print("method: summary()\n> FAILED - did not return type {}".format(DataFrame))
else:
print("> FAILED - no results collected.")
except Exception as e:
print("Testing URL: {}\n> FAILED with Exception:\n\t{}".format(u, e))
print()
def check_attribute(rightmove_data_object, attr_name, attr_type):
val = getattr(rightmove_data_object, attr_name)
if isinstance(val, attr_type):
print("attr: {}\n> Passed - returned type {}".format(attr_name, attr_type))
else:
print("attr: {}\n> FAILED - did not return type {}".format(attr_name, attr_type))
def check_all_attrs(rightmove_data_object):
valid_types = {"average_price":int,
"get_results":DataFrame,
"results_count":int,
"url":str}
for k in valid_types:
check_attribute(rightmove_data_object, k, valid_types[k])
def test_invalid_urls(urls):
for u in urls:
try:
data = rightmove_data(u)
if len(data.get_results)>0:
print("Testing URL: {}\n> FAILED - created results with invalid URL.".format(u))
else:
print("Testing URL: {}\n> Passed - no results collected.".format(u))
except Exception as e:
print("Testing URL: {}\n> Passed - raised Exception.".format(u))
print()
urls = ["https://www.rightmove.co.uk/new-homes-for-sale/find.html?locationIdentifier=REGION%5E70417&includeSSTC=false",
"https://www.rightmove.co.uk/property-to-rent/find.html?searchType=RENT&locationIdentifier=REGION%5E70417&insId=1&radius=0.0",
"https://www.rightmove.co.uk/property-for-sale/find.html?locationIdentifier=REGION%5E70417&maxDaysSinceAdded=7&includeSSTC=false"]
test_valid_urls(urls)
urls = ["https://www.rightmove.co.uk/stupidrightmove/find.html?locationIdentifier=REGION%5E70417&includeSSTC=false",
"https://www.google.com",
"just not a URL at all "]
test_invalid_urls(urls)