def initialize_tests(request: FixtureRequest) -> None: initializer( ["eventual_tortoise.relation"], db_url="sqlite://:memory:", app_label="default", ) request.addfinalizer(finalizer)
def initialize_tests(request): """This function creates the connection to test database (test database creates in ci)""" db_url = "postgres://*****:*****@database:5432/test" initializer(["models"], db_url=db_url, app_label="models") request.addfinalizer(finalizer)
def client(): """Test client.""" db_url = f"postgres://*****:*****@localhost:5432/testing" initializer(settings.DB_MODELS) with TestClient(app) as c: yield c finalizer()
def initialize_tests(request): initializer( ["api.api_v1.models.tortoise"], db_url=getattr(settings, "TORTOISE_TEST_DB", "sqlite://:memory:"), app_label="models", ) request.addfinalizer(finalizer)
def client() -> Generator: ''' Generates a TestClient instance for models.user ''' initializer(["app.models"]) with TestClient(app) as test_client: yield test_client finalizer()
def app() -> FastAPI: from app.app import app initializer( ["app.models"], db_url=os.environ["DATABASE_URL"], ) yield app finalizer()
def client() -> Generator: # setup test database and test client for all test cases initializer(["app.models"], db_url=f'postgres://{settings.PG_USER}:{settings.PG_PASSWORD}@{settings.PG_HOST}:{settings.PG_PORT}/test', app_label='app') with TestClient(app) as c: yield c # drop test database finalizer()
def client() -> Generator: app = create_app(db_conn='sqlite', generate_schemas=True) # 初始化数据库 initializer(['app.models']) with TestClient(app) as c: yield c # 测试完后清除数据 finalizer()
def setUp(self) -> None: initializer(settings.MODELS) self.user = loop.run_until_complete( create_user('*****@*****.**', password=self.PASSWORD)) self.tokens = create_tokens( AuthJWT(), self.user.id, email=self.user.email, domain=self.user.domain, )
def test_app(): # set up app = create_application() # new app.dependency_overrides[get_settings] = get_settings_override initializer(["app.models.tortoise"]) with TestClient(app) as test_client: # testing yield test_client finalizer()
def test_app_with_db() -> Generator: app = create_application() app.dependency_overrides[get_settings] = get_settings_override initializer(db_url=os.environ.get("DATABASE_TEST_URL"), modules=["app.models.tortoise.users"]) run_async(add_users()) with TestClient(app) as test_client: yield test_client finalizer()
async def client() -> Generator: """Async client with db connection.""" nest_asyncio.apply() initializer(modules=APP_MODELS) app = create_application() async with AsyncClient( app=app, base_url="https://testserver") as test_client, LifespanManager( app): yield test_client finalizer()
def client() -> Generator: # set up app = create_application() # app.dependency_overrides[get_settings] = get_settings_override settings = get_settings() initializer(["app.models"], db_url=settings.database_test_url) with TestClient(app) as test_client: # testing yield test_client finalizer()
def init_db(request): db_url = "sqlite://:memory:" initializer(["mnemeapi.models.models"], db_url=db_url) # Create the admin user since it's a private instance admin = UserCreate(username="******", admin=True, encrypted=False, password="******") loop.run_until_complete(create_user(admin)) request.addfinalizer(finalizer)
def event_loop(request): cfg = Config(".env") db_url = cfg.get("TEST_DB_URL", default="sqlite://:memory:") loop = asyncio.get_event_loop() initializer(["bnstats.models"], db_url=db_url, app_label="models", loop=loop) yield loop finalizer() loop.close()
def setUp(self): with open('config.json', 'rt') as f: c = json.load(f) asyncio.set_event_loop(None) self.loop = asyncio.new_event_loop() initializer( modules={"models": models}, loop=self.loop, db_url=models.db_url(), )
def yield_test_client(app, impl): if impl.__name__ == "tortoise_implementation": from tortoise.contrib.test import initializer, finalizer initializer(["tests.implementations.tortoise_"]) with TestClient(app) as c: yield c finalizer() else: with TestClient(app) as c: yield c
def client() -> Generator: # set up app = create_application() initializer(settings.MODELS, db_url=settings.TEST_DATABASE_URL) with TestClient(app) as test_client: # testing yield test_client # tear down finalizer()
def initialize_tests(request): global real_connection global access_denied_getter initializer( ["aiohttpdemo_polls.models"], loop=_LOOP, db_url=POSTGRES_URL, app_label="aiohttpdemo_polls", ) real_connection = current_transaction_map[APP_LABEL] access_denied_getter = GetMock(AccessDeniedDBException) current_transaction_map[APP_LABEL] = access_denied_getter request.addfinalizer(finalizer)
def initialize_tests(request): # Reduce the default timeout for psycopg because the tests become very slow otherwise try: from tortoise.backends.psycopg import PsycopgClient PsycopgClient.default_timeout = float( os.environ.get("TORTOISE_POSTGRES_TIMEOUT", "15")) except ImportError: pass db_url = os.environ.get("TORTOISE_TEST_DB", "sqlite://:memory:") initializer(["tests.testmodels"], db_url=db_url) request.addfinalizer(finalizer)
def client(request): impl = request.param if impl.__name__ == 'tortoise_implementation': from tortoise.contrib.test import initializer, finalizer initializer(["tests.implementations.tortoise_"]) with TestClient(impl()) as c: yield c finalizer() else: yield TestClient(impl())
def initialize_db_tests() -> None: db_url = os.environ.get("TORTOISE_TEST_DB", "sqlite://:memory:") initializer( [ "mobilizon_reshare.models.event", "mobilizon_reshare.models.notification", "mobilizon_reshare.models.publication", "mobilizon_reshare.models.publisher", ], db_url=db_url, app_label="models", ) yield None finalizer()
def test_app_with_db(): # set up app = create_application() app.dependency_overrides[get_settings] = get_settings_override # Link with DB for testing initializer( ["app.infra.postgres.models"], db_url=os.environ.get("DATABASE_TEST_URL"), ) with TestClient(app) as test_client: # testing yield test_client
def initialize_tests(request): """ Prepares the test session's setup and teardown according to Tortoise ORM's requirements: More at: https://tortoise-orm.readthedocs.io/en/latest/contrib/unittest.html """ # databases - tortoise orm: creates and drops a db with prefix 'test_' db_url = DATABASE_TESTING_CONNECTION_URI # initializes tortoise orm with testing features and drops and creates 'test_{}'db # also runs all models migrations due to 'Tortoise.generate_schemas()' call initializer(["src.core.models.entities"], db_url=db_url, app_label="models") # drops created 'test_{}' databases due to 'Tortoise._drop_databases()' call request.addfinalizer(finalizer)
def setUp(self): self.flush_db_at_the_end = True # flush db on the tearDown. Set this to False in test not to flush db import os config.load_from_yaml( current_file_folder + f'/../config/config.{os.getenv("ENVIRONMENT", "local")}.yaml') config.conf['test'] = True from base import store # store engine has to be patched not to use redis and not to change the config option with patch('base.store.Store.engine', store.DictStore()): asyncio_current_loop = self.get_new_ioloop() asyncio_current_loop.run_sync(app.init_orm) _models = config.conf['tortoise']['apps'][ base.config.conf['name']]['models'] _db_url = base.config.get_db_url(test=True) initializer(_models, app_label=base.config.conf['name'], db_url=_db_url, loop=asyncio_current_loop.asyncio_loop) registry.test = True self.prefix = config.conf['prefix'] config.init_logging() config.conf['verbose'] = False self.my_app = app.make_app(debug=True) with open(f'{root_folder}/keys/users.key.pem') as pubkey: store.set('users_service_public_key', pubkey.read()) config.load_private_key(f'{root_folder}/keys/users.key') super().setUp() registry.test_port = self.get_http_port()
def setUp(self) -> None: initializer(['app.models'])
def client() -> Generator: initializer(['main']) with TestClient(app) as c: yield c finalizer()
def pytest_runtest_setup(item): initializer(['tortoise.tests.testmodels'], db_url='sqlite://:memory:')
def client() -> Generator: initializer([MODELS_MODULE]) with TestClient(app) as c: yield c finalizer()
async def setUpAsync(self) -> None: # db_url=config.DB_TEST_URL, initializer(await get_extension_models(), loop=self.loop)