1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
from __future__ import annotations
from pathlib import PurePosixPath, PureWindowsPath
import pytest
from datamodel_code_generator.reference import ModelResolver, get_relative_path
@pytest.mark.parametrize(
("base_path", "target_path", "expected"),
[
("/a/b", "/a/b", "."),
("/a/b", "/a/b/c", "c"),
("/a/b", "/a/b/c/d", "c/d"),
("/a/b/c", "/a/b", ".."),
("/a/b/c/d", "/a/b", "../.."),
("/a/b/c/d", "/a", "../../.."),
("/a/b/c/d", "/a/x/y/z", "../../../x/y/z"),
("/a/b/c/d", "a/x/y/z", "a/x/y/z"),
("/a/b/c/d", "/a/b/e/d", "../../e/d"),
],
)
def test_get_relative_path_posix(base_path: str, target_path: str, expected: str) -> None:
assert PurePosixPath(get_relative_path(PurePosixPath(base_path), PurePosixPath(target_path))) == PurePosixPath(
expected
)
@pytest.mark.parametrize(
("base_path", "target_path", "expected"),
[
("c:/a/b", "c:/a/b", "."),
("c:/a/b", "c:/a/b/c", "c"),
("c:/a/b", "c:/a/b/c/d", "c/d"),
("c:/a/b/c", "c:/a/b", ".."),
("c:/a/b/c/d", "c:/a/b", "../.."),
("c:/a/b/c/d", "c:/a", "../../.."),
("c:/a/b/c/d", "c:/a/x/y/z", "../../../x/y/z"),
("c:/a/b/c/d", "a/x/y/z", "a/x/y/z"),
("c:/a/b/c/d", "c:/a/b/e/d", "../../e/d"),
],
)
def test_get_relative_path_windows(base_path: str, target_path: str, expected: str) -> None:
assert PureWindowsPath(
get_relative_path(PureWindowsPath(base_path), PureWindowsPath(target_path))
) == PureWindowsPath(expected)
def test_model_resolver_add_ref_with_hash() -> None:
model_resolver = ModelResolver()
reference = model_resolver.add_ref("https://json-schema.org/draft/2020-12/meta/core#")
assert reference.original_name == "core"
def test_model_resolver_add_ref_without_hash() -> None:
model_resolver = ModelResolver()
reference = model_resolver.add_ref("meta/core")
assert reference.original_name == "core"
def test_model_resolver_add_ref_unevaluated() -> None:
model_resolver = ModelResolver()
reference = model_resolver.add_ref("meta/unevaluated")
assert reference.original_name == "unevaluated"
|