Graphql test cases fails for plugins #7602

Closed
opened 2025-12-29 20:25:55 +01:00 by adam · 1 comment
Owner

Originally created by @abhi1693 on GitHub (Feb 5, 2023).

NetBox version

v3.4.4

Python version

3.10

Steps to Reproduce

Demo plugin can be used for reproducing the steps, however, some minor changes will be required to the graphql structure as the API tests requires a folder structure as

.
└── netbox_access_lists/
    └── graphql/
        ├── __init__.py
        ├── types.py
        └── schema.py

The structure is needed due to the dynamic import defined at https://github.com/netbox-community/netbox/blob/develop/netbox/utilities/api.py#L39

Once I made the above changes, and wrote a simple Testcase as

class MyTestCase(
    APIViewTestCases.GraphQLTestCase
):
    model = MyPlugin
    view_namespace = 'plugins-api:netbox_myplugin'

...

The tests fails.

Expected Behavior

GraphQL tests written for plugins should pass as seen in the core code

Observed Behavior

======================================================================
FAIL: test_graphql_get_object (netbox_myplugin.tests.test_api.MyTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/asaharan/PycharmProjects/netbox/venv/lib/python3.10/site-packages/django/test/utils.py", line 460, in inner
    return func(*args, **kwargs)
  File "/home/asaharan/PycharmProjects/netbox/venv/lib/python3.10/site-packages/django/test/utils.py", line 460, in inner
    return func(*args, **kwargs)
  File "/home/asaharan/PycharmProjects/netbox/netbox/utilities/testing/api.py", line 498, in test_graphql_get_object
    self.assertHttpStatus(response, status.HTTP_200_OK)
  File "/home/asaharan/PycharmProjects/netbox/netbox/utilities/testing/base.py", line 71, in assertHttpStatus
    self.assertEqual(response.status_code, expected_status, err_message)
AssertionError: 400 != 200 : Expected HTTP status 200; received 400: b'{"errors":[{"message":"Cannot query field \'myplugin\' on type \'MyPluginType\'.","locations":[{"line":17,"column":9}]}]}'

======================================================================
FAIL: test_graphql_list_objects (netbox_myplugin.tests.test_api.MyTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/asaharan/PycharmProjects/netbox/venv/lib/python3.10/site-packages/django/test/utils.py", line 460, in inner
    return func(*args, **kwargs)
  File "/home/asaharan/PycharmProjects/netbox/venv/lib/python3.10/site-packages/django/test/utils.py", line 460, in inner
    return func(*args, **kwargs)
  File "/home/asaharan/PycharmProjects/netbox/netbox/utilities/testing/api.py", line 523, in test_graphql_list_objects
    self.assertHttpStatus(response, status.HTTP_200_OK)
  File "/home/asaharan/PycharmProjects/netbox/netbox/utilities/testing/base.py", line 71, in assertHttpStatus
    self.assertEqual(response.status_code, expected_status, err_message)
AssertionError: 400 != 200 : Expected HTTP status 200; received 400: b'{"errors":[{"message":"Cannot query field \'myplugin\' on type \'MyPluginType\'.","locations":[{"line":17,"column":9}]}]}'
Originally created by @abhi1693 on GitHub (Feb 5, 2023). ### NetBox version v3.4.4 ### Python version 3.10 ### Steps to Reproduce Demo plugin can be used for reproducing the steps, however, some minor changes will be required to the `graphql` structure as the API tests requires a folder structure as ``` . └── netbox_access_lists/ └── graphql/ ├── __init__.py ├── types.py └── schema.py ``` The structure is needed due to the dynamic import defined at https://github.com/netbox-community/netbox/blob/develop/netbox/utilities/api.py#L39 Once I made the above changes, and wrote a simple Testcase as ``` class MyTestCase( APIViewTestCases.GraphQLTestCase ): model = MyPlugin view_namespace = 'plugins-api:netbox_myplugin' ... ``` The tests fails. ### Expected Behavior GraphQL tests written for plugins should pass as seen in the core code ### Observed Behavior ``` ====================================================================== FAIL: test_graphql_get_object (netbox_myplugin.tests.test_api.MyTestCase) ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/asaharan/PycharmProjects/netbox/venv/lib/python3.10/site-packages/django/test/utils.py", line 460, in inner return func(*args, **kwargs) File "/home/asaharan/PycharmProjects/netbox/venv/lib/python3.10/site-packages/django/test/utils.py", line 460, in inner return func(*args, **kwargs) File "/home/asaharan/PycharmProjects/netbox/netbox/utilities/testing/api.py", line 498, in test_graphql_get_object self.assertHttpStatus(response, status.HTTP_200_OK) File "/home/asaharan/PycharmProjects/netbox/netbox/utilities/testing/base.py", line 71, in assertHttpStatus self.assertEqual(response.status_code, expected_status, err_message) AssertionError: 400 != 200 : Expected HTTP status 200; received 400: b'{"errors":[{"message":"Cannot query field \'myplugin\' on type \'MyPluginType\'.","locations":[{"line":17,"column":9}]}]}' ====================================================================== FAIL: test_graphql_list_objects (netbox_myplugin.tests.test_api.MyTestCase) ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/asaharan/PycharmProjects/netbox/venv/lib/python3.10/site-packages/django/test/utils.py", line 460, in inner return func(*args, **kwargs) File "/home/asaharan/PycharmProjects/netbox/venv/lib/python3.10/site-packages/django/test/utils.py", line 460, in inner return func(*args, **kwargs) File "/home/asaharan/PycharmProjects/netbox/netbox/utilities/testing/api.py", line 523, in test_graphql_list_objects self.assertHttpStatus(response, status.HTTP_200_OK) File "/home/asaharan/PycharmProjects/netbox/netbox/utilities/testing/base.py", line 71, in assertHttpStatus self.assertEqual(response.status_code, expected_status, err_message) AssertionError: 400 != 200 : Expected HTTP status 200; received 400: b'{"errors":[{"message":"Cannot query field \'myplugin\' on type \'MyPluginType\'.","locations":[{"line":17,"column":9}]}]}' ```
adam added the type: bug label 2025-12-29 20:25:55 +01:00
adam closed this issue 2025-12-29 20:25:55 +01:00
Author
Owner

@abhi1693 commented on GitHub (Feb 5, 2023):

Closing this as it was human error. Had to define the fields as model names to make it work

@abhi1693 commented on GitHub (Feb 5, 2023): Closing this as it was human error. Had to define the fields as model names to make it work
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#7602