Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions tests/unit/quota/test_quota_exceed_error.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""Unit tests for QuotaExceedError class."""

import pytest

from quota.quota_exceed_error import QuotaExceedError


def test_quota_exceed_error_constructor():
"""Test the QuotaExceedError constructor."""
expected = "User 1234 has 100 tokens, but 1000 tokens are needed"
with pytest.raises(QuotaExceedError, match=expected):
raise QuotaExceedError("1234", "u", 100, 1000)

expected = "Cluster has 100 tokens, but 1000 tokens are needed"
with pytest.raises(QuotaExceedError, match=expected):
raise QuotaExceedError("", "c", 100, 1000)

expected = "Unknown subject 1234 has 100 tokens, but 1000 tokens are needed"
with pytest.raises(QuotaExceedError, match=expected):
raise QuotaExceedError("1234", "?", 100, 1000)

expected = "User 1234 has no available tokens"
with pytest.raises(QuotaExceedError, match=expected):
raise QuotaExceedError("1234", "u", 0, 0)

expected = "Cluster has no available tokens"
with pytest.raises(QuotaExceedError, match=expected):
raise QuotaExceedError("", "c", 0, 0)

expected = "Unknown subject 1234 has no available tokens"
with pytest.raises(QuotaExceedError, match=expected):
raise QuotaExceedError("1234", "?", 0, 0)
204 changes: 204 additions & 0 deletions tests/unit/quota/test_quota_limiter_factory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
"""Unit tests for quota limiter factory class."""

import pytest
from pytest_mock import MockerFixture

from models.config import (
QuotaLimiterConfiguration,
PostgreSQLDatabaseConfiguration,
SQLiteDatabaseConfiguration,
QuotaHandlersConfiguration,
)
from quota.cluster_quota_limiter import ClusterQuotaLimiter
from quota.quota_limiter_factory import QuotaLimiterFactory
from quota.user_quota_limiter import UserQuotaLimiter


def test_quota_limiters_no_storage():
"""Test the quota limiters creating when no storage is configured."""
configuration = QuotaHandlersConfiguration()
configuration.sqlite = None
configuration.postgres = None
configuration.limiters = []
limiters = QuotaLimiterFactory.quota_limiters(configuration)
assert limiters == []


def test_quota_limiters_no_limiters_pg_storage():
"""Test the quota limiters creating when no limiters are specified."""
configuration = QuotaHandlersConfiguration()
configuration.postgres = PostgreSQLDatabaseConfiguration(
db="test", user="user", password="password"
)
configuration.limiters = None
limiters = QuotaLimiterFactory.quota_limiters(configuration)
assert limiters == []


def test_quota_limiters_no_limiters_sqlite_storage():
"""Test the quota limiters creating when no limiters are specified."""
configuration = QuotaHandlersConfiguration()
configuration.sqlite = SQLiteDatabaseConfiguration(
db_path="/foo/bar",
)
configuration.limiters = None
limiters = QuotaLimiterFactory.quota_limiters(configuration)
assert limiters == []


def test_quota_limiters_empty_limiters_pg_storage():
"""Test the quota limiters creating when no limiters are specified."""
configuration = QuotaHandlersConfiguration()
configuration.postgres = PostgreSQLDatabaseConfiguration(
db="test", user="user", password="password"
)
configuration.limiters = []
limiters = QuotaLimiterFactory.quota_limiters(configuration)
assert limiters == []


def test_quota_limiters_empty_limiters_sqlite_storage():
"""Test the quota limiters creating when no limiters are specified."""
configuration = QuotaHandlersConfiguration()
configuration.sqlite = SQLiteDatabaseConfiguration(
db_path="/foo/bar",
)
configuration.limiters = []
limiters = QuotaLimiterFactory.quota_limiters(configuration)
assert limiters == []


def test_quota_limiters_user_quota_limiter_postgres_storage(mocker: MockerFixture):
"""Test the quota limiters creating when one limiter is specified."""
configuration = QuotaHandlersConfiguration()
configuration.postgres = PostgreSQLDatabaseConfiguration(
db="test", user="user", password="password"
)
configuration.limiters = [
QuotaLimiterConfiguration(
type="user_limiter",
name="foo",
initial_quota=100,
quota_increase=1,
period="5 days",
),
]
# do not use connection to real PostgreSQL instance
mocker.patch("psycopg2.connect")
limiters = QuotaLimiterFactory.quota_limiters(configuration)
assert len(limiters) == 1
assert isinstance(limiters[0], UserQuotaLimiter)


def test_quota_limiters_user_quota_limiter_sqlite_storage(mocker: MockerFixture):
"""Test the quota limiters creating when one limiter is specified."""
configuration = QuotaHandlersConfiguration()
configuration.sqlite = SQLiteDatabaseConfiguration(
db_path=":memory:",
)
configuration.limiters = [
QuotaLimiterConfiguration(
type="user_limiter",
name="foo",
initial_quota=100,
quota_increase=1,
period="5 days",
),
]
limiters = QuotaLimiterFactory.quota_limiters(configuration)
assert len(limiters) == 1
assert isinstance(limiters[0], UserQuotaLimiter)


def test_quota_limiters_cluster_quota_limiter_postgres_storage(mocker: MockerFixture):
"""Test the quota limiters creating when one limiter is specified."""
configuration = QuotaHandlersConfiguration()
configuration.postgres = PostgreSQLDatabaseConfiguration(
db="test", user="user", password="password"
)
configuration.limiters = [
QuotaLimiterConfiguration(
type="cluster_limiter",
name="foo",
initial_quota=100,
quota_increase=1,
period="5 days",
),
]
# do not use connection to real PostgreSQL instance
mocker.patch("psycopg2.connect")
limiters = QuotaLimiterFactory.quota_limiters(configuration)
assert len(limiters) == 1
assert isinstance(limiters[0], ClusterQuotaLimiter)


def test_quota_limiters_cluster_quota_limiter_sqlite_storage(mocker: MockerFixture):
"""Test the quota limiters creating when one limiter is specified."""
configuration = QuotaHandlersConfiguration()
configuration.sqlite = SQLiteDatabaseConfiguration(
db_path=":memory:",
)
configuration.limiters = [
QuotaLimiterConfiguration(
type="cluster_limiter",
name="foo",
initial_quota=100,
quota_increase=1,
period="5 days",
),
]
limiters = QuotaLimiterFactory.quota_limiters(configuration)
assert len(limiters) == 1
assert isinstance(limiters[0], ClusterQuotaLimiter)


def test_quota_limiters_two_limiters(mocker: MockerFixture):
"""Test the quota limiters creating when two limiters are specified."""
configuration = QuotaHandlersConfiguration()
configuration.postgres = PostgreSQLDatabaseConfiguration(
db="test", user="user", password="password"
)
configuration.limiters = [
QuotaLimiterConfiguration(
type="user_limiter",
name="foo",
initial_quota=100,
quota_increase=1,
period="5 days",
),
QuotaLimiterConfiguration(
type="cluster_limiter",
name="foo",
initial_quota=100,
quota_increase=1,
period="5 days",
),
]
# do not use connection to real PostgreSQL instance
mocker.patch("psycopg2.connect")
limiters = QuotaLimiterFactory.quota_limiters(configuration)
assert len(limiters) == 2
assert isinstance(limiters[0], UserQuotaLimiter)
assert isinstance(limiters[1], ClusterQuotaLimiter)


def test_quota_limiters_invalid_limiter_type(mocker: MockerFixture):
"""Test the quota limiters creating when invalid limiter type is specified."""
configuration = QuotaHandlersConfiguration()
configuration.postgres = PostgreSQLDatabaseConfiguration(
db="test", user="user", password="password"
)
configuration.limiters = [
QuotaLimiterConfiguration(
type="cluster_limiter",
name="foo",
initial_quota=100,
quota_increase=1,
period="5 days",
),
]
configuration.limiters[0].type = "foo"
# do not use connection to real PostgreSQL instance
mocker.patch("psycopg2.connect")
with pytest.raises(ValueError, match="Invalid limiter type: foo"):
_ = QuotaLimiterFactory.quota_limiters(configuration)
Comment on lines +200 to +204
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Fix error message match - missing period.

The error message match is incorrect. According to the factory implementation in src/quota/quota_limiter_factory.py (line 62), the error message includes a period at the end: f"Invalid limiter type: {limiter_type}." but the test matches without the period.

Apply this diff:

-    with pytest.raises(ValueError, match="Invalid limiter type: foo"):
+    with pytest.raises(ValueError, match="Invalid limiter type: foo."):

Alternatively, use a regex pattern that makes the period optional:

-    with pytest.raises(ValueError, match="Invalid limiter type: foo"):
+    with pytest.raises(ValueError, match=r"Invalid limiter type: foo\.?"):
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
configuration.limiters[0].type = "foo"
# do not use connection to real PostgreSQL instance
mocker.patch("psycopg2.connect")
with pytest.raises(ValueError, match="Invalid limiter type: foo"):
_ = QuotaLimiterFactory.quota_limiters(configuration)
configuration.limiters[0].type = "foo"
# do not use connection to real PostgreSQL instance
mocker.patch("psycopg2.connect")
with pytest.raises(ValueError, match="Invalid limiter type: foo."):
_ = QuotaLimiterFactory.quota_limiters(configuration)
🤖 Prompt for AI Agents
In tests/unit/quota/test_quota_limiter_factory.py around lines 200 to 204, the
pytest.raises match string is missing the trailing period present in the factory
error message; update the test to expect the exact message with the period (e.g.
"Invalid limiter type: foo.") or change the match argument to a regex that
allows an optional trailing period (e.g. make the period optional with \.?), so
the test matches the actual error text emitted by QuotaLimiterFactory.

Loading