#100: Test model station #104
|
@ -12,6 +12,26 @@ 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
|
||||||
|
def station_default():
|
||||||
|
return baker.make(models.Station, default=True)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def stations_inactive():
|
||||||
|
return baker.make(models.Station, _quantity=3, active=False)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def station_active():
|
||||||
|
return baker.make(models.Station, active=True)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def programs(stations):
|
def programs(stations):
|
||||||
items = list(
|
items = list(
|
||||||
|
|
|
@ -4,208 +4,229 @@ from model_bakery import baker
|
||||||
|
|
||||||
from aircox.models import Station, Port
|
from aircox.models import Station, Port
|
||||||
|
|
||||||
from aircox.tests.conftest import stations
|
|
||||||
|
|
||||||
from aircox.conf import settings
|
from aircox.conf import settings
|
||||||
|
|
||||||
#(j'ai utilisé Baker directement ici à plusieurs reprises car la fixture 'stations' du fichier conftest génère des champs aléatoires avec lesquelles j'avais du mal à réaliser les tests.)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.django_db
|
@pytest.mark.django_db
|
||||||
class TestStationQuerySet:
|
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_without_default):
|
||||||
|
returned_station = Station.objects.default()
|
||||||
|
|
||||||
# test the default method: the method consists in recovering a single default station in different cases. The definition of the station by this method is a univocal choice (not possible to have several stations by default in the db).
|
assert returned_station is not None
|
||||||
# Each case assumes that several stations are already in the database according to different parameters.
|
assert returned_station == Station.objects.first()
|
||||||
def test_default_case1(self):
|
|
||||||
""" case 1 :
|
|
||||||
- no default station in db
|
|
||||||
- no station defined in method """
|
|
||||||
|
|
||||||
#fake db
|
def test_default_with_pkargs_and_without_default_station(
|
||||||
baker.make(Station, _quantity=3, default=False)
|
self, stations_without_default
|
||||||
|
):
|
||||||
|
station = stations_without_default[1]
|
||||||
|
returned_station = Station.objects.default(
|
||||||
|
station=station.pk
|
||||||
|
)
|
||||||
|
|
||||||
#method test
|
assert station == returned_station
|
||||||
default_station = Station.objects.default()
|
|
||||||
|
|
||||||
# check: we have recovered one single default station as the method indicates.
|
def test_default_with_default_station(
|
||||||
assert default_station is not None
|
self, station_default, stations_without_default
|
||||||
assert default_station == Station.objects.first()
|
):
|
||||||
|
returned_default_station = Station.objects.default()
|
||||||
|
|
||||||
def test_default_case2(self):
|
assert returned_default_station == station_default
|
||||||
""" case 2 :
|
|
||||||
- one default station in db
|
|
||||||
- no station defined in method """
|
|
||||||
|
|
||||||
# fake db
|
def test_default_with_pkargs_and_default_station(
|
||||||
baker.make(Station, _quantity=2, default=False)
|
self, station_default, stations_without_default
|
||||||
default_station = baker.make(Station, default=True)
|
):
|
||||||
|
returned_default_station = Station.objects.default(
|
||||||
|
station=station_default.pk
|
||||||
|
)
|
||||||
|
|
||||||
# method test
|
assert returned_default_station == station_default
|
||||||
specific_station = Station.objects.default()
|
|
||||||
|
|
||||||
# check: default station is the one defined in the fake db
|
def test_active(self, station_active, stations_inactive):
|
||||||
assert specific_station == default_station
|
|
||||||
|
|
||||||
def test_default_case3(self):
|
|
||||||
""" case 3 :
|
|
||||||
- one default station in db
|
|
||||||
- one station define in method """
|
|
||||||
|
|
||||||
# fake db
|
|
||||||
baker.make(Station, _quantity=2, default=False)
|
|
||||||
default_station = baker.make(Station, default=True)
|
|
||||||
|
|
||||||
# method test
|
|
||||||
specific_station = Station.objects.default(station=default_station.pk)
|
|
||||||
|
|
||||||
# check: default station is the one defined in the method
|
|
||||||
assert specific_station == default_station
|
|
||||||
|
|
||||||
|
|
||||||
# test the active method: return all the active station's instance
|
|
||||||
def test_active(self):
|
|
||||||
baker.make(Station, _quantity=2, active=False)
|
|
||||||
active_station = baker.make(Station, active=True)
|
|
||||||
|
|
||||||
# test
|
|
||||||
filtered_active_station = Station.objects.active()
|
filtered_active_station = Station.objects.active()
|
||||||
|
|
||||||
# check: active station is returned
|
assert station_active in filtered_active_station
|
||||||
assert active_station in filtered_active_station
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.django_db
|
@pytest.mark.django_db
|
||||||
class TestStation:
|
class TestStation:
|
||||||
|
def test_stream_field_filled(self, stations):
|
||||||
# test the streams method: create an array based on a text by separating each element by the break line character(\n).
|
|
||||||
def test_stream_field_filled(self):
|
|
||||||
#case 1 : text data with break lines is entered in the audio_streams field
|
|
||||||
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)
|
station = baker.make(Station, audio_streams=streams_urls)
|
||||||
assert station.streams == ["http://radiocampus.be/stream1.mp3", "http://radiocampus.be/stream2.mp3"]
|
|
||||||
|
|
||||||
def test_stream_field_empty(self):
|
assert station.streams == [
|
||||||
#case 2 : no data in the audio_streams field
|
"http://radiocampus.be/stream1.mp3",
|
||||||
|
"http://radiocampus.be/stream2.mp3",
|
||||||
|
]
|
||||||
|
|
||||||
|
def test_stream_field_empty(self, stations):
|
||||||
station = baker.make(Station, audio_streams=None)
|
station = baker.make(Station, audio_streams=None)
|
||||||
|
|
||||||
assert station.streams == []
|
assert station.streams == []
|
||||||
|
|
||||||
# test the __str__ method = retrieve 'name' field of the station.
|
|
||||||
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__()
|
||||||
|
|
||||||
# test save method : save the station data in a folder.
|
def test_save_without_path(self, stations):
|
||||||
def test_save_without_path(self):
|
|
||||||
#case 1 : not directory path defined.
|
|
||||||
station = baker.make(Station, path=None, slug="a-slug-with-dash")
|
station = baker.make(Station, path=None, slug="a-slug-with-dash")
|
||||||
station.save()
|
station.save()
|
||||||
assert station.path == settings.CONTROLLERS_WORKING_DIR + '\\a_slug_with_dash'
|
|
||||||
|
|
||||||
def test_save_default_station_while_anotherone_is_default(self):
|
assert (
|
||||||
#case 2 : save a new station while another one is already set default.
|
station.path
|
||||||
station1 = baker.make(Station, default=True)
|
== settings.CONTROLLERS_WORKING_DIR + "\\a_slug_with_dash"
|
||||||
station2 = baker.make(Station, default=False)
|
)
|
||||||
|
|
||||||
# need to switch station2 default value to true before to try save method.
|
def test_save_default_station_while_anotherone_is_default(
|
||||||
station2.default = True
|
self, station_default, stations_without_default
|
||||||
station2.save()
|
):
|
||||||
|
old_default_station = station_default
|
||||||
|
new_default_station = stations_without_default[1]
|
||||||
|
new_default_station.default = True
|
||||||
|
new_default_station.save()
|
||||||
|
old_default_station.refresh_from_db()
|
||||||
|
new_default_station.refresh_from_db()
|
||||||
|
|
||||||
# need to refresh fake db from baker in order to make the test.
|
|
||||||
station1.refresh_from_db()
|
|
||||||
station2.refresh_from_db()
|
|
||||||
|
|
||||||
# check if the station we choose to be default is the only one
|
|
||||||
assert Station.objects.filter(default=True).count() == 1
|
assert Station.objects.filter(default=True).count() == 1
|
||||||
assert station1.default is False
|
assert old_default_station.default is False
|
||||||
assert station2.default is True
|
assert new_default_station.default is True
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.django_db
|
@pytest.mark.django_db
|
||||||
class TestPortQuerySet:
|
class TestPortQuerySet:
|
||||||
# test active methode : retrieve active ports.
|
# issues with the preset incompatibility between port and direction.
|
||||||
# issues with the preset incompatibility between port and direction. I need to control which type and direction for each fake model instance in order to run pytest without failing randomly.
|
# I need to control which type and direction for each fake model
|
||||||
def test_active(self):
|
# Otherwhise pytest fail randomly.
|
||||||
port1 = baker.make(Port, active=True, type=Port.TYPE_ICECAST, direction=Port.DIRECTION_OUTPUT)
|
def test_active_value_true(self):
|
||||||
port2 = baker.make(Port, active=False, type=Port.TYPE_ICECAST, direction=Port.DIRECTION_OUTPUT)
|
port1 = baker.make(
|
||||||
|
Port,
|
||||||
|
active=True,
|
||||||
|
type=Port.TYPE_ICECAST,
|
||||||
|
direction=Port.DIRECTION_OUTPUT,
|
||||||
|
)
|
||||||
|
port2 = baker.make(
|
||||||
|
Port,
|
||||||
|
active=False,
|
||||||
|
type=Port.TYPE_ICECAST,
|
||||||
|
direction=Port.DIRECTION_OUTPUT,
|
||||||
|
)
|
||||||
active_ports = Port.objects.active()
|
active_ports = Port.objects.active()
|
||||||
|
|
||||||
assert port1 in active_ports
|
assert port1 in active_ports
|
||||||
assert port2 not in active_ports
|
assert port2 not in active_ports
|
||||||
|
|
||||||
|
def test_active_value_false(self):
|
||||||
|
port1 = baker.make(
|
||||||
|
Port,
|
||||||
|
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)
|
inactive_ports = Port.objects.active(value=False)
|
||||||
assert port1 not in inactive_ports
|
assert port1 not in inactive_ports
|
||||||
assert port2 in inactive_ports
|
assert port2 in inactive_ports
|
||||||
|
|
||||||
# test output method : retrieve output ports.
|
|
||||||
def test_output(self):
|
def test_output(self):
|
||||||
port1 = baker.make(Port, type=Port.TYPE_ICECAST, direction=Port.DIRECTION_OUTPUT)
|
port1 = baker.make(
|
||||||
port2 = baker.make(Port, type=Port.TYPE_HTTP, direction=Port.DIRECTION_INPUT)
|
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()
|
output_ports = Port.objects.output()
|
||||||
assert port1 in output_ports
|
assert port1 in output_ports
|
||||||
assert port2 not in output_ports
|
assert port2 not in output_ports
|
||||||
|
|
||||||
# test input method : retrieve input ports.
|
|
||||||
def test_input(self):
|
def test_input(self):
|
||||||
port1 = baker.make(Port, type=Port.TYPE_ICECAST, direction=Port.DIRECTION_OUTPUT)
|
port1 = baker.make(
|
||||||
port2 = baker.make(Port, type=Port.TYPE_HTTP, direction=Port.DIRECTION_INPUT)
|
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()
|
input_ports = Port.objects.input()
|
||||||
assert port1 not in input_ports
|
assert port1 not in input_ports
|
||||||
assert port2 in input_ports
|
assert port2 in input_ports
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.django_db
|
@pytest.mark.django_db
|
||||||
class TestPort:
|
class TestPort:
|
||||||
# test __str__ method = return the "type", "direction" and "port id" data of a given "Port" model in one sentence.
|
|
||||||
def test__str__(self):
|
def test__str__(self):
|
||||||
port = baker.make(Port, type=Port.TYPE_ICECAST, direction=Port.DIRECTION_OUTPUT)
|
port = baker.make(
|
||||||
assert port.__str__() == 'output: icecast #1'
|
Port, type=Port.TYPE_ICECAST, direction=Port.DIRECTION_OUTPUT
|
||||||
|
)
|
||||||
|
assert port.__str__() == "output: icecast #1"
|
||||||
|
|
||||||
# test is_valid_type method = return a Boolean value according to the compatibility criteria between the type and the port of a given port's instance. Compatibilyt criteria are defined in the method.
|
|
||||||
def test_is_valid_type_and_type_is_input(self):
|
def test_is_valid_type_and_type_is_input(self):
|
||||||
# case 1 : input direction. return 'true' if the type is compatible
|
porthttp = baker.make(
|
||||||
porthttp = baker.make(Port, type=Port.TYPE_HTTP, direction=Port.DIRECTION_INPUT)
|
Port, type=Port.TYPE_HTTP, direction=Port.DIRECTION_INPUT
|
||||||
porthttps = baker.make(Port, type=Port.TYPE_HTTPS, direction=Port.DIRECTION_INPUT)
|
)
|
||||||
|
porthttps = baker.make(
|
||||||
|
Port, type=Port.TYPE_HTTPS, direction=Port.DIRECTION_INPUT
|
||||||
|
)
|
||||||
|
|
||||||
assert porthttp.is_valid_type() == True
|
assert porthttp.is_valid_type() == True
|
||||||
assert porthttps.is_valid_type() == True
|
assert porthttps.is_valid_type() == True
|
||||||
|
|
||||||
|
|
||||||
def test_is_valid_type_and_type_is_not_input(self):
|
def test_is_valid_type_and_type_is_not_input(self):
|
||||||
# case 2 : is not input direction return 'true' if the type is compatible
|
porticecast = baker.make(
|
||||||
porticecast = baker.make(Port, type=Port.TYPE_ICECAST, direction=Port.DIRECTION_OUTPUT)
|
Port, type=Port.TYPE_ICECAST, direction=Port.DIRECTION_OUTPUT
|
||||||
portfile = baker.make(Port, type=Port.TYPE_FILE, direction=Port.DIRECTION_OUTPUT)
|
)
|
||||||
|
portfile = baker.make(
|
||||||
|
Port, type=Port.TYPE_FILE, direction=Port.DIRECTION_OUTPUT
|
||||||
|
)
|
||||||
|
|
||||||
assert porticecast.is_valid_type() == True
|
assert porticecast.is_valid_type() == True
|
||||||
assert portfile.is_valid_type() == True
|
assert portfile.is_valid_type() == True
|
||||||
|
|
||||||
|
def test_save_with_valid_type_icecast_output(self):
|
||||||
|
port = baker.make(
|
||||||
|
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_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):
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
baker.make(
|
||||||
|
Port, type=Port.TYPE_HTTP, direction=Port.DIRECTION_OUTPUT
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user