Skip to content

Commit dbcd9ee

Browse files
authored
Merge pull request #1498 from cernymi/cernymi/unit_tests_data
Refactor unit-test data structure: improve flexibility and remove abandoned data
2 parents d32fa03 + a87c45b commit dbcd9ee

31 files changed

+99
-584
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
minor_changes:
3+
- Make the unit-test data structures more flexible.
4+
- Remove abandoned unit-test data.

hacking/local-test.sh

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,30 @@
11
#!/usr/bin/env bash
22

33
# Usage: ./hacking/local-test.sh
4+
set -euo pipefail
5+
6+
# shellcheck disable=SC2317
7+
cleanup_tests() {
8+
popd >/dev/null 2>&1 || true
9+
rm -rf "${COLLECTION_TMP_DIR}/ansible_collections"
10+
}
11+
12+
: "${COLLECTION_TMP_DIR:=.}"
413

514
# Run build, which will remove previously installed versions
615
./hacking/build.sh
716

817
# Install new built version
9-
ansible-galaxy collection install netbox-netbox-*.tar.gz -p .
18+
ansible-galaxy collection install netbox-netbox-*.tar.gz \
19+
--force \
20+
--collections-path "$COLLECTION_TMP_DIR"
21+
22+
23+
trap cleanup_tests EXIT INT TERM
1024

1125
# You can now cd into the installed version and run tests
12-
(cd ansible_collections/netbox/netbox/ && ansible-test units -v --python 3.10 && ansible-test sanity --requirements -v --python 3.10 --skip-test pep8 plugins/)
13-
rm -rf ansible_collections
26+
mkdir -p "${COLLECTION_TMP_DIR}"
27+
pushd "${COLLECTION_TMP_DIR}/ansible_collections/netbox/netbox/" >/dev/null || exit 1
28+
ansible-test units -v --python 3.13
29+
ansible-test sanity --requirements -v --python 3.13 --skip-test pep8 plugins/
30+

tests/test_data.py

Lines changed: 0 additions & 21 deletions
This file was deleted.

tests/unit/helpers/load_data.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# -*- coding: utf-8 -*-
2+
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
3+
4+
"""
5+
Helper functions for loading structured test data for unit tests.
6+
7+
Provides functions to locate JSON test_data files based on the
8+
location of the test script in tests/unit/ and to return the data
9+
as lists of tuples for use in pytest parameterization.
10+
"""
11+
12+
import json
13+
from pathlib import Path
14+
15+
CURRENT_FILE = Path(__file__).resolve() # Current file abs path
16+
UNIT_TESTS_DIR = CURRENT_FILE.parents[1] # project_root/tests/unit
17+
# Relative path from project root to unit tests dir - tests/unit
18+
ROOT_TO_UNIT_TESTS = CURRENT_FILE.parents[1].relative_to(CURRENT_FILE.parents[3])
19+
20+
21+
def load_test_data(script_dir: Path, file_name: str, use_subfold: bool = True) -> list:
22+
"""
23+
Load structured test data for a test file.
24+
25+
The directory structure is derived from:
26+
tests/unit/<unit_test_dir>/<optional_subfolder>/...
27+
28+
Args:
29+
test_script_dir: Directory containing the test file.
30+
file_name: Name of the test_data folder to load.
31+
use_sub: Dive into same subfolders as script_dir in test_data
32+
33+
Returns:
34+
List of tuples extracted from the JSON file.
35+
"""
36+
try:
37+
# Normal environments: script_dir is resolved under same path tests/unit
38+
units_to_test_file: list = script_dir.relative_to(UNIT_TESTS_DIR).parts
39+
except ValueError:
40+
# Fallback for tox/CI virtual environments where path tests/unit differ
41+
# Find the path fragment "tests/unit" and slice from there
42+
parts = list(script_dir.parts)
43+
try:
44+
idx = parts.index(UNIT_TESTS_DIR.parts[-1]) # project/tests/unit/...
45+
units_to_test_file: list = parts[idx + 1 :]
46+
except ValueError:
47+
# Last-resort fallback: treat as having no subfolder
48+
units_to_test_file = ()
49+
50+
unit_test_dir: str = units_to_test_file[0] if len(units_to_test_file) > 0 else ""
51+
subfolder = Path(*units_to_test_file[1:]) if len(units_to_test_file) > 1 else None
52+
53+
# Build data.json path
54+
base = ROOT_TO_UNIT_TESTS / unit_test_dir / "test_data"
55+
if subfolder and use_subfold:
56+
base /= subfolder
57+
58+
data_file = base / f"{file_name}.json"
59+
60+
with data_file.open("r", encoding="utf-8") as f:
61+
data = json.load(f)
62+
63+
return [tuple(item.values()) for item in data]

tests/unit/inventory/test_data/data.json

Lines changed: 0 additions & 12 deletions
This file was deleted.

tests/unit/inventory/test_data/extract_custom_fields/data.json renamed to tests/unit/inventory/test_data/extract_custom_fields.json

File renamed without changes.

tests/unit/inventory/test_data/filter_query_parameters/data.json renamed to tests/unit/inventory/test_data/filter_query_parameters.json

File renamed without changes.

tests/unit/inventory/test_data/get_resource_list_chunked/data.json renamed to tests/unit/inventory/test_data/get_resource_list_chunked.json

File renamed without changes.

tests/unit/inventory/test_data/group_extractors/data.json renamed to tests/unit/inventory/test_data/group_extractors.json

File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)