|
| 1 | +import asyncio |
| 2 | +from typing import Any, Generator |
| 3 | +from unittest.mock import Mock, patch |
| 4 | + |
1 | 5 | import pytest
|
| 6 | +from pytest import FixtureRequest |
| 7 | +from tortoise.backends.base.config_generator import generate_config |
| 8 | +from tortoise.contrib.test import finalizer, initializer |
2 | 9 |
|
3 |
| -from src.models.movies import MovieModel |
4 |
| -from src.models.users import UserModel |
| 10 | +from src.configs import config |
| 11 | +from src.configs.database import TORTOISE_APP_MODELS |
5 | 12 |
|
6 | 13 | TEST_BASE_URL = "http://test"
|
| 14 | +TEST_DB_LABEL = "models" |
| 15 | +TEST_DB_TZ = "Asia/Seoul" |
| 16 | + |
| 17 | + |
| 18 | +def get_test_db_config() -> dict[str, Any]: |
| 19 | + tortoise_config = generate_config( |
| 20 | + db_url=f"mysql://{config.MYSQL_USER}:{config.MYSQL_PASSWORD}@{config.MYSQL_HOST}:{config.MYSQL_PORT}/test", |
| 21 | + app_modules={TEST_DB_LABEL: TORTOISE_APP_MODELS}, |
| 22 | + connection_label=TEST_DB_LABEL, |
| 23 | + testing=True, |
| 24 | + ) |
| 25 | + tortoise_config["timezone"] = TEST_DB_TZ |
| 26 | + |
| 27 | + return tortoise_config |
| 28 | + |
| 29 | + |
| 30 | +@pytest.fixture(scope="session", autouse=True) |
| 31 | +def initialize(request: FixtureRequest) -> Generator[None, None, None]: |
| 32 | + loop = asyncio.new_event_loop() |
| 33 | + asyncio.set_event_loop(loop) |
| 34 | + with patch("tortoise.contrib.test.getDBConfig", Mock(return_value=get_test_db_config())): |
| 35 | + initializer(modules=TORTOISE_APP_MODELS) |
| 36 | + yield |
| 37 | + finalizer() |
| 38 | + loop.close() |
7 | 39 |
|
8 | 40 |
|
9 |
| -@pytest.fixture(scope="function", autouse=True) |
10 |
| -def user_model_clear() -> None: |
11 |
| - UserModel.clear() |
12 |
| - MovieModel.clear() |
| 41 | +@pytest.fixture(scope="session", autouse=True) |
| 42 | +def event_loop() -> None: |
| 43 | + pass |
0 commit comments