import time
import yaml
from pathlib import Path
from tests.helpers.test_data_generators import *
# Map YAML placeholder strings to generator functions
GENERATORS = {
'$random_rating': random_rating,
'$random_invalid_rating': random_invalid_rating,
'$random_movie_id': pick_random_movie_id,
'$random_network_id': pick_random_network_id,
'$random_review_id': pick_random_review_id,
'$invalid_session_id': lambda: 'a1b2c3d4e5f6g7h8i9j0',
'$random_person_id': lambda: random.randint(1, 14),
'$current_timestamp': lambda: int(time.time())
}
[docs]
def load_test_data(file_name: str, section: str | None = None) -> dict:
"""
Load test data from a YAML file and apply default values.
Reads a YAML file from the same directory as this module and applies
any defined defaults to test case entries. Also processes dynamic
placeholders like '$random_rating' by calling the mapped generator functions.
When ``section`` is given, only that top-level section is kept (plus the
global ``defaults``) before defaults and generators are applied, so the
generators of unrelated sections (e.g. the disk-reading
``$random_movie_id``) are never executed. The return shape is unchanged —
``data[section]`` still works — so callers only add the section name.
:param file_name: Name of the YAML file containing test data.
:param section: Optional top-level section to load exclusively (e.g.
``'get_movie_details'``). If ``None``, the whole file is loaded.
:return: Parsed data with defaults merged into each test case and
``$placeholder`` tokens resolved (scoped to ``section`` if given; the
``defaults`` keys are consumed, not returned). Example return for
``load_test_data("test_data.yaml", "get_movie_details")``::
{
"get_movie_details": {
"valid": [
{"movie_id": 27205, "status_code": 200, "reason": "OK",
"exp_max_elp_secs": 5, "exp_content_type": "application/json",
"exp_get_req_method": "GET"}
],
"invalid": [
{"movie_id": 0, "status_code": 404, "reason": "Not Found",
"expected_message": "Invalid id: The pre-requisite id is invalid or not found.",
"exp_max_elp_secs": 5}
],
}
}
:raises FileNotFoundError: If the specified file does not exist.
:raises KeyError: If ``section`` is given but not present in the file.
:raises yaml.YAMLError: If the file contains invalid YAML.
"""
data_path = Path(__file__).parent / file_name
with open(data_path, 'r') as file:
data = yaml.safe_load(file)
if section is not None:
if section not in data:
available = sorted(k for k in data if k != 'defaults')
raise KeyError(f"Section '{section}' not found in {file_name}. Available: {available}")
# Keep only global defaults + the requested section so defaults are
# still applied but generators elsewhere are never called.
data = {'defaults': data.get('defaults', {}) or {}, section: data[section]}
_apply_defaults(data)
_process_generators(data)
return data
def _apply_defaults(data: dict) -> None:
"""
Apply default values to test data sections in-place.
Iterates through each section in the data dictionary. If a section
contains a 'defaults' key, those default values are applied to each
test case in the corresponding category using setdefault (existing
values are not overwritten).
Global defaults (top-level 'defaults' key) are applied to ALL test cases
across all sections.
Expected YAML structure:
defaults: # Global defaults applied to all test cases
key: default_value
section_name:
defaults: # Section-specific defaults
category_name:
key: default_value
category_name:
- test_case_1
- test_case_2
:param data: Dictionary of test data to modify. Modified in-place.
:return: None. The input dictionary is modified directly.
"""
if not isinstance(data, dict):
return
# Extract global defaults
global_defaults = data.pop('defaults', {}) or {}
for section, content in data.items():
if not isinstance(content, dict):
continue # Skip non-dict values
defaults = content.pop('defaults', None)
# Apply defaults to each category in the section
for category, test_cases in content.items():
if not isinstance(test_cases, list):
continue
# Get category-specific defaults
category_defaults = {}
if isinstance(defaults, dict) and category in defaults:
category_defaults = defaults[category]
for test_case in test_cases:
if isinstance(test_case, dict):
# Apply category-specific defaults first so they win over
# global ones for shared keys (setdefault won't overwrite),
# then let global defaults fill any remaining gaps.
for k, v in category_defaults.items():
test_case.setdefault(k, v)
for k, v in global_defaults.items():
test_case.setdefault(k, v)
def _process_generators(data: dict) -> None:
"""
Process generator placeholders in test data.
Recursively searches for string values matching generator placeholders
(e.g., '$random_rating') and replaces them with generated values.
:param data: Dictionary of test data to modify. Modified in-place.
:return: None. The input dictionary is modified directly.
"""
if isinstance(data, dict):
for key, value in data.items():
if isinstance(value, str) and value in GENERATORS:
data[key] = GENERATORS[value]()
elif isinstance(value, dict):
_process_generators(value)
elif isinstance(value, list):
_process_generators_list(value)
elif isinstance(data, list):
_process_generators_list(data)
def _process_generators_list(data: list) -> None:
"""
Process generator placeholders in a list.
:param data: List to process. Modified in-place.
"""
for i, item in enumerate(data):
if isinstance(item, str) and item in GENERATORS:
data[i] = GENERATORS[item]()
elif isinstance(item, dict):
_process_generators(item)
elif isinstance(item, list):
_process_generators_list(item)