11#!/usr/bin/env python
22# Copyright 2025 NetBox Labs, Inc.
33"""Diode NetBox Plugin - Tests."""
4+ from unittest import mock
5+
6+ from django .apps import apps
47from django .core .exceptions import ValidationError
58from django .test import TestCase
69
@@ -17,19 +20,98 @@ def test_validators(self):
1720 with self .assertRaises (ValidationError ):
1821 setting .clean_fields ()
1922
20-
2123 def test_str (self ):
2224 """Check Setting model string representation."""
2325 setting = Setting (diode_target = "http://localhost:8080" )
2426 self .assertEqual (str (setting ), "" )
2527
26-
2728 def test_absolute_url (self ):
2829 """Check Setting model absolute URL."""
2930 setting = Setting ()
3031 self .assertEqual (setting .get_absolute_url (), "/netbox/plugins/diode/settings/" )
3132
32- def test_tags_disabled (self ):
33- """Check Setting model has tags disabled."""
34- setting = Setting (diode_target = "http://localhost:8080" )
35- self .assertIsNone (setting .tags )
33+ def test_branch_id_field_exists (self ):
34+ """Check Setting model has branch_id field."""
35+ setting = Setting (diode_target = "grpc://localhost:8080/diode" )
36+ self .assertIsNone (setting .branch_id )
37+
38+ # Set branch_id
39+ setting .branch_id = 123
40+ self .assertEqual (setting .branch_id , 123 )
41+
42+ def test_branch_property_returns_none_when_no_branch_id (self ):
43+ """Check branch property returns None when branch_id is not set."""
44+ setting = Setting (diode_target = "grpc://localhost:8080/diode" )
45+ self .assertIsNone (setting .branch )
46+
47+ def test_branch_property_returns_none_when_plugin_not_installed (self ):
48+ """Check branch property returns None when branching plugin is not installed."""
49+ setting = Setting (diode_target = "grpc://localhost:8080/diode" , branch_id = 123 )
50+
51+ # Mock the import to simulate plugin not being available
52+ with mock .patch .dict ('sys.modules' , {'netbox_branching.models' : None }):
53+ self .assertIsNone (setting .branch )
54+
55+ def test_branch_property_returns_branch_when_available (self ):
56+ """Check branch property returns Branch object when available."""
57+ if not apps .is_installed ("netbox_branching" ):
58+ self .skipTest ("netbox_branching plugin not installed" )
59+
60+ from netbox_branching .models import Branch
61+
62+ # Create a test branch
63+ branch = Branch .objects .create (name = "test-branch" )
64+
65+ setting = Setting (diode_target = "grpc://localhost:8080/diode" , branch_id = branch .id )
66+
67+ # Check branch property returns the correct branch
68+ self .assertEqual (setting .branch .id , branch .id )
69+ self .assertEqual (setting .branch .name , "test-branch" )
70+
71+ # Clean up
72+ branch .delete ()
73+
74+ def test_branch_setter (self ):
75+ """Check branch setter updates branch_id."""
76+ if not apps .is_installed ("netbox_branching" ):
77+ self .skipTest ("netbox_branching plugin not installed" )
78+
79+ from netbox_branching .models import Branch
80+
81+ # Create a test branch
82+ branch = Branch .objects .create (name = "test-branch-setter" )
83+
84+ setting = Setting (diode_target = "grpc://localhost:8080/diode" )
85+
86+ # Use setter to assign branch
87+ setting .branch = branch
88+ self .assertEqual (setting .branch_id , branch .id )
89+
90+ # Set to None
91+ setting .branch = None
92+ self .assertIsNone (setting .branch_id )
93+
94+ # Clean up
95+ branch .delete ()
96+
97+ def test_branch_schema_id_property (self ):
98+ """Check branch_schema_id property returns schema_id when branch is set."""
99+ if not apps .is_installed ("netbox_branching" ):
100+ self .skipTest ("netbox_branching plugin not installed" )
101+
102+ from netbox_branching .models import Branch
103+
104+ # Create a test branch
105+ branch = Branch .objects .create (name = "test-branch-schema" )
106+
107+ setting = Setting (diode_target = "grpc://localhost:8080/diode" , branch_id = branch .id )
108+
109+ # Check branch_schema_id returns the schema_id
110+ self .assertEqual (setting .branch_schema_id , branch .schema_id )
111+
112+ # Check it returns None when no branch
113+ setting .branch_id = None
114+ self .assertIsNone (setting .branch_schema_id )
115+
116+ # Clean up
117+ branch .delete ()
0 commit comments