Files
bugsink/releases/tests.py
Klaas van Schelven 28b2ce0eaf Various models: .project SET_NULL => DO_NOTHING
Like e45c61d6f0, but for .project.

I originally thought `SET_NULL` would be a good way to "do stuff later", but
that's only so the degree that [1] updates are cheaper than deletes and [2]
2nd-order effects (further deletes in the dep-tree) are avoided.

Now that we have explicit Project-deletion (deps-first, delayed, properly batched)
the SET_NULL behavior is always a no-op (but with cost in queries).

As a result, in the test for project deletion (which has deletes for many
of the altered models), the following 12 queries are no longer done:

```
SELECT "projects_project"."id", [..many fields..] FROM "projects_project" WHERE "projects_project"."id" = 1
DELETE FROM "projects_projectmembership" WHERE "projects_projectmembership"."project_id" IN (1)
DELETE FROM "alerts_messagingserviceconfig" WHERE "alerts_messagingserviceconfig"."project_id" IN (1)
UPDATE "releases_release" SET "project_id" = NULL WHERE "releases_release"."project_id" IN (1)
UPDATE "issues_issue" SET "project_id" = NULL WHERE "issues_issue"."project_id" IN (1)
UPDATE "issues_grouping" SET "project_id" = NULL WHERE "issues_grouping"."project_id" IN (1)
UPDATE "events_event" SET "project_id" = NULL WHERE "events_event"."project_id" IN (1)
UPDATE "tags_tagkey" SET "project_id" = NULL WHERE "tags_tagkey"."project_id" IN (1)
UPDATE "tags_tagvalue" SET "project_id" = NULL WHERE "tags_tagvalue"."project_id" IN (1)
UPDATE "tags_eventtag" SET "project_id" = NULL WHERE "tags_eventtag"."project_id" IN (1)
UPDATE "tags_issuetag" SET "project_id" = NULL WHERE "tags_issuetag"."project_id" IN (1)
```
2025-07-03 21:49:49 +02:00

64 lines
2.7 KiB
Python

from django.test import TestCase as DjangoTestCase
from datetime import timedelta
from projects.models import Project
from .models import Release, ordered_releases, RE_PACKAGE_VERSION
class ReleaseTestCase(DjangoTestCase):
def test_create_and_order(self):
project = Project.objects.create(name="Test Project")
r0 = Release.objects.create(project=project, version="e80f98923f7426a8087009f4c629d25a35565a6a")
self.assertFalse(r0.is_semver)
self.assertEqual(0, r0.sort_epoch)
# still using hash; stay at epoch 0
# the timedelta has 2 purposes:
# * when the tests are very fast, it ensures that r0 and r1 are not created at the same time (as it would be in
# real usage too)
# * it ensures that dates are ignored when comparing r1 and r2 (r2 has a smaller date than r1, but comes later)
r1 = Release.objects.create(
project=project,
version="2a678dbbbecd2978ccaa76c326a0fb2e70073582",
date_released=r0.date_released + timedelta(seconds=10),
)
self.assertFalse(r1.is_semver)
self.assertEqual(0, r1.sort_epoch)
# switch to semver, epoch 1
r2 = Release.objects.create(project=project, version="1.0.0")
self.assertTrue(r2.is_semver)
self.assertEqual(1, r2.sort_epoch)
# stick with semver, but use a lower version
r3 = Release.objects.create(project=project, version="0.1.0")
self.assertTrue(r3.is_semver)
self.assertEqual(1, r3.sort_epoch)
# put in package name; this is basically ignored for ordering purposes
r4 = Release.objects.create(project=project, version="package@2.0.0")
self.assertTrue(r4.is_semver)
self.assertEqual(ordered_releases(), [r0, r1, r3, r2, r4])
def test_re_package_version(self):
self.assertEqual({"package": None, "version": "foo"}, RE_PACKAGE_VERSION.match("foo").groupdict())
self.assertEqual({"package": None, "version": "1.2.3"}, RE_PACKAGE_VERSION.match("1.2.3").groupdict())
self.assertEqual(
{"package": "mypackage", "version": "1.2.3"},
RE_PACKAGE_VERSION.match("mypackage@1.2.3").groupdict())
self.assertEqual(
{"package": "@mypackage", "version": "1.2.3"},
RE_PACKAGE_VERSION.match("@mypackage@1.2.3").groupdict())
# Sentry (as of late 2023) only allows an at-sign at the beginning of a package name, not anywhere else. I can't
# find any documentation or git-logs for why, so this is not replicated here.
self.assertEqual(
{"package": "@mypac@kage", "version": "1.2.3"},
RE_PACKAGE_VERSION.match("@mypac@kage@1.2.3").groupdict())