""" Coverage tests for netbox_librenms_plugin/tables/vlans.py Tests cover all render methods and the configure() method of LibreNMSVLANTable. """ from unittest.mock import MagicMock, patch def _make_table(data=None, vlan_groups=None): """Create a LibreNMSVLANTable instance with minimal data.""" from netbox_librenms_plugin.tables.vlans import LibreNMSVLANTable return LibreNMSVLANTable(data=data or [], vlan_groups=vlan_groups) # =========================================================================== # __init__ / construction # =========================================================================== class TestLibreNMSVLANTableInit: """Tests for LibreNMSVLANTable.__init__().""" def test_default_prefix_set(self): table = _make_table() assert table.prefix == "vlans_" def test_vlan_groups_default_to_empty_list(self): table = _make_table() assert table.vlan_groups == [] def test_vlan_groups_stored_when_provided(self): mock_group = MagicMock() table = _make_table(vlan_groups=[mock_group]) assert table.vlan_groups == [mock_group] def test_none_vlan_groups_normalised_to_empty_list(self): table = _make_table(vlan_groups=None) assert table.vlan_groups == [] # =========================================================================== # render_vlan_id # =========================================================================== class TestRenderVlanId: """Tests for LibreNMSVLANTable.render_vlan_id().""" def test_text_success_when_exists_and_name_matches(self): table = _make_table() record = {"exists_in_netbox": True, "name_matches": True} html = str(table.render_vlan_id(100, record)) assert "text-success" in html assert "100" in html def test_text_warning_when_exists_but_name_mismatch(self): table = _make_table() record = {"exists_in_netbox": True, "name_matches": False} html = str(table.render_vlan_id(200, record)) assert "text-warning" in html assert "200" in html def test_text_danger_when_not_in_netbox(self): table = _make_table() record = {"exists_in_netbox": False, "name_matches": True} html = str(table.render_vlan_id(300, record)) assert "text-danger" in html assert "300" in html def test_default_name_matches_true_when_absent(self): """When name_matches key is absent, defaults to True → text-success if exists.""" table = _make_table() record = {"exists_in_netbox": True} # name_matches key absent html = str(table.render_vlan_id(10, record)) assert "text-success" in html # =========================================================================== # render_name # =========================================================================== class TestRenderName: """Tests for LibreNMSVLANTable.render_name().""" def test_text_success_when_synced(self): table = _make_table() record = {"exists_in_netbox": True, "name_matches": True} html = str(table.render_name("DATA", record)) assert "text-success" in html assert "DATA" in html def test_text_danger_when_not_in_netbox(self): table = _make_table() record = {"exists_in_netbox": False, "name_matches": True} html = str(table.render_name("VOICE", record)) assert "text-danger" in html assert "VOICE" in html def test_tooltip_added_on_name_mismatch(self): """When exists_in_netbox=True and name_matches=False, tooltip with NetBox name is shown.""" table = _make_table() record = { "exists_in_netbox": True, "name_matches": False, "netbox_vlan_name": "OLD_NAME", } html = str(table.render_name("NEW_NAME", record)) assert "text-warning" in html assert "NEW_NAME" in html assert "OLD_NAME" in html assert "title=" in html def test_empty_name_rendered_as_empty_string(self): """render_name handles None/empty value.""" table = _make_table() record = {"exists_in_netbox": False, "name_matches": True} html = str(table.render_name(None, record)) assert "text-danger" in html def test_tooltip_contains_both_names(self): table = _make_table() record = { "exists_in_netbox": True, "name_matches": False, "netbox_vlan_name": "NetBox-VLANName", } html = str(table.render_name("LibreNMSName", record)) assert "NetBox-VLANName" in html assert "LibreNMSName" in html def test_no_tooltip_when_names_match(self): table = _make_table() record = {"exists_in_netbox": True, "name_matches": True} html = str(table.render_name("MGMT", record)) # Tooltip (title=) should NOT be present when names match assert 'title="' not in html # =========================================================================== # render_vlan_group_selection # =========================================================================== class TestRenderVlanGroupSelection: """Tests for LibreNMSVLANTable.render_vlan_group_selection().""" def _make_group(self, pk, name, scope=None): group = MagicMock() group.pk = pk group.name = name group.scope = scope return group def test_select_element_rendered(self): table = _make_table(vlan_groups=[self._make_group(1, "Site VLANs")]) record = {"vlan_id": 10, "name": "DATA", "exists_in_netbox": False} html = str(table.render_vlan_group_selection(None, record)) assert "