File: test_config.py

package info (click to toggle)
python-maison 2.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 816 kB
  • sloc: python: 1,549; makefile: 9; sh: 5
file content (135 lines) | stat: -rw-r--r-- 3,644 bytes parent folder | download
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
import pathlib
import textwrap

import pytest

from maison import config
from maison import errors
from maison import typedefs


class TestUserConfig:
    def test_str(self):
        cfg = config.UserConfig(package_name="acme")

        assert str(cfg) == "<class 'UserConfig'>"

    def test_values(self, tmp_path: pathlib.Path):
        fp = tmp_path / "pyproject.toml"
        content = textwrap.dedent("""
            [tool.acme]
            hello = true
        """)
        _ = fp.write_text(content)

        cfg = config.UserConfig(package_name="acme", starting_path=tmp_path)

        assert cfg.values == {"hello": True}

    def test_values_setter(self):
        cfg = config.UserConfig(package_name="acme")

        cfg.values = {"hello": True}

        assert cfg.values == {"hello": True}

    def test_discovered_paths(self, tmp_path: pathlib.Path):
        fp = tmp_path / "pyproject.toml"
        content = textwrap.dedent("""
            [tool.acme]
            hello = true
        """)
        _ = fp.write_text(content)

        cfg = config.UserConfig(package_name="acme", starting_path=tmp_path)

        assert cfg.discovered_paths == [fp]

    def test_path_no_sources(self, tmp_path: pathlib.Path):
        cfg = config.UserConfig(package_name="acme", starting_path=tmp_path)

        assert cfg.path is None

    def test_path_with_sources(self, tmp_path: pathlib.Path):
        fp = tmp_path / "pyproject.toml"
        content = textwrap.dedent("""
            [tool.acme]
            hello = true
        """)
        _ = fp.write_text(content)

        cfg = config.UserConfig(package_name="acme", starting_path=tmp_path)

        assert cfg.path == fp

    def test_schema(self):
        class Schema:
            def model_dump(self):
                return {}

        cfg = config.UserConfig(package_name="acme", schema=Schema)

        assert cfg.schema == Schema

        class NewSchema:
            def model_dump(self):
                return {}

        cfg.schema = NewSchema
        assert cfg.schema == NewSchema


class TestValidate:
    def test_no_schema(self):
        cfg = config.UserConfig(package_name="acme")

        with pytest.raises(errors.NoSchemaError):
            _ = cfg.validate()

    def test_validaes_config_without_using_schema_values(self, tmp_path: pathlib.Path):
        fp = tmp_path / "pyproject.toml"
        content = textwrap.dedent("""
            [tool.acme]
            hello = true
        """)

        _ = fp.write_text(content)

        class Schema:
            def __init__(self, *args: object, **kwargs: object) -> None:
                pass

            def model_dump(self) -> typedefs.ConfigValues:
                return {"key": "validated"}

        cfg = config.UserConfig(
            package_name="acme", starting_path=tmp_path, schema=Schema
        )

        values = cfg.validate(use_schema_values=False)

        assert values == {"hello": True}

    def test_validaes_config_with_using_schema_values(self, tmp_path: pathlib.Path):
        fp = tmp_path / "pyproject.toml"
        content = textwrap.dedent("""
            [tool.acme]
            hello = true
        """)

        _ = fp.write_text(content)

        class Schema:
            def __init__(self, *args: object, **kwargs: object) -> None:
                pass

            def model_dump(self) -> typedefs.ConfigValues:
                return {"key": "validated"}

        cfg = config.UserConfig(
            package_name="acme", starting_path=tmp_path, schema=Schema
        )

        values = cfg.validate(use_schema_values=True)

        assert values == {"key": "validated"}