Skip to content

Commit

Permalink
BAI-1502 improve dependencies tests helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
PE39806 committed Nov 18, 2024
1 parent 4c52676 commit 75414bf
Showing 1 changed file with 37 additions and 7 deletions.
44 changes: 37 additions & 7 deletions lib/modelscan_api/bailo_modelscan_api/test_dependencies.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
"""Test for the main.py file.
"""Test for the dependencies.py file.
"""

from __future__ import annotations

import itertools
from pathlib import Path
from typing import Any, Iterable

import pytest

Expand All @@ -12,19 +15,46 @@
# Helpers


def string_path_matrix(path1, path2):
# List of pairs of paths, as a str and Path representation of each.
return itertools.product(*[[str(x), Path(x)] for x in [path1, path2]])
def type_matrix(data: Iterable[Any], types: Iterable[type]) -> itertools.product[tuple[Any, ...]]:
"""Generate a matrix of all combinations of `data` converted to each type in `types`.
For example:
`list(type_matrix(["foo", "bar"], [str, Path])) -> [(str(foo), str(bar)), (str(foo), Path(bar)), (Path(foo), str(bar)), (Path(foo), Path(bar))]`
:param data: The data to be converted.
:param types: The types to convert each item of data to.
:return: The resulting matrix of combinations.
"""
return itertools.product(*[[t(d) for t in types] for d in data])


def string_path_matrix(path1: str | Path, path2: str | Path) -> itertools.product[tuple[str, Path]]:
"""Wrap type_matrix for convenience with str and Path types.
:param path1: A path to process.
:param path2: Another path to process.
:return: The matrix of both paths with types str and Path.
"""
return type_matrix([path1, path2], [str, Path])

def helper_test_safe_join(path1, path2, output):
# check expected output given 2 inputs

def helper_test_safe_join(path1: str | Path, path2: str | Path, output: Path) -> None:
"""Helper method for testing that all str and Path representations of the two paths will match the given output when joined.
:param path1: Directory part of the final path.
:param path2: Filename part of the final path.
:param output: Expected final path value.
"""
for test_dir, test_file in string_path_matrix(path1, path2):
res = safe_join(test_dir, test_file)
assert res == output


def helper_test_safe_join_catch(path1, path2):
def helper_test_safe_join_catch(path1: str | Path, path2: str | Path) -> None:
"""Helper method for testing that all str and Path representation of the two paths will throw an error when joined.
:param path1: Directory part of the final path.
:param path2: Filename part of the final path.
"""
# check error thrown given two inputs
for test_dir, test_file in string_path_matrix(path1, path2):
with pytest.raises(ValueError):
Expand Down

0 comments on commit 75414bf

Please sign in to comment.