Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✅ (show) test happy path #13

Merged
merged 1 commit into from
Mar 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ pip install pre-commit
pip install -r requirements-dev.txt -U
pre-commit install
```

### testing

```bash
pytest
```
2 changes: 0 additions & 2 deletions nowplaying/show/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
2 changes: 1 addition & 1 deletion nowplaying/show/show.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Empty file added tests/__init__.py
Empty file.
8 changes: 8 additions & 0 deletions tests/fixtures/cast_now_during_show.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0"?>
<cast_now version="120826" created="2017-11-18T19:22:01+0100" created_int="20171118192201">
<real_name>Voice of Hindu Kush</real_name>
<duration_h>1</duration_h>
<start_time>2019-01-27T14:00:00+01:00</start_time>
<end_time>2319-01-27T15:00:00+01:00</end_time>
<url>https://www.rabe.ch/stimme-der-kutuesch/</url>
</cast_now>
7 changes: 7 additions & 0 deletions tests/fixtures/cast_now_no_url.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0"?>
<cast_now version="120826" created="2017-11-18T19:22:01+0100" created_int="20171118192201">
<real_name>Voice of Hindu Kush</real_name>
<duration_h>1</duration_h>
<start_time>2019-01-27T14:00:00+01:00</start_time>
<end_time>2319-01-27T15:00:00+01:00</end_time>
</cast_now>
8 changes: 8 additions & 0 deletions tests/fixtures/cast_now_past_show.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0"?>
<cast_now version="120826" created="2017-11-18T19:22:01+0100" created_int="20171118192201">
<real_name>Voice of Hindu Kush</real_name>
<duration_h>1</duration_h>
<start_time>2019-01-27T14:00:00+01:00</start_time>
<end_time>2019-01-27T15:00:00+01:00</end_time>
<url>https://www.rabe.ch/stimme-der-kutuesch/</url>
</cast_now>
51 changes: 51 additions & 0 deletions tests/test_show.py
Original file line number Diff line number Diff line change
@@ -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)
43 changes: 43 additions & 0 deletions tests/test_show_client.py
Original file line number Diff line number Diff line change
@@ -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"
)