userver: /home/antonyzhilin/arcadia/taxi/uservices/userver/testsuite/pytest_plugins/pytest_userver/plugins/sqlite.py Source File
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
sqlite.py
1import pytest
2
3pytest_plugins = ['pytest_userver.plugins.core']
4
5USERVER_CONFIG_HOOKS = ['userver_sqlite_config']
6
7
8@pytest.fixture(scope='session')
9def userver_sqlite_config(tmp_path_factory):
10 """
11 Returns a function that adjusts the SQLite static configuration file for the testsuite.
12 Sets the `db-path` to be an in memory DB with shared cache.
13
14 @ingroup userver_testsuite_fixtures
15 """
16
17 def _patch_config(config_yaml, config_vars):
18 components = config_yaml['components_manager']['components']
19 for _, params in components.items():
20 if params and 'db-path' in params:
21 params['db-path'] = f'file:{params["db-path"]}?mode=memory&cache=shared'
22
23 return _patch_config
24
25
26@pytest.fixture(scope='session')
27def _list_dbpath_components(service_config_yaml):
28 components_list = list()
29 components = service_config_yaml['components_manager']['components']
30 for commponent_name, params in components.items():
31 if params and 'db-path' in params:
32 components_list.append(commponent_name)
33 return components_list
34
35
36@pytest.fixture(autouse=True)
37async def sqlite_db(_list_dbpath_components, service_client, testpoint):
38 """
39 Removes all the data from SQLite tables at the end of the test to guarantee tests isolation.
40
41 @ingroup userver_testsuite_fixtures
42 """
43 yield
44
45 for component in _list_dbpath_components:
46 try:
47 await service_client.run_task(f'sqlite/{component}/clean-db')
48 except service_client.TestsuiteTaskNotFound:
49 pass