From c836e20412f08b404092929f36373face7c394f8 Mon Sep 17 00:00:00 2001 From: Lucas Bickel Date: Sun, 27 Jan 2019 14:55:17 +0100 Subject: [PATCH] :white_check_mark: (show) test happy path --- README.md | 6 +++ nowplaying/show/client.py | 2 - nowplaying/show/show.py | 2 +- requirements-dev.txt | 1 + tests/__init__.py | 0 tests/fixtures/cast_now_during_show.xml | 8 ++++ tests/fixtures/cast_now_no_url.xml | 7 ++++ tests/fixtures/cast_now_past_show.xml | 8 ++++ tests/test_show.py | 51 +++++++++++++++++++++++++ tests/test_show_client.py | 43 +++++++++++++++++++++ 10 files changed, 125 insertions(+), 3 deletions(-) create mode 100644 tests/__init__.py create mode 100644 tests/fixtures/cast_now_during_show.xml create mode 100644 tests/fixtures/cast_now_no_url.xml create mode 100644 tests/fixtures/cast_now_past_show.xml create mode 100644 tests/test_show.py create mode 100644 tests/test_show_client.py diff --git a/README.md b/README.md index c7c20ea3..5b7535b2 100644 --- a/README.md +++ b/README.md @@ -9,3 +9,9 @@ pip install pre-commit pip install -r requirements-dev.txt -U pre-commit install ``` + +### testing + +```bash +pytest +``` diff --git a/nowplaying/show/client.py b/nowplaying/show/client.py index 3d0f8e1d..c1c36edf 100644 --- a/nowplaying/show/client.py +++ b/nowplaying/show/client.py @@ -19,8 +19,6 @@ logger = logging.getLogger("now-playing") -DEFAULT_SHOW_NAME = "Sendung gemäss Programm" -DEFAULT_SHOW_URL = "http://www.rabe.ch" DEFAULT_SHOW_DURATION = 30 # 30 seconds diff --git a/nowplaying/show/show.py b/nowplaying/show/show.py index f9b3afd0..247ab946 100644 --- a/nowplaying/show/show.py +++ b/nowplaying/show/show.py @@ -14,7 +14,7 @@ logger = logging.getLogger("now-playing") -DEFAULT_SHOW_URL = "http://www.rabe.ch" +DEFAULT_SHOW_URL = "https://www.rabe.ch" class ShowError(Exception): diff --git a/requirements-dev.txt b/requirements-dev.txt index a59e7b3b..17ecbb96 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -11,3 +11,4 @@ isort==4.3.4 pytest==4.1.1 pytest-cov==2.6.1 pytest-env==0.6.2 +mock=2.0.0 diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/fixtures/cast_now_during_show.xml b/tests/fixtures/cast_now_during_show.xml new file mode 100644 index 00000000..d79b4ce2 --- /dev/null +++ b/tests/fixtures/cast_now_during_show.xml @@ -0,0 +1,8 @@ + + + Voice of Hindu Kush + 1 + 2019-01-27T14:00:00+01:00 + 2319-01-27T15:00:00+01:00 + https://www.rabe.ch/stimme-der-kutuesch/ + diff --git a/tests/fixtures/cast_now_no_url.xml b/tests/fixtures/cast_now_no_url.xml new file mode 100644 index 00000000..6a59d88a --- /dev/null +++ b/tests/fixtures/cast_now_no_url.xml @@ -0,0 +1,7 @@ + + + Voice of Hindu Kush + 1 + 2019-01-27T14:00:00+01:00 + 2319-01-27T15:00:00+01:00 + diff --git a/tests/fixtures/cast_now_past_show.xml b/tests/fixtures/cast_now_past_show.xml new file mode 100644 index 00000000..cefd1803 --- /dev/null +++ b/tests/fixtures/cast_now_past_show.xml @@ -0,0 +1,8 @@ + + + Voice of Hindu Kush + 1 + 2019-01-27T14:00:00+01:00 + 2019-01-27T15:00:00+01:00 + https://www.rabe.ch/stimme-der-kutuesch/ + diff --git a/tests/test_show.py b/tests/test_show.py new file mode 100644 index 00000000..d4c6a070 --- /dev/null +++ b/tests/test_show.py @@ -0,0 +1,51 @@ +from datetime import datetime + +import pytest +import pytz + +from nowplaying.show import show + + +class TestShow: + def test_init(self): + s = show.Show() + assert s.starttime == s.endtime + + def test_name(self): + s = show.Show() + assert s.name is None + s.set_name("Test") + assert s.name == "Test" + + def test_url(self): + s = show.Show() + assert s.url == show.DEFAULT_SHOW_URL + s.set_url("http://example.com/show") + assert s.url == "http://example.com/show" + + def test_rabe_default_url(self): + assert show.DEFAULT_SHOW_URL == "https://www.rabe.ch" + + def test_starttime(self): + s = show.Show() + t = datetime.now(pytz.timezone("UTC")) + o = s.starttime + s.set_starttime(t) + assert s.starttime == t + assert s.starttime != o + with pytest.raises(show.ShowError): + s.set_starttime("2019-01-01") + + def test_endtime(self): + s = show.Show() + t = datetime.now(pytz.timezone("UTC")) + o = s.endtime + s.set_endtime(t) + assert s.endtime == t + assert s.endtime != o + with pytest.raises(show.ShowError): + s.set_endtime("2019-01-01") + + def test_prettyprinting(self): + s = show.Show() + assert "Show 'None'" in str(s) diff --git a/tests/test_show_client.py b/tests/test_show_client.py new file mode 100644 index 00000000..87b3c4e9 --- /dev/null +++ b/tests/test_show_client.py @@ -0,0 +1,43 @@ +from datetime import datetime + +import mock +import pytest +import pytz + +from nowplaying.show import client + + +class TestShowClient: + def test_init(self): + s = client.ShowClient("http://example.com") + assert s.current_show_url == "http://example.com" + + @mock.patch("urllib.request.urlopen") + def test_update(self, mock_urlopen): + mock_urlopen.return_value = "tests/fixtures/cast_now_during_show.xml" + s = client.ShowClient("http://example.com") + s.update() + assert s.show.name == "Voice of Hindu Kush" + assert s.show.starttime == datetime( + 2019, 1, 27, 13, tzinfo=pytz.timezone("UTC") + ) + assert s.show.endtime == datetime(2319, 1, 27, 14, tzinfo=pytz.timezone("UTC")) + assert s.show.url == "https://www.rabe.ch/stimme-der-kutuesch/" + + @mock.patch("urllib.request.urlopen") + def test_update_no_url(self, mock_urlopen): + mock_urlopen.return_value = "tests/fixtures/cast_now_no_url.xml" + s = client.ShowClient("http://example.com") + s.update() + assert s.show.url == "https://www.rabe.ch" + + @mock.patch("urllib.request.urlopen") + def test_update_past_show(self, mock_urlopen): + mock_urlopen.return_value = "tests/fixtures/cast_now_past_show.xml" + s = client.ShowClient("http://example.com") + with pytest.raises(client.ShowClientError) as info: + s.update() + assert ( + str(info.value) + == "Show end time (2019-01-27 14:00:00+00:00) is in the past" + )