Files
netbox-plugin-dns-bridge/netbox_dns_bridge/tests/test_models.py
T
sven 4ad110ca53 new file: conftest.py
new file:   netbox_dns_bridge/tests/__init__.py
new file:   netbox_dns_bridge/tests/_helpers.py
new file:   netbox_dns_bridge/tests/conftest.py
new file:   netbox_dns_bridge/tests/test_bootstrap.py
new file:   netbox_dns_bridge/tests/test_catalog_zone_manager.py
new file:   netbox_dns_bridge/tests/test_dns_server.py
new file:   netbox_dns_bridge/tests/test_management_commands.py
new file:   netbox_dns_bridge/tests/test_models.py
new file:   netbox_dns_bridge/tests/test_notify_handler.py
new file:   netbox_dns_bridge/tests/test_request_handler_integration.py
new file:   netbox_dns_bridge/tests/test_request_handler_unit.py
new file:   netbox_dns_bridge/tests/test_signals.py
new file:   netbox_dns_bridge/tests/test_smoke.py
new file:   netbox_dns_bridge/tests/test_utils.py
new file:   netbox_dns_bridge/tests/test_views.py
modified:   pyproject.toml
Add some tests. #AI generated
2026-08-12 12:25:46 +02:00

139 lines
5.0 KiB
Python

"""Tests for plugin models: constraints, __str__, cascade behaviours."""
import pytest
from netbox_dns_bridge.models import (
IntegerKeyValueSetting,
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"
def test_unique_key_constraint(self):
IntegerKeyValueSetting.objects.create(key="dup-key", value=1)
from django.db import IntegrityError
with pytest.raises(IntegrityError):
IntegerKeyValueSetting.objects.create(key="dup-key", value=2)
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
@pytest.mark.django_db
class TestCatalogZoneMemberIdentifier:
def test_str_representation(self, make_zone):
zone = make_zone(name="str.test")
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()
other = make_zone(name="other.test")
other_ident = CatalogZoneMemberIdentifier.objects.get(zone=other)
from django.db import IntegrityError
with pytest.raises(IntegrityError):
other_ident.name = "shared-name"
other_ident.save()
def test_cascade_delete_when_zone_deleted(self, make_zone):
zone = make_zone(name="cascade.test")
m = CatalogZoneMemberIdentifier.objects.get(zone=zone)
assert CatalogZoneMemberIdentifier.objects.filter(pk=m.pk).exists()
zone.delete()
assert not CatalogZoneMemberIdentifier.objects.filter(pk=m.pk).exists()
def test_one_to_one_relationship(self, make_zone):
zone = make_zone(name="oto.test")
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")
CatalogZoneMemberIdentifier.objects.filter(zone=z3).update(name="gamma")
CatalogZoneMemberIdentifier.objects.filter(zone=z1).update(name="alpha")
CatalogZoneMemberIdentifier.objects.filter(zone=z2).update(name="beta")
names = list(
CatalogZoneMemberIdentifier.objects.filter(
zone__in=[z1, z2, z3]
).values_list("name", flat=True)
)
assert names == ["alpha", "beta", "gamma"]
@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",
view=view,
last_seen="2026-01-01T00:00:00Z",
)
assert str(client) == f"192.0.2.1 ({view.name})"
def test_unique_ip_view_constraint(self, make_view):
from django.db import IntegrityError
view = make_view(name="uniq-view")
SeenTransferClients.objects.create(
source_ip="10.0.0.1",
view=view,
last_seen="2026-01-01T00:00:00Z",
)
with pytest.raises(IntegrityError):
SeenTransferClients.objects.create(
source_ip="10.0.0.1",
view=view,
last_seen="2026-01-02T00:00:00Z",
)
def test_same_ip_different_views_allowed(self, make_view):
v1 = make_view(name="v1")
v2 = make_view(name="v2")
SeenTransferClients.objects.create(
source_ip="10.0.0.5", view=v1, last_seen="2026-01-01T00:00:00Z"
)
SeenTransferClients.objects.create(
source_ip="10.0.0.5", view=v2, last_seen="2026-01-01T00:00:00Z"
)
assert SeenTransferClients.objects.filter(source_ip="10.0.0.5").count() == 2
def test_protect_on_view_delete(self, make_view):
from django.db.models import ProtectedError
view = make_view(name="protected-view")
SeenTransferClients.objects.create(
source_ip="10.0.0.9",
view=view,
last_seen="2026-01-01T00:00:00Z",
)
with pytest.raises(ProtectedError):
view.delete()
def test_ordering_by_source_ip(self, make_view):
from django.utils import timezone
v = make_view(name="ord-view")
now = timezone.now()
for ip in ["10.0.0.3", "10.0.0.1", "10.0.0.2"]:
SeenTransferClients.objects.create(source_ip=ip, view=v, last_seen=now)
ips = list(SeenTransferClients.objects.values_list("source_ip", flat=True))
assert ips == ["10.0.0.1", "10.0.0.2", "10.0.0.3"]