From 3b3ce782c51c532cf7fe35d737d8f0f59b019ab8 Mon Sep 17 00:00:00 2001 From: Klaas van Schelven Date: Thu, 3 Jul 2025 11:33:27 +0200 Subject: [PATCH] Fix the tests for prev. commit tests were broken b/c not respecting constraints / not properly using the factories. --- events/factories.py | 17 +++++++++++++++-- events/tests.py | 9 ++++----- issues/factories.py | 1 + issues/tests.py | 24 +++++++++++------------- tags/tests.py | 12 ++++++------ 5 files changed, 37 insertions(+), 26 deletions(-) diff --git a/events/factories.py b/events/factories.py index bd3a9ef..18567e6 100644 --- a/events/factories.py +++ b/events/factories.py @@ -43,11 +43,24 @@ def create_event(project=None, issue=None, timestamp=None, event_data=None): ) -def create_event_data(): +def create_event_data(exception_type=None): # create minimal event data that is valid as per from_json() - return { + result = { "event_id": uuid.uuid4().hex, "timestamp": timezone.now().isoformat(), "platform": "python", } + + if exception_type is not None: + # allow for a specific exception type to get unique groupers/issues + result["exception"] = { + "values": [ + { + "type": exception_type, + "value": "This is a test exception", + } + ] + } + + return result diff --git a/events/tests.py b/events/tests.py index 82ac2be..672414c 100644 --- a/events/tests.py +++ b/events/tests.py @@ -9,8 +9,7 @@ from django.utils import timezone from bugsink.test_utils import TransactionTestCase25251 as TransactionTestCase from projects.models import Project, ProjectMembership -from issues.models import Issue -from issues.factories import denormalized_issue_fields +from issues.factories import get_or_create_issue from .factories import create_event from .retention import ( @@ -28,7 +27,7 @@ class ViewTests(TransactionTestCase): self.user = User.objects.create_user(username='test', password='test') self.project = Project.objects.create() ProjectMembership.objects.create(project=self.project, user=self.user) - self.issue = Issue.objects.create(project=self.project, **denormalized_issue_fields()) + self.issue, _ = get_or_create_issue(project=self.project) self.event = create_event(self.project, self.issue) self.client.force_login(self.user) @@ -154,7 +153,7 @@ class RetentionTestCase(DjangoTestCase): digested_at = timezone.now() self.project = Project.objects.create(retention_max_event_count=5) - self.issue = Issue.objects.create(project=self.project, **denormalized_issue_fields()) + self.issue, _ = get_or_create_issue(project=self.project) for digest_order in range(1, 7): project_stored_event_count += 1 # +1 pre-create, as in the ingestion view @@ -180,7 +179,7 @@ class RetentionTestCase(DjangoTestCase): project_stored_event_count = 0 self.project = Project.objects.create(retention_max_event_count=999) - self.issue = Issue.objects.create(project=self.project, **denormalized_issue_fields()) + self.issue, _ = get_or_create_issue(project=self.project) current_timestamp = datetime.datetime(2022, 1, 1, 0, 0, 0, tzinfo=datetime.timezone.utc) diff --git a/issues/factories.py b/issues/factories.py index 1438ec2..4b9efd6 100644 --- a/issues/factories.py +++ b/issues/factories.py @@ -12,6 +12,7 @@ def get_or_create_issue(project=None, event_data=None): if event_data is None: from events.factories import create_event_data event_data = create_event_data() + if project is None: project = Project.objects.create(name="Test project") diff --git a/issues/tests.py b/issues/tests.py index fd818f6..68a7faf 100644 --- a/issues/tests.py +++ b/issues/tests.py @@ -356,12 +356,11 @@ class MuteUnmuteTestCase(TransactionTestCase): def test_unmute_simple_case(self, send_unmute_alert): project = Project.objects.create() - issue = Issue.objects.create( - project=project, - unmute_on_volume_based_conditions='[{"period": "day", "nr_of_periods": 1, "volume": 1}]', - is_muted=True, - **denormalized_issue_fields(), - ) + issue, _ = get_or_create_issue(project) + + issue.unmute_on_volume_based_conditions = '[{"period": "day", "nr_of_periods": 1, "volume": 1}]' + issue.is_muted = True + issue.save() event = create_event(project, issue) BaseIngestAPIView.count_issue_periods_and_act_on_it(issue, event, datetime.now(timezone.utc)) @@ -376,15 +375,14 @@ class MuteUnmuteTestCase(TransactionTestCase): def test_unmute_two_simultaneously_should_lead_to_one_alert(self, send_unmute_alert): project = Project.objects.create() - issue = Issue.objects.create( - project=project, - unmute_on_volume_based_conditions='''[ + issue, _ = get_or_create_issue(project) + + issue. unmute_on_volume_based_conditions = '''[ {"period": "day", "nr_of_periods": 1, "volume": 1}, {"period": "month", "nr_of_periods": 1, "volume": 1} -]''', - is_muted=True, - **denormalized_issue_fields(), - ) +]''' + issue.is_muted = True + issue.save() event = create_event(project, issue) BaseIngestAPIView.count_issue_periods_and_act_on_it(issue, event, datetime.now(timezone.utc)) diff --git a/tags/tests.py b/tags/tests.py index 8c72d01..b124b1e 100644 --- a/tags/tests.py +++ b/tags/tests.py @@ -3,7 +3,7 @@ from django.test import TestCase as DjangoTestCase from projects.models import Project from issues.factories import get_or_create_issue, denormalized_issue_fields -from events.factories import create_event +from events.factories import create_event, create_event_data from issues.models import Issue from .models import store_tags, EventTag @@ -178,12 +178,12 @@ class SearchTestCase(DjangoTestCase): # scenario (in which there would be some relation between the tags of issues and events), but it allows us to # test event_search more easily (if each event is tied to a different issue, searching for tags is meaningless, # since you always search within the context of an issue). - self.global_issue = Issue.objects.create(project=self.project, **denormalized_issue_fields()) + self.global_issue, _ = get_or_create_issue(project=self.project, event_data=create_event_data("global")) - issue_with_tags_and_text = Issue.objects.create(project=self.project, **denormalized_issue_fields()) + issue_with_tags_and_text, _ = get_or_create_issue(project=self.project, event_data=create_event_data("tag_txt")) event_with_tags_and_text = create_event(self.project, issue=self.global_issue) - issue_with_tags_no_text = Issue.objects.create(project=self.project, **denormalized_issue_fields()) + issue_with_tags_no_text, _ = get_or_create_issue(project=self.project, event_data=create_event_data("no_text")) event_with_tags_no_text = create_event(self.project, issue=self.global_issue) store_tags(event_with_tags_and_text, issue_with_tags_and_text, {f"k-{i}": f"v-{i}" for i in range(5)}) @@ -191,7 +191,7 @@ class SearchTestCase(DjangoTestCase): # fix the EventTag objects' issue, which is broken per the non-real-world setup (see above) EventTag.objects.all().update(issue=self.global_issue) - issue_without_tags = Issue.objects.create(project=self.project, **denormalized_issue_fields()) + issue_without_tags, _ = get_or_create_issue(project=self.project, event_data=create_event_data("no_tags")) event_without_tags = create_event(self.project, issue=self.global_issue) for obj in [issue_with_tags_and_text, event_with_tags_and_text, issue_without_tags, event_without_tags]: @@ -199,7 +199,7 @@ class SearchTestCase(DjangoTestCase): obj.calculated_value = "findable value" obj.save() - Issue.objects.create(project=self.project, **denormalized_issue_fields()) + get_or_create_issue(project=self.project, event_data=create_event_data("no_text")) create_event(self.project, issue=self.global_issue) def _test_search(self, search_x):