Files
HartOMat/backend/tests/test_asset_library_paths.py
T

68 lines
2.3 KiB
Python

from pathlib import Path
from app.config import settings
from app.domains.materials.library_paths import (
asset_library_dir,
list_asset_library_blends,
resolve_asset_library_blend_path,
)
def test_asset_library_dir_uses_upload_dir(monkeypatch, tmp_path):
monkeypatch.setattr(settings, "upload_dir", str(tmp_path / "uploads"))
assert asset_library_dir() == tmp_path / "uploads" / "asset-libraries"
def test_resolve_asset_library_blend_path_prefers_existing_configured_path(monkeypatch, tmp_path):
upload_dir = tmp_path / "uploads"
library_dir = upload_dir / "asset-libraries"
library_dir.mkdir(parents=True, exist_ok=True)
configured = tmp_path / "external" / "materials.blend"
configured.parent.mkdir(parents=True, exist_ok=True)
configured.write_bytes(b"blend")
monkeypatch.setattr(settings, "upload_dir", str(upload_dir))
resolved = resolve_asset_library_blend_path(
blend_file_path=str(configured),
asset_library_id="ignored",
)
assert resolved == str(configured)
def test_resolve_asset_library_blend_path_falls_back_to_id_named_file(monkeypatch, tmp_path):
upload_dir = tmp_path / "uploads"
library_dir = upload_dir / "asset-libraries"
library_dir.mkdir(parents=True, exist_ok=True)
expected = library_dir / "1234.blend"
expected.write_bytes(b"blend")
monkeypatch.setattr(settings, "upload_dir", str(upload_dir))
resolved = resolve_asset_library_blend_path(
blend_file_path=str(library_dir / "missing.blend"),
asset_library_id="1234",
)
assert resolved == str(expected)
def test_resolve_asset_library_blend_path_falls_back_to_newest_available_file(monkeypatch, tmp_path):
upload_dir = tmp_path / "uploads"
library_dir = upload_dir / "asset-libraries"
library_dir.mkdir(parents=True, exist_ok=True)
older = library_dir / "older.blend"
newer = library_dir / "newer.blend"
older.write_bytes(b"older")
newer.write_bytes(b"newer")
newer.touch()
monkeypatch.setattr(settings, "upload_dir", str(upload_dir))
resolved = resolve_asset_library_blend_path(
blend_file_path=str(library_dir / "missing.blend"),
asset_library_id="missing",
)
assert resolved == str(newer)
assert list_asset_library_blends() == [newer, older]