Skip to content

Commit e35ebe5

Browse files
committed
tests for new changes
1 parent 793e80b commit e35ebe5

File tree

3 files changed

+1162
-0
lines changed

3 files changed

+1162
-0
lines changed

tests/crud/test_admin.py

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,3 +502,116 @@ async def test_crud_admin_error_handling_invalid_session(async_session):
502502

503503
# The session should be None in the db_config
504504
assert admin.db_config.session is None
505+
506+
507+
@pytest.mark.asyncio
508+
async def test_crud_admin_session_backend_switching(async_session):
509+
"""Test that session backend methods work after initialization."""
510+
secret_key = "test-secret-key-for-testing-only-32-chars"
511+
db_config = create_test_db_config(async_session)
512+
513+
admin = CRUDAdmin(
514+
session=async_session,
515+
SECRET_KEY=secret_key,
516+
db_config=db_config,
517+
setup_on_initialization=False,
518+
)
519+
520+
# Test initial memory backend
521+
assert "MemorySessionStorage" in str(type(admin.session_manager.storage))
522+
523+
# Test switching to database backend
524+
admin.use_database_sessions()
525+
assert "DatabaseSessionStorage" in str(type(admin.session_manager.storage))
526+
assert admin.track_sessions_in_db is True
527+
528+
# Test switching back to memory
529+
admin.use_memory_sessions()
530+
assert "MemorySessionStorage" in str(type(admin.session_manager.storage))
531+
532+
# Test Redis URL parsing
533+
parsed = admin._parse_redis_url("redis://user:pass@localhost:6379/2")
534+
expected = {
535+
"host": "localhost",
536+
"port": 6379,
537+
"db": 2,
538+
"username": "user",
539+
"password": "pass",
540+
}
541+
assert parsed == expected
542+
543+
# Test Redis URL parsing with defaults
544+
parsed_simple = admin._parse_redis_url("redis://localhost")
545+
expected_simple = {
546+
"host": "localhost",
547+
"port": 6379,
548+
"db": 0,
549+
}
550+
assert parsed_simple == expected_simple
551+
552+
# Test Redis backend switching (if redis is available)
553+
try:
554+
admin.use_redis_sessions(redis_url="redis://localhost:6379/0")
555+
storage_type_name = type(admin.session_manager.storage).__name__
556+
assert storage_type_name == "RedisSessionStorage"
557+
except ImportError:
558+
# Redis not available, which is fine for tests
559+
pass
560+
561+
562+
@pytest.mark.asyncio
563+
async def test_crud_admin_backend_parameter_validation(async_session):
564+
"""Test parameter validation for session backend methods."""
565+
secret_key = "test-secret-key-for-testing-only-32-chars"
566+
db_config = create_test_db_config(async_session)
567+
568+
admin = CRUDAdmin(
569+
session=async_session,
570+
SECRET_KEY=secret_key,
571+
db_config=db_config,
572+
setup_on_initialization=False,
573+
)
574+
575+
# Test Redis parameter validation
576+
try:
577+
# Test individual parameters work
578+
admin.use_redis_sessions(host="localhost", port=6379, db=1)
579+
storage_type_name = type(admin.session_manager.storage).__name__
580+
assert storage_type_name == "RedisSessionStorage"
581+
582+
# Test defaults work
583+
admin.use_redis_sessions()
584+
assert type(admin.session_manager.storage).__name__ == "RedisSessionStorage"
585+
586+
# Test conflict detection
587+
with pytest.raises(
588+
ValueError, match="Cannot specify both redis_url and individual parameters"
589+
):
590+
admin.use_redis_sessions(
591+
redis_url="redis://localhost:6379", host="localhost"
592+
)
593+
594+
except ImportError:
595+
# Redis not available, skip Redis tests
596+
pass
597+
598+
# Test Memcached parameter validation
599+
try:
600+
# Test individual parameters work
601+
admin.use_memcached_sessions(host="localhost", port=11211)
602+
storage_type_name = type(admin.session_manager.storage).__name__
603+
assert storage_type_name == "MemcachedSessionStorage"
604+
605+
# Test defaults work
606+
admin.use_memcached_sessions()
607+
assert type(admin.session_manager.storage).__name__ == "MemcachedSessionStorage"
608+
609+
# Test conflict detection
610+
with pytest.raises(
611+
ValueError, match="Cannot specify both servers and individual parameters"
612+
):
613+
admin.use_memcached_sessions(servers=["localhost:11211"], host="localhost")
614+
615+
except ImportError:
616+
# Memcached not available, skip Memcached tests
617+
pass

0 commit comments

Comments
 (0)