From 8a53d0e4c0a1a95b2db36695046aceb74907871d Mon Sep 17 00:00:00 2001 From: Michal Cerny Date: Sun, 30 Nov 2025 19:11:38 +0100 Subject: [PATCH 1/5] Bump python version and add trap for local-test.sh --- hacking/local-test.sh | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/hacking/local-test.sh b/hacking/local-test.sh index 642dc30f0..dc532b6ac 100755 --- a/hacking/local-test.sh +++ b/hacking/local-test.sh @@ -1,13 +1,30 @@ #!/usr/bin/env bash # Usage: ./hacking/local-test.sh +set -euo pipefail + +# shellcheck disable=SC2317 +cleanup_tests() { + popd >/dev/null 2>&1 || true + rm -rf "${COLLECTION_TMP_DIR}/ansible_collections" +} + +: "${COLLECTION_TMP_DIR:=.}" # Run build, which will remove previously installed versions ./hacking/build.sh # Install new built version -ansible-galaxy collection install netbox-netbox-*.tar.gz -p . +ansible-galaxy collection install netbox-netbox-*.tar.gz \ + --force \ + --collections-path "$COLLECTION_TMP_DIR" + + +trap cleanup_tests EXIT INT TERM # You can now cd into the installed version and run tests -(cd ansible_collections/netbox/netbox/ && ansible-test units -v --python 3.10 && ansible-test sanity --requirements -v --python 3.10 --skip-test pep8 plugins/) -rm -rf ansible_collections +mkdir -p "${COLLECTION_TMP_DIR}" +pushd "${COLLECTION_TMP_DIR}/ansible_collections/netbox/netbox/" >/dev/null || exit 1 +ansible-test units -v --python 3.13 +ansible-test sanity --requirements -v --python 3.13 --skip-test pep8 plugins/ + From e70e1e824f240922abe5ca956d95442c2ee5c9b3 Mon Sep 17 00:00:00 2001 From: Michal Cerny Date: Sat, 29 Nov 2025 19:42:06 +0100 Subject: [PATCH 2/5] Remove old fixtures choices in unit tests --- .../fixtures/choices/circuits.json | 28 -- .../fixtures/choices/device_types.json | 12 - .../fixtures/choices/devices.json | 42 --- .../fixtures/choices/interfaces.json | 306 ------------------ .../fixtures/choices/prefixes.json | 20 -- .../module_utils/fixtures/choices/racks.json | 66 ---- .../fixtures/choices/services.json | 12 - .../module_utils/fixtures/choices/sites.json | 16 - .../fixtures/choices/virtual_machines.json | 16 - .../module_utils/fixtures/choices/vlans.json | 16 - 10 files changed, 534 deletions(-) delete mode 100644 tests/unit/module_utils/fixtures/choices/circuits.json delete mode 100644 tests/unit/module_utils/fixtures/choices/device_types.json delete mode 100644 tests/unit/module_utils/fixtures/choices/devices.json delete mode 100644 tests/unit/module_utils/fixtures/choices/interfaces.json delete mode 100644 tests/unit/module_utils/fixtures/choices/prefixes.json delete mode 100644 tests/unit/module_utils/fixtures/choices/racks.json delete mode 100644 tests/unit/module_utils/fixtures/choices/services.json delete mode 100644 tests/unit/module_utils/fixtures/choices/sites.json delete mode 100644 tests/unit/module_utils/fixtures/choices/virtual_machines.json delete mode 100644 tests/unit/module_utils/fixtures/choices/vlans.json diff --git a/tests/unit/module_utils/fixtures/choices/circuits.json b/tests/unit/module_utils/fixtures/choices/circuits.json deleted file mode 100644 index e7f112b72..000000000 --- a/tests/unit/module_utils/fixtures/choices/circuits.json +++ /dev/null @@ -1,28 +0,0 @@ -{ - "status": [ - { - "value": "planned", - "display_name": "Planned" - }, - { - "value": "provisioning", - "display_name": "Provisioning" - }, - { - "value": "active", - "display_name": "Active" - }, - { - "value": "offline", - "display_name": "Offline" - }, - { - "value": "deprovisioning", - "display_name": "Deprovisioning" - }, - { - "value": "decommissioned", - "display_name": "Decommissioned" - } - ] -} \ No newline at end of file diff --git a/tests/unit/module_utils/fixtures/choices/device_types.json b/tests/unit/module_utils/fixtures/choices/device_types.json deleted file mode 100644 index e82964eef..000000000 --- a/tests/unit/module_utils/fixtures/choices/device_types.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "subdevice_role": [ - { - "value": "parent", - "display_name": "Parent" - }, - { - "value": "child", - "display_name": "Child" - } - ] -} \ No newline at end of file diff --git a/tests/unit/module_utils/fixtures/choices/devices.json b/tests/unit/module_utils/fixtures/choices/devices.json deleted file mode 100644 index 5bf1f2f39..000000000 --- a/tests/unit/module_utils/fixtures/choices/devices.json +++ /dev/null @@ -1,42 +0,0 @@ -{ - "face": [ - { - "value": "front", - "display_name": "Front" - }, - { - "value": "rear", - "display_name": "Rear" - } - ], - "status": [ - { - "value": "offline", - "display_name": "Offline" - }, - { - "value": "active", - "display_name": "Active" - }, - { - "value": "planned", - "display_name": "Planned" - }, - { - "value": "staged", - "display_name": "Staged" - }, - { - "value": "failed", - "display_name": "Failed" - }, - { - "value": "inventory", - "display_name": "Inventory" - }, - { - "value": "decommissioning", - "display_name": "Decommissioning" - } - ] -} \ No newline at end of file diff --git a/tests/unit/module_utils/fixtures/choices/interfaces.json b/tests/unit/module_utils/fixtures/choices/interfaces.json deleted file mode 100644 index 2bda690ff..000000000 --- a/tests/unit/module_utils/fixtures/choices/interfaces.json +++ /dev/null @@ -1,306 +0,0 @@ -{ - "type": [ - { - "value": "virtual", - "display_name": "Virtual" - }, - { - "value": "lag", - "display_name": "Link Aggregation Group (LAG)" - }, - { - "value": "100base-tx", - "display_name": "100BASE-TX (10/100ME)" - }, - { - "value": "1000base-t", - "display_name": "1000BASE-T (1GE)" - }, - { - "value": "2.5gbase-t", - "display_name": "2.5GBASE-T (2.5GE)" - }, - { - "value": "5gbase-t", - "display_name": "5GBASE-T (5GE)" - }, - { - "value": "10gbase-t", - "display_name": "10GBASE-T (10GE)" - }, - { - "value": "10gbase-cx4", - "display_name": "10GBASE-CX4 (10GE)" - }, - { - "value": "1000base-x-gbic", - "display_name": "GBIC (1GE)" - }, - { - "value": "1000base-x-sfp", - "display_name": "SFP (1GE)" - }, - { - "value": "10gbase-x-sfpp", - "display_name": "SFP+ (10GE)" - }, - { - "value": "10gbase-x-xfp", - "display_name": "XFP (10GE)" - }, - { - "value": "10gbase-x-xenpak", - "display_name": "XENPAK (10GE)" - }, - { - "value": "10gbase-x-x2", - "display_name": "X2 (10GE)" - }, - { - "value": "25gbase-x-sfp28", - "display_name": "SFP28 (25GE)" - }, - { - "value": "40gbase-x-qsfpp", - "display_name": "QSFP+ (40GE)" - }, - { - "value": "50gbase-x-sfp28", - "display_name": "QSFP28 (50GE)" - }, - { - "value": "100gbase-x-cfp", - "display_name": "CFP (100GE)" - }, - { - "value": "100gbase-x-cfp2", - "display_name": "CFP2 (100GE)" - }, - { - "value": "200gbase-x-cfp2", - "display_name": "CFP2 (200GE)" - }, - { - "value": "100gbase-x-cfp4", - "display_name": "CFP4 (100GE)" - }, - { - "value": "100gbase-x-cpak", - "display_name": "Cisco CPAK (100GE)" - }, - { - "value": "100gbase-x-qsfp28", - "display_name": "QSFP28 (100GE)" - }, - { - "value": "200gbase-x-qsfp56", - "display_name": "QSFP56 (200GE)" - }, - { - "value": "400gbase-x-qsfpdd", - "display_name": "QSFP-DD (400GE)" - }, - { - "value": "400gbase-x-osfp", - "display_name": "OSFP (400GE)" - }, - { - "value": "ieee802.11a", - "display_name": "IEEE 802.11a" - }, - { - "value": "ieee802.11g", - "display_name": "IEEE 802.11b/g" - }, - { - "value": "ieee802.11n", - "display_name": "IEEE 802.11n" - }, - { - "value": "ieee802.11ac", - "display_name": "IEEE 802.11ac" - }, - { - "value": "ieee802.11ad", - "display_name": "IEEE 802.11ad" - }, - { - "value": "ieee802.11ax", - "display_name": "IEEE 802.11ax" - }, - { - "value": "gsm", - "display_name": "GSM" - }, - { - "value": "cdma", - "display_name": "CDMA" - }, - { - "value": "lte", - "display_name": "LTE" - }, - { - "value": "sonet-oc3", - "display_name": "OC-3/STM-1" - }, - { - "value": "sonet-oc12", - "display_name": "OC-12/STM-4" - }, - { - "value": "sonet-oc48", - "display_name": "OC-48/STM-16" - }, - { - "value": "sonet-oc192", - "display_name": "OC-192/STM-64" - }, - { - "value": "sonet-oc768", - "display_name": "OC-768/STM-256" - }, - { - "value": "sonet-oc1920", - "display_name": "OC-1920/STM-640" - }, - { - "value": "sonet-oc3840", - "display_name": "OC-3840/STM-1234" - }, - { - "value": "1gfc-sfp", - "display_name": "SFP (1GFC)" - }, - { - "value": "2gfc-sfp", - "display_name": "SFP (2GFC)" - }, - { - "value": "4gfc-sfp", - "display_name": "SFP (4GFC)" - }, - { - "value": "8gfc-sfpp", - "display_name": "SFP+ (8GFC)" - }, - { - "value": "16gfc-sfpp", - "display_name": "SFP+ (16GFC)" - }, - { - "value": "32gfc-sfp28", - "display_name": "SFP28 (32GFC)" - }, - { - "value": "128gfc-sfp28", - "display_name": "QSFP28 (128GFC)" - }, - { - "value": "inifiband-sdr", - "display_name": "SDR (2 Gbps)" - }, - { - "value": "inifiband-ddr", - "display_name": "DDR (4 Gbps)" - }, - { - "value": "inifiband-qdr", - "display_name": "QDR (8 Gbps)" - }, - { - "value": "inifiband-fdr10", - "display_name": "FDR10 (10 Gbps)" - }, - { - "value": "inifiband-fdr", - "display_name": "FDR (13.5 Gbps)" - }, - { - "value": "inifiband-edr", - "display_name": "EDR (25 Gbps)" - }, - { - "value": "inifiband-hdr", - "display_name": "HDR (50 Gbps)" - }, - { - "value": "inifiband-ndr", - "display_name": "NDR (100 Gbps)" - }, - { - "value": "inifiband-xdr", - "display_name": "XDR (250 Gbps)" - }, - { - "value": "t1", - "display_name": "T1 (1.544 Mbps)" - }, - { - "value": "e1", - "display_name": "E1 (2.048 Mbps)" - }, - { - "value": "t3", - "display_name": "T3 (45 Mbps)" - }, - { - "value": "e3", - "display_name": "E3 (34 Mbps)" - }, - { - "value": "cisco-stackwise", - "display_name": "Cisco StackWise" - }, - { - "value": "cisco-stackwise-plus", - "display_name": "Cisco StackWise Plus" - }, - { - "value": "cisco-flexstack", - "display_name": "Cisco FlexStack" - }, - { - "value": "cisco-flexstack-plus", - "display_name": "Cisco FlexStack Plus" - }, - { - "value": "juniper-vcp", - "display_name": "Juniper VCP" - }, - { - "value": "extreme-summitstack", - "display_name": "Extreme SummitStack" - }, - { - "value": "extreme-summitstack-128", - "display_name": "Extreme SummitStack-128" - }, - { - "value": "extreme-summitstack-256", - "display_name": "Extreme SummitStack-256" - }, - { - "value": "extreme-summitstack-512", - "display_name": "Extreme SummitStack-512" - }, - { - "value": "other", - "display_name": "Other" - } - ], - "mode": [ - { - "value": "access", - "display_name": "Access" - }, - { - "value": "tagged", - "display_name": "Tagged" - }, - { - "value": "tagged-all", - "display_name": "Tagged (All)" - } - ] -} \ No newline at end of file diff --git a/tests/unit/module_utils/fixtures/choices/prefixes.json b/tests/unit/module_utils/fixtures/choices/prefixes.json deleted file mode 100644 index 135ce2bba..000000000 --- a/tests/unit/module_utils/fixtures/choices/prefixes.json +++ /dev/null @@ -1,20 +0,0 @@ -{ - "status": [ - { - "value": "container", - "display_name": "Container" - }, - { - "value": "active", - "display_name": "Active" - }, - { - "value": "reserved", - "display_name": "Reserved" - }, - { - "value": "deprecated", - "display_name": "Deprecated" - } - ] -} \ No newline at end of file diff --git a/tests/unit/module_utils/fixtures/choices/racks.json b/tests/unit/module_utils/fixtures/choices/racks.json deleted file mode 100644 index 3068db3da..000000000 --- a/tests/unit/module_utils/fixtures/choices/racks.json +++ /dev/null @@ -1,66 +0,0 @@ -{ - "status": [ - { - "value": "reserved", - "display_name": "Reserved" - }, - { - "value": "available", - "display_name": "Available" - }, - { - "value": "planned", - "display_name": "Planned" - }, - { - "value": "active", - "display_name": "Active" - }, - { - "value": "deprecated", - "display_name": "Deprecated" - } - ], - "type": [ - { - "value": "2-post-frame", - "display_name": "2-post frame" - }, - { - "value": "4-post-frame", - "display_name": "4-post frame" - }, - { - "value": "4-post-cabinet", - "display_name": "4-post cabinet" - }, - { - "value": "wall-frame", - "display_name": "Wall-mounted frame" - }, - { - "value": "wall-cabinet", - "display_name": "Wall-mounted cabinet" - } - ], - "width": [ - { - "value": 19, - "display_name": "19 inches" - }, - { - "value": 23, - "display_name": "23 inches" - } - ], - "outer_unit": [ - { - "value": "mm", - "display_name": "Millimeters" - }, - { - "value": "in", - "display_name": "Inches" - } - ] -} \ No newline at end of file diff --git a/tests/unit/module_utils/fixtures/choices/services.json b/tests/unit/module_utils/fixtures/choices/services.json deleted file mode 100644 index e14147a55..000000000 --- a/tests/unit/module_utils/fixtures/choices/services.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "protocol": [ - { - "value": "tcp", - "display_name": "TCP" - }, - { - "value": "udp", - "display_name": "UDP" - } - ] -} \ No newline at end of file diff --git a/tests/unit/module_utils/fixtures/choices/sites.json b/tests/unit/module_utils/fixtures/choices/sites.json deleted file mode 100644 index 636289373..000000000 --- a/tests/unit/module_utils/fixtures/choices/sites.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "status": [ - { - "value": "active", - "display_name": "Active" - }, - { - "value": "planned", - "display_name": "Planned" - }, - { - "value": "retired", - "display_name": "Retired" - } - ] -} \ No newline at end of file diff --git a/tests/unit/module_utils/fixtures/choices/virtual_machines.json b/tests/unit/module_utils/fixtures/choices/virtual_machines.json deleted file mode 100644 index d4e781e8c..000000000 --- a/tests/unit/module_utils/fixtures/choices/virtual_machines.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "status": [ - { - "value": "active", - "display_name": "Active" - }, - { - "value": "offline", - "display_name": "Offline" - }, - { - "value": "staged", - "display_name": "Staged" - } - ] -} \ No newline at end of file diff --git a/tests/unit/module_utils/fixtures/choices/vlans.json b/tests/unit/module_utils/fixtures/choices/vlans.json deleted file mode 100644 index 1f0e6765f..000000000 --- a/tests/unit/module_utils/fixtures/choices/vlans.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "status": [ - { - "value": "active", - "display_name": "Active" - }, - { - "value": "reserved", - "display_name": "Reserved" - }, - { - "value": "deprecated", - "display_name": "Deprecated" - } - ] -} \ No newline at end of file From 07339b8e163f808500a2baf8eacf47c4a7bee01c Mon Sep 17 00:00:00 2001 From: Michal Cerny Date: Sat, 29 Nov 2025 22:26:30 +0100 Subject: [PATCH 3/5] Rewrite load test data function in unit tests Helper to load test data for unit tests. This function supports loading data files relative to the test file. It works even if the test file is in a subfolder, allowing your test data to be organized in corresponding subfolders alongside the tests. --- tests/test_data.py | 21 ------- tests/unit/helpers/load_data.py | 63 +++++++++++++++++++ tests/unit/inventory/test_nb_inventory.py | 13 ++-- .../module_utils/test_netbox_base_class.py | 13 ++-- 4 files changed, 75 insertions(+), 35 deletions(-) delete mode 100644 tests/test_data.py create mode 100644 tests/unit/helpers/load_data.py diff --git a/tests/test_data.py b/tests/test_data.py deleted file mode 100644 index e1cc73e99..000000000 --- a/tests/test_data.py +++ /dev/null @@ -1,21 +0,0 @@ -# -*- coding: utf-8 -*- -# Copyright: (c) 2019, Bruno Inec (@sweenu) -# Copyright: (c) 2019, Mikhail Yohman (@FragmentedPacket) -# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) - -from __future__ import absolute_import, division, print_function - -__metaclass__ = type - -import json - - -# Load test data from a json file, for a pytest parametrize -def load_test_data(path, test_path): - with open(f"{path}/test_data/{test_path}/data.json", "r") as f: - data = json.loads(f.read()) - tests = [] - for test in data: - tuple_data = tuple(test.values()) - tests.append(tuple_data) - return tests diff --git a/tests/unit/helpers/load_data.py b/tests/unit/helpers/load_data.py new file mode 100644 index 000000000..c6b9d0254 --- /dev/null +++ b/tests/unit/helpers/load_data.py @@ -0,0 +1,63 @@ +# -*- coding: utf-8 -*- +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +""" +Helper functions for loading structured test data for unit tests. + +Provides functions to locate JSON test_data files based on the +location of the test script in tests/unit/ and to return the data +as lists of tuples for use in pytest parameterization. +""" + +import json +from pathlib import Path + +CURRENT_FILE = Path(__file__).resolve() # Current file abs path +UNIT_TESTS_DIR = CURRENT_FILE.parents[1] # project_root/tests/unit +# Relative path from project root to unit tests dir - tests/unit +ROOT_TO_UNIT_TESTS = CURRENT_FILE.parents[1].relative_to(CURRENT_FILE.parents[3]) + + +def load_test_data(script_dir: Path, data_path: str, use_subfold: bool = True) -> list: + """ + Load structured test data for a test file. + + The directory structure is derived from: + tests/unit///... + + Args: + test_script_dir: Directory containing the test file. + data_path: Name of the test_data folder to load. + use_sub: Dive into same subfolders as script_dir in test_data + + Returns: + List of tuples extracted from the JSON file. + """ + try: + # Normal environments: script_dir is resolved under same path tests/unit + units_to_test_file: list = script_dir.relative_to(UNIT_TESTS_DIR).parts + except ValueError: + # Fallback for tox/CI virtual environments where path tests/unit differ + # Find the path fragment "tests/unit" and slice from there + parts = list(script_dir.parts) + try: + idx = parts.index(UNIT_TESTS_DIR.parts[-1]) # project/tests/unit/... + units_to_test_file: list = parts[idx + 1 :] + except ValueError: + # Last-resort fallback: treat as having no subfolder + units_to_test_file = () + + unit_test_dir: str = units_to_test_file[0] if len(units_to_test_file) > 0 else "" + subfolder = Path(*units_to_test_file[1:]) if len(units_to_test_file) > 1 else None + + # Build data.json path + base = ROOT_TO_UNIT_TESTS / unit_test_dir / "test_data" + if subfolder and use_subfold: + base /= subfolder + + data_file = base / data_path / "data.json" + + with data_file.open("r", encoding="utf-8") as f: + data = json.load(f) + + return [tuple(item.values()) for item in data] diff --git a/tests/unit/inventory/test_nb_inventory.py b/tests/unit/inventory/test_nb_inventory.py index 93a59217f..6dbaca8d2 100644 --- a/tests/unit/inventory/test_nb_inventory.py +++ b/tests/unit/inventory/test_nb_inventory.py @@ -6,8 +6,8 @@ __metaclass__ = type -import os from functools import partial +from pathlib import Path from unittest.mock import Mock, call, mock_open, patch import pytest @@ -17,7 +17,9 @@ from ansible_collections.netbox.netbox.plugins.inventory.nb_inventory import ( InventoryModule, ) - from ansible_collections.netbox.netbox.tests.test_data import load_test_data + from ansible_collections.netbox.netbox.tests.unit.helpers.load_data import ( + load_test_data, + ) except ImportError: import sys @@ -27,15 +29,12 @@ sys.path.append("plugins/inventory") sys.path.append("tests") - from test_data import load_test_data + from tests.unit.helpers.load_data import load_test_data -load_relative_test_data = partial( - load_test_data, os.path.dirname(os.path.abspath(__file__)) -) +load_relative_test_data = partial(load_test_data, Path(__file__).resolve().parent) class MockInventory: - def __init__(self): self.variables = {} diff --git a/tests/unit/module_utils/test_netbox_base_class.py b/tests/unit/module_utils/test_netbox_base_class.py index 5d259a277..da73b5c7a 100644 --- a/tests/unit/module_utils/test_netbox_base_class.py +++ b/tests/unit/module_utils/test_netbox_base_class.py @@ -8,10 +8,9 @@ __metaclass__ = type import re -import os from functools import partial +from pathlib import Path from unittest.mock import MagicMock - import pytest try: @@ -21,7 +20,9 @@ from ansible_collections.netbox.netbox.plugins.module_utils.netbox_utils import ( NetboxModule, ) - from ansible_collections.netbox.netbox.tests.test_data import load_test_data + from ansible_collections.netbox.netbox.tests.unit.helpers.load_data import ( + load_test_data, + ) MOCKER_PATCH_PATH = "ansible_collections.netbox.netbox.plugins.module_utils.netbox_utils.NetboxModule" except ImportError: @@ -34,13 +35,11 @@ sys.path.append("tests") from netbox_dcim import NB_DEVICES from netbox_utils import NetboxModule - from test_data import load_test_data + from tests.unit.helpers.load_data import load_test_data MOCKER_PATCH_PATH = "netbox_utils.NetboxModule" -load_relative_test_data = partial( - load_test_data, os.path.dirname(os.path.abspath(__file__)) -) +load_relative_test_data = partial(load_test_data, Path(__file__).resolve().parent) @pytest.fixture From 06ea34d8b3b987bed9c7f42b4575f8d1e4c24c04 Mon Sep 17 00:00:00 2001 From: Michal Cerny Date: Sun, 30 Nov 2025 00:57:26 +0100 Subject: [PATCH 4/5] Move unit module_utils tests to netbox_utils folder --- .../test_netbox_module.py} | 0 .../test_data/{ => netbox_utils}/arg_spec_default/data.json | 0 .../{ => netbox_utils}/build_query_params_child/data.json | 0 .../{ => netbox_utils}/build_query_params_no_child/data.json | 0 .../build_query_params_user_query_params/data.json | 0 .../test_data/{ => netbox_utils}/choices_id/data.json | 0 .../module_utils/test_data/{ => netbox_utils}/find_app/data.json | 0 .../test_data/{ => netbox_utils}/normalize_data/data.json | 0 .../unit/module_utils/test_data/{ => netbox_utils}/slug/data.json | 0 9 files changed, 0 insertions(+), 0 deletions(-) rename tests/unit/module_utils/{test_netbox_base_class.py => netbox_utils/test_netbox_module.py} (100%) rename tests/unit/module_utils/test_data/{ => netbox_utils}/arg_spec_default/data.json (100%) rename tests/unit/module_utils/test_data/{ => netbox_utils}/build_query_params_child/data.json (100%) rename tests/unit/module_utils/test_data/{ => netbox_utils}/build_query_params_no_child/data.json (100%) rename tests/unit/module_utils/test_data/{ => netbox_utils}/build_query_params_user_query_params/data.json (100%) rename tests/unit/module_utils/test_data/{ => netbox_utils}/choices_id/data.json (100%) rename tests/unit/module_utils/test_data/{ => netbox_utils}/find_app/data.json (100%) rename tests/unit/module_utils/test_data/{ => netbox_utils}/normalize_data/data.json (100%) rename tests/unit/module_utils/test_data/{ => netbox_utils}/slug/data.json (100%) diff --git a/tests/unit/module_utils/test_netbox_base_class.py b/tests/unit/module_utils/netbox_utils/test_netbox_module.py similarity index 100% rename from tests/unit/module_utils/test_netbox_base_class.py rename to tests/unit/module_utils/netbox_utils/test_netbox_module.py diff --git a/tests/unit/module_utils/test_data/arg_spec_default/data.json b/tests/unit/module_utils/test_data/netbox_utils/arg_spec_default/data.json similarity index 100% rename from tests/unit/module_utils/test_data/arg_spec_default/data.json rename to tests/unit/module_utils/test_data/netbox_utils/arg_spec_default/data.json diff --git a/tests/unit/module_utils/test_data/build_query_params_child/data.json b/tests/unit/module_utils/test_data/netbox_utils/build_query_params_child/data.json similarity index 100% rename from tests/unit/module_utils/test_data/build_query_params_child/data.json rename to tests/unit/module_utils/test_data/netbox_utils/build_query_params_child/data.json diff --git a/tests/unit/module_utils/test_data/build_query_params_no_child/data.json b/tests/unit/module_utils/test_data/netbox_utils/build_query_params_no_child/data.json similarity index 100% rename from tests/unit/module_utils/test_data/build_query_params_no_child/data.json rename to tests/unit/module_utils/test_data/netbox_utils/build_query_params_no_child/data.json diff --git a/tests/unit/module_utils/test_data/build_query_params_user_query_params/data.json b/tests/unit/module_utils/test_data/netbox_utils/build_query_params_user_query_params/data.json similarity index 100% rename from tests/unit/module_utils/test_data/build_query_params_user_query_params/data.json rename to tests/unit/module_utils/test_data/netbox_utils/build_query_params_user_query_params/data.json diff --git a/tests/unit/module_utils/test_data/choices_id/data.json b/tests/unit/module_utils/test_data/netbox_utils/choices_id/data.json similarity index 100% rename from tests/unit/module_utils/test_data/choices_id/data.json rename to tests/unit/module_utils/test_data/netbox_utils/choices_id/data.json diff --git a/tests/unit/module_utils/test_data/find_app/data.json b/tests/unit/module_utils/test_data/netbox_utils/find_app/data.json similarity index 100% rename from tests/unit/module_utils/test_data/find_app/data.json rename to tests/unit/module_utils/test_data/netbox_utils/find_app/data.json diff --git a/tests/unit/module_utils/test_data/normalize_data/data.json b/tests/unit/module_utils/test_data/netbox_utils/normalize_data/data.json similarity index 100% rename from tests/unit/module_utils/test_data/normalize_data/data.json rename to tests/unit/module_utils/test_data/netbox_utils/normalize_data/data.json diff --git a/tests/unit/module_utils/test_data/slug/data.json b/tests/unit/module_utils/test_data/netbox_utils/slug/data.json similarity index 100% rename from tests/unit/module_utils/test_data/slug/data.json rename to tests/unit/module_utils/test_data/netbox_utils/slug/data.json From a87c45b497549701ddf5af3f526f9ef45d839299 Mon Sep 17 00:00:00 2001 From: Michal Cerny Date: Mon, 1 Dec 2025 17:48:10 +0100 Subject: [PATCH 5/5] Rename unit test data.json files to file_name.json --- changelogs/fragments/unit_test_data.yml | 4 ++++ tests/unit/helpers/load_data.py | 6 +++--- tests/unit/inventory/test_data/data.json | 12 ------------ .../data.json => extract_custom_fields.json} | 0 .../data.json => filter_query_parameters.json} | 0 .../data.json => get_resource_list_chunked.json} | 0 .../data.json => group_extractors.json} | 0 .../{refresh_url/data.json => refresh_url.json} | 0 .../data.json => validate_query_parameter.json} | 0 .../data.json => arg_spec_default.json} | 0 .../data.json => build_query_params_child.json} | 0 .../data.json => build_query_params_no_child.json} | 0 ...son => build_query_params_user_query_params.json} | 0 .../{choices_id/data.json => choices_id.json} | 0 .../{find_app/data.json => find_app.json} | 0 .../data.json => normalize_data.json} | 0 .../netbox_utils/{slug/data.json => slug.json} | 0 17 files changed, 7 insertions(+), 15 deletions(-) create mode 100644 changelogs/fragments/unit_test_data.yml delete mode 100644 tests/unit/inventory/test_data/data.json rename tests/unit/inventory/test_data/{extract_custom_fields/data.json => extract_custom_fields.json} (100%) rename tests/unit/inventory/test_data/{filter_query_parameters/data.json => filter_query_parameters.json} (100%) rename tests/unit/inventory/test_data/{get_resource_list_chunked/data.json => get_resource_list_chunked.json} (100%) rename tests/unit/inventory/test_data/{group_extractors/data.json => group_extractors.json} (100%) rename tests/unit/inventory/test_data/{refresh_url/data.json => refresh_url.json} (100%) rename tests/unit/inventory/test_data/{validate_query_parameter/data.json => validate_query_parameter.json} (100%) rename tests/unit/module_utils/test_data/netbox_utils/{arg_spec_default/data.json => arg_spec_default.json} (100%) rename tests/unit/module_utils/test_data/netbox_utils/{build_query_params_child/data.json => build_query_params_child.json} (100%) rename tests/unit/module_utils/test_data/netbox_utils/{build_query_params_no_child/data.json => build_query_params_no_child.json} (100%) rename tests/unit/module_utils/test_data/netbox_utils/{build_query_params_user_query_params/data.json => build_query_params_user_query_params.json} (100%) rename tests/unit/module_utils/test_data/netbox_utils/{choices_id/data.json => choices_id.json} (100%) rename tests/unit/module_utils/test_data/netbox_utils/{find_app/data.json => find_app.json} (100%) rename tests/unit/module_utils/test_data/netbox_utils/{normalize_data/data.json => normalize_data.json} (100%) rename tests/unit/module_utils/test_data/netbox_utils/{slug/data.json => slug.json} (100%) diff --git a/changelogs/fragments/unit_test_data.yml b/changelogs/fragments/unit_test_data.yml new file mode 100644 index 000000000..17cf75cdd --- /dev/null +++ b/changelogs/fragments/unit_test_data.yml @@ -0,0 +1,4 @@ +--- +minor_changes: + - Make the unit-test data structures more flexible. + - Remove abandoned unit-test data. diff --git a/tests/unit/helpers/load_data.py b/tests/unit/helpers/load_data.py index c6b9d0254..5849cb554 100644 --- a/tests/unit/helpers/load_data.py +++ b/tests/unit/helpers/load_data.py @@ -18,7 +18,7 @@ ROOT_TO_UNIT_TESTS = CURRENT_FILE.parents[1].relative_to(CURRENT_FILE.parents[3]) -def load_test_data(script_dir: Path, data_path: str, use_subfold: bool = True) -> list: +def load_test_data(script_dir: Path, file_name: str, use_subfold: bool = True) -> list: """ Load structured test data for a test file. @@ -27,7 +27,7 @@ def load_test_data(script_dir: Path, data_path: str, use_subfold: bool = True) - Args: test_script_dir: Directory containing the test file. - data_path: Name of the test_data folder to load. + file_name: Name of the test_data folder to load. use_sub: Dive into same subfolders as script_dir in test_data Returns: @@ -55,7 +55,7 @@ def load_test_data(script_dir: Path, data_path: str, use_subfold: bool = True) - if subfolder and use_subfold: base /= subfolder - data_file = base / data_path / "data.json" + data_file = base / f"{file_name}.json" with data_file.open("r", encoding="utf-8") as f: data = json.load(f) diff --git a/tests/unit/inventory/test_data/data.json b/tests/unit/inventory/test_data/data.json deleted file mode 100644 index 41726270e..000000000 --- a/tests/unit/inventory/test_data/data.json +++ /dev/null @@ -1,12 +0,0 @@ -[ - { - "name": "disk1", - "vm_name": "test100-vm", - "size_gb": 100 - }, - { - "name": "disk2", - "vm_name": "test100-vm", - "size_gb": 200 - } -] diff --git a/tests/unit/inventory/test_data/extract_custom_fields/data.json b/tests/unit/inventory/test_data/extract_custom_fields.json similarity index 100% rename from tests/unit/inventory/test_data/extract_custom_fields/data.json rename to tests/unit/inventory/test_data/extract_custom_fields.json diff --git a/tests/unit/inventory/test_data/filter_query_parameters/data.json b/tests/unit/inventory/test_data/filter_query_parameters.json similarity index 100% rename from tests/unit/inventory/test_data/filter_query_parameters/data.json rename to tests/unit/inventory/test_data/filter_query_parameters.json diff --git a/tests/unit/inventory/test_data/get_resource_list_chunked/data.json b/tests/unit/inventory/test_data/get_resource_list_chunked.json similarity index 100% rename from tests/unit/inventory/test_data/get_resource_list_chunked/data.json rename to tests/unit/inventory/test_data/get_resource_list_chunked.json diff --git a/tests/unit/inventory/test_data/group_extractors/data.json b/tests/unit/inventory/test_data/group_extractors.json similarity index 100% rename from tests/unit/inventory/test_data/group_extractors/data.json rename to tests/unit/inventory/test_data/group_extractors.json diff --git a/tests/unit/inventory/test_data/refresh_url/data.json b/tests/unit/inventory/test_data/refresh_url.json similarity index 100% rename from tests/unit/inventory/test_data/refresh_url/data.json rename to tests/unit/inventory/test_data/refresh_url.json diff --git a/tests/unit/inventory/test_data/validate_query_parameter/data.json b/tests/unit/inventory/test_data/validate_query_parameter.json similarity index 100% rename from tests/unit/inventory/test_data/validate_query_parameter/data.json rename to tests/unit/inventory/test_data/validate_query_parameter.json diff --git a/tests/unit/module_utils/test_data/netbox_utils/arg_spec_default/data.json b/tests/unit/module_utils/test_data/netbox_utils/arg_spec_default.json similarity index 100% rename from tests/unit/module_utils/test_data/netbox_utils/arg_spec_default/data.json rename to tests/unit/module_utils/test_data/netbox_utils/arg_spec_default.json diff --git a/tests/unit/module_utils/test_data/netbox_utils/build_query_params_child/data.json b/tests/unit/module_utils/test_data/netbox_utils/build_query_params_child.json similarity index 100% rename from tests/unit/module_utils/test_data/netbox_utils/build_query_params_child/data.json rename to tests/unit/module_utils/test_data/netbox_utils/build_query_params_child.json diff --git a/tests/unit/module_utils/test_data/netbox_utils/build_query_params_no_child/data.json b/tests/unit/module_utils/test_data/netbox_utils/build_query_params_no_child.json similarity index 100% rename from tests/unit/module_utils/test_data/netbox_utils/build_query_params_no_child/data.json rename to tests/unit/module_utils/test_data/netbox_utils/build_query_params_no_child.json diff --git a/tests/unit/module_utils/test_data/netbox_utils/build_query_params_user_query_params/data.json b/tests/unit/module_utils/test_data/netbox_utils/build_query_params_user_query_params.json similarity index 100% rename from tests/unit/module_utils/test_data/netbox_utils/build_query_params_user_query_params/data.json rename to tests/unit/module_utils/test_data/netbox_utils/build_query_params_user_query_params.json diff --git a/tests/unit/module_utils/test_data/netbox_utils/choices_id/data.json b/tests/unit/module_utils/test_data/netbox_utils/choices_id.json similarity index 100% rename from tests/unit/module_utils/test_data/netbox_utils/choices_id/data.json rename to tests/unit/module_utils/test_data/netbox_utils/choices_id.json diff --git a/tests/unit/module_utils/test_data/netbox_utils/find_app/data.json b/tests/unit/module_utils/test_data/netbox_utils/find_app.json similarity index 100% rename from tests/unit/module_utils/test_data/netbox_utils/find_app/data.json rename to tests/unit/module_utils/test_data/netbox_utils/find_app.json diff --git a/tests/unit/module_utils/test_data/netbox_utils/normalize_data/data.json b/tests/unit/module_utils/test_data/netbox_utils/normalize_data.json similarity index 100% rename from tests/unit/module_utils/test_data/netbox_utils/normalize_data/data.json rename to tests/unit/module_utils/test_data/netbox_utils/normalize_data.json diff --git a/tests/unit/module_utils/test_data/netbox_utils/slug/data.json b/tests/unit/module_utils/test_data/netbox_utils/slug.json similarity index 100% rename from tests/unit/module_utils/test_data/netbox_utils/slug/data.json rename to tests/unit/module_utils/test_data/netbox_utils/slug.json