Disallow duplicate heartbeats and crashreports
Add unique constraints and corresponding schema and data migration.
Adapt all test cases so that only unique heartbeats and crashreports
are sent. Delete test cases that are inappropriate as no duplicate
entries can exist in the database anymore.
Issue: HIC-180
Change-Id: I768d1610d4482c9d61b76cdbc588334198bfe415
diff --git a/crashreports/tests/test_models.py b/crashreports/tests/test_models.py
new file mode 100644
index 0000000..d12189d
--- /dev/null
+++ b/crashreports/tests/test_models.py
@@ -0,0 +1,42 @@
+"""Tests for the crashreports models."""
+import logging
+
+from django.forms import model_to_dict
+from django.test import TestCase
+
+from crashreports.models import HeartBeat, Crashreport
+from crashreports.tests.utils import Dummy
+
+
+class DuplicatesTestCase(TestCase):
+ """Test cases for the uniqueness for model instances."""
+
+ def test_creation_of_duplicate_heartbeats(self):
+ """Test creation of duplicate heartbeats."""
+ self._assert_duplicate_entries_can_not_be_created(HeartBeat)
+
+ def test_creation_of_duplicate_crashreports(self):
+ """Test creation of duplicate crashreports."""
+ self._assert_duplicate_entries_can_not_be_created(Crashreport)
+
+ def _assert_duplicate_entries_can_not_be_created(self, object_type):
+ # Create a user, device and a report
+ user = Dummy.create_dummy_user()
+ device = Dummy.create_dummy_device(user)
+ Dummy.create_dummy_report(object_type, device)
+
+ # Assert creating a duplicate report fails
+ logger = logging.getLogger("crashreports")
+ with self.assertLogs(logger, "DEBUG") as logging_watcher:
+ report = Dummy.create_dummy_report(object_type, device)
+ self.assertEqual(
+ logging_watcher.output,
+ [
+ "DEBUG:crashreports.models:"
+ "Duplicate {} received and dropped: {}".format(
+ object_type.__name__, str(model_to_dict(report))
+ )
+ ],
+ )
+
+ self.assertEqual(object_type.objects.count(), 1)