From 940534e563b49e29733e2ca0a6df0fd3751e940f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Sat, 2 Jan 2016 21:54:04 +0200 Subject: [PATCH] Test for check.prune_pings() method --- hc/api/models.py | 4 ++-- hc/api/tests/test_check_model.py | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 hc/api/tests/test_check_model.py diff --git a/hc/api/models.py b/hc/api/models.py index 287bb9ba..8735b87f 100644 --- a/hc/api/models.py +++ b/hc/api/models.py @@ -112,8 +112,8 @@ class Check(models.Model): if len(cutoff) == 0: return 0 - cutoff_date = cutoff[0].created - q = Ping.objects.filter(owner=self, created__lte=cutoff_date) + cutoff_id = cutoff[0].id + q = Ping.objects.filter(owner=self, id__lte=cutoff_id) n_pruned, _ = q.delete() self.n_pings = keep_limit diff --git a/hc/api/tests/test_check_model.py b/hc/api/tests/test_check_model.py new file mode 100644 index 00000000..f8a2ead7 --- /dev/null +++ b/hc/api/tests/test_check_model.py @@ -0,0 +1,19 @@ +from django.test import TestCase +from hc.api.models import Check, Ping + + +class CheckModelTestCase(TestCase): + + def test_prune_pings(self): + check = Check() + check.save() + + for i in range(0, 6): + p = Ping(owner=check, ua="UA%d" % i) + p.save() + + check.prune_pings(keep_limit=3) + + ua_set = set(Ping.objects.values_list("ua", flat=True)) + # UA0, UA1, UA2 should have been pruned-- + self.assertEqual(ua_set, set(["UA3", "UA4", "UA5"]))