From 07a574cbb059a49c1c05eb04fe265c500c052f21 Mon Sep 17 00:00:00 2001 From: Johannes Kirschbauer Date: Wed, 28 May 2025 15:06:17 +0200 Subject: [PATCH] Tests(inventoryStore): add fixture, make extensible --- .../clan_lib/persist/inventory_store_test.py | 166 +++++++++--------- 1 file changed, 84 insertions(+), 82 deletions(-) diff --git a/pkgs/clan-cli/clan_lib/persist/inventory_store_test.py b/pkgs/clan-cli/clan_lib/persist/inventory_store_test.py index ee8d09496..85d96e25d 100644 --- a/pkgs/clan-cli/clan_lib/persist/inventory_store_test.py +++ b/pkgs/clan-cli/clan_lib/persist/inventory_store_test.py @@ -4,7 +4,6 @@ import os import shutil import subprocess from pathlib import Path -from tempfile import TemporaryDirectory from typing import Any import pytest @@ -72,101 +71,104 @@ class MockFlake: folder_path = Path(__file__).parent.resolve() -@pytest.mark.with_core -def test_simple_read_write() -> None: - entry_file = "1.nix" +@pytest.fixture +def setup_test_files(tmp_path: Path, request: pytest.FixtureRequest) -> Path: + entry_file = request.param # e.g., "1.nix" inventory_file = entry_file.replace(".nix", ".json") - nix_file = folder_path / f"fixtures/{entry_file}" - json_file = folder_path / f"fixtures/{inventory_file}" - with TemporaryDirectory() as tmp: - shutil.copyfile( - str(nix_file), - str(Path(tmp) / entry_file), - ) - shutil.copyfile( - str(json_file), - str(Path(tmp) / inventory_file), - ) + nix_src = folder_path / "fixtures" / entry_file + json_src = folder_path / "fixtures" / inventory_file - store = InventoryStore( - flake=MockFlake(Path(tmp) / entry_file), - inventory_file_name=inventory_file, - _keys=[], # disable toplevel filtering - ) - store._flake.invalidate_cache() - data: dict = store.read() # type: ignore - assert data == {"foo": "bar", "protected": "protected"} + nix_dst = tmp_path / entry_file + json_dst = tmp_path / inventory_file - set_value_by_path(data, "foo", "foo") # type: ignore - store.write(data, "test", commit=False) # type: ignore - # Default method to access the inventory - assert store.read() == {"foo": "foo", "protected": "protected"} + shutil.copyfile(nix_src, nix_dst) + shutil.copyfile(json_src, json_dst) - # Test the data is actually persisted - assert store._get_persisted() == {"foo": "foo"} - - # clan_lib.errors.ClanError: Key 'protected' is not writeable. - invalid_data = {"protected": "foo"} - with pytest.raises(ClanError) as e: - store.write(invalid_data, "test", commit=False) # type: ignore - assert str(e.value) == "Key 'protected' is not writeable." - - # Test the data is not touched - assert store.read() == data - assert store._get_persisted() == {"foo": "foo"} - - # Remove the foo key from the persisted data - # Technically data = { } should also work - data = {"protected": "protected"} - store.write(data, "test", commit=False) # type: ignore + return tmp_path @pytest.mark.with_core -def test_read_deferred() -> None: - entry_file = "deferred.nix" - inventory_file = entry_file.replace(".nix", ".json") +@pytest.mark.parametrize("setup_test_files", ["1.nix"], indirect=True) +def test_simple_read_write(setup_test_files: Path) -> None: + files = list(setup_test_files.iterdir()) + nix_file = next(f for f in files if f.suffix == ".nix") + json_file = next(f for f in files if f.suffix == ".json") - nix_file = folder_path / f"fixtures/{entry_file}" - json_file = folder_path / f"fixtures/{inventory_file}" - with TemporaryDirectory() as tmp: - shutil.copyfile( - str(nix_file), - str(Path(tmp) / entry_file), - ) - shutil.copyfile( - str(json_file), - str(Path(tmp) / inventory_file), - ) + assert nix_file.exists() + assert json_file.exists() - store = InventoryStore( - flake=MockFlake(Path(tmp) / entry_file), - inventory_file_name=inventory_file, - _allowed_path_transforms=["foo.*"], - _keys=[], # disable toplevel filtering - ) + store = InventoryStore( + flake=MockFlake(nix_file), + inventory_file_name=json_file.name, + _keys=[], # disable toplevel filtering + ) + store._flake.invalidate_cache() + data: dict = store.read() # type: ignore + assert data == {"foo": "bar", "protected": "protected"} - data = store.read() - assert data == {"foo": {"a": {}, "b": {}}} + set_value_by_path(data, "foo", "foo") # type: ignore + store.write(data, "test", commit=False) # type: ignore + # Default method to access the inventory + assert store.read() == {"foo": "foo", "protected": "protected"} - # Create a new "deferredModule" "C" - set_value_by_path(data, "foo.c", {}) - store.write(data, "test", commit=False) # type: ignore + # Test the data is actually persisted + assert store._get_persisted() == {"foo": "foo"} - assert store.read() == {"foo": {"a": {}, "b": {}, "c": {}}} + # clan_lib.errors.ClanError: Key 'protected' is not writeable. + invalid_data = {"protected": "foo"} + with pytest.raises(ClanError) as e: + store.write(invalid_data, "test", commit=False) # type: ignore + assert str(e.value) == "Key 'protected' is not writeable." - # Remove the "deferredModule" "C" - delete_by_path(data, "foo.c") # type: ignore - store.write(data, "test", commit=False) - assert store.read() == {"foo": {"a": {}, "b": {}}} + # Test the data is not touched + assert store.read() == data + assert store._get_persisted() == {"foo": "foo"} - # Write settings into a new "deferredModule" "C" and read them back - set_value_by_path(data, "foo.c", {"timeout": "1s"}) - store.write(data, "test", commit=False) # type: ignore + # Remove the foo key from the persisted data + # Technically data = { } should also work + data = {"protected": "protected"} + store.write(data, "test", commit=False) # type: ignore - assert store.read() == {"foo": {"a": {}, "b": {}, "c": {"timeout": "1s"}}} - # Remove the "deferredModule" "C" along with its settings - delete_by_path(data, "foo.c") # type: ignore - store.write(data, "test", commit=False) - assert store.read() == {"foo": {"a": {}, "b": {}}} +@pytest.mark.with_core +@pytest.mark.parametrize("setup_test_files", ["deferred.nix"], indirect=True) +def test_read_deferred(setup_test_files: Path) -> None: + files = list(setup_test_files.iterdir()) + nix_file = next(f for f in files if f.suffix == ".nix") + json_file = next(f for f in files if f.suffix == ".json") + + assert nix_file.exists() + assert json_file.exists() + + store = InventoryStore( + flake=MockFlake(nix_file), + inventory_file_name=json_file.name, + _allowed_path_transforms=["foo.*"], + _keys=[], # disable toplevel filtering + ) + + data = store.read() + assert data == {"foo": {"a": {}, "b": {}}} + + # Create a new "deferredModule" "C" + set_value_by_path(data, "foo.c", {}) + store.write(data, "test", commit=False) # type: ignore + + assert store.read() == {"foo": {"a": {}, "b": {}, "c": {}}} + + # Remove the "deferredModule" "C" + delete_by_path(data, "foo.c") # type: ignore + store.write(data, "test", commit=False) + assert store.read() == {"foo": {"a": {}, "b": {}}} + + # Write settings into a new "deferredModule" "C" and read them back + set_value_by_path(data, "foo.c", {"timeout": "1s"}) + store.write(data, "test", commit=False) # type: ignore + + assert store.read() == {"foo": {"a": {}, "b": {}, "c": {"timeout": "1s"}}} + + # Remove the "deferredModule" "C" along with its settings + delete_by_path(data, "foo.c") # type: ignore + store.write(data, "test", commit=False) + assert store.read() == {"foo": {"a": {}, "b": {}}}