mirror of
https://github.com/netbox-community/netbox.git
synced 2026-03-21 20:18:38 -06:00
Add GraphQL support and API tests
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user