From 23eadc5e64190885430c0bf3387e80e350a7b440 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Sat, 2 Sep 2023 17:19:31 +0300 Subject: [PATCH] Fix type warnings --- hc/accounts/models.py | 2 +- hc/api/tests/test_ping.py | 10 +++++++++- hc/api/tests/test_ping_by_slug.py | 3 ++- hc/front/views.py | 13 ++++++++----- 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/hc/accounts/models.py b/hc/accounts/models.py index d65f7e24151c..566abd4d20c9 100644 --- a/hc/accounts/models.py +++ b/hc/accounts/models.py @@ -181,7 +181,7 @@ def send_call_limit_notice(self): emails.call_limit(self.user.email, ctx) - def projects(self) -> list["Project"]: + def projects(self): """Return a queryset of all projects we have access to.""" is_owner = Q(owner_id=self.user_id) diff --git a/hc/api/tests/test_ping.py b/hc/api/tests/test_ping.py index b876caebc17f..55c1588bddd5 100644 --- a/hc/api/tests/test_ping.py +++ b/hc/api/tests/test_ping.py @@ -29,6 +29,7 @@ def test_it_works(self) -> None: self.check.refresh_from_db() self.assertEqual(self.check.status, "up") + assert self.check.last_ping expected_aa = self.check.last_ping + td(days=1, hours=1) self.assertEqual(self.check.alert_after, expected_aa) @@ -65,6 +66,7 @@ def test_post_works(self) -> None: ping = Ping.objects.get() self.assertEqual(ping.method, "POST") + assert ping.body_raw self.assertEqual(bytes(ping.body_raw), b"hello world") def test_head_works(self) -> None: @@ -154,7 +156,7 @@ def test_it_reads_forwarded_protocol(self) -> None: def test_it_never_caches(self) -> None: r = self.client.get(self.url) - assert "no-cache" in r.get("Cache-Control") + assert "no-cache" in r["Cache-Control"] def test_it_updates_confirmation_flag(self) -> None: payload = "Please Confirm ..." @@ -222,6 +224,7 @@ def test_it_sets_last_duration(self) -> None: self.assertEqual(r.status_code, 200) self.check.refresh_from_db() + assert self.check.last_duration self.assertTrue(self.check.last_duration.total_seconds() >= 10) def test_it_does_not_update_last_ping_on_rid_mismatch(self) -> None: @@ -249,6 +252,7 @@ def test_it_clears_last_ping_and_sets_last_duration_if_rid_matches(self) -> None self.check.refresh_from_db() self.assertIsNone(self.check.last_start) + assert self.check.last_duration self.assertTrue(self.check.last_duration.total_seconds() >= 10) def test_it_clears_last_ping_if_rid_is_absent(self) -> None: @@ -297,6 +301,7 @@ def test_it_chops_long_body(self) -> None: ping = Ping.objects.get() self.assertEqual(ping.method, "POST") + assert ping.body_raw self.assertEqual(bytes(ping.body_raw), b"hello") @override_settings(PING_BODY_LIMIT=None) @@ -305,6 +310,7 @@ def test_it_allows_unlimited_body(self) -> None: self.assertNotIn("Ping-Body-Limit", r.headers) ping = Ping.objects.get() + assert ping.body_raw self.assertEqual(len(ping.body_raw), 20000) def test_it_handles_manual_resume_flag(self) -> None: @@ -355,6 +361,7 @@ def test_it_accepts_bad_unicode(self) -> None: ping = Ping.objects.get() self.assertEqual(ping.method, "POST") + assert ping.body_raw self.assertEqual(bytes(ping.body_raw), b"Hello \xe9 World") @override_settings(S3_BUCKET="test-bucket", PING_BODY_LIMIT=None) @@ -383,6 +390,7 @@ def test_log_endpoint_works(self) -> None: ping = Ping.objects.get() self.assertEqual(ping.kind, "log") + assert ping.body_raw self.assertEqual(bytes(ping.body_raw), b"hello") self.assertFalse(Flip.objects.exists()) diff --git a/hc/api/tests/test_ping_by_slug.py b/hc/api/tests/test_ping_by_slug.py index eedd2fd04081..1537ee43de54 100644 --- a/hc/api/tests/test_ping_by_slug.py +++ b/hc/api/tests/test_ping_by_slug.py @@ -28,6 +28,7 @@ def test_post_works(self) -> None: ping = Ping.objects.get() self.assertEqual(ping.method, "POST") + assert ping.body_raw self.assertEqual(bytes(ping.body_raw), b"hello world") def test_head_works(self) -> None: @@ -38,7 +39,7 @@ def test_head_works(self) -> None: def test_it_never_caches(self) -> None: r = self.client.get(self.url) - assert "no-cache" in r.get("Cache-Control") + assert "no-cache" in r["Cache-Control"] def test_fail_endpoint_works(self) -> None: r = self.client.get(self.url + "/fail") diff --git a/hc/front/views.py b/hc/front/views.py index 0cb64b370b57..d0b97083f23f 100644 --- a/hc/front/views.py +++ b/hc/front/views.py @@ -804,17 +804,20 @@ def _get_events(check: Check, page_limit: int, start=None, end=None): for ping in pings: ping.duration = None - alerts = Notification.objects.select_related("channel") - alerts = alerts.filter(owner=check, check_status="down") + alerts: list[Notification] + q = Notification.objects.select_related("channel") + q = q.filter(owner=check, check_status="down") if start and end: - alerts = alerts.filter(created__gte=start, created__lte=end) + q = q.filter(created__gte=start, created__lte=end) + alerts = list(q) elif len(pings): cutoff = pings[-1].created - alerts = alerts.filter(created__gt=cutoff) + q = q.filter(created__gt=cutoff) + alerts = list(q) else: alerts = [] - events = pings + list(alerts) + events = pings + alerts events.sort(key=lambda el: el.created, reverse=True) return events