Replace global IntegerKeyValueSetting with per-view CatalogZone model
Track the catalog zone SOA serial per view instead of a single global value. The new CatalogZone model has a 1:1 FK to netbox_dns.models.View and its own soa_serial field; each view's catalog zone now increments independently. The existing global serial is preserved during migration by copying it into every view's new CatalogZone row. Change CatalogZoneMemberIdentifier.name from globally unique to unique per catalog zone (UniqueConstraint(name, catalog_zone)) and add a catalog_zone FK to enforce it. Remove the dns-settings management command, which was a generic interface over the now-removed IntegerKeyValueSetting model. Increment both catalog zone serials when a zone moves between views: the old view's catalog zone loses a member and the new view's gains one, so both must be bumped. Also refresh the member identifier's catalog_zone FK on a view move so it does not go stale. The pre_save handler now captures _old_view_id and the post_save handler detects view changes, increments the old view's serial inside on_commit (before the new view's), and re-creates the identifier. AI generated, code manually verified.
This commit is contained in:
1 parent
4ad110ca53
commit
9f5e40ae48
17 files changed
+429
-240
No files matched your search
@@ -2,29 +2,46 @@
|
||||
import pytest
|
||||
|
||||
from netbox_dns_bridge.models import (
|
||||
IntegerKeyValueSetting,
|
||||
CatalogZone,
|
||||
CatalogZoneMemberIdentifier,
|
||||
SeenTransferClients,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
class TestIntegerKeyValueSetting:
|
||||
def test_str_representation(self):
|
||||
obj = IntegerKeyValueSetting.objects.create(key="my-key", value=42)
|
||||
assert str(obj) == "my-key: 42"
|
||||
class TestCatalogZone:
|
||||
def test_str_representation(self, make_view):
|
||||
view = make_view(name="cz-str-view")
|
||||
cz = CatalogZone.objects.create(view=view, soa_serial=42)
|
||||
assert str(cz) == "cz-str-view: 42"
|
||||
|
||||
def test_unique_key_constraint(self):
|
||||
IntegerKeyValueSetting.objects.create(key="dup-key", value=1)
|
||||
from django.db import IntegrityError
|
||||
def test_default_soa_serial(self, make_view):
|
||||
view = make_view(name="cz-default-view")
|
||||
cz = CatalogZone.objects.create(view=view)
|
||||
assert cz.soa_serial == 1
|
||||
|
||||
with pytest.raises(IntegrityError):
|
||||
IntegerKeyValueSetting.objects.create(key="dup-key", value=2)
|
||||
def test_one_to_one_relationship(self, make_view):
|
||||
view = make_view(name="cz-oto-view")
|
||||
cz = CatalogZone.objects.create(view=view, soa_serial=7)
|
||||
assert view.catalog_zone == cz
|
||||
|
||||
def test_create_and_retrieve(self):
|
||||
IntegerKeyValueSetting.objects.create(key="catz-serial", value=99)
|
||||
obj = IntegerKeyValueSetting.objects.get(key="catz-serial")
|
||||
assert obj.value == 99
|
||||
def test_create_and_retrieve(self, make_view):
|
||||
view = make_view(name="cz-retrieve-view")
|
||||
CatalogZone.objects.create(view=view, soa_serial=99)
|
||||
cz = CatalogZone.objects.get(view=view)
|
||||
assert cz.soa_serial == 99
|
||||
|
||||
def test_ordering_by_view_name(self, make_view):
|
||||
v1 = make_view(name="z-view")
|
||||
v2 = make_view(name="a-view")
|
||||
v3 = make_view(name="m-view")
|
||||
CatalogZone.objects.create(view=v1, soa_serial=1)
|
||||
CatalogZone.objects.create(view=v2, soa_serial=2)
|
||||
CatalogZone.objects.create(view=v3, soa_serial=3)
|
||||
names = list(
|
||||
CatalogZone.objects.values_list("view__name", flat=True)
|
||||
)
|
||||
assert names == ["a-view", "m-view", "z-view"]
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
@@ -34,19 +51,35 @@ class TestCatalogZoneMemberIdentifier:
|
||||
m = CatalogZoneMemberIdentifier.objects.get(zone=zone)
|
||||
assert str(m) == m.name
|
||||
|
||||
def test_unique_name_constraint(self, make_zone):
|
||||
zone = make_zone(name="uniq.test")
|
||||
existing = CatalogZoneMemberIdentifier.objects.get(zone=zone)
|
||||
existing.name = "shared-name"
|
||||
existing.save()
|
||||
def test_unique_name_per_catalog_zone(self, make_zone, make_view):
|
||||
view = make_view(name="shared-view")
|
||||
zone1 = make_zone(name="uniq1.test", view=view)
|
||||
zone2 = make_zone(name="uniq2.test", view=view)
|
||||
|
||||
other = make_zone(name="other.test")
|
||||
other_ident = CatalogZoneMemberIdentifier.objects.get(zone=other)
|
||||
ident1 = CatalogZoneMemberIdentifier.objects.get(zone=zone1)
|
||||
ident1.name = "shared-name"
|
||||
ident1.save()
|
||||
|
||||
ident2 = CatalogZoneMemberIdentifier.objects.get(zone=zone2)
|
||||
from django.db import IntegrityError
|
||||
|
||||
with pytest.raises(IntegrityError):
|
||||
other_ident.name = "shared-name"
|
||||
other_ident.save()
|
||||
ident2.name = "shared-name"
|
||||
ident2.save()
|
||||
|
||||
def test_same_name_allowed_in_different_catalog_zones(self, make_zone, make_view):
|
||||
view1 = make_view(name="view-a")
|
||||
view2 = make_view(name="view-b")
|
||||
zone1 = make_zone(name="diff1.test", view=view1)
|
||||
zone2 = make_zone(name="diff2.test", view=view2)
|
||||
|
||||
ident1 = CatalogZoneMemberIdentifier.objects.get(zone=zone1)
|
||||
ident1.name = "shared-name"
|
||||
ident1.save()
|
||||
|
||||
ident2 = CatalogZoneMemberIdentifier.objects.get(zone=zone2)
|
||||
ident2.name = "shared-name"
|
||||
ident2.save()
|
||||
|
||||
def test_cascade_delete_when_zone_deleted(self, make_zone):
|
||||
zone = make_zone(name="cascade.test")
|
||||
@@ -60,10 +93,11 @@ class TestCatalogZoneMemberIdentifier:
|
||||
m = CatalogZoneMemberIdentifier.objects.get(zone=zone)
|
||||
assert zone.catz_identifier == m
|
||||
|
||||
def test_ordering_by_name(self, make_zone):
|
||||
z1 = make_zone(name="z1.test")
|
||||
z2 = make_zone(name="z2.test")
|
||||
z3 = make_zone(name="z3.test")
|
||||
def test_ordering_by_name(self, make_zone, make_view):
|
||||
view = make_view(name="ord-view")
|
||||
z1 = make_zone(name="z1.test", view=view)
|
||||
z2 = make_zone(name="z2.test", view=view)
|
||||
z3 = make_zone(name="z3.test", view=view)
|
||||
CatalogZoneMemberIdentifier.objects.filter(zone=z3).update(name="gamma")
|
||||
CatalogZoneMemberIdentifier.objects.filter(zone=z1).update(name="alpha")
|
||||
CatalogZoneMemberIdentifier.objects.filter(zone=z2).update(name="beta")
|
||||
@@ -78,8 +112,6 @@ class TestCatalogZoneMemberIdentifier:
|
||||
@pytest.mark.django_db
|
||||
class TestSeenTransferClients:
|
||||
def test_str_representation(self, make_view):
|
||||
from netbox_dns.models import View
|
||||
|
||||
view = make_view(name="str-view")
|
||||
client = SeenTransferClients.objects.create(
|
||||
source_ip="192.0.2.1",
|
||||
|
||||
Reference in new issue
Block a user