|
3 | 3 | import json
|
4 | 4 | from pathlib import Path
|
5 | 5 | from types import ModuleType
|
6 |
| -from typing import Type |
7 | 6 |
|
8 |
| -import pydantic |
9 | 7 | import pytest
|
10 | 8 | import tomli_w
|
11 | 9 |
|
12 | 10 | from configaroo import Configuration
|
13 |
| - |
14 |
| - |
15 |
| -# Configuration schema |
16 |
| -class StrictSchema(pydantic.BaseModel): |
17 |
| - model_config = pydantic.ConfigDict(extra="forbid") |
18 |
| - |
19 |
| - |
20 |
| -class DeeplyNestedSchema(StrictSchema): |
21 |
| - sea: str |
22 |
| - |
23 |
| - |
24 |
| -class NestedSchema(StrictSchema): |
25 |
| - pie: float |
26 |
| - seven: int |
27 |
| - deep: DeeplyNestedSchema |
28 |
| - |
29 |
| - |
30 |
| -class LogSchema(StrictSchema): |
31 |
| - level: str |
32 |
| - format: str |
33 |
| - |
34 |
| - |
35 |
| -class WithDotSchema(StrictSchema): |
36 |
| - org_num: int = pydantic.Field(alias="org.num") |
37 |
| - |
38 |
| - |
39 |
| -class PathsSchema(StrictSchema): |
40 |
| - relative: Path |
41 |
| - dynamic: Path |
42 |
| - absolute: Path |
43 |
| - directory: Path |
44 |
| - nested: Path |
45 |
| - |
46 |
| - |
47 |
| -class ConfigSchema(StrictSchema): |
48 |
| - number: int |
49 |
| - word: str |
50 |
| - phrase: str |
51 |
| - things: list[str] |
52 |
| - nested: NestedSchema |
53 |
| - log: LogSchema |
54 |
| - with_dot: WithDotSchema |
55 |
| - paths: PathsSchema |
| 11 | +from tests.schema import ConfigSchema |
56 | 12 |
|
57 | 13 |
|
58 | 14 | @pytest.fixture
|
59 |
| -def model() -> Type[ConfigSchema]: |
| 15 | +def model() -> type[ConfigSchema]: |
60 | 16 | """A schema for the test configuration"""
|
61 | 17 | return ConfigSchema
|
62 | 18 |
|
@@ -89,30 +45,32 @@ def base_path() -> Path:
|
89 | 45 |
|
90 | 46 |
|
91 | 47 | @pytest.fixture
|
92 |
| -def toml_path(base_path, config) -> Path: |
| 48 | +def toml_path(base_path: Path, config: Configuration) -> Path: |
93 | 49 | """A path to a TOML file representing the configuration"""
|
94 | 50 | return write_file(base_path / "files" / "config.toml", tomli_w, config)
|
95 | 51 |
|
96 | 52 |
|
97 | 53 | @pytest.fixture
|
98 |
| -def other_toml_path(base_path, config) -> Path: |
| 54 | +def other_toml_path(base_path: Path, config: Configuration) -> Path: |
99 | 55 | """An alternative path to a TOML file representing the configuration"""
|
100 | 56 | return write_file(base_path / "files" / "tomlfile", tomli_w, config)
|
101 | 57 |
|
102 | 58 |
|
103 | 59 | @pytest.fixture
|
104 |
| -def json_path(base_path, config) -> Path: |
| 60 | +def json_path(base_path: Path, config: Configuration) -> Path: |
105 | 61 | """A path to a JSON file representing the configuration"""
|
106 | 62 | return write_file(base_path / "files" / "config.json", json, config, indent=4)
|
107 | 63 |
|
108 | 64 |
|
109 | 65 | @pytest.fixture
|
110 |
| -def other_json_path(base_path, config) -> Path: |
| 66 | +def other_json_path(base_path: Path, config: Configuration) -> Path: |
111 | 67 | """A path to a JSON file representing the configuration"""
|
112 | 68 | return write_file(base_path / "files" / "jsonfile", json, config, indent=4)
|
113 | 69 |
|
114 | 70 |
|
115 |
| -def write_file(path: Path, lib: ModuleType, config: Configuration, **kwargs) -> Path: |
| 71 | +def write_file( |
| 72 | + path: Path, lib: ModuleType, config: Configuration, **kwargs: str | int |
| 73 | +) -> Path: |
116 | 74 | """Write a configuration to file. Return path for convenience"""
|
117 | 75 | path.write_text(lib.dumps(config.to_dict(), **kwargs), encoding="utf-8")
|
118 | 76 | return path
|
0 commit comments