Skip to content

Commit

Permalink
Use now fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
juuso-j committed Sep 3, 2024
1 parent 2979eb8 commit ab76d40
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
13 changes: 6 additions & 7 deletions exceptional_situations/tests/test_api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from datetime import datetime, timedelta

import pytest
from django.utils import timezone
from rest_framework.reverse import reverse

SITUATION_LIST_URL = reverse("exceptional_situations:situation-list")
Expand Down Expand Up @@ -70,8 +69,8 @@ def test_situation_retrieve(api_client, situations):


@pytest.mark.django_db
def test_situation_filter_by_start_time(api_client, situations):
start_time = timezone.now() - timedelta(hours=6)
def test_situation_filter_by_start_time(api_client, situations, now):
start_time = now - timedelta(hours=6)
response = api_client.get(
SITUATION_LIST_URL
+ f"?start_time__gt={datetime.strftime(start_time, DATETIME_FORMAT)}"
Expand All @@ -84,7 +83,7 @@ def test_situation_filter_by_start_time(api_client, situations):
)
assert response.json()["count"] == 1

start_time = timezone.now() - timedelta(days=2)
start_time = now - timedelta(days=2)
response = api_client.get(
SITUATION_LIST_URL
+ f"?start_time__gt={datetime.strftime(start_time, DATETIME_FORMAT)}"
Expand All @@ -98,8 +97,8 @@ def test_situation_filter_by_start_time(api_client, situations):


@pytest.mark.django_db
def test_situation_filter_by_end_time(api_client, situations):
end_time = timezone.now()
def test_situation_filter_by_end_time(api_client, situations, now):
end_time = now
response = api_client.get(
SITUATION_LIST_URL
+ f"?end_time__gt={datetime.strftime(end_time, DATETIME_FORMAT)}"
Expand All @@ -111,7 +110,7 @@ def test_situation_filter_by_end_time(api_client, situations):
)
assert response.json()["count"] == 0

end_time = timezone.now() - timedelta(days=2)
end_time = now - timedelta(days=2)
response = api_client.get(
SITUATION_LIST_URL
+ f"?end_time__gt={datetime.strftime(end_time, DATETIME_FORMAT)}"
Expand Down
11 changes: 4 additions & 7 deletions exceptional_situations/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
from datetime import timedelta

import pytest
from django.utils import timezone

from exceptional_situations.models import Situation, SituationAnnouncement


@pytest.mark.django_db
def test_situation_is_active(situation_types):
now = timezone.now()
def test_situation_is_active(situation_types, now):

announcement_1 = SituationAnnouncement.objects.create(start_time=now, title="test1")
announcement_2 = SituationAnnouncement.objects.create(start_time=now, title="test2")
situation = Situation.objects.create(
release_time=now, situation_type=situation_types.first(), situation_id="TestID"
)
# No announcements, returns False
assert situation.is_active is False

situation.announcements.add(announcement_1)
Expand Down Expand Up @@ -44,8 +43,7 @@ def test_situation_is_active(situation_types):


@pytest.mark.django_db
def test_situation_start_time(situation_types):
now = timezone.now()
def test_situation_start_time(situation_types, now):
announcement_1 = SituationAnnouncement.objects.create(
start_time=now, title="starts now"
)
Expand All @@ -61,8 +59,7 @@ def test_situation_start_time(situation_types):


@pytest.mark.django_db
def test_situation_end_time(situation_types):
now = timezone.now()
def test_situation_end_time(situation_types, now):
announcement_1 = SituationAnnouncement.objects.create(
start_time=now - timedelta(hours=1),
end_time=now + timedelta(hours=2),
Expand Down

0 comments on commit ab76d40

Please sign in to comment.