Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #141 LTM #145

Merged
merged 1 commit into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions pynautobot/core/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,19 @@ def custom_choices(self):
logger.warning(
"WARNING: The method 'custom_choices()' will be removed in "
"the next major version (2.x) of pynautobot. Please use "
"`custom_fields()` instead."
"`get_custom_fields()` instead."
)

return self.custom_fields()
return self.get_custom_fields()

def custom_fields(self):
def get_custom_fields(self):
"""Returns custom-fields response from app

:Returns: Raw response from Nautobot's custom-fields endpoint.
:Raises: :py:class:`.RequestError` if called for an invalid endpoint.
:Example:

>>> nb.extras.custom_fields()
>>> nb.extras.get_custom_fields()
[
{
"id": "5b39ba88-e5ab-4be2-89f5-5a016473b53c",
Expand All @@ -142,21 +142,20 @@ def custom_fields(self):
},
]
"""
custom_fields = Request(
return Request(
base=f"{self.api.base_url}/{self.name}/custom-fields/",
token=self.api.token,
http_session=self.api.http_session,
).get()
return custom_fields

def custom_field_choices(self):
def get_custom_field_choices(self):
"""Returns custom-field-choices response from app

:Returns: Raw response from Nautobot's custom-field-choices endpoint.
:Raises: :py:class:`.RequestError` if called for an invalid endpoint.
:Example:

>>> nb.extras.custom_field_choices()
>>> nb.extras.get_custom_field_choices()
[
{
"id": "5b39ba88-e5ab-4be2-89f5-5a016473b53c",
Expand All @@ -175,12 +174,11 @@ def custom_field_choices(self):
},
]
"""
custom_fields = Request(
return Request(
base=f"{self.api.base_url}/{self.name}/custom-field-choices/",
token=self.api.token,
http_session=self.api.http_session,
).get()
return custom_fields

def config(self):
"""Returns config response from app
Expand Down
36 changes: 36 additions & 0 deletions tests/integration/test_extras.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import pytest


class TestCustomField:

"""Verify we can create, custom field, and custom field choices."""

@pytest.fixture(scope="session")
def create_custom_field(self, nb_client):
data = {
"name": "test_cf",
"slug": "test_cf",
"content_types": ["dcim.device"],
"type": "select",
"weight": 100,
"filter_logic": "loose",
}
return nb_client.extras.custom_fields.create(**data)

@pytest.fixture(scope="session")
def create_custom_field_choices(self, nb_client, create_custom_field):
data = {
"value": "A",
"field": create_custom_field["id"],
"weight": 100,
}
return nb_client.extras.custom_field_choices.create(**data)

def test_custom_field(self, create_custom_field):
assert create_custom_field["name"] == "test_cf"

def test_custom_field_choice_value(self, create_custom_field_choices):
assert create_custom_field_choices["value"] == "A"

def test_custom_field_choice_to_cf(self, create_custom_field_choices, create_custom_field):
assert create_custom_field_choices["field"]["id"] == create_custom_field["id"]
36 changes: 33 additions & 3 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class AppCustomFieldsTestCase(unittest.TestCase):
@patch("pynautobot.api.version", "1.999")
def test_custom_fields(self, session_get_mock):
api = pynautobot.api(host, **def_kwargs)
cfs = api.extras.custom_fields()
cfs = api.extras.get_custom_fields()

session_get_mock.assert_called_once()
expect_url = f"{api.base_url}/extras/custom-fields/"
Expand All @@ -37,6 +37,25 @@ def test_custom_fields(self, session_get_mock):
self.assertIsInstance(field.get("slug"), str)
self.assertIn("type", field)

@patch(
"requests.sessions.Session.get",
return_value=Response(fixture="extras/custom_fields.json"),
)
@patch("pynautobot.api.version", "1.999")
def test_custom_choices(self, session_get_mock):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this test to test backwards compatibility.

api = pynautobot.api(host, **def_kwargs)
cfs = api.extras.custom_choices()

session_get_mock.assert_called_once()
expect_url = f"{api.base_url}/extras/custom-fields/"
self.assertGreaterEqual(len(session_get_mock.call_args), 2)
url_passed_in_args = expect_url in session_get_mock.call_args[0]
url_passed_in_kwargs = expect_url == session_get_mock.call_args[1].get("url")
self.assertTrue(url_passed_in_args or url_passed_in_kwargs)

self.assertIsInstance(cfs, list)
self.assertEqual(len(cfs), 2)


class AppCustomFieldChoicesTestCase(unittest.TestCase):
@patch(
Expand All @@ -46,7 +65,7 @@ class AppCustomFieldChoicesTestCase(unittest.TestCase):
@patch("pynautobot.api.version", "1.999")
def test_custom_field_choices(self, session_get_mock):
api = pynautobot.api(host, **def_kwargs)
choices = api.extras.custom_field_choices()
choices = api.extras.get_custom_field_choices()

session_get_mock.assert_called_once()
expect_url = f"{api.base_url}/extras/custom-field-choices/"
Expand Down Expand Up @@ -87,7 +106,18 @@ class PluginAppCustomChoicesTestCase(unittest.TestCase):
@patch("pynautobot.api.version", "1.999")
def test_custom_choices(self, *_):
api = pynautobot.api(host, **def_kwargs)
choices = api.plugins.test_plugin.custom_fields()
choices = api.plugins.test_plugin.custom_choices()
self.assertEqual(len(choices), 2)
self.assertEqual(sorted(choices.keys()), ["Testfield1", "Testfield2"])

@patch(
"pynautobot.core.query.Request.get",
return_value={"Testfield1": {"TF1_1": 1, "TF1_2": 2}, "Testfield2": {"TF2_1": 3, "TF2_2": 4}},
)
@patch("pynautobot.api.version", "1.999")
def test_custom_fields(self, *_):
api = pynautobot.api(host, **def_kwargs)
choices = api.plugins.test_plugin.get_custom_fields()
self.assertEqual(len(choices), 2)
self.assertEqual(sorted(choices.keys()), ["Testfield1", "Testfield2"])

Expand Down