420 lines
16 KiB
Python
420 lines
16 KiB
Python
import pytest
|
|
from model_bakery import baker
|
|
|
|
import random
|
|
import datetime
|
|
from django.utils import timezone as tz
|
|
from datetime import timedelta
|
|
from django.utils.timezone import make_aware
|
|
import pytz
|
|
|
|
from aircox.models import Diffusion
|
|
|
|
|
|
class TestDiffusionQuerySet:
|
|
@pytest.mark.django_db
|
|
def test_episode_by_obj(self, diff_episode_var):
|
|
key_episode = diff_episode_var[0]
|
|
diff_with_key_episode = diff_episode_var[2]
|
|
diff_without_key_episode = diff_episode_var[3]
|
|
result = Diffusion.objects.episode(episode=key_episode)
|
|
|
|
assert all(diff in result for diff in diff_with_key_episode)
|
|
assert all(diff not in result for diff in diff_without_key_episode)
|
|
|
|
@pytest.mark.django_db
|
|
def test_episode_by_id(self, diff_episode_var):
|
|
key_episode = diff_episode_var[0]
|
|
diff_with_key_episode = diff_episode_var[2]
|
|
diff_without_key_episode = diff_episode_var[3]
|
|
result = Diffusion.objects.episode(id=key_episode.id)
|
|
|
|
assert all(diff in result for diff in diff_with_key_episode)
|
|
assert all(diff not in result for diff in diff_without_key_episode)
|
|
|
|
@pytest.mark.django_db
|
|
def test_on_air(self, episodes):
|
|
for episode in episodes:
|
|
baker.make(
|
|
Diffusion,
|
|
episode=episode,
|
|
type=random.choice(
|
|
[Diffusion.TYPE_ON_AIR, Diffusion.TYPE_UNCONFIRMED]
|
|
),
|
|
)
|
|
assert all(
|
|
diff.type == Diffusion.TYPE_ON_AIR
|
|
for diff in Diffusion.objects.on_air()
|
|
)
|
|
|
|
# TODO test si il y a doublon qu'un seul objet soit récupéré.
|
|
@pytest.mark.django_db
|
|
def test_now_test(self, diff_time_var):
|
|
now_diff, result = diff_time_var[4], Diffusion.objects.now(
|
|
now=tz.now(), order=True
|
|
)
|
|
assert (
|
|
len(result) == 1
|
|
and result[0] == now_diff
|
|
and now_diff.start <= tz.now() <= now_diff.end
|
|
)
|
|
|
|
@pytest.mark.django_db
|
|
def test_date_without_date_arg(self, diff_time_var):
|
|
before_diff, now_diff, after_diff = diff_time_var[3:6]
|
|
result = Diffusion.objects.date()
|
|
assert len(result) == 3
|
|
assert set([before_diff, now_diff, after_diff]).issubset(
|
|
set(result)
|
|
)
|
|
|
|
@pytest.mark.django_db
|
|
def test_date_with_date_arg(self, diff_time_var):
|
|
tomorrow_date = datetime.date.today() + timedelta(days=1)
|
|
tomorrow_diff = diff_time_var[6]
|
|
assert Diffusion.objects.date(date=tomorrow_date).count() == 1
|
|
assert Diffusion.objects.date(date=tomorrow_date)[0] == tomorrow_diff
|
|
|
|
@pytest.mark.django_db
|
|
def test_at_datetimeobj(self, diff_time_var):
|
|
now_datetime, now_diff = diff_time_var[1], diff_time_var[4]
|
|
assert Diffusion.objects.at(date=now_datetime).count() == 1
|
|
assert Diffusion.objects.at(date=now_datetime)[0] == now_diff
|
|
|
|
@pytest.mark.django_db
|
|
def test_at_dateobj(self, diff_time_var):
|
|
now_date = diff_time_var[0]
|
|
before_diff = diff_time_var[3]
|
|
now_diff = diff_time_var[4]
|
|
after_diff = diff_time_var[5]
|
|
result = Diffusion.objects.at(date=now_date)
|
|
assert len(result) == 3
|
|
assert set([before_diff, now_diff, after_diff]).issubset(
|
|
set(result)
|
|
)
|
|
|
|
@pytest.mark.django_db
|
|
def test_after_without_arg(self, diff_time_var):
|
|
yesterday_diff = diff_time_var[2]
|
|
before_diff = diff_time_var[3]
|
|
now_diff = diff_time_var[4]
|
|
after_diff = diff_time_var[5]
|
|
tomorrow_diff = diff_time_var[6]
|
|
from_now_diff = Diffusion.objects.after()
|
|
assert set([now_diff, after_diff, tomorrow_diff]).issubset(
|
|
set(from_now_diff)
|
|
)
|
|
assert (
|
|
before_diff not in from_now_diff
|
|
and yesterday_diff not in from_now_diff
|
|
)
|
|
|
|
@pytest.mark.django_db
|
|
def test_after_with_datetime_arg(self, diff_time_var):
|
|
now_datetime = diff_time_var[1]
|
|
yesterday_diff = diff_time_var[2]
|
|
before_diff = diff_time_var[3]
|
|
now_diff = diff_time_var[4]
|
|
after_diff = diff_time_var[5]
|
|
tomorrow_diff = diff_time_var[6]
|
|
from_now_diff = Diffusion.objects.after(date=now_datetime)
|
|
assert set([now_diff, after_diff, tomorrow_diff]).issubset(
|
|
set(from_now_diff)
|
|
)
|
|
assert (
|
|
before_diff not in from_now_diff
|
|
and yesterday_diff not in from_now_diff
|
|
)
|
|
|
|
@pytest.mark.django_db
|
|
def test_after_with_date_arg(self, diff_time_var):
|
|
now_date = diff_time_var[0]
|
|
yesterday_diff = diff_time_var[2]
|
|
before_diff = diff_time_var[3]
|
|
now_diff = diff_time_var[4]
|
|
after_diff = diff_time_var[5]
|
|
tomorrow_diff = diff_time_var[6]
|
|
from_today_diff = Diffusion.objects.after(date=now_date)
|
|
assert set(
|
|
[tomorrow_diff, after_diff, now_diff, before_diff]
|
|
).issubset(set(from_today_diff))
|
|
assert yesterday_diff not in from_today_diff
|
|
|
|
@pytest.mark.django_db
|
|
def test_before_without_arg(self, diff_time_var):
|
|
yesterday_diff = diff_time_var[2]
|
|
before_diff = diff_time_var[3]
|
|
now_diff = diff_time_var[4]
|
|
after_diff = diff_time_var[5]
|
|
tomorrow_diff = diff_time_var[6]
|
|
before_now_diff = Diffusion.objects.before()
|
|
assert set([now_diff, before_diff, yesterday_diff]).issubset(
|
|
set(before_now_diff)
|
|
)
|
|
assert (
|
|
tomorrow_diff not in before_now_diff
|
|
and after_diff not in before_now_diff
|
|
)
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_before_with_datetime_arg(self, diff_time_var):
|
|
now_datetime = diff_time_var[1]
|
|
yesterday_diff = diff_time_var[2]
|
|
before_diff = diff_time_var[3]
|
|
now_diff = diff_time_var[4]
|
|
after_diff = diff_time_var[5]
|
|
tomorrow_diff = diff_time_var[6]
|
|
before_now_diff = Diffusion.objects.before(date=now_datetime)
|
|
assert (
|
|
tomorrow_diff not in before_now_diff
|
|
and after_diff not in before_now_diff
|
|
)
|
|
assert set([now_diff, before_diff, yesterday_diff]).issubset(
|
|
set(before_now_diff)
|
|
)
|
|
|
|
@pytest.mark.django_db
|
|
def test_before_with_date_arg(self, diff_time_var):
|
|
now_date = diff_time_var[0]
|
|
yesterday_diff = diff_time_var[2]
|
|
now_diff = diff_time_var[4]
|
|
after_diff = diff_time_var[5]
|
|
tomorrow_diff = diff_time_var[6]
|
|
before_today_diff = Diffusion.objects.before(date=now_date)
|
|
assert not set([tomorrow_diff, after_diff, now_diff]).issubset(
|
|
set(before_today_diff)
|
|
)
|
|
assert yesterday_diff in before_today_diff
|
|
|
|
@pytest.mark.django_db
|
|
def test_range(self, episodes, diff_time_var):
|
|
(
|
|
yesterday_diff,
|
|
before_diff,
|
|
now_diff,
|
|
after_diff,
|
|
tomorrow_diff
|
|
) = diff_time_var[2:9]
|
|
start, end = before_diff.start, after_diff.end
|
|
overlaping_start_diff = baker.make(
|
|
Diffusion,
|
|
episode=episodes[0],
|
|
start=start - timedelta(minutes=30),
|
|
end=end,
|
|
)
|
|
overlaping_end_diff = baker.make(
|
|
Diffusion,
|
|
episode=episodes[0],
|
|
start=start,
|
|
end=end + timedelta(minutes=30),
|
|
)
|
|
range_diff = Diffusion.objects.range(start=start, end=end)
|
|
assert set([before_diff, after_diff, now_diff]).issubset(range_diff)
|
|
assert not set(
|
|
[
|
|
overlaping_start_diff,
|
|
overlaping_end_diff,
|
|
tomorrow_diff,
|
|
yesterday_diff,
|
|
]
|
|
).intersection(range_diff)
|
|
|
|
|
|
class TestDiffusion:
|
|
@pytest.mark.django_db
|
|
def test__str__initial_diff(self, init_diff):
|
|
init_diff.episode.title = "Episode title"
|
|
init_diff.start = datetime.datetime(
|
|
2023, 5, 12, 13, 0, tzinfo=pytz.UTC
|
|
)
|
|
assert (
|
|
init_diff.__str__() == "Episode title - 2023/05/12 13:00+0000"
|
|
)
|
|
|
|
@pytest.mark.django_db
|
|
def test__str__rerun_diff(self, diff_with_rerun):
|
|
initial_diff, rerun_diff = diff_with_rerun[0], diff_with_rerun[1]
|
|
initial_diff.episode.title = "Episode title"
|
|
rerun_diff.start = datetime.datetime(
|
|
2023, 5, 12, 13, 0, tzinfo=pytz.UTC
|
|
)
|
|
assert (
|
|
rerun_diff.__str__()
|
|
== "Episode title - 2023/05/12 13:00+0000 (rerun)"
|
|
)
|
|
|
|
""" je ne comprends pas dans la méthode save() le rerun_set.update, que la méthode l'update ou pas, on accède quand même à l'épisode mis à jour sur la diff initiale depuis la diff rerun"""
|
|
@pytest.mark.django_db
|
|
def test_save(self, episodes, diff_with_rerun):
|
|
initial_diff, rerun_diff = diff_with_rerun[0], diff_with_rerun[1]
|
|
initial_diff.episode = episodes[1]
|
|
initial_diff.program = episodes[1].program
|
|
initial_diff.save()
|
|
assert rerun_diff.initial.episode == episodes[1]
|
|
assert rerun_diff.initial.program == episodes[1].program
|
|
|
|
@pytest.mark.django_db
|
|
def test_save_rerun(self, diff_with_rerun):
|
|
initial_diff, rerun_diff = diff_with_rerun[0], diff_with_rerun[1]
|
|
rerun_diff.save_rerun()
|
|
assert rerun_diff.episode == initial_diff.episode
|
|
|
|
@pytest.mark.django_db
|
|
def test_save_initial(self, init_diff):
|
|
init_diff.save_initial()
|
|
assert init_diff.program == init_diff.episode.program
|
|
|
|
@pytest.mark.django_db
|
|
def test_duration(self, init_diff):
|
|
assert init_diff.duration == datetime.timedelta(seconds=1800)
|
|
|
|
@pytest.mark.django_db
|
|
def test_date(self, init_diff):
|
|
assert init_diff.date == datetime.date(2023, 5, 12)
|
|
|
|
@pytest.mark.django_db
|
|
def test_local_start(self, init_diff):
|
|
assert init_diff.local_start == datetime.datetime(
|
|
2023, 5, 12, 13, 00, tzinfo=pytz.UTC
|
|
)
|
|
|
|
@pytest.mark.django_db
|
|
def test_local_end(self, init_diff):
|
|
assert init_diff.local_end == datetime.datetime(
|
|
2023, 5, 12, 13, 30, tzinfo=pytz.UTC
|
|
)
|
|
|
|
@pytest.mark.django_db
|
|
def test_is_now(self, init_diff):
|
|
init_diff.type = Diffusion.TYPE_ON_AIR
|
|
init_diff.start = tz.now()
|
|
init_diff.end = tz.now() + timedelta(minutes=30)
|
|
assert init_diff.is_now
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_is_live(self, init_diff):
|
|
init_diff.type = Diffusion.TYPE_ON_AIR
|
|
init_diff.episode.sound_set == None
|
|
assert init_diff.is_live
|
|
|
|
''' pas possible de tester playlist car impossible de créer des sons archives. Ils sont toujours dans la catégorie "removed" quoi que je fasse..'''
|
|
#@pytest.mark.django_db
|
|
#def test_get_playlist(self, init_diff):
|
|
|
|
|
|
'''diffusion don't have sound_set but episode and program within the diff do have. Only Program and Sound are ForeignKey in "Sound"'s model.'''
|
|
'''need to updtate the method get_sound with episode.sound_set or program.sound_set to get it working.'''
|
|
'''issues with orderby path. Error: Cannot resolve keyword 'path' into field. Join on 'file' not permitted'''
|
|
'''by analysing, path is self.program.path + subdir + filename, so i by ordonning file, we order by filename as expected. right ?'''
|
|
''' BUT file are register only if they are Archive or Excerpt. I don't understand where is register if the sound is removed or other.'''
|
|
'''use fixtures podcasts as test sound file'''
|
|
'''something do the sound get erased immediately so i cannot test with other type than removed'''
|
|
@pytest.mark.django_db
|
|
def test_get_sound(self, init_diff, podcasts):
|
|
for podcast in podcasts:
|
|
podcast.save()
|
|
init_diff.episode.sound_set.add(podcast)
|
|
init_diff.save()
|
|
|
|
assert init_diff.get_sounds(removed=True).count() == 8
|
|
assert podcasts[0] in init_diff.get_sounds(removed=True)
|
|
|
|
@pytest.mark.django_db
|
|
def test_is_date_in_range_with_arg(self, init_diff):
|
|
duration = (init_diff.end - init_diff.start).total_seconds()
|
|
random_time_in_range = init_diff.start + timedelta(seconds=int(random.uniform(0, duration)))
|
|
assert init_diff.is_date_in_range(date=random_time_in_range)
|
|
|
|
@pytest.mark.django_db
|
|
def test_is_date_in_range_without_arg(self, init_diff):
|
|
init_diff.start = make_aware(datetime.datetime.now() + datetime.timedelta(hours=1))
|
|
init_diff.end = make_aware(datetime.datetime.now() + datetime.timedelta(hours=2))
|
|
assert not init_diff.is_date_in_range()
|
|
|
|
@pytest.mark.django_db
|
|
def test_get_conflicts_overlapping_diffusions(self, diff_time_var, episodes):
|
|
new_diff = baker.make(
|
|
Diffusion,
|
|
start=diff_time_var[4].end - timedelta(minutes=10),
|
|
end=diff_time_var[4].end + timedelta(hours=1),
|
|
episode=episodes[0]
|
|
)
|
|
conflicts = new_diff.get_conflicts()
|
|
assert conflicts.count() == 2
|
|
assert all(x in conflicts for x in [diff_time_var[4], diff_time_var[5]])
|
|
|
|
@pytest.mark.django_db
|
|
def test_get_conflicts_no_conflict(self, diff_time_var):
|
|
assert diff_time_var[4].get_conflicts().count() == 0
|
|
|
|
'''AttributeError: 'Diffusion' object has no attribute 'conflicts'. Did you mean: 'get_conflicts'?'''
|
|
'''j'ai changé la méthode pour qu'elle ajoute un attribut 'conflicts' temporaire et dynamique à l'instance seulement si il y a des conflits.'''
|
|
@pytest.mark.django_db
|
|
def test_check_conflicts_new_conflicts(self, diff_time_var, episodes):
|
|
new_diff = baker.make(
|
|
Diffusion,
|
|
start=diff_time_var[4].end - timedelta(minutes=10),
|
|
end=diff_time_var[4].end + timedelta(hours=1),
|
|
episode=episodes[0]
|
|
)
|
|
new_diff.check_conflicts()
|
|
assert new_diff.conflicts.count() == 2
|
|
assert all(x in new_diff.conflicts for x in [diff_time_var[4], diff_time_var[5]])
|
|
|
|
@pytest.mark.django_db
|
|
def test_check_conflicts_additional_conflicts(self, diff_time_var, episodes):
|
|
new_diff = baker.make(
|
|
Diffusion,
|
|
start=diff_time_var[4].end - timedelta(minutes=10),
|
|
end=diff_time_var[4].end + timedelta(hours=1),
|
|
episode=episodes[0]
|
|
)
|
|
new_diff.check_conflicts()
|
|
new_diff.start = diff_time_var[3].end - timedelta(minutes=10)
|
|
new_diff.end = diff_time_var[3].end + timedelta(hours=1)
|
|
new_diff.save()
|
|
new_diff.check_conflicts()
|
|
assert new_diff.conflicts.count() == 2
|
|
assert all(x in new_diff.conflicts for x in [diff_time_var[3], diff_time_var[4]])
|
|
|
|
@pytest.mark.django_db
|
|
def test_check_conflicts_remove_conflicts(self, diff_time_var, episodes):
|
|
new_diff = baker.make(
|
|
Diffusion,
|
|
start=diff_time_var[4].end - timedelta(minutes=10),
|
|
end=diff_time_var[4].end + timedelta(hours=1),
|
|
episode=episodes[0]
|
|
)
|
|
new_diff.check_conflicts()
|
|
new_diff.start = diff_time_var[5].end
|
|
new_diff.end = diff_time_var[5].end + timedelta(minutes=30)
|
|
new_diff.save()
|
|
new_diff.check_conflicts()
|
|
assert getattr(new_diff, 'conflicts', None) is None
|
|
|
|
@pytest.mark.django_db
|
|
def test_check_conflicts_without_conflicts(self, diff_time_var):
|
|
diff_time_var[4].check_conflicts()
|
|
assert getattr(diff_time_var[4], 'conflicts', None) is None
|
|
|
|
@pytest.mark.django_db
|
|
def test__init__with_default_values(self, init_diff):
|
|
assert init_diff._initial == {
|
|
"start": init_diff.start,
|
|
"end": init_diff.end,
|
|
"episode": init_diff.episode
|
|
}
|
|
|
|
@pytest.mark.django_db
|
|
def test__init__(self, init_diff):
|
|
assert init_diff._initial == {
|
|
"start": init_diff.start,
|
|
"end": init_diff.end,
|
|
"episode": init_diff.episode
|
|
}
|
|
|
|
# souci avec save
|
|
# un autre souci ailleurs : est overlaping devrait être testé partout aussi ?
|