Skip to content

Commit

Permalink
✅ fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
j1g5awi committed Aug 5, 2024
1 parent 30c0547 commit 5989fef
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 117 deletions.
9 changes: 5 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ ci:
autoupdate_schedule: monthly
autoupdate_commit_msg: ":arrow_up: auto update by pre-commit hooks"
repos:
- repo: https://github.com/hadialqattan/pycln
rev: v2.4.0
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.5.0
hooks:
- id: pycln
args: [--config, pyproject.toml]
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
stages: [commit]

- repo: https://github.com/pycqa/isort
rev: 5.13.2
Expand Down
185 changes: 81 additions & 104 deletions pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ requires = ["pdm-backend"]
build-backend = "pdm.backend"

[tool.pdm.dev-dependencies]
pre-commit = ["pycln", "isort", "black", "nonemoji", "pre-commit"]
pre-commit = ["ruff", "isort", "black", "nonemoji", "pre-commit"]
nonebot = [
"httpx>=0.23.3,<1.0.0",
"fastapi>=0.110.0,<1.0.0",
Expand All @@ -36,6 +36,8 @@ tests = [
"pytest-asyncio>=0.20.3",
"pytest-cov>=4.0.0",
"pytest-xdist>=3.2.0",
"pytest-mock>=3.14.0",
"freezegun>=1.5.1",
]
impl = ["nonebot-plugin-sentry>=0.2.2"]

Expand Down Expand Up @@ -64,6 +66,7 @@ ignore = [
"RUF001", # ambiguous-unicode-character-string
"RUF002", # ambiguous-unicode-character-docstring
"RUF003", # ambiguous-unicode-character-comment
"PYI051",
]

[tool.ruff.lint.flake8-pytest-style]
Expand Down
52 changes: 47 additions & 5 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import datetime
from pathlib import Path
from contextlib import contextmanager

import pytest
import nonebot
from sqlalchemy import delete
from freezegun import freeze_time
from sqlalchemy import event, delete
from pytest_mock import MockerFixture
from nonebug import NONEBOT_INIT_KWARGS, App
from nonebot.adapters.telegram import Adapter as TelegramAdapter
from nonebot.adapters.onebot.v11 import Adapter as OnebotV11Adapter
Expand All @@ -13,11 +17,12 @@ def pytest_configure(config: pytest.Config) -> None:
config.stash[NONEBOT_INIT_KWARGS] = {
"driver": "~fastapi+~httpx+~websockets",
"datastore_database_url": "sqlite+aiosqlite:///:memory:",
"alembic_startup_check": False,
}


@pytest.fixture(scope="session", autouse=True)
def load_adapters(nonebug_init: None):
def _load_adapters(nonebug_init: None):
driver = nonebot.get_driver()
driver.register_adapter(OnebotV11Adapter)
driver.register_adapter(OnebotV12Adapter)
Expand All @@ -43,13 +48,20 @@ async def to_onebot_event(self, event):


@pytest.fixture
async def app(nonebug_init: None, tmp_path: Path, monkeypatch: pytest.MonkeyPatch):
async def app(
nonebug_init: None,
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
mocker: MockerFixture,
):
nonebot.require("nonebot_plugin_all4one")
from nonebot_plugin_orm import _init_orm, get_session
from nonebot_plugin_orm import init_orm, get_session

import nonebot_plugin_all4one.database

_init_orm()
mocker.patch("nonebot_plugin_orm._data_dir", tmp_path / "orm")

await init_orm()

with monkeypatch.context() as m:
m.setattr(nonebot_plugin_all4one.database, "FILE_PATH", tmp_path)
Expand All @@ -61,3 +73,33 @@ async def app(nonebug_init: None, tmp_path: Path, monkeypatch: pytest.MonkeyPatc

async with get_session() as session:
await session.execute(delete(File))


@pytest.fixture
async def session(app: App):
from nonebot_plugin_orm import get_session

async with get_session() as session:
yield session


# https://stackoverflow.com/questions/29116718/how-to-mocking-created-time-in-sqlalchemy
@contextmanager
def patch_time(time_to_freeze, tick=True):
from nonebot_plugin_all4one.database import File

with freeze_time(time_to_freeze, tick=tick) as frozen_time:

def set_timestamp(mapper, connection, target):
now = datetime.datetime.utcnow()
if hasattr(target, "created_at"):
target.created_at = now

event.listen(File, "before_insert", set_timestamp, propagate=True)
yield frozen_time
event.remove(File, "before_insert", set_timestamp)


@pytest.fixture
def patch_current_time():
return patch_time
5 changes: 3 additions & 2 deletions tests/database/test_database.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from nonebug import App
from anyio import open_file
from sqlalchemy import select


Expand Down Expand Up @@ -27,5 +28,5 @@ async def test_database(app: App):

# 确认文件存在
assert file.path
with open(file.path, "rb") as f:
assert f.read() == b"test"
async with await open_file(file.path, "rb") as f:
assert (await f.read()) == b"test"
Loading

0 comments on commit 5989fef

Please sign in to comment.