Skip to content

Commit

Permalink
test: fix tests
Browse files Browse the repository at this point in the history
Running `pytest` would work, but not `pytest -k contractor`.
This commit makes various fixes to fix the flakiness of the
tests.

Refs: PS-160, PS-159
  • Loading branch information
danipran committed Dec 3, 2024
1 parent 00f325a commit 0552e29
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 110 deletions.
3 changes: 2 additions & 1 deletion areas/factories.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import factory
import factory.fuzzy
from django.contrib.gis.geos import MultiPolygon, Point, Polygon
from factory.random import randgen
Expand All @@ -9,6 +8,8 @@

class ContractZoneFactory(factory.django.DjangoModelFactory):
name = factory.Faker("bs")
email = factory.Faker("email")
secondary_email = factory.Faker("email")
boundary = factory.Sequence(
lambda n: MultiPolygon(
Polygon(
Expand Down
45 changes: 17 additions & 28 deletions areas/tests/test_contract_zone_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import pytest
from datetime import datetime
from django.contrib.gis.geos import MultiPolygon, Polygon
from django.utils.timezone import make_aware
from rest_framework.reverse import reverse

Expand All @@ -15,24 +14,7 @@

@pytest.fixture
def contract_zone():
return ContractZoneFactory(
boundary=MultiPolygon(
Polygon(((24, 60), (25, 60), (25, 61), (24, 61), (24, 60)))
),
contact_person="John Doe",
email="[email protected]",
phone="555-1234567",
)


@pytest.fixture
def two_events_2018():
return EventFactory.create_batch(2)


@pytest.fixture
def two_events_2019():
return EventFactory.create_batch(2, start_time=make_aware(datetime(2019, 3, 3)))
return ContractZoneFactory()


def get_expected_base_data(contract_zone):
Expand All @@ -52,36 +34,43 @@ def get_expected_base_data_with_contact_data(contract_zone):
)


def test_get_list_non_official_no_stats_no_contact_info(
contract_zone, two_events_2018, user_api_client
):
def test_get_list_non_official_no_stats_no_contact_info(contract_zone, user_api_client):
EventFactory.create_batch(
2, contract_zone=contract_zone, start_time=make_aware(datetime(2018, 3, 3))
)
response_data = get(user_api_client, LIST_URL + "?stats_year=2018")

assert response_data["results"] == [get_expected_base_data(contract_zone)]


def test_get_list_official_no_filter_no_stats_check_contact_data(
contract_zone, two_events_2018, official_api_client
contract_zone, official_api_client
):
EventFactory.create_batch(2, contract_zone=contract_zone)
response_data = get(official_api_client, LIST_URL)

assert response_data["results"] == [
get_expected_base_data_with_contact_data(contract_zone)
]


def test_get_list_official_check_stats(
contract_zone, two_events_2018, two_events_2019, official_api_client
):
def test_get_list_official_check_stats(contract_zone, official_api_client):
event_1, event_2 = EventFactory.create_batch(
2, contract_zone=contract_zone, start_time=make_aware(datetime(2018, 3, 3))
)
EventFactory.create_batch(
2, contract_zone=contract_zone, start_time=make_aware(datetime(2019, 3, 3))
)
non_approved_event_that_should_be_ignored = EventFactory(
estimated_attendee_count=100000, state=Event.WAITING_FOR_APPROVAL
estimated_attendee_count=100000,
state=Event.WAITING_FOR_APPROVAL,
contract_zone=contract_zone,
)
assert non_approved_event_that_should_be_ignored.contract_zone == contract_zone
assert non_approved_event_that_should_be_ignored.start_time.date().year == 2018

response_data = get(official_api_client, LIST_URL + "?stats_year=2018")

event_1, event_2 = two_events_2018
expected_data = dict(
get_expected_base_data_with_contact_data(contract_zone),
event_count=2,
Expand Down
4 changes: 3 additions & 1 deletion areas/tests/test_geo_query_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@ def test_unavailable_dates(
"""
for event_day in event_days:
d = make_aware(datetime(2018, 12, event_day, 11))
EventFactory(start_time=d, end_time=d + timedelta(hours=1))
EventFactory(
start_time=d, end_time=d + timedelta(hours=1), contract_zone=contract_zone
)

response_data = get(api_client, get_url(60, 24))
dates = response_data["contract_zone"]["unavailable_dates"][8:]
Expand Down
19 changes: 10 additions & 9 deletions events/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.utils import timezone
from factory.random import randgen

from areas.models import ContractZone
from areas.factories import ContractZoneFactory

from .models import Event

Expand All @@ -19,11 +19,6 @@ class EventFactory(factory.django.DjangoModelFactory):
end_time = factory.LazyAttribute(
lambda e: e.start_time + timedelta(hours=randgen.randint(1, 6))
)
location = factory.LazyFunction(
lambda: Point(
24.915 + randgen.uniform(0, 0.040), 60.154 + randgen.uniform(0, 0.022)
)
)
organizer_first_name = factory.Faker("first_name")
organizer_last_name = factory.Faker("last_name")
organizer_email = factory.Faker("email")
Expand All @@ -36,9 +31,15 @@ class EventFactory(factory.django.DjangoModelFactory):
large_trash_bag_count = factory.fuzzy.FuzzyInteger(1, 500)
trash_picker_count = factory.fuzzy.FuzzyInteger(1, 50)
equipment_information = factory.Faker("text")
contract_zone = factory.LazyAttribute(
lambda e: ContractZone.objects.get_active_by_location(e.location)
)
contract_zone = factory.SubFactory(ContractZoneFactory)

class Meta:
model = Event

@factory.lazy_attribute
def location(self):
centroid = self.contract_zone.boundary[0].centroid
return Point(
centroid.x + randgen.uniform(0, 0.040),
centroid.y + randgen.uniform(0, 0.022),
)
15 changes: 3 additions & 12 deletions events/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import pytest
from django.contrib.gis.geos import MultiPolygon, Point, Polygon

from areas.factories import ContractZoneFactory
from common.tests.conftest import * # noqa
Expand All @@ -10,17 +9,9 @@

@pytest.fixture
def contract_zone():
return ContractZoneFactory(
boundary=MultiPolygon(
Polygon(((24, 60), (25, 60), (25, 61), (24, 61), (24, 60)))
),
email="[email protected]",
secondary_email="[email protected]",
)
return ContractZoneFactory()


@pytest.fixture
def event(contract_zone):
event = EventFactory(location=Point(24.5, 60.5))
assert event.contract_zone == contract_zone
return event
def event():
return EventFactory()
Loading

0 comments on commit 0552e29

Please sign in to comment.