From 96440a55289a18a2ff3112f9ba0593dd73fb4dc6 Mon Sep 17 00:00:00 2001 From: Dmitry Volodin Date: Fri, 25 Jun 2021 11:15:04 +0200 Subject: [PATCH] tests: Check @on_delete for ReferenceField --- fm/models/alarmclass.py | 3 ++- gis/models/layer.py | 4 ++-- inv/models/interfaceprofile.py | 6 +++--- inv/models/networksegment.py | 1 + main/models/remotesystem.py | 7 ++++++- maintenance/models/maintenancetype.py | 4 ++++ sa/models/action.py | 11 ++++++++++- sa/models/service.py | 9 ++++++++- sa/models/serviceprofile.py | 3 +++ tests/models/test_0002_reference.py | 10 +++++++++- 10 files changed, 48 insertions(+), 10 deletions(-) diff --git a/fm/models/alarmclass.py b/fm/models/alarmclass.py index a96b07bf8c..50bc70a839 100644 --- a/fm/models/alarmclass.py +++ b/fm/models/alarmclass.py @@ -1,7 +1,7 @@ # --------------------------------------------------------------------- # AlarmClass model # --------------------------------------------------------------------- -# Copyright (C) 2007-2020 The NOC Project +# Copyright (C) 2007-2021 The NOC Project # See LICENSE for details # --------------------------------------------------------------------- @@ -51,6 +51,7 @@ handlers_lock = Lock() ("fm.ActiveAlarm", "alarm_class"), ("fm.AlarmClassConfig", "alarm_class"), ("fm.ArchivedAlarm", "alarm_class"), + ("fm.AlarmDiagnosticConfig", "alarm_class"), ] ) class AlarmClass(Document): diff --git a/gis/models/layer.py b/gis/models/layer.py index 65b0b324d5..89a0756ff1 100644 --- a/gis/models/layer.py +++ b/gis/models/layer.py @@ -1,7 +1,7 @@ # --------------------------------------------------------------------- # Map Layer # --------------------------------------------------------------------- -# Copyright (C) 2007-2020 The NOC Project +# Copyright (C) 2007-2021 The NOC Project # See LICENSE for details # --------------------------------------------------------------------- @@ -25,7 +25,7 @@ id_lock = Lock() DEFAULT_ZOOM = 11 -@on_delete_check(check=[("inv.Object", "layer")]) +@on_delete_check(check=[("inv.Object", "layer"), ("inv.ObjectConnection", "layer")]) class Layer(Document): meta = { "collection": "noc.layers", diff --git a/inv/models/interfaceprofile.py b/inv/models/interfaceprofile.py index 9f93689d81..ce6818e376 100644 --- a/inv/models/interfaceprofile.py +++ b/inv/models/interfaceprofile.py @@ -1,7 +1,7 @@ # --------------------------------------------------------------------- # Interface Profile models # --------------------------------------------------------------------- -# Copyright (C) 2007-2020 The NOC Project +# Copyright (C) 2007-2021 The NOC Project # See LICENSE for details # --------------------------------------------------------------------- @@ -57,8 +57,8 @@ class InterfaceProfileMetrics(EmbeddedDocument): check=[ ("inv.Interface", "profile"), ("inv.InterfaceClassificationRule", "profile"), - ("inv.SubInterface", "profile") - # ("sa.ServiceProfile", "") + ("inv.SubInterface", "profile"), + ("sa.ServiceProfile", "interface_profile"), ] ) class InterfaceProfile(Document): diff --git a/inv/models/networksegment.py b/inv/models/networksegment.py index 3e1063dc50..6846719196 100644 --- a/inv/models/networksegment.py +++ b/inv/models/networksegment.py @@ -64,6 +64,7 @@ class VLANTranslation(EmbeddedDocument): ("sa.AdministrativeDomain", "bioseg_floating_parent_segment"), ("sa.ManagedObject", "segment"), ("inv.NetworkSegment", "parent"), + ("inv.NetworkSegment", "sibling"), ("vc.VLAN", "segment"), ] ) diff --git a/main/models/remotesystem.py b/main/models/remotesystem.py index 2288f8cc71..1193921d85 100644 --- a/main/models/remotesystem.py +++ b/main/models/remotesystem.py @@ -1,7 +1,7 @@ # ---------------------------------------------------------------------- # RemoteSystem model # ---------------------------------------------------------------------- -# Copyright (C) 2007-2020 The NOC Project +# Copyright (C) 2007-2021 The NOC Project # See LICENSE for details # ---------------------------------------------------------------------- @@ -49,6 +49,10 @@ class EnvItem(EmbeddedDocument): ("crm.Supplier", "remote_system"), ("crm.SupplierProfile", "remote_system"), ("fm.TTSystem", "remote_system"), + ("gis.Address", "remote_system"), + ("gis.Building", "remote_system"), + ("gis.Division", "remote_system"), + ("gis.Street", "remote_system"), ("inv.AllocationGroup", "remote_system"), ("inv.InterfaceProfile", "remote_system"), ("inv.NetworkSegment", "remote_system"), @@ -57,6 +61,7 @@ class EnvItem(EmbeddedDocument): ("inv.Object", "remote_system"), ("ip.AddressProfile", "remote_system"), ("ip.PrefixProfile", "remote_system"), + ("main.Label", "remote_system"), ("sa.ManagedObject", "remote_system"), ("sa.AdministrativeDomain", "remote_system"), ("sa.ManagedObjectProfile", "remote_system"), diff --git a/maintenance/models/maintenancetype.py b/maintenance/models/maintenancetype.py index c51c24b25f..0d99dc3b3b 100644 --- a/maintenance/models/maintenancetype.py +++ b/maintenance/models/maintenancetype.py @@ -9,7 +9,11 @@ from mongoengine.document import Document from mongoengine.fields import StringField, BooleanField +# NOC modules +from noc.core.model.decorator import on_delete_check + +@on_delete_check(check=[("maintenance.Maintenance", "type")]) class MaintenanceType(Document): meta = { "collection": "noc.maintenancetype", diff --git a/sa/models/action.py b/sa/models/action.py index 501ac2d5ec..38e88a9950 100644 --- a/sa/models/action.py +++ b/sa/models/action.py @@ -1,7 +1,7 @@ # ---------------------------------------------------------------------- # Action # ---------------------------------------------------------------------- -# Copyright (C) 2007-2020 The NOC Project +# Copyright (C) 2007-2021 The NOC Project # See LICENSE for details # ---------------------------------------------------------------------- @@ -27,6 +27,7 @@ import cachetools from noc.core.text import quote_safe_path from noc.core.prettyjson import to_json from noc.core.ip import IP +from noc.core.model.decorator import on_delete_check id_lock = threading.Lock() @@ -63,6 +64,14 @@ class ActionParameter(EmbeddedDocument): return r +@on_delete_check( + check=[ + ("sa.ActionCommands", "action"), + ("fm.AlarmDiagnosticConfig", "on_clear_action"), + ("fm.AlarmDiagnosticConfig", "periodic_action"), + ("fm.AlarmDiagnosticConfig", "on_raise_action"), + ] +) class Action(Document): meta = { "collection": "noc.actions", diff --git a/sa/models/service.py b/sa/models/service.py index 25b139fe97..ad50535a11 100644 --- a/sa/models/service.py +++ b/sa/models/service.py @@ -51,7 +51,14 @@ id_lock = Lock() @resourcegroup @workflow @on_delete -@on_delete_check(clean=[("phone.PhoneNumber", "service"), ("inv.Interface", "service")]) +@on_delete_check( + clean=[ + ("phone.PhoneNumber", "service"), + ("inv.Interface", "service"), + ("inv.SubInterface", "service"), + ("sa.Service", "parent"), + ] +) class Service(Document): meta = { "collection": "noc.services", diff --git a/sa/models/serviceprofile.py b/sa/models/serviceprofile.py index cd0408ae54..b1d62edffb 100644 --- a/sa/models/serviceprofile.py +++ b/sa/models/serviceprofile.py @@ -33,6 +33,8 @@ from noc.core.bi.decorator import bi_sync from noc.core.defer import call_later from noc.inv.models.capsitem import CapsItem from noc.wf.models.workflow import Workflow +from noc.core.model.decorator import on_delete_check + id_lock = Lock() @@ -40,6 +42,7 @@ id_lock = Lock() @Label.model @bi_sync @on_save +@on_delete_check(check=[("sa.Service", "profile")]) class ServiceProfile(Document): meta = {"collection": "noc.serviceprofiles", "strict": False, "auto_create_index": False} name = StringField(unique=True) diff --git a/tests/models/test_0002_reference.py b/tests/models/test_0002_reference.py index 062ffc33ba..5b96f56d55 100644 --- a/tests/models/test_0002_reference.py +++ b/tests/models/test_0002_reference.py @@ -10,6 +10,7 @@ from collections import defaultdict # Third-party modules from django.db.models import ForeignKey +from mongoengine.fields import ReferenceField import pytest # NOC modules @@ -19,6 +20,11 @@ from noc.models import is_document, iter_model_id, get_model def iter_references(): + def q_self(m, ref): + if ref == "self": + return m + return ref + for model_id in iter_model_id(): model = get_model(model_id) if not model: @@ -28,7 +34,9 @@ def iter_references(): for fn in model._fields: f = model._fields[fn] if isinstance(f, PlainReferenceField): - yield f.document_type, model_id, fn + yield q_self(model, f.document_type), model_id, fn + if isinstance(f, ReferenceField): + yield q_self(model, f.document_type_obj), model_id, fn elif isinstance(f, ForeignKeyField): yield f.document_type, model_id, fn else: -- GitLab