def database_mock(request, mocker): """Mock database for testing.""" from snippy.storage.database import Database as Db Config.init(['snippy', '-q'] + Database.get_cli_params() ) # Prevent unnecessary CLI help output with quiet option. mocker.patch.object(Config, 'storage_file', Database.get_storage(), create=True) mocker.patch.object(Config, 'storage_schema', Database.get_schema(), create=True) database = Db() database.init() def fin(): """Clear the resources at the end.""" database.disconnect() Database.delete_all_contents() Database.delete_storage() request.addfinalizer(fin) return database
def _create_snippy(mocker, request, params, database): """Create snippy with mocks. Args: params (list): Command line arguments to start the Snippy. database (str): Database used with the tests. Returns: obj: Snippy object. """ if request.config.getoption("--snippy-logs"): params.append('--debug') # Mock only objects from the Snippy package. If system calls like os.open # are mocked from here, it will mock all the third party packages that are # imported when the Snippy object is created. System calls must be mocked # after the Snippy object is in a such state that it can accept test case # input. mocker.patch.object(Config, '_storage_file', return_value=Database.get_storage()) if database == Database.DB_POSTGRESQL: params = params + Database.get_cli_params() snippy = Snippy(params) return snippy
def db_cli_params(): """Return required CLI parameters for database.""" return Database.get_cli_params()