diff --git a/pynautobot/core/app.py b/pynautobot/core/app.py index a42407c..8d04e4e 100644 --- a/pynautobot/core/app.py +++ b/pynautobot/core/app.py @@ -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", @@ -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", @@ -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 diff --git a/tests/integration/test_extras.py b/tests/integration/test_extras.py new file mode 100644 index 0000000..fe77a96 --- /dev/null +++ b/tests/integration/test_extras.py @@ -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"] diff --git a/tests/test_app.py b/tests/test_app.py index acbc648..95084f7 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -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/" @@ -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): + 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( @@ -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/" @@ -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"])