Skip to content

Commit

Permalink
refactor: api and formatter i/o
Browse files Browse the repository at this point in the history
  • Loading branch information
u8slvn committed Jul 7, 2019
1 parent ecedf9e commit 4ca8008
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 19 deletions.
5 changes: 3 additions & 2 deletions rerwatcher/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ def __init__(self, config):
)

def fetch_data(self):
response = None
data = None

try:
logger.info(f"Fetching data from {self._url}.")
response = requests.get(url=self._url, auth=self._auth)
data = response.text
except (RequestException, ReadTimeout):
logger.error(f"Fetching {self._url} failed.")

return response
return data
2 changes: 1 addition & 1 deletion rerwatcher/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def start(self):
while self.is_running:
response = self.api.fetch_data()
timetables = self.formatter.format_response(response=response)
self.display.print(timetables)
self.display.print(messages=timetables)
self._manage_refresh_time()

def _manage_refresh_time(self):
Expand Down
2 changes: 1 addition & 1 deletion rerwatcher/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def format_response(self, response, limit=2):
return timetables

def _format_response(self, response, limit):
response_body = response.text.encode(self.encoding)
response_body = response.encode(self.encoding)

tree = etree.fromstring(response_body)
trains = tree.xpath('/passages/train')
Expand Down
13 changes: 3 additions & 10 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,16 @@ def load_config():
monkeypatch.setattr(RerWatcher, 'load_config', load_config)


@pytest.fixture(scope='function')
def requests_fixture():
with open('tests/fixture.xml') as file:
return file.read()


class MockResponse:
def __init__(self, text):
self.text = text


FAKE_RESPONSE = MockResponse(requests_fixture())


@pytest.fixture(scope='function')
def mock_requests(monkeypatch):
def mock_requests(monkeypatch, requests_fixture):
def get():
return FAKE_RESPONSE
return requests_fixture

monkeypatch.setattr(requests, 'get', get)

Expand Down
4 changes: 2 additions & 2 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ def test_rerwatcher_workflow(mocker, mock_config):
]
assert expected_formatter_calls == formatter.call_args_list
expected_display_calls = [
call([sentinel.timetable1, sentinel.timetable2]),
call([sentinel.timetable1, sentinel.timetable2]),
call(messages=[sentinel.timetable1, sentinel.timetable2]),
call(messages=[sentinel.timetable1, sentinel.timetable2]),
]
assert expected_display_calls == display.print.call_args_list
assert 2 == sleep.call_count
5 changes: 2 additions & 3 deletions tests/test_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from rerwatcher.formatter import (TimeTable, format_timedelta,
calculate_time_delta_with_now,
TransilienApiFormatter)
from tests.conftest import FAKE_RESPONSE


def test_timetable_text():
Expand Down Expand Up @@ -39,10 +38,10 @@ def test_calculate_time_delta_with_now():


@freeze_time("27-10-2018 13:30")
def test_transilien_api_formatter():
def test_transilien_api_formatter(requests_fixture):
formatter = TransilienApiFormatter()

result = formatter.format_response(FAKE_RESPONSE)
result = formatter.format_response(requests_fixture)

assert result[0].miss == 'DACA'
assert result[0].time == '8h'
Expand Down

0 comments on commit 4ca8008

Please sign in to comment.