From e0079a537f3a22822e7794154ca98ae17211324796a8915ed9267967b37b82ba Mon Sep 17 00:00:00 2001 From: f4lco Date: Sat, 28 Mar 2020 21:12:17 +0100 Subject: [PATCH] Support empty menu response In recent weeks many API calls return "null". Instead of letting the request fail, the parser now treats "null" as empty menu. --- stw_potsdam/feed.py | 5 +++-- tests/resources/empty.json | 1 + tests/resources/empty_menu_output.xml | 4 ++++ tests/test_consistency.py | 28 +++++++++++++++------------ 4 files changed, 24 insertions(+), 14 deletions(-) create mode 100644 tests/resources/empty.json create mode 100644 tests/resources/empty_menu_output.xml diff --git a/stw_potsdam/feed.py b/stw_potsdam/feed.py index d5d6803..35e1acf 100644 --- a/stw_potsdam/feed.py +++ b/stw_potsdam/feed.py @@ -58,8 +58,9 @@ def render_menu(menu): """ builder = LazyBuilder() - for day in _active_days(menu): - _process_day(builder, day) + if menu: + for day in _active_days(menu): + _process_day(builder, day) return builder.toXMLFeed() diff --git a/tests/resources/empty.json b/tests/resources/empty.json new file mode 100644 index 0000000..d5bca82 --- /dev/null +++ b/tests/resources/empty.json @@ -0,0 +1 @@ +null \ No newline at end of file diff --git a/tests/resources/empty_menu_output.xml b/tests/resources/empty_menu_output.xml new file mode 100644 index 0000000..5e6c19b --- /dev/null +++ b/tests/resources/empty_menu_output.xml @@ -0,0 +1,4 @@ + + + + diff --git a/tests/test_consistency.py b/tests/test_consistency.py index 9afa76c..efb155f 100644 --- a/tests/test_consistency.py +++ b/tests/test_consistency.py @@ -16,18 +16,13 @@ def _canteen(): return read_canteen_config()['griebnitzsee'] -def _menu(): - with open(_resource_path('input.json')) as menu_file: +def _read_menu(resource_name): + with open(_resource_path(resource_name)) as menu_file: return json.load(menu_file) -def _expected_meta_feed(): - with io.open(_resource_path('meta_output.xml'), encoding='utf-8') as xml: - return xml.read() - - -def _expected_menu_feed(): - with io.open(_resource_path('menu_output.xml'), encoding='utf-8') as xml: +def _read_feed(resource_name): + with io.open(_resource_path(resource_name), encoding='utf-8') as xml: return xml.read() @@ -37,14 +32,23 @@ def test_meta_consistency(): actual = feed.render_meta(canteen, menu_feed_url) - expected = _expected_meta_feed() + expected = _read_feed('meta_output.xml') assert expected == actual def test_menu_consistency(): - menu = _menu() + menu = _read_menu('input.json') actual = feed.render_menu(menu) - expected = _expected_menu_feed() + expected = _read_feed('menu_output.xml') + assert expected == actual + + +def test_empty_menu(): + menu = _read_menu('empty.json') + + actual = feed.render_menu(menu) + + expected = _read_feed('empty_menu_output.xml') assert expected == actual