@@ -0,0 +1,1602 @@
+from django.test import TestCase%0Afrom django.utils import timezone%0Afrom dateutil.relativedelta import relativedelta%0Afrom freezegun import freeze_time%0A%0Afrom core.tests.mommy_utils import make_recipe%0Afrom cla_eventlog.models import Log%0Afrom legalaid.models import Case%0A%0A%0Aclass FindAndDeleteOldCases(TestCase):%0A    def create_three_year_old_case(self):%0A        case = None%0A        freezer = freeze_time(timezone.now() + relativedelta(years=-3))%0A        freezer.start()%0A        case = make_recipe(%22legalaid.case%22)%0A        freezer.stop()%0A        return case%0A%0A    def test_find_cases_viewed_two_years_ago(self):%0A        new_case = make_recipe(%22legalaid.case%22)%0A        old_case_1 = self.create_three_year_old_case()%0A        old_case_2 = self.create_three_year_old_case()%0A%0A        self.assertEqual(len(Case.objects.all()), 3)%0A%0A        make_recipe(%22cla_eventlog.log%22, case=new_case, code=%22CASE_VIEWED%22)%0A        make_recipe(%22cla_eventlog.log%22, case=old_case_1, code=%22CASE_VIEWED%22)%0A        make_recipe(%0A            %22cla_eventlog.log%22, case=old_case_2, code=%22CASE_VIEWED%22, created=timezone.now() + relativedelta(years=-3)%0A        )%0A%0A        self.assertEqual(len(Log.objects.all()), 3)%0A%0A        logs_for_cases_created_two_years_old = Log.objects.filter(%0A            case__created__lte=timezone.now() + relativedelta(years=-2)%0A        )%0A        case_viewed_logs = logs_for_cases_created_two_years_old.filter(code__contains=%22CASE_VIEWED%22)%0A        cases_viewed_two_years_ago = case_viewed_logs.filter(created__lte=timezone.now() + relativedelta(years=-2))%0A%0A        self.assertEqual(len(cases_viewed_two_years_ago), 1)%0A

Create and find cases that were viewed two years ago