|
1 | 1 | import json |
2 | 2 |
|
3 | 3 | import pytest |
| 4 | +from django.contrib.auth import get_user_model |
4 | 5 | from pyfakefs.fake_filesystem import FakeFilesystem |
5 | | -from pytest_django.fixtures import SettingsWrapper |
| 6 | +from pytest_django.fixtures import DjangoAssertNumQueries, SettingsWrapper |
| 7 | +from pytest_mock import MockerFixture |
6 | 8 |
|
7 | 9 | from common.core.utils import ( |
8 | 10 | get_file_contents, |
|
13 | 15 | is_enterprise, |
14 | 16 | is_oss, |
15 | 17 | is_saas, |
| 18 | + using_database_replica, |
16 | 19 | ) |
17 | 20 |
|
18 | 21 | pytestmark = pytest.mark.django_db |
@@ -181,3 +184,85 @@ def test_get_version__invalid_file_contents__returns_unknown( |
181 | 184 |
|
182 | 185 | # Then |
183 | 186 | assert result == "unknown" |
| 187 | + |
| 188 | + |
| 189 | +@pytest.mark.django_db(databases="__all__") |
| 190 | +def test_maybe_use_replicas__no_replicas__points_to_default( |
| 191 | + django_assert_num_queries: DjangoAssertNumQueries, |
| 192 | + mocker: MockerFixture, |
| 193 | +) -> None: |
| 194 | + # Given |
| 195 | + mocker.patch("common.core.utils.connections", {"default": {}}) |
| 196 | + manager = get_user_model().objects |
| 197 | + |
| 198 | + # When / Then |
| 199 | + with django_assert_num_queries(1, using="default"): |
| 200 | + using_database_replica(manager).first() |
| 201 | + |
| 202 | + |
| 203 | +@pytest.mark.django_db(databases="__all__") |
| 204 | +def test_maybe_use_replicas__with_replicas__sequential_strategy__picks_databases_sequentially( |
| 205 | + django_assert_num_queries: DjangoAssertNumQueries, |
| 206 | + settings: SettingsWrapper, |
| 207 | +) -> None: |
| 208 | + # Given |
| 209 | + settings.REPLICA_READ_STRATEGY = "sequential" |
| 210 | + manager = get_user_model().objects |
| 211 | + |
| 212 | + # When / Then |
| 213 | + with django_assert_num_queries(1, using="replica_1"): |
| 214 | + using_database_replica(manager).first() |
| 215 | + with django_assert_num_queries(1, using="replica_2"): |
| 216 | + using_database_replica(manager).first() |
| 217 | + with django_assert_num_queries(1, using="replica_3"): |
| 218 | + using_database_replica(manager).first() |
| 219 | + with django_assert_num_queries(1, using="replica_1"): |
| 220 | + using_database_replica(manager).first() |
| 221 | + |
| 222 | + |
| 223 | +@pytest.mark.django_db(databases="__all__") |
| 224 | +def test_maybe_use_replicas__with_replicas__distributed_strategy__picks_databases_randomly( |
| 225 | + django_assert_max_num_queries: DjangoAssertNumQueries, |
| 226 | + settings: SettingsWrapper, |
| 227 | +) -> None: |
| 228 | + # Given |
| 229 | + settings.REPLICA_READ_STRATEGY = "distributed" |
| 230 | + manager = get_user_model().objects |
| 231 | + |
| 232 | + # When / Then |
| 233 | + with django_assert_max_num_queries(10, using="replica_1") as captured: |
| 234 | + using_database_replica(manager).first() |
| 235 | + using_database_replica(manager).first() |
| 236 | + using_database_replica(manager).first() |
| 237 | + using_database_replica(manager).first() |
| 238 | + using_database_replica(manager).first() |
| 239 | + using_database_replica(manager).first() |
| 240 | + using_database_replica(manager).first() |
| 241 | + using_database_replica(manager).first() |
| 242 | + using_database_replica(manager).first() |
| 243 | + using_database_replica(manager).first() |
| 244 | + assert (captured.final_queries or 0) >= 1 |
| 245 | + with django_assert_max_num_queries(10, using="replica_2") as captured: |
| 246 | + using_database_replica(manager).first() |
| 247 | + using_database_replica(manager).first() |
| 248 | + using_database_replica(manager).first() |
| 249 | + using_database_replica(manager).first() |
| 250 | + using_database_replica(manager).first() |
| 251 | + using_database_replica(manager).first() |
| 252 | + using_database_replica(manager).first() |
| 253 | + using_database_replica(manager).first() |
| 254 | + using_database_replica(manager).first() |
| 255 | + using_database_replica(manager).first() |
| 256 | + assert (captured.final_queries or 0) >= 1 |
| 257 | + with django_assert_max_num_queries(10, using="replica_3") as captured: |
| 258 | + using_database_replica(manager).first() |
| 259 | + using_database_replica(manager).first() |
| 260 | + using_database_replica(manager).first() |
| 261 | + using_database_replica(manager).first() |
| 262 | + using_database_replica(manager).first() |
| 263 | + using_database_replica(manager).first() |
| 264 | + using_database_replica(manager).first() |
| 265 | + using_database_replica(manager).first() |
| 266 | + using_database_replica(manager).first() |
| 267 | + using_database_replica(manager).first() |
| 268 | + assert (captured.final_queries or 0) >= 1 |
0 commit comments