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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
import os
import pathlib
import pytest
from pydantic import BaseModel
from pydantic_extra_types.path import (
ExistingPath,
ResolvedDirectoryPath,
ResolvedExistingPath,
ResolvedFilePath,
ResolvedNewPath,
)
class File(BaseModel):
file: ResolvedFilePath
class Directory(BaseModel):
directory: ResolvedDirectoryPath
class NewPath(BaseModel):
new_path: ResolvedNewPath
class Existing(BaseModel):
existing: ExistingPath
class ResolvedExisting(BaseModel):
resolved_existing: ResolvedExistingPath
@pytest.fixture
def absolute_file_path(tmp_path: pathlib.Path) -> pathlib.Path:
directory = tmp_path / 'test-relative'
directory.mkdir()
file_path = directory / 'test-relative.txt'
file_path.touch()
return file_path
@pytest.fixture
def relative_file_path(absolute_file_path: pathlib.Path) -> pathlib.Path:
cwd = pathlib.Path.cwd()
if os.name == 'nt' and absolute_file_path.anchor != cwd.anchor:
# on windows, cant create relative path when paths are on different drives
return absolute_file_path
return pathlib.Path(os.path.relpath(absolute_file_path, os.getcwd()))
@pytest.fixture
def absolute_directory_path(tmp_path: pathlib.Path) -> pathlib.Path:
directory = tmp_path / 'test-relative'
directory.mkdir()
return directory
@pytest.fixture
def relative_directory_path(absolute_directory_path: pathlib.Path) -> pathlib.Path:
cwd = pathlib.Path.cwd()
if os.name == 'nt' and absolute_directory_path.anchor != cwd.anchor:
# on windows, cant create relative path when paths are on different drives
return absolute_directory_path
return pathlib.Path(os.path.relpath(absolute_directory_path, os.getcwd()))
@pytest.fixture
def absolute_new_path(tmp_path: pathlib.Path) -> pathlib.Path:
return tmp_path / 'test-relative'
@pytest.fixture
def relative_new_path(absolute_new_path: pathlib.Path) -> pathlib.Path:
cwd = pathlib.Path.cwd()
if os.name == 'nt' and absolute_new_path.anchor != cwd.anchor:
# on windows, cant create relative path when paths are on different drives
return absolute_new_path
return pathlib.Path(os.path.relpath(absolute_new_path, os.getcwd()))
def test_relative_file(absolute_file_path: pathlib.Path, relative_file_path: pathlib.Path):
file = File(file=relative_file_path)
assert file.file == absolute_file_path
def test_absolute_file(absolute_file_path: pathlib.Path):
file = File(file=absolute_file_path)
assert file.file == absolute_file_path
def test_relative_directory(absolute_directory_path: pathlib.Path, relative_directory_path: pathlib.Path):
directory = Directory(directory=relative_directory_path)
assert directory.directory == absolute_directory_path
def test_absolute_directory(absolute_directory_path: pathlib.Path):
directory = Directory(directory=absolute_directory_path)
assert directory.directory == absolute_directory_path
def test_relative_new_path(absolute_new_path: pathlib.Path, relative_new_path: pathlib.Path):
new_path = NewPath(new_path=relative_new_path)
assert new_path.new_path == absolute_new_path
def test_absolute_new_path(absolute_new_path: pathlib.Path):
new_path = NewPath(new_path=absolute_new_path)
assert new_path.new_path == absolute_new_path
@pytest.mark.parametrize(
('pass_fixture', 'expect_fixture'),
(
('relative_file_path', 'relative_file_path'),
('absolute_file_path', 'absolute_file_path'),
('relative_directory_path', 'relative_directory_path'),
('absolute_directory_path', 'absolute_directory_path'),
),
)
def test_existing_path(request: pytest.FixtureRequest, pass_fixture: str, expect_fixture: str):
existing = Existing(existing=request.getfixturevalue(pass_fixture))
assert existing.existing == request.getfixturevalue(expect_fixture)
@pytest.mark.parametrize(
('pass_fixture', 'expect_fixture'),
(
('relative_file_path', 'absolute_file_path'),
('absolute_file_path', 'absolute_file_path'),
('relative_directory_path', 'absolute_directory_path'),
('absolute_directory_path', 'absolute_directory_path'),
),
)
def test_resolved_existing_path(request: pytest.FixtureRequest, pass_fixture: str, expect_fixture: str):
resolved_existing = ResolvedExisting(resolved_existing=request.getfixturevalue(pass_fixture))
assert resolved_existing.resolved_existing == request.getfixturevalue(expect_fixture)
|