forked from limebrains/pyolx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
153 lines (109 loc) · 4.98 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
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import pytest
from bs4 import BeautifulSoup
import olx
import olx.category
import olx.offer
import olx.utils
if sys.version_info < (3, 3):
from mock import mock
else:
from unittest import mock
GDANSK_URL = "https://www.olx.pl/nieruchomosci/mieszkania/wynajem/gdansk/"
OFFER_URL = "https://www.olx.pl/oferta/gdansk-przymorze-dla-studentow-CID3-IDnT89A.html#1d9db51b24"
@pytest.mark.parametrize("filter_name,filter_value", [
("[filter_float_price:from]", 2000),
("[filter_enum_floor_select][0]", 2),
("[filter_enum_furniture][0]", True),
("[filter_enum_builttype][0]", "blok"),
("[filter_enum_rooms][0]", 3)
])
def test_get_search_filter(filter_name, filter_value):
assert "search%5B" in olx.utils.get_search_filter(filter_name, filter_value)
response = olx.utils.get_content_for_url(GDANSK_URL)
html_parser = BeautifulSoup(response.content, "html.parser")
offers = html_parser.find_all(class_='offer')
parsed_urls = [OFFER_URL]
@pytest.mark.skipif(sys.version_info < (3, 1), reason="requires Python3")
@pytest.mark.parametrize("city", [
"Gdańsk", "Sopot", "Gdynia", "Ruda Śląska", "Łódź"
])
def test_city_name(city):
result = olx.utils.city_name(city)
for value in olx.utils.POLISH_CHARACTERS_MAPPING.keys():
if value in result:
assert False
assert " " not in result and result.islower()
@pytest.mark.parametrize("offers", [response.content])
def test_parse_available_offers(offers):
assert olx.category.parse_available_offers(offers)
@pytest.mark.parametrize("maincat,subcat,detailcat,region,filters", [
("nieruchomosci", "mieszkania", "wynajem", "gdansk", {"[filter_float_price:from]": 2000}),
])
def test_get_url(maincat, subcat, detailcat, region, filters):
assert olx.utils.get_url(maincat, subcat, detailcat, region, **filters) == \
"https://www.olx.pl/nieruchomosci/mieszkania/wynajem/gdansk/?search%5Bfilter_float_price%3Afrom%5D=2000&"
@pytest.mark.parametrize("page_count", [response.content])
def test_get_page_count(page_count):
assert olx.category.get_page_count(page_count) >= 10
@pytest.mark.parametrize("test_url", ['https://www.olx.pl/', GDANSK_URL])
def test_get_conntent_for_url(test_url):
assert olx.utils.get_content_for_url(test_url)
@pytest.mark.parametrize("offer_url", [
str(offer) for offer in offers if offer
])
def test_parse_offer_url(offer_url):
olx.category.parse_offer_url(offer_url)
@pytest.fixture
def offer_parser():
response = olx.utils.get_content_for_url(OFFER_URL)
html_parser = BeautifulSoup(response.content, "html.parser")
return html_parser
@pytest.fixture
def offer_content(offer_parser):
return str(offer_parser.find(class_='offerbody'))
@pytest.fixture
def parsed_body(offer_parser):
return str(offer_parser.find("body"))
@pytest.mark.skipif(sys.version_info < (3, 1), reason="requires Python3")
def test_parse_description(offer_content):
assert olx.offer.parse_description(offer_content)
@pytest.mark.skipif(sys.version_info < (3, 1), reason="requires Python3")
def test_get_title(offer_content):
assert olx.offer.get_title(offer_content) == "Gdańsk Przymorze dla studentów"
def test_get_surface(offer_content):
assert olx.offer.get_surface(offer_content) == 38.0
def test_get_img_url(offer_content):
images = olx.offer.get_img_url(offer_content)
assert isinstance(images, list)
for img in images:
assert "https://" in img
def test_get_date_added(parsed_body):
assert olx.offer.get_date_added(parsed_body)
@pytest.mark.parametrize("offer_url", [OFFER_URL])
def test_parse_offer(offer_url):
assert isinstance(olx.offer.parse_offer(offer_url), (dict, type(None)))
html_parser = BeautifulSoup(olx.utils.get_content_for_url(OFFER_URL).content, "html.parser")
data_dict = olx.offer.get_gpt_script(str(html_parser.body))
def test_parse_flat_data(parsed_body):
test = olx.offer.parse_flat_data(parsed_body, data_dict)
assert test["floor"] == 6
assert test["rooms"] == 2
assert test["built_type"] == "blok"
assert test["furniture"]
@pytest.mark.parametrize('main_category,subcategory,detail_category,region', [
("nieruchomosci", "mieszkania", "wynajem", 'tczew'),
("nieruchomosci", "mieszkania", "wynajem", 'gdansk'),
("nieruchomosci", "mieszkania", "wynajem", 'gdynia'),
("nieruchomosci", "mieszkania", "wynajem", 'sopot'),
])
def test_get_category(main_category, subcategory, detail_category, region):
with mock.patch("olx.utils.get_url") as get_url:
with mock.patch("olx.utils.get_content_for_url") as get_content_for_url:
with mock.patch("olx.category.parse_available_offers") as parse_available_offers:
parse_available_offers.return_value = olx.category.parse_available_offers(response.content)
get_content_for_url.return_value = response
get_url.return_value = olx.utils.get_url
olx.category.get_category(main_category, subcategory, detail_category, region)