Skip to content

Commit

Permalink
chore: pylint most tests (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
hairmare authored Dec 26, 2021
1 parent f696455 commit 32ed250
Show file tree
Hide file tree
Showing 7 changed files with 413 additions and 315 deletions.
101 changes: 55 additions & 46 deletions tests/test_show.py
Original file line number Diff line number Diff line change
@@ -1,51 +1,60 @@
"""Tests for :class:`Show`."""

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)
from nowplaying.show.show import DEFAULT_SHOW_URL, Show, ShowError


def test_init():
"""Test :class:`Show`'s :meth:`.__init__` method."""
show = Show()
assert show.starttime == show.endtime


def test_name():
"""Test :class:`Show`'s :meth:`name` property."""
show = Show()
assert show.name is None
show.set_name("Test")
assert show.name == "Test"


def test_url():
"""Test :class:`Show`'s :meth:`url` property."""
show = Show()
assert show.url == DEFAULT_SHOW_URL
show.set_url("http://example.com/show")
assert show.url == "http://example.com/show"


def test_starttime():
"""Test :class:`Show`'s :meth:`starttime` property."""
show = Show()
time = datetime.now(pytz.timezone("UTC"))
original_time = show.starttime
show.set_starttime(time)
assert show.starttime == time
assert show.starttime != original_time
with pytest.raises(ShowError):
show.set_starttime("2019-01-01")


def test_endtime():
"""Test :class:`Show`'s :meth:`endtime` property."""
show = Show()
time = datetime.now(pytz.timezone("UTC"))
original_time = show.endtime
show.set_endtime(time)
assert show.endtime == time
assert show.endtime != original_time
with pytest.raises(ShowError):
show.set_endtime("2019-01-01")


def test_prettyprinting():
"""Test :class:`Show`'s :meth:`__str__` method."""
show = Show()
assert "Show 'None'" in str(show)
186 changes: 103 additions & 83 deletions tests/test_show_client.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""Tests for :class:`ShowClient`."""

import json
from datetime import datetime
from unittest.mock import Mock
Expand All @@ -7,97 +9,115 @@
import pytz
import requests

from nowplaying.show import client
from nowplaying.show.client import ShowClient, ShowClientError

_BASE_URL = "http://example.com/api/live-info-v2/format/json"

def file_get_contents(filename):
with open(filename) as f:
return f.read()

def file_get_contents(filename: str) -> str:
"""Read a file and returns its contents."""
with open(filename) as file:
return file.read()

class TestShowClient:
def test_init(self):
s = client.ShowClient("http://example.com/api/live-info-v2/format/json")
assert s.current_show_url == "http://example.com/api/live-info-v2/format/json"

@mock.patch("requests.get")
def test_update(self, mock_requests_get):
mock_requests_get.return_value.json = Mock(
return_value=json.loads(
file_get_contents("tests/fixtures/cast_now_during_show.json")
)
)
s = client.ShowClient("http://example.com/api/live-info-v2/format/json")
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("requests.get")
def test_update_connection_error(self, mock_requests_get):
"""Don't crash if external api refuses to connect."""
mock_requests_get.side_effect = requests.exceptions.ConnectionError()
s = client.ShowClient("http://example.com/api/live-info-v2/format/json")
s.update()
assert s.show.name is None
assert s.show.url == "https://www.rabe.ch"

