|
1 | 1 | """Unit tests for QuotaHandlersConfiguration model.""" |
2 | 2 |
|
3 | 3 | # pylint: disable=no-member |
4 | | -from typing import Any |
5 | | - |
6 | 4 | import pytest |
7 | 5 | from pydantic import ValidationError |
8 | 6 | from pytest_subtests import SubTests |
@@ -55,3 +53,173 @@ def test_quota_handlers_configuration(subtests: SubTests) -> None: |
55 | 53 | assert cfg.scheduler.database_reconnection_delay == 60 |
56 | 54 | assert cfg.scheduler.period == 10 |
57 | 55 | assert cfg.enable_token_history |
| 56 | + |
| 57 | + |
| 58 | +def test_quota_handlers_scheduler_configuration(subtests: SubTests) -> None: |
| 59 | + """Test the quota handlers configuration.""" |
| 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