From 158b26e5295bbe247a1e381cd57c3cb4c4cd0579 Mon Sep 17 00:00:00 2001 From: chentiantian Date: Fri, 14 Jun 2024 10:55:11 +0800 Subject: [PATCH] test (schedulers): add test for when model.last_run_at is None --- t/unit/test_schedulers.py | 48 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/t/unit/test_schedulers.py b/t/unit/test_schedulers.py index dbafe666..88727aa2 100644 --- a/t/unit/test_schedulers.py +++ b/t/unit/test_schedulers.py @@ -213,6 +213,54 @@ def test_entry_and_model_last_run_at_with_utc_no_use_tz(self, monkeypatch): if hasattr(time, "tzset"): time.tzset() + @override_settings( + USE_TZ=False, + DJANGO_CELERY_BEAT_TZ_AWARE=False + ) + @pytest.mark.usefixtures('depends_on_current_app') + @timezone.override('Europe/Berlin') + @pytest.mark.celery(timezone='Europe/Berlin') + def test_entry_and_model_last_run_at_when_model_changed(self, monkeypatch): + old_tz = os.environ.get("TZ") + os.environ["TZ"] = "Europe/Berlin" + if hasattr(time, "tzset"): + time.tzset() + assert self.app.timezone.key == 'Europe/Berlin' + # simulate last_run_at from DB - not TZ aware but localtime + right_now = datetime.utcnow() + # make sure to use fixed date time + monkeypatch.setattr(self.Entry, '_default_now', lambda o: right_now) + m = self.create_model_crontab( + crontab(minute='*/10') + ) + m.save() + e = self.Entry(m, app=self.app) + e.save() + m.refresh_from_db() + + # The just created model has no value for last_run_at + # so last_run_at should be set to the Entry._default_now() + assert m.last_run_at == e.last_run_at + + # If the model has been updated and the entry.last_run_at is None, + # then entry.last_run_at should be set to the value of model.date_changed. + # see #717 + m.save() + e2 = self.Entry(m, app=self.app) + e2.save() + m.refresh_from_db() + + t1 = m.last_run_at.strftime("%Y-%m-%d %H:%M:%S") + t2 = m.date_changed.strftime("%Y-%m-%d %H:%M:%S") + assert t1 == t2 + + if old_tz is not None: + os.environ["TZ"] = old_tz + else: + del os.environ["TZ"] + if hasattr(time, "tzset"): + time.tzset() + @override_settings( USE_TZ=False, DJANGO_CELERY_BEAT_TZ_AWARE=False,