#100: Test model station #104

Closed
lvanwambeke wants to merge 4 commits from dev-1.0-100-pytest-model-station into develop-1.0
2 changed files with 166 additions and 0 deletions

View File

@ -12,6 +12,31 @@ def stations():
return baker.make(models.Station, _quantity=2)
@pytest.fixture
def station_default():
return baker.make(models.Station, default=True)
@pytest.fixture
def station_active():
return baker.make(models.Station, active=True)
@pytest.fixture
def ports():
compatible_param = (
[models.Port.TYPE_ICECAST, models.Port.DIRECTION_OUTPUT],
[models.Port.TYPE_FILE, models.Port.DIRECTION_OUTPUT],
[models.Port.TYPE_HTTP, models.Port.DIRECTION_INPUT],
[models.Port.TYPE_HTTPS, models.Port.DIRECTION_INPUT],
)
items = [
baker.make(models.Port, type=param[0], direction=param[1])
for param in compatible_param
]
return items
@pytest.fixture
def programs(stations):
items = list(

View File

@ -0,0 +1,141 @@
import pytest
import random
from model_bakery import baker
from aircox.models import Station, Port
from aircox.conf import settings
@pytest.mark.django_db
class TestStationQuerySet:
# default method : not possible to have several stations by default.
# default method : return a selected instance of station but do not save it.
def test_default_without_default_station(self, stations):
for station in stations:
station.default = False
station.save()
assert Station.objects.default() is not None
def test_default_by_pk(self, stations):
for station in stations:
station.default = False
station.save()
assert Station.objects.default(station=stations[0].pk) == stations[0]
def test_default_with_default_station(self, stations, station_default):
for station in stations:
station.default = False
station.save()
assert Station.objects.default() == station_default
def test_active(self, stations, station_active):
for station in stations:
station.active = False
station.save()
assert station not in Station.objects.active()
assert station_active in Station.objects.active()
@pytest.mark.django_db
class TestStation:
def test_stream_field_filled(self, stations):
streams_urls = "http://radiocampus.be/stream1.mp3\nhttp://radiocampus.be/stream2.mp3"
stations[0].audio_streams = streams_urls
assert stations[0].streams == [
"http://radiocampus.be/stream1.mp3",
"http://radiocampus.be/stream2.mp3",
]
def test_stream_field_empty(self, stations):
stations[0].audio_streams = None
assert stations[0].streams == []
def test__str__(self, stations):
for station in stations:
assert station.name == station.__str__()
def test_save_without_path(self, stations):
stations[0].path = None
stations[0].slug = "a-slug-with-dash"
stations[0].save()
assert (
stations[0].path
== settings.CONTROLLERS_WORKING_DIR + "\\a_slug_with_dash"
)
def test_save_default_station_while_anotherone_is_default(
self, stations, station_default
):
for station in stations:
station.default = False
station.save()
stations[0].default = True
stations[0].save()
assert Station.objects.filter(default=True).count() == 1
assert stations[0] in Station.objects.filter(default=True)
@pytest.mark.django_db
class TestPortQuerySet:
def test_active_value_true(self, ports):
for port in ports:
random_value = random.choice([True, False])
port.active = random_value
port.save()
active_ports = Port.objects.active(value=True)
for port in active_ports:
assert port.active == True
def test_active_value_false(self, ports):
for port in ports:
random_value = random.choice([True, False])
port.active = random_value
port.save()
for port in Port.objects.active(value=False):
assert port.active == False
def test_output(self, ports):
for port in Port.objects.output():
assert port.direction == Port.DIRECTION_OUTPUT
def test_input(self, ports):
for port in Port.objects.input():
assert port.direction == Port.DIRECTION_INPUT
@pytest.mark.django_db
class TestPort:
def test__str__(self, ports):
for port in ports:
if (
port.type == Port.TYPE_ICECAST
and port.direction == Port.DIRECTION_OUTPUT
):
assert port.__str__() == "output: icecast #1"
def test_is_valid_type_and_type_is_input(self, ports):
for port in ports:
if port.direction == Port.DIRECTION_INPUT:
assert port.is_valid_type() == True
def test_is_valid_type_and_type_is_not_input(self, ports):
for port in ports:
if port.direction == Port.DIRECTION_OUTPUT:
assert port.is_valid_type() == True
def test_save_with_valid_type_icecast_output(self, ports):
assert Port.objects.get(id=ports[0].id) is not None
def test_save_with_valid_type_file_output(self, ports):
assert Port.objects.get(id=ports[1].id) is not None
def test_save_with_valid_type_http_input(self, ports):
assert Port.objects.get(id=ports[2].id) is not None
def test_save_with_valid_type_https_input(self, ports):
assert Port.objects.get(id=ports[3].id) is not None
def test_save_without_valid_type(self):
with pytest.raises(ValueError):
baker.make(
Port, type=Port.TYPE_HTTP, direction=Port.DIRECTION_OUTPUT
)