@mock.patch("requests.get")
def test_update_no_url(self, mock_requests_get):
mock_requests_get.return_value.json = Mock(
return_value=json.loads(
file_get_contents("tests/fixtures/cast_now_no_url.json")
)
def test_init():
"""Test :class:`ShowClient`'s :meth:`.__init__` method."""
show_client = ShowClient(_BASE_URL)
assert show_client.current_show_url == _BASE_URL


@mock.patch("requests.get")
def test_update(mock_requests_get):
"""Test :class:`ShowClient`'s :meth:`update` method."""
mock_requests_get.return_value.json = Mock(
return_value=json.loads(
file_get_contents("tests/fixtures/cast_now_during_show.json")
)
s = client.ShowClient("http://example.com/api/live-info-v2/format/json")
s.update()
assert s.show.url == "https://www.rabe.ch"

@mock.patch("requests.get")
@pytest.mark.parametrize(
"fixture,field",
[
("cast_now_no_name", "name"),
("cast_now_no_end", "end time"),
("cast_now_no_start", "start time"),
],
)
def test_update_empty_field(self, mock_requests_get, fixture, field):
mock_requests_get.return_value.json = Mock(
return_value=json.loads(file_get_contents(f"tests/fixtures/{fixture}.json"))
)
s = client.ShowClient("http://example.com/api/live-info-v2/format/json")
with pytest.raises(client.ShowClientError) as info:
s.update()
assert str(info.value) == f"Missing show {field}"

@mock.patch("requests.get")
def test_update_past_show(self, mock_requests_get):
mock_requests_get.return_value.json = Mock(
return_value=json.loads(
file_get_contents("tests/fixtures/cast_now_past_show.json")
)
show_client = ShowClient(_BASE_URL)
show_client.update()
assert show_client.show.name == "Voice of Hindu Kush"
assert show_client.show.starttime == datetime(
2019, 1, 27, 13, tzinfo=pytz.timezone("UTC")
)
assert show_client.show.endtime == datetime(
2319, 1, 27, 14, tzinfo=pytz.timezone("UTC")
)
assert show_client.show.url == "https://www.rabe.ch/stimme-der-kutuesch/"


@mock.patch("requests.get")
def test_update_connection_error(mock_requests_get):
"""Test :class:`ShowClient`'s :meth:`update` method when a connection error occurs.
It should not crash if external api refuses to connect.
"""
mock_requests_get.side_effect = requests.exceptions.ConnectionError()
show_client = ShowClient(_BASE_URL)
show_client.update()
assert show_client.show.name is None
assert show_client.show.url == "https://www.rabe.ch"


@mock.patch("requests.get")
def test_update_no_url(mock_requests_get):
"""Test :class:`ShowClient`'s :meth:`update` method when no url is returned."""
mock_requests_get.return_value.json = Mock(
return_value=json.loads(
file_get_contents("tests/fixtures/cast_now_no_url.json")
)
s = client.ShowClient("http://example.com/api/live-info-v2/format/json")
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"
)
show_client = ShowClient(_BASE_URL)
show_client.update()
assert show_client.show.url == "https://www.rabe.ch"


@mock.patch("requests.get")
@pytest.mark.parametrize(
"fixture,field",
[
("cast_now_no_name", "name"),
("cast_now_no_end", "end time"),
("cast_now_no_start", "start time"),
],
)
def test_update_empty_field(mock_requests_get, fixture, field):
"""Test :class:`ShowClient`'s :meth:`update` method when a field is empty."""
mock_requests_get.return_value.json = Mock(
return_value=json.loads(file_get_contents(f"tests/fixtures/{fixture}.json"))
)
show_client = ShowClient(_BASE_URL)
with pytest.raises(ShowClientError) as info:
show_client.update()
assert str(info.value) == f"Missing show {field}"


@mock.patch("requests.get")
def test_update_past_show(mock_requests_get):
"""Test :class:`ShowClient`'s :meth:`update` method when the show is in the past."""
mock_requests_get.return_value.json = Mock(
return_value=json.loads(
file_get_contents("tests/fixtures/cast_now_past_show.json")
)
)
show_client = ShowClient(_BASE_URL)
with pytest.raises(ShowClientError) as info:
show_client.update()
assert str(info.value) == "Show end time (2019-01-27 14:00:00+00:00) is in the past"


@mock.patch("requests.get")
def test_update_show_empty(self, mock_requests_get):
"""Should not crash if external api has no info."""
mock_requests_get.return_value.json = Mock(
return_value=json.loads(
file_get_contents("tests/fixtures/cast_now_show_empty.json")
)
@mock.patch("requests.get")
def test_update_show_empty(mock_requests_get):
"""Test :class:`ShowClient`'s :meth:`update` method when the show is empty.
It should not crash if external api has no info.
"""
mock_requests_get.return_value.json = Mock(
return_value=json.loads(
file_get_contents("tests/fixtures/cast_now_show_empty.json")
)
s = client.ShowClient("http://example.com/api/live-info-v2/format/json")
s.update()
assert s.show.name is None
assert s.show.url == "https://www.rabe.ch"
)
show_client = ShowClient(_BASE_URL)
show_client.update()
assert show_client.show.name is None
assert show_client.show.url == "https://www.rabe.ch"
Loading

0 comments on commit 32ed250

Please sign in to comment.