forked from flathunters/flathunter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_gmaps_duration_processor.py
50 lines (45 loc) · 1.83 KB
/
test_gmaps_duration_processor.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
import unittest
import yaml
import re
import requests_mock
from flathunter.hunter import Hunter
from flathunter.idmaintainer import IdMaintainer
from test.dummy_crawler import DummyCrawler
from test.test_util import count
from test.utils.config import StringConfig
class GMapsDurationProcessorTest(unittest.TestCase):
DUMMY_CONFIG = """
urls:
- https://www.example.com/liste/berlin/wohnungen/mieten?roomi=2&prima=1500&wflmi=70&sort=createdate%2Bdesc
google_maps_api:
key: SOME_KEY
url: https://maps.googleapis.com/maps/api/distancematrix/json?origins={origin}&destinations={dest}&mode={mode}&sensor=true&key={key}&arrival_time={arrival}
enable: true
durations:
- destination: Buckingham Palace UK
name: The Queen
modes:
- gm_id: transit
title: By Bus
- destination: главная площадь
name: Москва
modes:
- gm_id: bicycling
title: Bicyle
- gm_id: driving
title: Car
"""
@requests_mock.Mocker()
def test_resolve_durations(self, m):
config = StringConfig(string=self.DUMMY_CONFIG)
config.set_searchers([DummyCrawler()])
hunter = Hunter(config, IdMaintainer(":memory:"))
matcher = re.compile('maps.googleapis.com/maps/api/distancematrix/json')
m.get(matcher, text='{"status": "OK", "rows": [ { "elements": [ { "distance": { "text": "far", "value": 123 }, "duration": { "text": "days", "value": 123 } } ] } ]}')
exposes = hunter.hunt_flats()
self.assertTrue(count(exposes) > 4, "Expected to find exposes")
without_durations = list(filter(lambda expose: 'durations' not in expose, exposes))
if len(without_durations) > 0:
for expose in without_durations:
print("Got expose: ", expose)
self.assertTrue(len(without_durations) == 0, "Expected durations to be calculated")