Skip to content

Commit 1ec6b18

Browse files
authored
Merge pull request #1829 from tisnik/lcore-2297-quota-handlers-unit-tests
LCORE-2297: quota handlers unit tests
2 parents bf4bba3 + 8f8a15c commit 1ec6b18

1 file changed

Lines changed: 219 additions & 18 deletions

File tree

Lines changed: 219 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,225 @@
11
"""Unit tests for QuotaHandlersConfiguration model."""
22

3+
# pylint: disable=no-member
4+
import pytest
5+
from pydantic import ValidationError
6+
from pytest_subtests import SubTests
7+
38
from models.config import QuotaHandlersConfiguration, QuotaSchedulerConfiguration
49

510

6-
def test_quota_handlers_configuration() -> None:
11+
def test_quota_handlers_configuration(subtests: SubTests) -> None:
12+
"""Test the quota handlers configuration."""
13+
with subtests.test(msg="Token history disabled"):
14+
cfg = QuotaHandlersConfiguration(
15+
sqlite=None,
16+
postgres=None,
17+
limiters=[],
18+
scheduler=QuotaSchedulerConfiguration(
19+
database_reconnection_count=10,
20+
database_reconnection_delay=60,
21+
period=10,
22+
),
23+
enable_token_history=False,
24+
)
25+
assert cfg is not None
26+
assert cfg.sqlite is None
27+
assert cfg.postgres is None
28+
assert cfg.limiters == []
29+
assert cfg.scheduler is not None
30+
assert cfg.scheduler.database_reconnection_count == 10
31+
assert cfg.scheduler.database_reconnection_delay == 60
32+
assert cfg.scheduler.period == 10
33+
assert not cfg.enable_token_history
34+
35+
with subtests.test(msg="Token history enabled"):
36+
cfg = QuotaHandlersConfiguration(
37+
sqlite=None,
38+
postgres=None,
39+
limiters=[],
40+
scheduler=QuotaSchedulerConfiguration(
41+
database_reconnection_count=10,
42+
database_reconnection_delay=60,
43+
period=10,
44+
),
45+
enable_token_history=True,
46+
)
47+
assert cfg is not None
48+
assert cfg.sqlite is None
49+
assert cfg.postgres is None
50+
assert cfg.limiters == []
51+
assert cfg.scheduler is not None
52+
assert cfg.scheduler.database_reconnection_count == 10
53+
assert cfg.scheduler.database_reconnection_delay == 60
54+
assert cfg.scheduler.period == 10
55+
assert cfg.enable_token_history
56+
57+
58+
def test_quota_handlers_scheduler_configuration(subtests: SubTests) -> None:
759
"""Test the quota handlers configuration."""
8-
cfg = QuotaHandlersConfiguration(
9-
sqlite=None,
10-
postgres=None,
11-
limiters=[],
12-
scheduler=QuotaSchedulerConfiguration(
13-
database_reconnection_count=10,
14-
database_reconnection_delay=60,
15-
period=10,
16-
),
17-
enable_token_history=False,
18-
)
19-
assert cfg is not None
20-
assert cfg.sqlite is None
21-
assert cfg.postgres is None
22-
assert cfg.limiters == []
23-
assert cfg.scheduler is not None
24-
assert not cfg.enable_token_history
60+
with subtests.test(msg="Different reconnection_count"):
61+
cfg = QuotaHandlersConfiguration(
62+
sqlite=None,
63+
postgres=None,
64+
limiters=[],
65+
scheduler=QuotaSchedulerConfiguration(
66+
database_reconnection_count=42,
67+
database_reconnection_delay=60,
68+
period=10,
69+
),
70+
enable_token_history=True,
71+
)
72+
assert cfg is not None
73+
assert cfg.sqlite is None
74+
assert cfg.postgres is None
75+
assert cfg.limiters == []
76+
assert cfg.scheduler is not None
77+
assert cfg.scheduler.database_reconnection_count == 42
78+
assert cfg.scheduler.database_reconnection_delay == 60
79+
assert cfg.scheduler.period == 10
80+
assert cfg.enable_token_history
81+
82+
with subtests.test(msg="Different reconnection_delay"):
83+
cfg = QuotaHandlersConfiguration(
84+
sqlite=None,
85+
postgres=None,
86+
limiters=[],
87+
scheduler=QuotaSchedulerConfiguration(
88+
database_reconnection_count=10,
89+
database_reconnection_delay=42,
90+
period=10,
91+
),
92+
enable_token_history=True,
93+
)
94+
assert cfg is not None
95+
assert cfg.sqlite is None
96+
assert cfg.postgres is None
97+
assert cfg.limiters == []
98+
assert cfg.scheduler is not None
99+
assert cfg.scheduler.database_reconnection_count == 10
100+
assert cfg.scheduler.database_reconnection_delay == 42
101+
assert cfg.scheduler.period == 10
102+
assert cfg.enable_token_history
103+
104+
with subtests.test(msg="Different period"):
105+
cfg = QuotaHandlersConfiguration(
106+
sqlite=None,
107+
postgres=None,
108+
limiters=[],
109+
scheduler=QuotaSchedulerConfiguration(
110+
database_reconnection_count=10,
111+
database_reconnection_delay=60,
112+
period=600,
113+
),
114+
enable_token_history=True,
115+
)
116+
assert cfg is not None
117+
assert cfg.sqlite is None
118+
assert cfg.postgres is None
119+
assert cfg.limiters == []
120+
assert cfg.scheduler is not None
121+
assert cfg.scheduler.database_reconnection_count == 10
122+
assert cfg.scheduler.database_reconnection_delay == 60
123+
assert cfg.scheduler.period == 600
124+
assert cfg.enable_token_history
125+
126+
127+
def test_quota_handlers_configuration_improper_values(subtests: SubTests) -> None:
128+
"""Test the quota handlers configuration."""
129+
with subtests.test(msg="Improper SQLite settings"):
130+
with pytest.raises(
131+
ValidationError,
132+
match="Input should be a valid dictionary or instance of SQLiteDatabaseConfiguration",
133+
):
134+
QuotaHandlersConfiguration(
135+
sqlite="not a proper config", # pyright: ignore[reportArgumentType]
136+
postgres=None,
137+
limiters=[],
138+
scheduler=QuotaSchedulerConfiguration(
139+
database_reconnection_count=10,
140+
database_reconnection_delay=60,
141+
period=10,
142+
),
143+
enable_token_history=False,
144+
)
145+
146+
with subtests.test(msg="Improper PostgreSQL settings"):
147+
with pytest.raises(
148+
ValidationError,
149+
match="should be a valid dictionary or instance of PostgreSQLDatabaseConfiguration",
150+
):
151+
QuotaHandlersConfiguration(
152+
sqlite=None,
153+
postgres="not a proper config", # pyright: ignore[reportArgumentType]
154+
limiters=[],
155+
scheduler=QuotaSchedulerConfiguration(
156+
database_reconnection_count=10,
157+
database_reconnection_delay=60,
158+
period=10,
159+
),
160+
enable_token_history=False,
161+
)
162+
163+
with subtests.test(msg="None limiters settings"):
164+
with pytest.raises(
165+
ValidationError,
166+
match="Input should be a valid list",
167+
):
168+
QuotaHandlersConfiguration(
169+
sqlite=None,
170+
postgres=None,
171+
limiters=None, # pyright: ignore[reportArgumentType]
172+
scheduler=QuotaSchedulerConfiguration(
173+
database_reconnection_count=10,
174+
database_reconnection_delay=60,
175+
period=10,
176+
),
177+
enable_token_history=False,
178+
)
179+
180+
with subtests.test(msg="Improper limiters settings"):
181+
with pytest.raises(
182+
ValidationError,
183+
match="Input should be a valid list",
184+
):
185+
QuotaHandlersConfiguration(
186+
sqlite=None,
187+
postgres=None,
188+
limiters="should be a list of limiters", # pyright: ignore[reportArgumentType]
189+
scheduler=QuotaSchedulerConfiguration(
190+
database_reconnection_count=10,
191+
database_reconnection_delay=60,
192+
period=10,
193+
),
194+
enable_token_history=False,
195+
)
196+
197+
with subtests.test(msg="Improper enable_token_history"):
198+
with pytest.raises(
199+
ValidationError,
200+
match="Input should be a valid boolean",
201+
):
202+
QuotaHandlersConfiguration(
203+
sqlite=None,
204+
postgres=None,
205+
limiters=[],
206+
scheduler=QuotaSchedulerConfiguration(
207+
database_reconnection_count=10,
208+
database_reconnection_delay=60,
209+
period=10,
210+
),
211+
enable_token_history="enabled", # pyright: ignore[reportArgumentType]
212+
)
213+
214+
with subtests.test(msg="Improper scheduler"):
215+
with pytest.raises(
216+
ValidationError,
217+
match="Input should be a valid dictionary or instance of QuotaSchedulerConfiguration",
218+
):
219+
QuotaHandlersConfiguration(
220+
sqlite=None,
221+
postgres=None,
222+
limiters=[],
223+
scheduler="scheduler", # pyright: ignore[reportArgumentType]
224+
enable_token_history=False,
225+
)

0 commit comments

Comments
 (0)