0
0
Fork 0
This repository has been archived on 2025-05-11. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
Heliotrope/tests/conftest.py
Ryu juheon a03b0f3c19
feat: apply hitomi changed (#171)
* fix(interpreter): remove statement (#168)

* refactor(table): hitomi changes applied

* refactor(domain): hitomi changes applied

* refactor(table): hitomi changes applied

* refactor(types): hitomi changes applied

* chore(vscode): add snippets

* refactor(orm): hitomi changes applied

* feat(odm): migration to mongodb (#169)

* feat(odm): migration to mongodb

* feat(config): add config for atlas search

* feat: apply changed

* chore(deps): add dependency

* test: apply changed

* test(config): apply changed

* chore(deps): bump up version motor

* feat!(parser): remove parser

* feat(interpreter): add get thumbnail method

* feat!(info): using galleryinfo

* feat(functions): add getthumbnail method

* feat!(hitomi): remove get info method

* fix(image): now return only webp or avif

* refactor(info): apply hitomi changed

* refactor(mirroring): apply hitomi changed

* fix(types): thumbnail is file

* test(common): edit dict

* style: apply code style

* feat(info): add from dict method

* style(info): fix type issue

* test(arg): fix test

* style(info): apply isort

* fix(domain): init false

* test(common): edit info

* test(conftest): edit image url

* test(mirroring): fix test

* fix(function): add base

* test(conftest): edit image url fixture

* test(conftest): fix conftest

* test(conftest): fix conftest
2022-02-03 19:41:56 +09:00

108 lines
3.3 KiB
Python

import json
from asyncio.events import AbstractEventLoop, get_running_loop, new_event_loop
from pytest import fixture, mark
from js2py.pyjs import undefined
from sanic_ext.extensions.http.extension import HTTPExtension
from sanic_ext.extensions.injection.extension import InjectionExtension
from sanic_ext.extensions.openapi.extension import OpenAPIExtension
from sanic_testing import TestManager # type:ignore
from heliotrope.config import HeliotropeConfig
from heliotrope.database.orm.base import mapper_registry
from heliotrope.domain.galleryinfo import Galleryinfo
from heliotrope.domain.info import Info
from heliotrope.interpreter import CommonJS
from heliotrope.request.hitomi import HitomiRequest
from heliotrope.sanic import Heliotrope
from heliotrope.server import create_app
from tests.common import galleryinfo, info
class LoopThatIsNeverClosed(AbstractEventLoop):
def __init__(self):
self._loop = None
def __getattribute__(self, name: str):
if name == "_loop":
return super().__getattribute__("_loop")
if self._loop is None or self._loop.is_closed():
self._loop = new_event_loop()
return getattr(self._loop, name)
def get_config():
with open("./tests/config.json", "r") as f:
config = HeliotropeConfig()
config.update(json.load(f))
return config
async def startup_test(heliotrope: Heliotrope, loop: AbstractEventLoop):
await heliotrope.ctx.orm.add_galleryinfo(Galleryinfo.from_dict(galleryinfo))
await heliotrope.ctx.odm.add_info(Info.from_dict(info))
async def closeup_test(heliotrope: Heliotrope, loop: AbstractEventLoop):
async with heliotrope.ctx.orm.engine.begin() as connection:
await connection.run_sync(mapper_registry.metadata.drop_all)
await heliotrope.ctx.odm.collection.delete_many({})
@fixture
def event_loop():
loop = LoopThatIsNeverClosed()
yield loop
loop.close()
@fixture
def app():
heliotrope_config = get_config()
heliotrope = create_app(heliotrope_config)
TestManager(heliotrope)
heliotrope.before_server_start(startup_test)
heliotrope.before_server_stop(closeup_test)
yield heliotrope
@fixture(autouse=True)
def reset_extensions():
yield
for ext in (HTTPExtension, InjectionExtension, OpenAPIExtension):
ext._singleton = None
@fixture
@mark.asyncio
async def image_url():
hitomi_request = await HitomiRequest.setup()
common_js = await CommonJS.setup(hitomi_request)
yield common_js.interpreter.url_from_url_from_hash(
galleryinfo["id"], galleryinfo["files"][0], "webp", undefined, "a"
)
@fixture
@mark.asyncio
async def fake_app():
heliotrope_config = get_config()
heliotrope = create_app(heliotrope_config)
heliotrope.before_server_stop(closeup_test)
before_server_start = []
before_server_stop = []
# Apply listeners
for future_listener in heliotrope._future_listeners:
if future_listener.event == "before_server_start":
before_server_start.append(future_listener.listener)
else:
before_server_stop.append(future_listener.listener)
# do not run app
for listener in before_server_start:
# None is loop argument but not needed
await listener(heliotrope, None)
# return app
yield heliotrope
for listener in before_server_stop:
await listener(heliotrope, None)