#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 96 additions and 182 deletions
Showing only changes of commit a8c989617a - Show all commits

View File

@ -12,24 +12,29 @@ def stations():
return baker.make(models.Station, _quantity=2) return baker.make(models.Station, _quantity=2)
@pytest.fixture
def stations_without_default():
return baker.make(models.Station, _quantity=3, default=False)
@pytest.fixture @pytest.fixture
def station_default(): def station_default():
return baker.make(models.Station, default=True) return baker.make(models.Station, default=True)
@pytest.fixture @pytest.fixture
def stations_inactive(): def station_active():
return baker.make(models.Station, _quantity=3, active=False) return baker.make(models.Station, active=True)
@pytest.fixture @pytest.fixture
def station_active(): def ports():
return baker.make(models.Station, active=True) 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 @pytest.fixture

View File

@ -1,9 +1,7 @@
import pytest import pytest
import random
from model_bakery import baker from model_bakery import baker
from aircox.models import Station, Port from aircox.models import Station, Port
from aircox.conf import settings from aircox.conf import settings
@ -11,219 +9,130 @@ from aircox.conf import settings
class TestStationQuerySet: class TestStationQuerySet:
# default method : not possible to have several stations by default. # default method : not possible to have several stations by default.
# default method : return a selected instance of station but do not save it. # default method : return a selected instance of station but do not save it.
def test_default_without_default_station(self, stations_without_default): def test_default_without_default_station(self, stations):
returned_station = Station.objects.default() for station in stations:
station.default = False
station.save()
assert Station.objects.default() is not None
assert returned_station is not None def test_default_by_pk(self, stations):
assert returned_station == Station.objects.first() for station in stations:
station.default = False
station.save()
assert Station.objects.default(station=stations[0].pk) == stations[0]
def test_default_with_pkargs_and_without_default_station( def test_default_with_default_station(self, stations, station_default):
self, stations_without_default for station in stations:
): station.default = False
station = stations_without_default[1] station.save()
returned_station = Station.objects.default( assert Station.objects.default() == station_default
station=station.pk
)
assert station == returned_station def test_active(self, stations, station_active):
for station in stations:
def test_default_with_default_station( station.active = False
self, station_default, stations_without_default station.save()
): assert station not in Station.objects.active()
returned_default_station = Station.objects.default() assert station_active in Station.objects.active()
assert returned_default_station == station_default
def test_default_with_pkargs_and_default_station(
self, station_default, stations_without_default
):
returned_default_station = Station.objects.default(
station=station_default.pk
)
assert returned_default_station == station_default
def test_active(self, station_active, stations_inactive):
filtered_active_station = Station.objects.active()
assert station_active in filtered_active_station
@pytest.mark.django_db @pytest.mark.django_db
class TestStation: class TestStation:
def test_stream_field_filled(self, stations): def test_stream_field_filled(self, stations):
streams_urls = "http://radiocampus.be/stream1.mp3\nhttp://radiocampus.be/stream2.mp3" streams_urls = "http://radiocampus.be/stream1.mp3\nhttp://radiocampus.be/stream2.mp3"
station = baker.make(Station, audio_streams=streams_urls) stations[0].audio_streams = streams_urls
assert stations[0].streams == [
assert station.streams == [
"http://radiocampus.be/stream1.mp3", "http://radiocampus.be/stream1.mp3",
"http://radiocampus.be/stream2.mp3", "http://radiocampus.be/stream2.mp3",
] ]
def test_stream_field_empty(self, stations): def test_stream_field_empty(self, stations):
station = baker.make(Station, audio_streams=None) stations[0].audio_streams = None
assert stations[0].streams == []
assert station.streams == []
def test__str__(self, stations): def test__str__(self, stations):
for station in stations: for station in stations:
assert station.name == station.__str__() assert station.name == station.__str__()
def test_save_without_path(self, stations): def test_save_without_path(self, stations):
station = baker.make(Station, path=None, slug="a-slug-with-dash") stations[0].path = None
station.save() stations[0].slug = "a-slug-with-dash"
stations[0].save()
assert ( assert (
station.path stations[0].path
== settings.CONTROLLERS_WORKING_DIR + "\\a_slug_with_dash" == settings.CONTROLLERS_WORKING_DIR + "\\a_slug_with_dash"
) )
def test_save_default_station_while_anotherone_is_default( def test_save_default_station_while_anotherone_is_default(
self, station_default, stations_without_default self, stations, station_default
): ):
old_default_station = station_default for station in stations:
new_default_station = stations_without_default[1] station.default = False
new_default_station.default = True station.save()
new_default_station.save() stations[0].default = True
old_default_station.refresh_from_db() stations[0].save()
new_default_station.refresh_from_db()
assert Station.objects.filter(default=True).count() == 1 assert Station.objects.filter(default=True).count() == 1
assert old_default_station.default is False assert stations[0] in Station.objects.filter(default=True)
assert new_default_station.default is True
@pytest.mark.django_db @pytest.mark.django_db
class TestPortQuerySet: class TestPortQuerySet:
# issues with the preset incompatibility between port and direction. def test_active_value_true(self, ports):
# I need to control which type and direction for each fake model for port in ports:
# Otherwhise pytest fail randomly. random_value = random.choice([True, False])
def test_active_value_true(self): port.active = random_value
port1 = baker.make( port.save()
Port, active_ports = Port.objects.active(value=True)
active=True, for port in active_ports:
type=Port.TYPE_ICECAST, assert port.active == True
direction=Port.DIRECTION_OUTPUT,
)
port2 = baker.make(
Port,
active=False,
type=Port.TYPE_ICECAST,
direction=Port.DIRECTION_OUTPUT,
)
active_ports = Port.objects.active()
assert port1 in active_ports def test_active_value_false(self, ports):
assert port2 not in active_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_active_value_false(self): def test_output(self, ports):
port1 = baker.make( for port in Port.objects.output():
Port, assert port.direction == Port.DIRECTION_OUTPUT
active=True,
type=Port.TYPE_ICECAST,
direction=Port.DIRECTION_OUTPUT,
)
port2 = baker.make(
Port,
active=False,
type=Port.TYPE_ICECAST,
direction=Port.DIRECTION_OUTPUT,
)
inactive_ports = Port.objects.active(value=False) def test_input(self, ports):
assert port1 not in inactive_ports for port in Port.objects.input():
assert port2 in inactive_ports assert port.direction == Port.DIRECTION_INPUT
def test_output(self):
port1 = baker.make(
Port, type=Port.TYPE_ICECAST, direction=Port.DIRECTION_OUTPUT
)
port2 = baker.make(
Port, type=Port.TYPE_HTTP, direction=Port.DIRECTION_INPUT
)
output_ports = Port.objects.output()
assert port1 in output_ports
assert port2 not in output_ports
def test_input(self):
port1 = baker.make(
Port, type=Port.TYPE_ICECAST, direction=Port.DIRECTION_OUTPUT
)
port2 = baker.make(
Port, type=Port.TYPE_HTTP, direction=Port.DIRECTION_INPUT
)
input_ports = Port.objects.input()
assert port1 not in input_ports
assert port2 in input_ports
@pytest.mark.django_db @pytest.mark.django_db
class TestPort: class TestPort:
def test__str__(self): def test__str__(self, ports):
port = baker.make( for port in ports:
Port, type=Port.TYPE_ICECAST, direction=Port.DIRECTION_OUTPUT if (
) port.type == Port.TYPE_ICECAST
and port.direction == Port.DIRECTION_OUTPUT
):
assert port.__str__() == "output: icecast #1" assert port.__str__() == "output: icecast #1"
def test_is_valid_type_and_type_is_input(self): def test_is_valid_type_and_type_is_input(self, ports):
porthttp = baker.make( for port in ports:
Port, type=Port.TYPE_HTTP, direction=Port.DIRECTION_INPUT if port.direction == Port.DIRECTION_INPUT:
) assert port.is_valid_type() == True
porthttps = baker.make(
Port, type=Port.TYPE_HTTPS, direction=Port.DIRECTION_INPUT
)
assert porthttp.is_valid_type() == True def test_is_valid_type_and_type_is_not_input(self, ports):
assert porthttps.is_valid_type() == True for port in ports:
if port.direction == Port.DIRECTION_OUTPUT:
assert port.is_valid_type() == True
def test_is_valid_type_and_type_is_not_input(self): def test_save_with_valid_type_icecast_output(self, ports):
porticecast = baker.make( assert Port.objects.get(id=ports[0].id) is not None
Port, type=Port.TYPE_ICECAST, direction=Port.DIRECTION_OUTPUT
)
portfile = baker.make(
Port, type=Port.TYPE_FILE, direction=Port.DIRECTION_OUTPUT
)
assert porticecast.is_valid_type() == True def test_save_with_valid_type_file_output(self, ports):
assert portfile.is_valid_type() == True assert Port.objects.get(id=ports[1].id) is not None
def test_save_with_valid_type_icecast_output(self): def test_save_with_valid_type_http_input(self, ports):
port = baker.make( assert Port.objects.get(id=ports[2].id) is not None
Port, type=Port.TYPE_ICECAST, direction=Port.DIRECTION_OUTPUT
)
port.save()
saved_port = Port.objects.get(id=port.id)
assert saved_port 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_with_valid_type_file_output(self):
port = baker.make(
Port, type=Port.TYPE_FILE, direction=Port.DIRECTION_OUTPUT
)
port.save()
saved_port = Port.objects.get(id=port.id)
assert saved_port is not None
def test_save_with_valid_type_http_input(self):
port = baker.make(
Port, type=Port.TYPE_HTTP, direction=Port.DIRECTION_INPUT
)
port.save()
saved_port = Port.objects.get(id=port.id)
assert saved_port is not None
def test_save_with_valid_type_https_input(self):
port = baker.make(
Port, type=Port.TYPE_HTTPS, direction=Port.DIRECTION_INPUT
)
port.save()
saved_port = Port.objects.get(id=port.id)
assert saved_port is not None
def test_save_without_valid_type(self): def test_save_without_valid_type(self):
with pytest.raises(ValueError): with pytest.raises(ValueError):