From b9d6293546ef041af3d81759762a5bd2e279ee53 Mon Sep 17 00:00:00 2001 From: moanos Date: Sun, 3 Nov 2024 20:36:13 +0100 Subject: [PATCH] test:Add tests for deactivation tasks --- src/tests/test_admin_tasks.py | 51 +++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/tests/test_admin_tasks.py diff --git a/src/tests/test_admin_tasks.py b/src/tests/test_admin_tasks.py new file mode 100644 index 0000000..20f5edd --- /dev/null +++ b/src/tests/test_admin_tasks.py @@ -0,0 +1,51 @@ +from datetime import timedelta +from django.utils import timezone + +from fellchensammlung.tools.admin import get_unchecked_adoption_notices, deactivate_unchecked_adoption_notices +from django.test import TestCase + +from model_bakery import baker +from fellchensammlung.models import AdoptionNotice + + +class DeactiviationTest(TestCase): + @classmethod + def setUpTestData(cls): + now = timezone.now() + more_than_three_weeks_ago = now - timedelta(weeks=3, days=2) + less_than_three_weeks_ago = now - timedelta(weeks=1, days=2) + + cls.adoption1 = baker.make(AdoptionNotice, + name="TestAdoption1", + created_at=more_than_three_weeks_ago, + last_checked=more_than_three_weeks_ago) + cls.adoption2 = baker.make(AdoptionNotice, name="TestAdoption2") + cls.adoption3 = baker.make(AdoptionNotice, + name="TestAdoption3", + created_at=less_than_three_weeks_ago, + last_checked=less_than_three_weeks_ago) + + cls.adoption1.set_active() + cls.adoption3.set_active() + + def test_get_unchecked_adoption_notices(self): + result = get_unchecked_adoption_notices() + + self.assertContains(result, self.adoption1) + self.assertNotContains(result, self.adoption2) + self.assertNotContains(result, self.adoption3) + + def test_deactivate_unchecked_adoption_notices(self): + self.assertTrue(self.adoption1.is_active) + self.assertFalse(self.adoption2.is_active) + self.assertTrue(self.adoption3.is_active) + + deactivate_unchecked_adoption_notices() + + self.adoption1.refresh_from_db() + self.adoption2.refresh_from_db() + self.adoption3.refresh_from_db() + + self.assertFalse(self.adoption1.is_active) + self.assertFalse(self.adoption2.is_active) + self.assertTrue(self.adoption3.is_active) \ No newline at end of file