Skip to content
Open
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
4 changes: 4 additions & 0 deletions changelogs/fragments/unit_test_data.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
minor_changes:
- Make the unit-test data structures more flexible.
- Remove abandoned unit-test data.
23 changes: 20 additions & 3 deletions hacking/local-test.sh
Original file line number Diff line number Diff line change
@@ -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/

21 changes: 0 additions & 21 deletions tests/test_data.py

This file was deleted.

63 changes: 63 additions & 0 deletions tests/unit/helpers/load_data.py
Original file line number Diff line number Diff line change
@@ -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, file_name: str, use_subfold: bool = True) -> list:
"""
Load structured test data for a test file.

The directory structure is derived from:
tests/unit/<unit_test_dir>/<optional_subfolder>/...

Args:
test_script_dir: Directory containing the test file.
file_name: 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 / f"{file_name}.json"

with data_file.open("r", encoding="utf-8") as f:
data = json.load(f)

return [tuple(item.values()) for item in data]
12 changes: 0 additions & 12 deletions tests/unit/inventory/test_data/data.json

This file was deleted.

13 changes: 6 additions & 7 deletions tests/unit/inventory/test_nb_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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 = {}

Expand Down
28 changes: 0 additions & 28 deletions tests/unit/module_utils/fixtures/choices/circuits.json

This file was deleted.

12 changes: 0 additions & 12 deletions tests/unit/module_utils/fixtures/choices/device_types.json

This file was deleted.

42 changes: 0 additions & 42 deletions tests/unit/module_utils/fixtures/choices/devices.json

This file was deleted.

Loading
Loading