Skip to content

Commit 3e07673

Browse files
committed
Fixes #133: Fix search index display_attrs referencing non-existent fields
SupportContractAssignmentIndex and LicenseAssignmentIndex had display_attrs that referenced fields from their parent models instead of their own fields. This caused a FieldDoesNotExist error when searching for devices with support contract assignments. Changes: - SupportContractAssignmentIndex: use contract, sku, device instead of vendor, start, renewal - LicenseAssignmentIndex: use license, vendor, device instead of manufacturer - Add regression tests for display_attrs validation
1 parent de206a2 commit 3e07673

2 files changed

Lines changed: 54 additions & 2 deletions

File tree

netbox_lifecycle/search.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class SupportContractAssignmentIndex(SearchIndex):
4646
('description', 4000),
4747
('comments', 5000),
4848
)
49-
display_attrs = ('vendor', 'start', 'renewal', 'end', 'description')
49+
display_attrs = ('contract', 'sku', 'device', 'end', 'description')
5050

5151

5252
@register_search
@@ -70,4 +70,4 @@ class LicenseAssignmentIndex(SearchIndex):
7070
('description', 4000),
7171
('comments', 5000),
7272
)
73-
display_attrs = ('manufacturer', 'description')
73+
display_attrs = ('license', 'vendor', 'device', 'description')
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
from django.core.exceptions import FieldDoesNotExist
2+
from django.test import TestCase
3+
4+
from netbox_lifecycle.search import (
5+
SupportContractAssignmentIndex,
6+
LicenseAssignmentIndex,
7+
)
8+
9+
10+
class SearchIndexDisplayAttrsTestCase(TestCase):
11+
"""
12+
Test that search index display_attrs reference valid model fields.
13+
14+
Regression test for GitHub issue #133:
15+
Search error "SupportContractAssignment has no field named 'vendor'"
16+
"""
17+
18+
def test_support_contract_assignment_display_attrs_are_valid_fields(self):
19+
"""
20+
SupportContractAssignmentIndex.display_attrs should only reference
21+
fields that exist on the SupportContractAssignment model.
22+
"""
23+
index = SupportContractAssignmentIndex()
24+
model = index.model
25+
26+
for attr in index.display_attrs:
27+
# This should not raise FieldDoesNotExist
28+
try:
29+
model._meta.get_field(attr)
30+
except FieldDoesNotExist:
31+
self.fail(
32+
f"SupportContractAssignmentIndex.display_attrs references "
33+
f"non-existent field '{attr}' on {model.__name__}"
34+
)
35+
36+
def test_license_assignment_display_attrs_are_valid_fields(self):
37+
"""
38+
LicenseAssignmentIndex.display_attrs should only reference
39+
fields that exist on the LicenseAssignment model.
40+
"""
41+
index = LicenseAssignmentIndex()
42+
model = index.model
43+
44+
for attr in index.display_attrs:
45+
# This should not raise FieldDoesNotExist
46+
try:
47+
model._meta.get_field(attr)
48+
except FieldDoesNotExist:
49+
self.fail(
50+
f"LicenseAssignmentIndex.display_attrs references "
51+
f"non-existent field '{attr}' on {model.__name__}"
52+
)

0 commit comments

Comments
 (0)