-
Notifications
You must be signed in to change notification settings - Fork 55
LCORE-741: unit tests for quota limiter factory #706
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
tisnik
merged 2 commits into
lightspeed-core:main
from
tisnik:lcore-741-unit-tests-for-quota-limiter-factory
Oct 22, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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:
Alternatively, use a regex pattern that makes the period optional:
📝 Committable suggestion
🤖 Prompt for AI Agents