mirror of
https://github.com/invoke-ai/InvokeAI
synced 2024-08-30 20:32:17 +00:00
tests: redo config tests
This commit is contained in:
parent
53c8f36029
commit
5606f4d627
@ -1,11 +1,49 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
from tempfile import TemporaryDirectory
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
from omegaconf import OmegaConf
|
||||
from pydantic import ValidationError
|
||||
|
||||
from invokeai.app.services.config.config_default import InvokeAIAppConfig, get_config, load_and_migrate_config
|
||||
|
||||
v4_config = """
|
||||
meta:
|
||||
schema_version: 4
|
||||
|
||||
host: "192.168.1.1"
|
||||
port: 8080
|
||||
"""
|
||||
|
||||
invalid_v5_config = """
|
||||
meta:
|
||||
schema_version: 5
|
||||
|
||||
host: "192.168.1.1"
|
||||
port: 8080
|
||||
"""
|
||||
|
||||
|
||||
v3_config = """
|
||||
InvokeAI:
|
||||
Web Server:
|
||||
host: 192.168.1.1
|
||||
port: 8080
|
||||
Features:
|
||||
esrgan: true
|
||||
internet_available: true
|
||||
log_tokenization: false
|
||||
patchmatch: true
|
||||
ignore_missing_core_models: false
|
||||
Paths:
|
||||
outdir: /some/outputs/dir
|
||||
"""
|
||||
|
||||
invalid_config = """
|
||||
i like turtles
|
||||
"""
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def patch_rootdir(tmp_path: Path, monkeypatch: Any) -> None:
|
||||
@ -13,151 +51,101 @@ def patch_rootdir(tmp_path: Path, monkeypatch: Any) -> None:
|
||||
monkeypatch.setenv("INVOKEAI_ROOT", str(tmp_path))
|
||||
|
||||
|
||||
init1 = OmegaConf.create(
|
||||
"""
|
||||
InvokeAI:
|
||||
Features:
|
||||
always_use_cpu: false
|
||||
Model Cache:
|
||||
convert_cache: 5
|
||||
Generation:
|
||||
force_tiled_decode: false
|
||||
"""
|
||||
)
|
||||
|
||||
init2 = OmegaConf.create(
|
||||
"""
|
||||
InvokeAI:
|
||||
Features:
|
||||
always_use_cpu: true
|
||||
Model Cache:
|
||||
convert_cache: 2
|
||||
Generation:
|
||||
force_tiled_decode: true
|
||||
"""
|
||||
)
|
||||
|
||||
init3 = OmegaConf.create(
|
||||
"""
|
||||
InvokeAI:
|
||||
Generation:
|
||||
sequential_guidance: true
|
||||
attention_type: xformers
|
||||
attention_slice_size: 7
|
||||
forced_tiled_decode: True
|
||||
Device:
|
||||
device: cpu
|
||||
Model Cache:
|
||||
ram: 1.25
|
||||
"""
|
||||
)
|
||||
def test_path_resolution_root_not_set():
|
||||
"""Test path resolutions when the root is not explicitly set."""
|
||||
config = InvokeAIAppConfig()
|
||||
expected_root = InvokeAIAppConfig.find_root()
|
||||
assert config.root_path == expected_root
|
||||
|
||||
|
||||
def test_use_init(patch_rootdir):
|
||||
# note that we explicitly set omegaconf dict and argv here
|
||||
# so that the values aren't read from ~invokeai/invokeai.yaml and
|
||||
# sys.argv respectively.
|
||||
from invokeai.app.services.config import InvokeAIAppConfig
|
||||
def test_read_config_from_file(tmp_path: Path):
|
||||
"""Test reading configuration from a file."""
|
||||
temp_config_file = tmp_path / "temp_invokeai.yaml"
|
||||
temp_config_file.write_text(v4_config)
|
||||
|
||||
conf1 = InvokeAIAppConfig.get_config()
|
||||
assert conf1
|
||||
conf1.parse_args(conf=init1, argv=[])
|
||||
assert not conf1.force_tiled_decode
|
||||
assert conf1.convert_cache == 5
|
||||
assert not conf1.always_use_cpu
|
||||
|
||||
conf2 = InvokeAIAppConfig.get_config()
|
||||
assert conf2
|
||||
conf2.parse_args(conf=init2, argv=[])
|
||||
assert conf2.force_tiled_decode
|
||||
assert conf2.convert_cache == 2
|
||||
assert not hasattr(conf2, "invalid_attribute")
|
||||
config = load_and_migrate_config(temp_config_file)
|
||||
assert config.host == "192.168.1.1"
|
||||
assert config.port == 8080
|
||||
|
||||
|
||||
def test_legacy():
|
||||
from invokeai.app.services.config import InvokeAIAppConfig
|
||||
def test_migrate_v3_config_from_file(tmp_path: Path):
|
||||
"""Test reading configuration from a file."""
|
||||
temp_config_file = tmp_path / "temp_invokeai.yaml"
|
||||
temp_config_file.write_text(v3_config)
|
||||
|
||||
conf = InvokeAIAppConfig.get_config()
|
||||
assert conf
|
||||
conf.parse_args(conf=init3, argv=[])
|
||||
assert conf.xformers_enabled
|
||||
assert conf.device == "cpu"
|
||||
assert conf.use_cpu
|
||||
assert conf.ram == 1.25
|
||||
assert conf.ram_cache_size == 1.25
|
||||
config = load_and_migrate_config(temp_config_file)
|
||||
assert config.outputs_dir == Path("/some/outputs/dir")
|
||||
assert config.host == "192.168.1.1"
|
||||
assert config.port == 8080
|
||||
# This should be stripped out
|
||||
assert not hasattr(config, "esrgan")
|
||||
|
||||
|
||||
def test_argv_override():
|
||||
from invokeai.app.services.config import InvokeAIAppConfig
|
||||
def test_bails_on_invalid_config(tmp_path: Path):
|
||||
"""Test reading configuration from a file."""
|
||||
temp_config_file = tmp_path / "temp_invokeai.yaml"
|
||||
temp_config_file.write_text(invalid_config)
|
||||
|
||||
conf = InvokeAIAppConfig.get_config()
|
||||
conf.parse_args(conf=init1, argv=["--always_use_cpu", "--max_cache=10"])
|
||||
assert conf.always_use_cpu
|
||||
assert conf.max_cache_size == 10
|
||||
assert conf.outdir == Path("outputs") # this is the default
|
||||
with pytest.raises(AssertionError):
|
||||
load_and_migrate_config(temp_config_file)
|
||||
|
||||
|
||||
def test_env_override(patch_rootdir):
|
||||
from invokeai.app.services.config import InvokeAIAppConfig
|
||||
def test_bails_on_config_with_unsupported_version(tmp_path: Path):
|
||||
"""Test reading configuration from a file."""
|
||||
temp_config_file = tmp_path / "temp_invokeai.yaml"
|
||||
temp_config_file.write_text(invalid_v5_config)
|
||||
|
||||
# argv overrides
|
||||
conf = InvokeAIAppConfig()
|
||||
conf.parse_args(conf=init1, argv=["--max_cache=10"])
|
||||
assert conf.always_use_cpu is False
|
||||
os.environ["INVOKEAI_always_use_cpu"] = "True"
|
||||
conf.parse_args(conf=init1, argv=["--max_cache=10"])
|
||||
assert conf.always_use_cpu is True
|
||||
|
||||
# environment variables should be case insensitive
|
||||
os.environ["InvokeAI_Max_Cache_Size"] = "15"
|
||||
conf = InvokeAIAppConfig()
|
||||
conf.parse_args(conf=init1, argv=[])
|
||||
assert conf.max_cache_size == 15
|
||||
|
||||
conf = InvokeAIAppConfig()
|
||||
conf.parse_args(conf=init1, argv=["--no-always_use_cpu", "--max_cache=10"])
|
||||
assert conf.always_use_cpu is False
|
||||
assert conf.max_cache_size == 10
|
||||
|
||||
conf = InvokeAIAppConfig.get_config(max_cache_size=20)
|
||||
conf.parse_args(conf=init1, argv=[])
|
||||
assert conf.max_cache_size == 20
|
||||
|
||||
# make sure that prefix is respected
|
||||
del os.environ["INVOKEAI_always_use_cpu"]
|
||||
os.environ["always_use_cpu"] = "True"
|
||||
conf.parse_args(conf=init1, argv=[])
|
||||
assert conf.always_use_cpu is False
|
||||
with pytest.raises(RuntimeError, match="Invalid schema version"):
|
||||
load_and_migrate_config(temp_config_file)
|
||||
|
||||
|
||||
def test_root_resists_cwd(patch_rootdir):
|
||||
from invokeai.app.services.config import InvokeAIAppConfig
|
||||
def test_write_config_to_file():
|
||||
"""Test writing configuration to a file, checking for correct output."""
|
||||
with TemporaryDirectory() as tmpdir:
|
||||
temp_config_path = Path(tmpdir) / "invokeai.yaml"
|
||||
config = InvokeAIAppConfig(host="192.168.1.1", port=8080)
|
||||
config.set_root(Path(tmpdir))
|
||||
config.write_file(exclude_defaults=False)
|
||||
|
||||
previous = os.environ["INVOKEAI_ROOT"]
|
||||
cwd = Path(os.getcwd()).resolve()
|
||||
|
||||
os.environ["INVOKEAI_ROOT"] = "."
|
||||
conf = InvokeAIAppConfig.get_config()
|
||||
conf.parse_args([])
|
||||
assert conf.root_path == cwd
|
||||
|
||||
os.chdir("..")
|
||||
assert conf.root_path == cwd
|
||||
os.environ["INVOKEAI_ROOT"] = previous
|
||||
os.chdir(cwd)
|
||||
# Load the file and check contents
|
||||
with open(temp_config_path, "r") as file:
|
||||
content = file.read()
|
||||
assert "host: 192.168.1.1" in content
|
||||
assert "port: 8080" in content
|
||||
|
||||
|
||||
def test_type_coercion(patch_rootdir):
|
||||
from invokeai.app.services.config import InvokeAIAppConfig
|
||||
def test_update_config_with_dict():
|
||||
"""Test updating the config with a dictionary."""
|
||||
config = InvokeAIAppConfig()
|
||||
update_dict = {"host": "10.10.10.10", "port": 6060}
|
||||
config.update_config(update_dict)
|
||||
assert config.host == "10.10.10.10"
|
||||
assert config.port == 6060
|
||||
|
||||
conf = InvokeAIAppConfig().get_config()
|
||||
conf.parse_args(argv=["--root=/tmp/foobar"])
|
||||
assert conf.root == Path("/tmp/foobar")
|
||||
assert isinstance(conf.root, Path)
|
||||
conf = InvokeAIAppConfig.get_config(root="/tmp/different")
|
||||
conf.parse_args(argv=["--root=/tmp/foobar"])
|
||||
assert conf.root == Path("/tmp/different")
|
||||
assert isinstance(conf.root, Path)
|
||||
|
||||
def test_update_config_with_object():
|
||||
"""Test updating the config with another config object."""
|
||||
config = InvokeAIAppConfig()
|
||||
new_config = InvokeAIAppConfig(host="10.10.10.10", port=6060)
|
||||
config.update_config(new_config)
|
||||
assert config.host == "10.10.10.10"
|
||||
assert config.port == 6060
|
||||
|
||||
|
||||
def test_set_and_resolve_paths():
|
||||
"""Test setting root and resolving paths based on it."""
|
||||
with TemporaryDirectory() as tmpdir:
|
||||
config = InvokeAIAppConfig()
|
||||
config.set_root(Path(tmpdir))
|
||||
assert config.models_path == Path(tmpdir) / "models"
|
||||
assert config.db_path == Path(tmpdir) / "databases" / "invokeai.db"
|
||||
|
||||
|
||||
def test_singleton_behavior():
|
||||
"""Test that get_config always returns the same instance."""
|
||||
config1 = get_config()
|
||||
config2 = get_config()
|
||||
assert config1 is config2
|
||||
|
||||
|
||||
@pytest.mark.xfail(
|
||||
@ -171,13 +159,11 @@ def test_type_coercion(patch_rootdir):
|
||||
|
||||
This test passes when `test_config.py` is tested in isolation.
|
||||
|
||||
Perhaps a solution would be to call `InvokeAIAppConfig.get_config().parse_args()` in
|
||||
Perhaps a solution would be to call `get_app_config().parse_args()` in
|
||||
other test files?
|
||||
"""
|
||||
)
|
||||
def test_deny_nodes(patch_rootdir):
|
||||
from invokeai.app.services.config import InvokeAIAppConfig
|
||||
|
||||
# Allow integer, string and float, but explicitly deny float
|
||||
allow_deny_nodes_conf = OmegaConf.create(
|
||||
"""
|
||||
@ -192,8 +178,8 @@ def test_deny_nodes(patch_rootdir):
|
||||
"""
|
||||
)
|
||||
# must parse config before importing Graph, so its nodes union uses the config
|
||||
conf = InvokeAIAppConfig().get_config()
|
||||
conf.parse_args(conf=allow_deny_nodes_conf, argv=[])
|
||||
conf = get_config()
|
||||
conf.read_config(conf=allow_deny_nodes_conf, argv=[])
|
||||
from invokeai.app.services.shared.graph import Graph
|
||||
|
||||
# confirm graph validation fails when using denied node
|
||||
|
Loading…
Reference in New Issue
Block a user