Skip to content

Commit 8830f88

Browse files
committed
Test docstrings in quota limiters unit tests
1 parent da713b8 commit 8830f88

4 files changed

Lines changed: 83 additions & 14 deletions

File tree

tests/unit/quota/test_cluster_quota_limiter.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,22 @@
1616
def create_quota_limiter(
1717
name: str, initial_quota: int, quota_limit: int
1818
) -> ClusterQuotaLimiter:
19-
"""Create new quota limiter instance."""
19+
"""Create new quota limiter instance.
20+
21+
Builds and returns a ClusterQuotaLimiter using an in-memory
22+
SQLite configuration for testing.
23+
24+
Parameters:
25+
- name (str): Name assigned to the configured quota limiter.
26+
- initial_quota (int): Initial quota value passed to the
27+
ClusterQuotaLimiter instance.
28+
- quota_limit (int): Initial quota value placed into the limiter
29+
configuration (mapped to the configuration's `initial_quota`).
30+
31+
Returns:
32+
ClusterQuotaLimiter: A configured ClusterQuotaLimiter backed by an
33+
in-memory SQLite database.
34+
"""
2035
configuration = QuotaHandlersConfiguration() # pyright: ignore[reportCallIssue]
2136
configuration.sqlite = SQLiteDatabaseConfiguration(
2237
db_path=":memory:",
@@ -138,7 +153,14 @@ def test_ensure_available_quota() -> None:
138153

139154

140155
def test_ensure_available_quota_no_quota() -> None:
141-
"""Test the ensure_available_quota operation."""
156+
"""Test the ensure_available_quota operation.
157+
158+
Verify ensure_available_quota raises QuotaExceedError when the cluster has zero tokens.
159+
160+
Sets up a limiter with initial quota 0, initializes quota for cluster
161+
"foo", and asserts that calling ensure_available_quota("foo") raises
162+
QuotaExceedError with a message matching "Cluster has no available tokens".
163+
"""
142164
initial_quota = 0
143165
quota_limit = 100
144166

tests/unit/quota/test_connect_pg.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@
1111

1212

1313
def test_connect_pg_when_connection_established(mocker: MockerFixture) -> None:
14-
"""Test the connection to PostgreSQL database."""
14+
"""Test the connection to PostgreSQL database.
15+
16+
Verify that connect_pg returns a non-None connection object when the
17+
underlying psycopg2 connection succeeds.
18+
19+
Patches psycopg2.connect to avoid a real network call and asserts the
20+
returned connection is not None.
21+
"""
1522
# any correct PostgreSQL configuration can be used
1623
configuration = PostgreSQLDatabaseConfiguration(
1724
db="db",
@@ -34,7 +41,15 @@ def test_connect_pg_when_connection_established(mocker: MockerFixture) -> None:
3441

3542

3643
def test_connect_pg_when_connection_error(mocker: MockerFixture) -> None:
37-
"""Test the connection to PostgreSQL database."""
44+
"""Test the connection to PostgreSQL database.
45+
46+
Verifies that connect_pg propagates psycopg2.OperationalError when the
47+
underlying connection attempt fails.
48+
49+
Asserts that calling connect_pg with a valid configuration raises an
50+
OperationalError with the original error message when psycopg2.connect
51+
raises OperationalError.
52+
"""
3853
# any correct PostgreSQL configuration can be used
3954
configuration = PostgreSQLDatabaseConfiguration(
4055
host="foo",

tests/unit/quota/test_quota_limiter_factory.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,14 @@ def test_quota_limiters_no_limiters_sqlite_storage() -> None:
5757

5858

5959
def test_quota_limiters_empty_limiters_pg_storage() -> None:
60-
"""Test the quota limiters creating when no limiters are specified."""
60+
"""Test the quota limiters creating when no limiters are specified.
61+
62+
Verify no quota limiters are created when PostgreSQL storage is configured
63+
but the limiter list is empty.
64+
65+
Asserts that QuotaLimiterFactory.quota_limiters(configuration) returns an
66+
empty collection.
67+
"""
6168
configuration = QuotaHandlersConfiguration() # pyright: ignore[reportCallIssue]
6269
configuration.postgres = PostgreSQLDatabaseConfiguration(
6370
db="test",
@@ -89,7 +96,16 @@ def test_quota_limiters_empty_limiters_sqlite_storage() -> None:
8996
def test_quota_limiters_user_quota_limiter_postgres_storage(
9097
mocker: MockerFixture,
9198
) -> None:
92-
"""Test the quota limiters creating when one limiter is specified."""
99+
"""Test the quota limiters creating when one limiter is specified.
100+
101+
Verify that configuring PostgreSQL storage with a single limiter of type
102+
"user_limiter" produces exactly one UserQuotaLimiter.
103+
104+
Sets up a QuotaHandlersConfiguration with PostgreSQL connection settings
105+
and one QuotaLimiterConfiguration(type="user_limiter"), patches
106+
psycopg2.connect to avoid real DB connections, and asserts the factory
107+
returns a single UserQuotaLimiter instance.
108+
"""
93109
configuration = QuotaHandlersConfiguration() # pyright: ignore[reportCallIssue]
94110
configuration.postgres = PostgreSQLDatabaseConfiguration(
95111
db="test",
@@ -119,7 +135,14 @@ def test_quota_limiters_user_quota_limiter_postgres_storage(
119135

120136

121137
def test_quota_limiters_user_quota_limiter_sqlite_storage() -> None:
122-
"""Test the quota limiters creating when one limiter is specified."""
138+
"""Test the quota limiters creating when one limiter is specified.
139+
140+
Verify that providing a single `user_limiter` configuration with in-memory
141+
SQLite storage produces one UserQuotaLimiter instance.
142+
143+
Asserts that exactly one limiter is returned and that it is an instance of
144+
`UserQuotaLimiter`.
145+
"""
123146
configuration = QuotaHandlersConfiguration() # pyright: ignore[reportCallIssue]
124147
configuration.sqlite = SQLiteDatabaseConfiguration(
125148
db_path=":memory:",

tests/unit/quota/test_user_quota_limiter.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def test_available_quota() -> None:
6464

6565
quota_limiter = create_quota_limiter("foo", initial_quota, quota_limit)
6666

67-
# init quota for given cluster
67+
# init quota for given user
6868
quota_limiter._init_quota()
6969

7070
available_quota = quota_limiter.available_quota("foo")
@@ -78,7 +78,7 @@ def test_consume_tokens() -> None:
7878

7979
quota_limiter = create_quota_limiter("foo", initial_quota, quota_limit)
8080

81-
# init quota for given cluster
81+
# init quota for given user
8282
quota_limiter._init_quota()
8383

8484
available_quota = quota_limiter.available_quota("foo")
@@ -107,7 +107,7 @@ def test_increase_quota() -> None:
107107

108108
quota_limiter = create_quota_limiter("foo", initial_quota, quota_limit)
109109

110-
# init quota for given cluster
110+
# init quota for given user
111111
quota_limiter._init_quota()
112112

113113
available_quota = quota_limiter.available_quota("foo")
@@ -129,20 +129,29 @@ def test_ensure_available_quota() -> None:
129129

130130
quota_limiter = create_quota_limiter("foo", initial_quota, quota_limit)
131131

132-
# init quota for given cluster
132+
# init quota for given user
133133
quota_limiter._init_quota()
134134

135135
quota_limiter.ensure_available_quota("foo")
136136

137137

138138
def test_ensure_available_quota_no_quota() -> None:
139-
"""Test the ensure_available_quota operation."""
139+
"""Test the ensure_available_quota operation.
140+
141+
Verify that ensure_available_quota raises QuotaExceedError when the user's
142+
available quota is zero.
143+
144+
Initializes a limiter with initial_quota set to 0, calls the limiter
145+
initialization routine, and asserts that calling
146+
ensure_available_quota("foo") raises QuotaExceedError with the message
147+
"User foo has no available tokens".
148+
"""
140149
initial_quota = 0
141150
quota_limit = 100
142151

143152
quota_limiter = create_quota_limiter("foo", initial_quota, quota_limit)
144153

145-
# init quota for given cluster
154+
# init quota for given user
146155
quota_limiter._init_quota()
147156

148157
with pytest.raises(QuotaExceedError, match="User foo has no available tokens"):
@@ -156,7 +165,7 @@ def test_revoke_quota() -> None:
156165

157166
quota_limiter = create_quota_limiter("foo", initial_quota, quota_limit)
158167

159-
# init quota for given cluster
168+
# init quota for given user
160169
quota_limiter._init_quota()
161170

162171
available_quota = quota_limiter.available_quota("foo")

0 commit comments

Comments
 (0)