Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
"-s",
"tests",
// --no-cov needed so that breakpoints work: https://github.com/microsoft/vscode-python/issues/693
"--no-cov"
"--no-cov",
// Do not use parallel workers in VSCode testing panel.
"-n",
"0"
],
"workbench.editorAssociations": {
"*.ipynb": "jupyter-notebook"
Expand Down
5 changes: 3 additions & 2 deletions docs/source/using_the_ve/configuration/new_config.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ kernelspec:
import tomli_w
import json
from virtual_ecosystem.core.configuration import model_config_to_html, Configuration
from virtual_ecosystem.core.registry import get_model_configuration_class
from IPython.display import display, Markdown, HTML
from importlib import import_module

Expand All @@ -32,8 +33,8 @@ def get_config_class(module_name: str) -> Configuration:
"""

# Get the config class for the module
module = import_module(f"{module_name}.model_config")
config_class = getattr(module, "ModelConfiguration")
short_name = module_name.split(".")[-1]
config_class = get_model_configuration_class(module_name, short_name)

return module_name.split(".")[-1], config_class

Expand Down
6 changes: 5 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ def record_found_in_log(
try:
# Iterate over the record tuples, ignoring the leading element
# giving the logger name
_ = next(msg for msg in caplog.record_tuples if msg[1:] == find)
_ = next(
_
for _, level, message in caplog.record_tuples
if level == find[0] and message.startswith(find[1])
)
return True
except StopIteration:
return False
Expand Down
26 changes: 14 additions & 12 deletions tests/test_pydantic_config.py → tests/core/test_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,42 @@ def test_pydantic(tmp_path):
"""Builds a combined config model from all models and then dumps and reloads it."""

modules = (
"virtual_ecosystem.core",
"virtual_ecosystem.models.abiotic",
"virtual_ecosystem.models.abiotic_simple",
"virtual_ecosystem.models.animal",
"virtual_ecosystem.models.hydrology",
"virtual_ecosystem.models.litter",
"virtual_ecosystem.models.plants",
"virtual_ecosystem.models.soil",
("virtual_ecosystem.core", "CoreConfiguration"),
("virtual_ecosystem.models.abiotic", "AbioticConfiguration"),
("virtual_ecosystem.models.abiotic_simple", "AbioticSimpleConfiguration"),
("virtual_ecosystem.models.animal", "AnimalConfiguration"),
("virtual_ecosystem.models.hydrology", "HydrologyConfiguration"),
("virtual_ecosystem.models.litter", "LitterConfiguration"),
("virtual_ecosystem.models.plants", "PlantsConfiguration"),
("virtual_ecosystem.models.soil", "SoilConfiguration"),
)

submodel_details = {}
for module_name in modules:
for module_name, config_name in modules:
module = import_module(f"{module_name}.model_config")
config_class = getattr(module, "ModelConfiguration")
config_class = getattr(module, config_name)
submodel_details[module_name.split(".")[-1]] = (config_class, config_class())

# Combine
combined = create_model("Config", **submodel_details)

# Dump config to file
with open("config.toml", "wb") as tomlfile:
config_path = tmp_path / "config.toml"
with open(config_path, "wb") as tomlfile:
tomli_w.dump(json.loads(combined().model_dump_json()), tomlfile)

# Reload it - substituting path placeholders for a temporary real file.
tmp_file = tmp_path / "temp_file.txt"
tmp_file.touch()

with open("config.toml") as tomlfile:
with open(config_path) as tomlfile:
content = tomlfile.read()
content = content.replace('"<PLACEHOLDER>"', f"'{tmp_file!s}'")
content_parsed = tomllib.loads(content)
config = combined().model_validate_json(json.dumps(content_parsed))

tmp_file.unlink()
config_path.unlink()

# Very basic check for submodels
for submodel in submodel_details:
Expand Down
Loading