1+ import os
2+ from unittest import mock
3+
14import pytest
25from redis import Redis
36from testcontainers .core .container import DockerContainer
47from testcontainers .core .waiting_utils import wait_for_logs
58
9+ from src .api .dependencies import get_settings , get_settings_cached
10+ from src .api .main import app
11+ from src .common import PasswordStorage
12+
613KVROCKS_IMAGE = "apache/kvrocks:2.11.0"
714
815
16+ @pytest .fixture (scope = "session" , autouse = True )
17+ def settings (): # disable the cache on settings, so it'll be reloaded everytime
18+ app .dependency_overrides [get_settings_cached ] = get_settings
19+ try :
20+ yield get_settings # to give tests access to settings easily if needed
21+ finally :
22+ app .dependency_overrides = {}
23+
24+
925@pytest .fixture (scope = "session" )
1026def _kvrocks () -> Redis :
1127 with DockerContainer (image = KVROCKS_IMAGE ).with_exposed_ports (6666 ) as kvrocks_container :
1228 wait_for_logs (kvrocks_container , "Ready to accept connections" )
1329 host = kvrocks_container .get_container_host_ip ()
1430 port = kvrocks_container .get_exposed_port (6666 )
1531
16- yield Redis (host = host , port = port )
32+ with mock .patch .dict (os .environ , {"KVROCKS_URL" : f"redis://{ host } :{ port } " }):
33+ yield Redis (host = host , port = port )
1734
1835
1936@pytest .fixture ()
@@ -24,3 +41,10 @@ def kvrocks(_kvrocks) -> Redis:
2441 # Clean up keys after the test is done
2542 for key in _kvrocks .keys ():
2643 _kvrocks .delete (key )
44+
45+
46+ @pytest .fixture
47+ def password_storage (kvrocks ):
48+ password_storage_ = PasswordStorage (client = kvrocks )
49+ password_storage_ .PIPELINE_MAX_SIZE = 1 # so we don't have to flush in our test
50+ yield password_storage_
0 commit comments