Skip to content

Commit 195692e

Browse files
Added more tests
1 parent 475f0e6 commit 195692e

File tree

10 files changed

+376
-8
lines changed

10 files changed

+376
-8
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ pyya = "pyya.__main__:main"
4141
[dependency-groups]
4242
dev = [
4343
"pytest>=8.3.5",
44+
"pytest-cov>=5.0.0",
4445
]
4546

4647
[tool.mypy]

tests/test_common.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
from pathlib import Path
2+
3+
import pytest
4+
5+
import pyya
6+
7+
8+
config_path = Path(__file__).parent / 'testdata/config.yaml'
9+
default_config_path = Path(__file__).parent / 'testdata/default.config.yaml'
10+
config_wrong_ext = Path(__file__).parent / 'testdata/config.txt'
11+
config_corrupted = Path(__file__).parent / 'testdata/corrupted.yaml'
12+
13+
config_extra = Path(__file__).parent / 'testdata/extra.yaml'
14+
default_config_extra = Path(__file__).parent / 'testdata/default_extra.yaml'
15+
16+
17+
def test_raise_err_default_file_not_found() -> None:
18+
with pytest.raises(pyya.PyyaError, match=r'missing.yaml file is missing or empty'):
19+
_ = pyya.init_config(
20+
config=config_path,
21+
default_config='missing.yaml',
22+
)
23+
24+
25+
def test_raise_err_wrong_ext() -> None:
26+
with pytest.raises(pyya.PyyaError, match=r'config.txt file format is not supported'):
27+
_ = pyya.init_config(
28+
config=config_wrong_ext,
29+
default_config=default_config_path,
30+
)
31+
32+
33+
def test_raise_err_default_wrong_ext() -> None:
34+
with pytest.raises(pyya.PyyaError, match=r'config.txt file format is not supported'):
35+
_ = pyya.init_config(
36+
config=config_path,
37+
default_config=config_wrong_ext,
38+
)
39+
40+
41+
def test_raise_err_corrupted() -> None:
42+
with pytest.raises(pyya.PyyaError, match=r'corrupted.yaml file is corrupted'):
43+
_ = pyya.init_config(
44+
config=config_corrupted,
45+
default_config=default_config_path,
46+
)
47+
48+
49+
def test_raise_err_default_corrupted() -> None:
50+
with pytest.raises(pyya.PyyaError, match=r'corrupted.yaml file is corrupted'):
51+
_ = pyya.init_config(
52+
config=config_path,
53+
default_config=config_corrupted,
54+
)
55+
56+
57+
def test_raise_err_sections_ignored_on_merge() -> None:
58+
with pytest.raises(pyya.PyyaError, match=r'Failed parsing `sections_ignored_on_merge`'):
59+
_ = pyya.init_config(
60+
config=config_path,
61+
default_config=default_config_path,
62+
sections_ignored_on_merge=[0], # type: ignore
63+
validate_data_types=False,
64+
replace_dashes_with_underscores=True,
65+
)
66+
67+
68+
def test_no_error_config_not_found() -> None:
69+
_ = pyya.init_config(config='missing.yaml', default_config=default_config_path)
70+
71+
72+
def test_raise_err_extra_sections() -> None:
73+
with pytest.raises(pyya.PyyaError, match=r'Extra inputs are not permitted'):
74+
_ = pyya.init_config(
75+
config=config_extra,
76+
default_config=default_config_extra,
77+
allow_extra_sections=False,
78+
)
79+
80+
81+
def test_raise_err_extra_sections_access() -> None:
82+
with pytest.raises(AttributeError, match=r'garbage'):
83+
config = pyya.init_config(
84+
config=config_extra,
85+
default_config=default_config_extra,
86+
)
87+
_ = config.database.garbage

tests/test_toml.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def test_default_setup() -> None:
2121
assert len(config.cache.servers) == 1
2222

2323

24-
def test_replace_dashes_with_undescores() -> None:
24+
def test_replace_dashes_with_underscores() -> None:
2525
config = pyya.init_config(
2626
config=config_path,
2727
default_config=default_config_path,

tests/test_yaml.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def test_raise_err_non_identifier() -> None:
5252
)
5353

5454

55-
def test_replace_dashes_with_undescores() -> None:
55+
def test_replace_dashes_with_underscores() -> None:
5656
config = pyya.init_config(
5757
config=config_path,
5858
default_config=default_config_path,

tests/testdata/config.txt

Whitespace-only changes.

tests/testdata/config.yaml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ cache:
1717
port: 6379
1818
ttlSeconds: 500
1919

20-
features:
21-
- auth
22-
- metrics
23-
- notifications
24-
2520
admins:
2621
- name: Alice
2722

tests/testdata/corrupted.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
database: "postgres"
2+
host: 0.0.0.0
3+
port: 5432

tests/testdata/default_extra.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
app:
2+
name: myapp
3+
debug: false
4+
port: 8080
5+
class: true
6+
7+
database:
8+
host: localhost
9+
port: 5432
10+
user: myuser
11+
password: mypass
12+
name: mydb
13+
replicas:
14+
- host: db-replica-1.local
15+
port: 5433
16+
- host: db-replica-2.local
17+
port: 5434

tests/testdata/extra.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
app:
2+
name: myapp
3+
debug: false
4+
port: 8080
5+
class: true
6+
7+
database:
8+
host: localhost
9+
port: 5432
10+
user: myuser
11+
password: mypass
12+
name: mydb
13+
replicas:
14+
- host: db-replica-1.local
15+
port: 5433
16+
- host: db-replica-2.local
17+
port: 5434
18+
garbage: true
19+
20+
extra:
21+
garbage: false

uv.lock

Lines changed: 245 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)