Mitja Nikolaus | fd452f8 | 2018-11-07 11:53:59 +0100 | [diff] [blame] | 1 | """Tests for the crashreports models.""" |
| 2 | import logging |
| 3 | |
| 4 | from django.forms import model_to_dict |
| 5 | from django.test import TestCase |
| 6 | |
| 7 | from crashreports.models import HeartBeat, Crashreport |
| 8 | from crashreports.tests.utils import Dummy |
| 9 | |
| 10 | |
| 11 | class DuplicatesTestCase(TestCase): |
| 12 | """Test cases for the uniqueness for model instances.""" |
| 13 | |
| 14 | def test_creation_of_duplicate_heartbeats(self): |
| 15 | """Test creation of duplicate heartbeats.""" |
| 16 | self._assert_duplicate_entries_can_not_be_created(HeartBeat) |
| 17 | |
| 18 | def test_creation_of_duplicate_crashreports(self): |
| 19 | """Test creation of duplicate crashreports.""" |
| 20 | self._assert_duplicate_entries_can_not_be_created(Crashreport) |
| 21 | |
| 22 | def _assert_duplicate_entries_can_not_be_created(self, object_type): |
| 23 | # Create a user, device and a report |
Mitja Nikolaus | 77dd565 | 2018-12-06 11:27:01 +0100 | [diff] [blame^] | 24 | user = Dummy.create_user() |
| 25 | device = Dummy.create_device(user) |
| 26 | Dummy.create_report(object_type, device) |
Mitja Nikolaus | fd452f8 | 2018-11-07 11:53:59 +0100 | [diff] [blame] | 27 | |
| 28 | # Assert creating a duplicate report fails |
| 29 | logger = logging.getLogger("crashreports") |
| 30 | with self.assertLogs(logger, "DEBUG") as logging_watcher: |
Mitja Nikolaus | 77dd565 | 2018-12-06 11:27:01 +0100 | [diff] [blame^] | 31 | report = Dummy.create_report(object_type, device) |
Mitja Nikolaus | fd452f8 | 2018-11-07 11:53:59 +0100 | [diff] [blame] | 32 | self.assertEqual( |
| 33 | logging_watcher.output, |
| 34 | [ |
| 35 | "DEBUG:crashreports.models:" |
| 36 | "Duplicate {} received and dropped: {}".format( |
| 37 | object_type.__name__, str(model_to_dict(report)) |
| 38 | ) |
| 39 | ], |
| 40 | ) |
| 41 | |
| 42 | self.assertEqual(object_type.objects.count(), 1) |