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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
|
"""Tests for the `Config` classes."""
from pathlib import Path
from typing import Callable
import pytest
from pydantic import BaseModel
from pydantic import ValidationError
from maison.config import UserConfig
from maison.errors import NoSchemaError
class TestUserConfig:
"""Tests for the `UserConfig` class."""
def test_str(self, create_tmp_file: Callable[..., Path]) -> None:
pyproject_path = create_tmp_file(filename="pyproject.toml")
config = UserConfig(package_name="foo", starting_path=pyproject_path)
assert str(config) == "<class 'UserConfig'>"
class TestDictObject:
"""Tests to ensure that the config is accessible as a dict."""
def test_valid_pyproject(self, create_pyproject_toml: Callable[..., Path]) -> None:
"""A valid pyproject is parsed to a dict object."""
pyproject_path = create_pyproject_toml()
config = UserConfig(package_name="foo", starting_path=pyproject_path)
assert config.values == {"bar": "baz"}
class TestSourceFiles:
"""Tests for the `source_files` init argument."""
def test_not_found(self) -> None:
"""Non existent source files are handled."""
config = UserConfig(package_name="foo", source_files=["foo"])
assert config.path is None
assert config.values == {}
def test_unrecognised_file_extension(
self,
create_tmp_file: Callable[..., Path],
) -> None:
"""Unrecognised source file extensions are handled."""
source_path = create_tmp_file(filename="foo.txt")
config = UserConfig(
package_name="foo",
source_files=["foo.txt"],
starting_path=source_path,
)
assert config.path is None
assert config.values == {}
def test_single_valid_toml_source(self, create_toml: Callable[..., Path]) -> None:
"""Toml files other than pyproject.toml files are handled."""
source_path = create_toml(filename="another.toml", content={"bar": "baz"})
config = UserConfig(
package_name="foo",
starting_path=source_path,
source_files=["another.toml"],
)
assert config.path == source_path
assert config.values["bar"] == "baz"
def test_multiple_valid_toml_sources(
self,
create_pyproject_toml: Callable[..., Path],
create_toml: Callable[..., Path],
) -> None:
"""When there are multiple sources, the first one is used"""
source_path_1 = create_toml(filename="another.toml", content={"bar": "baz"})
source_path_2 = create_pyproject_toml(
section_name="oof", content={"rab": "zab"}
)
config = UserConfig(
package_name="foo",
starting_path=source_path_2,
source_files=["another.toml", "pyproject.toml"],
)
assert config.discovered_paths == [source_path_1, source_path_2]
assert config.values["bar"] == "baz"
def test_absolute_path(self, create_tmp_file: Callable[..., Path]) -> None:
"""Source files can be found using absolute paths"""
path = create_tmp_file(filename="acme.ini")
config = UserConfig(
package_name="foo",
source_files=[str(path)],
)
assert config.discovered_paths == [path]
def test_absolute_path_not_exist(
self,
create_pyproject_toml: Callable[..., Path],
) -> None:
"""Non existent absolute paths are handled."""
pyproject_path = create_pyproject_toml()
config = UserConfig(
package_name="foo",
source_files=["~/.config/acme.ini", "pyproject.toml"],
starting_path=pyproject_path,
)
assert config.discovered_paths == [pyproject_path]
class TestIniFiles:
"""Tests for handling x.ini config files."""
def test_valid_ini_file(self, create_tmp_file: Callable[..., Path]) -> None:
ini_file = """
[section 1]
option_1 = value_1
[section 2]
option_2 = value_2
"""
source_path = create_tmp_file(content=ini_file, filename="foo.ini")
config = UserConfig(
package_name="foo",
starting_path=source_path,
source_files=["foo.ini"],
)
assert config.discovered_paths == [source_path]
assert config.values == {
"section 1": {"option_1": "value_1"},
"section 2": {"option_2": "value_2"},
}
class TestValidation:
"""Tests for schema validation."""
def test_no_schema(self) -> None:
config = UserConfig(package_name="acme", starting_path=Path("/"))
assert config.values == {}
with pytest.raises(NoSchemaError):
config.validate()
def test_one_schema_with_valid_config(
self,
create_pyproject_toml: Callable[..., Path],
) -> None:
"""The config is validated with a given schema."""
class Schema(BaseModel):
"""Defines schema."""
bar: str
pyproject_path = create_pyproject_toml()
config = UserConfig(
package_name="foo",
starting_path=pyproject_path,
schema=Schema,
)
config.validate()
assert config.values["bar"] == "baz"
def test_one_schema_injected_at_validation(
self,
create_pyproject_toml: Callable[..., Path],
) -> None:
"""Schemas supplied as an argument are used"""
class Schema(BaseModel):
"""Defines schema."""
bar: str
pyproject_path = create_pyproject_toml()
config = UserConfig(
package_name="foo",
starting_path=pyproject_path,
)
config.validate(schema=Schema)
assert config.values["bar"] == "baz"
def test_use_schema_values(
self,
create_pyproject_toml: Callable[..., Path],
) -> None:
"""Config values can be cast to the validated values."""
class Schema(BaseModel, coerce_numbers_to_str=True):
"""Defines schema."""
bar: str
other: str = "hello"
pyproject_path = create_pyproject_toml(content={"bar": 1})
config = UserConfig(
package_name="foo",
starting_path=pyproject_path,
schema=Schema,
)
config.validate()
assert config.values["bar"] == "1"
assert config.values["other"] == "hello"
def test_not_use_schema_values(
self,
create_pyproject_toml: Callable[..., Path],
) -> None:
"""If `use_schema_values` is set to False then don't use validated values."""
class Schema(BaseModel, coerce_numbers_to_str=True):
"""Defines schema."""
bar: str
other: str = "hello"
pyproject_path = create_pyproject_toml(content={"bar": 1})
config = UserConfig(
package_name="foo",
starting_path=pyproject_path,
schema=Schema,
)
config.validate(use_schema_values=False)
assert config.values["bar"] == 1
assert "other" not in config.values
def test_schema_override(
self,
create_pyproject_toml: Callable[..., Path],
) -> None:
"""Schemas given as an argument are preferred"""
class InitSchema(BaseModel):
"""Defines schema for 1."""
bar: str = "schema_1"
class ArgumentSchema(BaseModel):
"""Defines schema for 2."""
bar: str = "schema_2"
pyproject_path = create_pyproject_toml(content={"baz": "baz"})
config = UserConfig(
package_name="foo",
starting_path=pyproject_path,
schema=InitSchema,
)
config.validate(schema=ArgumentSchema)
assert config.values["bar"] == "schema_2"
def test_invalid_configuration(
self,
create_pyproject_toml: Callable[..., Path],
) -> None:
"""Validation errors are raised when config fails validation."""
class Schema(BaseModel):
"""Defines schema."""
bar: str
pyproject_path = create_pyproject_toml(content={"baz": "baz"})
config = UserConfig(
package_name="foo",
starting_path=pyproject_path,
schema=Schema,
)
with pytest.raises(ValidationError):
config.validate()
def test_setter(self) -> None:
"""Schemas can be set using the setter."""
class Schema(BaseModel):
"""Defines schema."""
config = UserConfig(package_name="foo")
assert config.schema is None
config.schema = Schema
assert config.schema is Schema
class TestMergeConfig:
"""Tests for the merging of multiple config sources."""
def test_no_overwrites(
self,
create_toml: Callable[..., Path],
create_tmp_file: Callable[..., Path],
create_pyproject_toml: Callable[..., Path],
) -> None:
"""Configs without overlapping values are merged."""
config_1_path = create_toml(filename="config.toml", content={"option_1": True})
ini_file = """
[foo]
option_2 = true
"""
config_2_path = create_tmp_file(filename="config.ini", content=ini_file)
pyproject_path = create_pyproject_toml(content={"option_3": True})
config = UserConfig(
package_name="foo",
source_files=[str(config_1_path), str(config_2_path), "pyproject.toml"],
starting_path=pyproject_path,
merge_configs=True,
)
assert config.path == [config_1_path, config_2_path, pyproject_path]
assert config.values == {
"option_1": True,
"foo": {
"option_2": "true",
},
"option_3": True,
}
def test_overwrites(
self,
create_toml: Callable[..., Path],
create_pyproject_toml: Callable[..., Path],
) -> None:
"""Configs with overlapping values are merged."""
config_1_path = create_toml(
filename="config_1.toml", content={"option": "config_1"}
)
config_2_path = create_toml(
filename="config_2.toml", content={"option": "config_2"}
)
pyproject_path = create_pyproject_toml(content={"option": "config_3"})
config = UserConfig(
package_name="foo",
source_files=[str(config_1_path), str(config_2_path), "pyproject.toml"],
starting_path=pyproject_path,
merge_configs=True,
)
assert config.values == {
"option": "config_3",
}
def test_nested(
self,
create_toml: Callable[..., Path],
create_pyproject_toml: Callable[..., Path],
) -> None:
"""Configs with nested overlapping values are deep merged."""
config_1_path = create_toml(
filename="config_1.toml", content={"option": {"nested_1": "config_1"}}
)
config_2_path = create_toml(
filename="config_2.toml", content={"option": {"nested_2": "config_2"}}
)
pyproject_path = create_pyproject_toml(
content={"option": {"nested_2": "config_3"}}
)
config = UserConfig(
package_name="foo",
source_files=[str(config_1_path), str(config_2_path), "pyproject.toml"],
starting_path=pyproject_path,
merge_configs=True,
)
assert config.values == {
"option": {"nested_1": "config_1", "nested_2": "config_3"},
}
|