88from django .utils import timezone
99
1010from netbox_custom_objects .models import CustomObjectTypeField
11+ from core .models import ObjectType
1112from .base import CustomObjectsTestCase
1213
1314
@@ -374,7 +375,16 @@ class CustomObjectTestCase(CustomObjectsTestCase, TestCase):
374375 def setUpTestData (cls ):
375376 """Set up test data that should be created once for the entire test class."""
376377 super ().setUpTestData ()
377- cls .custom_object_type = cls .create_custom_object_type (name = "TestObject" )
378+ cls .custom_object_type = cls .create_custom_object_type (name = "TestObject" , slug = "test-objects" )
379+ cls .cot_1_model_name = (
380+ cls .custom_object_type .get_table_model_name (cls .custom_object_type .id ).lower ()
381+ )
382+ first_object_ct = ObjectType .objects .get (app_label = 'netbox_custom_objects' , model = cls .cot_1_model_name )
383+ cls .second_custom_object_type = cls .create_custom_object_type (name = "TestObject2" , slug = "test-objects2" )
384+ cls .cot_2_model_name = (
385+ cls .second_custom_object_type .get_table_model_name (cls .second_custom_object_type .id ).lower ()
386+ )
387+ second_object_ct = ObjectType .objects .get (app_label = 'netbox_custom_objects' , model = cls .cot_2_model_name )
378388
379389 # Add a primary field
380390 cls .create_custom_object_type_field (
@@ -474,7 +484,6 @@ def setUpTestData(cls):
474484 related_object_type = site_ct ,
475485 )
476486
477- site_ct = cls .get_site_object_type ()
478487 cls .create_custom_object_type_field (
479488 cls .custom_object_type ,
480489 name = "sites" ,
@@ -483,6 +492,58 @@ def setUpTestData(cls):
483492 related_object_type = site_ct ,
484493 )
485494
495+ # Custom Object single- and multi-object fields
496+
497+ cls .create_custom_object_type_field (
498+ cls .custom_object_type ,
499+ name = "second_object_single" ,
500+ label = "Second Object Single" ,
501+ type = "object" ,
502+ related_object_type = second_object_ct ,
503+ )
504+
505+ cls .create_custom_object_type_field (
506+ cls .custom_object_type ,
507+ name = "second_object_single_2" ,
508+ label = "Second Object Single 2" ,
509+ type = "object" ,
510+ related_object_type = second_object_ct ,
511+ )
512+
513+ cls .create_custom_object_type_field (
514+ cls .custom_object_type ,
515+ name = "second_object_multi" ,
516+ label = "Second Object Multi" ,
517+ type = "multiobject" ,
518+ related_object_type = second_object_ct ,
519+ )
520+
521+ cls .create_custom_object_type_field (
522+ cls .custom_object_type ,
523+ name = "second_object_multi_2" ,
524+ label = "Second Object Multi 2" ,
525+ type = "multiobject" ,
526+ related_object_type = second_object_ct ,
527+ )
528+
529+ # Self-referential
530+
531+ cls .create_custom_object_type_field (
532+ cls .custom_object_type ,
533+ name = "self_ref_single" ,
534+ label = "Self Ref Single" ,
535+ type = "object" ,
536+ related_object_type = first_object_ct ,
537+ )
538+
539+ cls .create_custom_object_type_field (
540+ cls .custom_object_type ,
541+ name = "self_ref_multi" ,
542+ label = "Self Ref Multi" ,
543+ type = "multiobject" ,
544+ related_object_type = first_object_ct ,
545+ )
546+
486547 # Get the dynamic model
487548 cls .model = cls .custom_object_type .get_model ()
488549
@@ -496,6 +557,11 @@ def test_custom_object_creation(self):
496557 now = timezone .now ()
497558 site_ct = self .get_site_object_type ()
498559 site = site_ct .model_class ().objects .create ()
560+ first_object_1 = self .model .objects .create ()
561+ first_object_2 = self .model .objects .create ()
562+ second_object_model = self .second_custom_object_type .get_model ()
563+ second_object_1 = second_object_model .objects .create ()
564+ second_object_2 = second_object_model .objects .create ()
499565
500566 instance = self .model .objects .create (
501567 name = "Test Instance" ,
@@ -510,8 +576,16 @@ def test_custom_object_creation(self):
510576 country = "US" ,
511577 countries = ["US" , "AU" ],
512578 site = site ,
579+ second_object_single = second_object_1 ,
580+ second_object_single_2 = second_object_2 ,
581+ self_ref_single = first_object_1 ,
513582 )
514583 instance .sites .add (site )
584+ instance .second_object_multi .add (second_object_1 )
585+ instance .second_object_multi .add (second_object_2 )
586+ instance .second_object_multi_2 .add (second_object_1 )
587+ instance .self_ref_multi .add (first_object_1 )
588+ instance .self_ref_multi .add (first_object_2 )
515589
516590 self .assertEqual (instance .name , "Test Instance" )
517591 self .assertEqual (instance .description , "A test instance" )
@@ -528,6 +602,14 @@ def test_custom_object_creation(self):
528602 self .assertEqual (instance .sites .all ().count (), 1 )
529603 self .assertIn (site , instance .sites .all ())
530604 self .assertEqual (str (instance ), "Test Instance" )
605+ # Object Fields pointing to Custom Objects
606+ self .assertEqual (instance .second_object_single , second_object_1 )
607+ self .assertEqual (instance .second_object_single_2 , second_object_2 )
608+ self .assertEqual (instance .second_object_multi .count (), 2 )
609+ self .assertEqual (instance .second_object_multi_2 .count (), 1 )
610+ # Self-referential Object Fields
611+ self .assertEqual (instance .self_ref_single , first_object_1 )
612+ self .assertEqual (instance .self_ref_multi .count (), 2 )
531613
532614 def test_custom_object_get_absolute_url (self ):
533615 """Test get_absolute_url method for custom objects."""
0 commit comments