Add GraphQL support and API tests

This commit is contained in:
Brian Tiemann
2026-03-12 21:08:09 -04:00
parent 29b2823d4c
commit a6238920fc
4 changed files with 23 additions and 1 deletions
+1 -1
View File
@@ -14,7 +14,7 @@ The rack unit or units being reserved. Multiple units can be expressed using com
### Total U's
A calculated (read-only) field that reflects the total size of the range of units in the reservation. Can be filtered upon using `unit_count_min` and `unit_count_max` parameters in the UI or API.
A calculated (read-only) field that reflects the total number of units in the reservation. Can be filtered upon using `unit_count_min` and `unit_count_max` parameters in the UI or API.
### Status
+1
View File
@@ -997,6 +997,7 @@ class RackReservationFilter(TenancyFilterMixin, PrimaryModelFilter):
units: Annotated['IntegerArrayLookup', strawberry.lazy('netbox.graphql.filter_lookups')] | None = (
strawberry_django.filter_field()
)
unit_count: ComparisonFilterLookup[int] | None = strawberry_django.filter_field()
user: Annotated['UserFilter', strawberry.lazy('users.graphql.filters')] | None = strawberry_django.filter_field()
user_id: ID | None = strawberry_django.filter_field()
description: StrFilterLookup[str] | None = strawberry_django.filter_field()
+12
View File
@@ -2,6 +2,7 @@ from typing import TYPE_CHECKING, Annotated
import strawberry
import strawberry_django
from django.db.models import Func, IntegerField
from core.graphql.mixins import ChangelogMixin
from dcim import models
@@ -803,6 +804,17 @@ class RackReservationType(PrimaryObjectType):
tenant: Annotated["TenantType", strawberry.lazy('tenancy.graphql.types')] | None
user: Annotated["UserType", strawberry.lazy('users.graphql.types')]
@classmethod
def get_queryset(cls, queryset, info, **kwargs):
queryset = super().get_queryset(queryset, info, **kwargs)
return queryset.annotate(
unit_count=Func('units', function='CARDINALITY', output_field=IntegerField())
)
@strawberry.field
def unit_count(self) -> int:
return len(self.units)
@strawberry_django.type(
models.RackRole,
+9
View File
@@ -570,6 +570,15 @@ class RackReservationTest(APIViewTestCases.APIViewTestCase):
},
]
def test_unit_count(self):
"""unit_count should reflect the number of units in the reservation."""
url = reverse('dcim-api:rackreservation-list')
self.add_permissions('dcim.view_rackreservation')
response = self.client.get(url, **self.header)
self.assertHttpStatus(response, 200)
for result in response.data['results']:
self.assertEqual(result['unit_count'], len(result['units']))
class ManufacturerTest(APIViewTestCases.APIViewTestCase):
model = Manufacturer