|
16 | 16 | import redis |
17 | 17 | import redis.asyncio as async_redis |
18 | 18 |
|
19 | | -from sql_redis.executor import AsyncExecutor, Executor |
| 19 | +from sql_redis.executor import ( |
| 20 | + AsyncExecutor, |
| 21 | + Executor, |
| 22 | + create_async_executor, |
| 23 | + create_executor, |
| 24 | +) |
20 | 25 | from sql_redis.schema import AsyncSchemaRegistry, SchemaRegistry |
21 | 26 |
|
22 | 27 | # --------------------------------------------------------------------------- |
@@ -322,6 +327,42 @@ def test_executor_multiple_queries_reuse_cache( |
322 | 327 | assert schema_after_first is schema_after_second |
323 | 328 |
|
324 | 329 |
|
| 330 | +class TestExecutorFactories: |
| 331 | + """Factory helpers configure schema loading strategies correctly.""" |
| 332 | + |
| 333 | + def test_create_executor_defaults_to_lazy( |
| 334 | + self, redis_client: redis.Redis, caching_indexes: list[str] |
| 335 | + ): |
| 336 | + """Default factory behavior should not preload schemas.""" |
| 337 | + executor = create_executor(redis_client) |
| 338 | + |
| 339 | + assert isinstance(executor, Executor) |
| 340 | + assert executor._schema_registry._schemas == {} |
| 341 | + |
| 342 | + def test_create_executor_load_all_preloads_schemas( |
| 343 | + self, redis_client: redis.Redis, caching_indexes: list[str] |
| 344 | + ): |
| 345 | + """load_all strategy preserves eager schema loading.""" |
| 346 | + executor = create_executor(redis_client, schema_cache_strategy="load_all") |
| 347 | + |
| 348 | + assert set(executor._schema_registry._schemas) == set(caching_indexes) |
| 349 | + |
| 350 | + def test_create_executor_reuses_registry( |
| 351 | + self, redis_client: redis.Redis, caching_indexes: list[str] |
| 352 | + ): |
| 353 | + """Factories should preserve a caller-provided registry instance.""" |
| 354 | + registry = SchemaRegistry(redis_client) |
| 355 | + |
| 356 | + executor = create_executor( |
| 357 | + redis_client, |
| 358 | + schema_registry=registry, |
| 359 | + schema_cache_strategy="load_all", |
| 360 | + ) |
| 361 | + |
| 362 | + assert executor._schema_registry is registry |
| 363 | + assert set(registry._schemas) == set(caching_indexes) |
| 364 | + |
| 365 | + |
325 | 366 | # --------------------------------------------------------------------------- |
326 | 367 | # Tests: Negative caching (sync) |
327 | 368 | # --------------------------------------------------------------------------- |
@@ -767,3 +808,48 @@ async def test_async_executor_multiple_queries_reuse_cache( |
767 | 808 | assert result2.count == 1 |
768 | 809 | # Same dict object — was not re-fetched |
769 | 810 | assert schema_after_first is schema_after_second |
| 811 | + |
| 812 | + |
| 813 | +class TestAsyncExecutorFactories: |
| 814 | + """Async factory helpers configure schema loading strategies correctly.""" |
| 815 | + |
| 816 | + async def test_create_async_executor_defaults_to_lazy( |
| 817 | + self, |
| 818 | + async_caching_client: async_redis.Redis, |
| 819 | + async_caching_indexes: list[str], |
| 820 | + ): |
| 821 | + """Default async factory behavior should not preload schemas.""" |
| 822 | + executor = await create_async_executor(async_caching_client) |
| 823 | + |
| 824 | + assert isinstance(executor, AsyncExecutor) |
| 825 | + assert executor._schema_registry._schemas == {} |
| 826 | + |
| 827 | + async def test_create_async_executor_load_all_preloads_schemas( |
| 828 | + self, |
| 829 | + async_caching_client: async_redis.Redis, |
| 830 | + async_caching_indexes: list[str], |
| 831 | + ): |
| 832 | + """Async load_all strategy preserves eager schema loading.""" |
| 833 | + executor = await create_async_executor( |
| 834 | + async_caching_client, |
| 835 | + schema_cache_strategy="load_all", |
| 836 | + ) |
| 837 | + |
| 838 | + assert set(executor._schema_registry._schemas) == set(async_caching_indexes) |
| 839 | + |
| 840 | + async def test_create_async_executor_reuses_registry( |
| 841 | + self, |
| 842 | + async_caching_client: async_redis.Redis, |
| 843 | + async_caching_indexes: list[str], |
| 844 | + ): |
| 845 | + """Async factories should preserve a caller-provided registry instance.""" |
| 846 | + registry = AsyncSchemaRegistry(async_caching_client) |
| 847 | + |
| 848 | + executor = await create_async_executor( |
| 849 | + async_caching_client, |
| 850 | + schema_registry=registry, |
| 851 | + schema_cache_strategy="load_all", |
| 852 | + ) |
| 853 | + |
| 854 | + assert executor._schema_registry is registry |
| 855 | + assert set(registry._schemas) == set(async_caching_indexes) |
0 commit comments