Skip to content

Commit

Permalink
Better test coverage for the observation import
Browse files Browse the repository at this point in the history
  • Loading branch information
niconoe committed Nov 21, 2024
1 parent 1a34972 commit a8c6af4
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
7 changes: 1 addition & 6 deletions dashboard/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -690,12 +690,8 @@ class ObservationUnseen(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)

def relevant_for_user(self, date_new_observation) -> bool:
"""Return True if the observation is still relevant for the user"""
"""Return True if this "unseen object" is still relevant for the user"""
# TODO: test this
# return self.observation.considered_seen_delay_by(
# self.user
# ) or not self.user.obs_match_alerts(self.observation)

if self.observation.date_older_than_user_delay(
self.user, the_date=date_new_observation
):
Expand Down Expand Up @@ -826,7 +822,6 @@ def as_dict(self) -> dict[str, Any]:
def observations(self) -> QuerySet[Observation]:
"""Return all observations matching this alert"""
# TODO: test this
# TODO: check if used
return Observation.objects.filtered_from_my_params(
species_ids=[s.pk for s in self.species.all()],
datasets_ids=[d.pk for d in self.datasets.all()],
Expand Down
52 changes: 50 additions & 2 deletions dashboard/tests/commands/test_import_observations.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import datetime
from pathlib import Path
from unittest import mock
from zoneinfo import ZoneInfo

import requests_mock
from django.contrib.gis.geos import Point
Expand Down Expand Up @@ -64,6 +65,7 @@ def setUp(self) -> None:
first_name="John",
last_name="Frusciante",
email="[email protected]",
notification_delay_days=365,
)

Species.objects.all().delete() # There are initially a few species in the database (loaded in data migration)
Expand Down Expand Up @@ -549,12 +551,58 @@ def test_seen_status_new_to_seen_because_no_alert(self) -> None:
def test_seen_status_new_to_seen_because_old(self) -> None:
"""New observation in the system. It's older than the user delay, so it's
marked as seen (even if it's part of an alert)"""
# TODO: implement

# We create an alert that matches the observation (all lixus bardanae observations)
alert = Alert.objects.create(
user=self.user, email_notifications_frequency=Alert.DAILY_EMAILS
)
alert.species.add(self.lixus)

with open(SAMPLE_DATA_PATH / "gbif_download.zip", "rb") as gbif_download_file:
call_command("import_observations", source_dwca=gbif_download_file)

observations_after = Observation.objects.all()
last_di = DataImport.objects.latest("id")

# Those are the observations that are totally new in the system
fresh_observations = observations_after.filter(initial_data_import=last_di)

old_lixus_bardanae = fresh_observations.get(occurrence_id="Ugent:UGMD:16879")

# It is considered seen because it's older than the user delay
with self.assertRaises(ObservationUnseen.DoesNotExist):
ObservationUnseen.objects.get(
observation=old_lixus_bardanae, user=self.user
)

def test_seen_status_new_to_unseen(self) -> None:
"""New observation in the system. It's more recent than the user delay, and is
part of an alert, so it's marked as unseen"""
# TODO: implement

# Test code: identical to test_seen_status_new_to_seen_because_old() but we
# pretend we're running the import in 1950
alert = Alert.objects.create(
user=self.user, email_notifications_frequency=Alert.DAILY_EMAILS
)
alert.species.add(self.lixus)

mocked = datetime.datetime(1950, 7, 1, tzinfo=ZoneInfo("UTC"))
with mock.patch("django.utils.timezone.now", mock.Mock(return_value=mocked)):
with open(
SAMPLE_DATA_PATH / "gbif_download.zip", "rb"
) as gbif_download_file:
call_command("import_observations", source_dwca=gbif_download_file)

observations_after = Observation.objects.all()
last_di = DataImport.objects.latest("id")

# Those are the observations that are totally new in the system
fresh_observations = observations_after.filter(initial_data_import=last_di)

old_lixus_bardanae = fresh_observations.get(occurrence_id="Ugent:UGMD:16879")

# It is considered not seen because it's only a few days old
ObservationUnseen.objects.get(observation=old_lixus_bardanae, user=self.user)

@override_settings(
GBIF_ALERT={
Expand Down

0 comments on commit a8c6af4

Please sign in to comment.