Skip to content

Commit

Permalink
DBC22-1449: fixed events unit tests, inactive events are now removed
Browse files Browse the repository at this point in the history
  • Loading branch information
ray-oxd authored and fatbird committed Feb 8, 2024
1 parent e57b461 commit 278b702
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
8 changes: 4 additions & 4 deletions src/backend/apps/event/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ def populate_event_from_data(new_event_data):
event_serializer.save()


def populate_all_event_data():
def populate_all_event_data(include_closures=True):
client = FeedClient()
closures = client.get_closures_dict()
closures = client.get_closures_dict() if include_closures else {}
feed_data = client.get_event_list()['events']

active_event_ids = []
Expand All @@ -83,10 +83,10 @@ def populate_all_event_data():
except Exception as e:
logger.warning(e)

# Mark events absent in the feed as inactive
# Purge events absent in the feed
Event.objects.filter(status=EVENT_STATUS.ACTIVE)\
.exclude(id__in=active_event_ids)\
.update(status=EVENT_STATUS.INACTIVE)
.delete()

# Rebuild cache
cache.delete(CacheKey.EVENT_LIST)
13 changes: 6 additions & 7 deletions src/backend/apps/event/tests/test_event_populate.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def test_populate_and_update_event(self, mock_requests_get):
MockResponse(self.mock_updated_event_feed_result, status_code=200),
]

populate_all_event_data()
populate_all_event_data(include_closures=False)
assert Event.objects.count() == 5

event_id_list = sorted(Event.objects.all().order_by("id")
Expand All @@ -118,15 +118,14 @@ def test_populate_and_update_event(self, mock_requests_get):
assert event.direction == EVENT_DIRECTION.NONE

# Second call with one missing event
populate_all_event_data()
populate_all_event_data(include_closures=False)

assert Event.objects.filter(status=EVENT_STATUS.ACTIVE).count() == 4
assert Event.objects.filter(status=EVENT_STATUS.INACTIVE).count() == 1
assert Event.objects.get(id="drivebc.ca/DBC-28386").status \
== EVENT_STATUS.INACTIVE
# Inactive events are deleted
assert Event.objects.filter(id="drivebc.ca/DBC-28386").count() == 0

# Third call with updated data
populate_all_event_data()
populate_all_event_data(include_closures=False)
# Not updated due to same updated datetime
assert Event.objects.get(id="drivebc.ca/DBC-46014").route_from \
!= "Updated Rd"
Expand All @@ -142,7 +141,7 @@ def test_populate_event_with_validation_error(self, mock_requests_get):
]

# Only cams with validation error(DBC-46014) are omitted
populate_all_event_data()
populate_all_event_data(include_closures=False)
assert Event.objects.count() == 4

event_id_list = sorted(Event.objects.all().values_list("id", flat=True))
Expand Down
4 changes: 2 additions & 2 deletions src/backend/apps/event/tests/test_event_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ def setUp(self):
self.serializer_two = EventSerializer(self.event_two)

def test_serializer_data(self):
assert len(self.serializer.data) == 19
assert len(self.serializer.data) == 20
# route_from beings with 'at '
assert self.serializer.data['route_display'] == \
"Test Road to Test Avenue"
assert self.serializer.data['direction_display'] == \
"Northbound"

assert len(self.serializer_two.data) == 19
assert len(self.serializer_two.data) == 20
# route_from doesn't being with 'at '
assert self.serializer_two.data['route_display'] == \
"Test Road Two to Test Avenue Two"
Expand Down

0 comments on commit 278b702

Please sign in to comment.