Skip to content

Commit

Permalink
Merge pull request #13 from hairmare/chore/testing-for-show-parts
Browse files Browse the repository at this point in the history
✅ (show) test happy path
  • Loading branch information
hairmare authored Mar 24, 2019
2 parents c197aa3 + c836e20 commit b3f4e0a
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 3 deletions.
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"
)

0 comments on commit b3f4e0a

Please sign in to comment.