From a8c6af4a94604ffd333c9b5c30274f7a3ee3c22c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20No=C3=A9?= Date: Thu, 21 Nov 2024 11:36:28 +0100 Subject: [PATCH] Better test coverage for the observation import --- dashboard/models.py | 7 +-- .../commands/test_import_observations.py | 52 ++++++++++++++++++- 2 files changed, 51 insertions(+), 8 deletions(-) diff --git a/dashboard/models.py b/dashboard/models.py index 965afcc..2ee23db 100644 --- a/dashboard/models.py +++ b/dashboard/models.py @@ -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 ): @@ -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()], diff --git a/dashboard/tests/commands/test_import_observations.py b/dashboard/tests/commands/test_import_observations.py index a037d37..4767078 100644 --- a/dashboard/tests/commands/test_import_observations.py +++ b/dashboard/tests/commands/test_import_observations.py @@ -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 @@ -64,6 +65,7 @@ def setUp(self) -> None: first_name="John", last_name="Frusciante", email="frusciante@gmail.com", + notification_delay_days=365, ) Species.objects.all().delete() # There are initially a few species in the database (loaded in data migration) @@ -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={