Make the NetBox test suite for GraphQL more adaptable to plugins #9401

Open
opened 2025-12-29 20:49:21 +01:00 by adam · 0 comments
Owner

Originally created by @peteeckel on GitHub (Mar 28, 2024).

NetBox version

4.0.0-dev

Feature type

Change to existing functionality

Proposed functionality

As a plugin developer I rely on some - admittedly undocumented - parts of the NetBox test framework for my unit tests. This can be done quite easily for API and view tests using the APITestCase and ModelViewTestCase classes and most of the subclasses of APIViewTestCases and ViewTestCases and for ChangeLoggedFilterSetTests.

There is one exception where adjustment to specific plugin conditions is unnecessarily complicated, though, and that's APITestCase.GraphQLTestCase, and that's for a trivial reason.

The _build_query method calls get_graphql_type_for_model(self.model), which is a utility function in the utilities.api module. This is the only place where this function is used, so it could be moved to a private class method which would make it overridable. Unfortunately this is not the case, so the whole _build_query method needs to be overriden in order to replace it with a function that works for a specific plugin. Overriding the much more complex _build_query method is IMHO problematic.

I see that for _get_graphql_base_name provisions have been made to read graphql_base_name from the model's Meta class, but unfortunately it's not possible to add this property to it yet - that could be a way forward that would make overriding test classes unnecessary altogether if it were pursued further, as get_graphql_type_for_model could be adjusted in much the same was, and the same goes for all the test methods. This is much more flexible and elegant than trying to derive the classes and base names etc. from verbose_name etc.

Use case

Extensive testing of plugin functionality

Database changes

None

External dependencies

None

Originally created by @peteeckel on GitHub (Mar 28, 2024). ### NetBox version 4.0.0-dev ### Feature type Change to existing functionality ### Proposed functionality As a plugin developer I rely on some - admittedly undocumented - parts of the NetBox test framework for my unit tests. This can be done quite easily for API and view tests using the `APITestCase` and `ModelViewTestCase` classes and most of the subclasses of `APIViewTestCases` and `ViewTestCases` and for `ChangeLoggedFilterSetTests`. There is one exception where adjustment to specific plugin conditions is unnecessarily complicated, though, and that's `APITestCase.GraphQLTestCase`, and that's for a trivial reason. The `_build_query` method calls `get_graphql_type_for_model(self.model)`, which is a utility function in the `utilities.api` module. This is the only place where this function is used, so it could be moved to a private class method which would make it overridable. Unfortunately this is not the case, so the whole `_build_query` method needs to be overriden in order to replace it with a function that works for a specific plugin. Overriding the much more complex `_build_query` method is IMHO problematic. I see that for `_get_graphql_base_name` provisions have been made to read `graphql_base_name` from the model's `Meta` class, but unfortunately it's not possible to add this property to it yet - that could be a way forward that would make overriding test classes unnecessary altogether if it were pursued further, as `get_graphql_type_for_model` could be adjusted in much the same was, and the same goes for all the test methods. This is much more flexible and elegant than trying to derive the classes and base names etc. from `verbose_name` etc. ### Use case Extensive testing of plugin functionality ### Database changes None ### External dependencies None
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#9401