Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize unit test #841

Merged
merged 2 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import pytest
from lyrebird import application
from lyrebird.mock import context

SERVER_NAMES = ['event', 'log', 'mock', 'task', 'checker', 'db', 'plugin']

@pytest.fixture(scope='module', autouse=True)
def setup_and_teardown_environment():
bak_server = {}
for server in SERVER_NAMES:
bak_server[server] = application.server.get(server)
bak_reporter = application.reporter
bak_cm = application._cm
bak_config = application.config
bak_encoder_decoder = application.encoders_decoders
bak_on_request = application.on_request
bak_on_request_upstream = application.on_request_upstream
bak_on_response = application.on_response
bak_on_response_upstream = application.on_response_upstream
bak_socketio = context.application.socket_io
bak_dm = context.application.data_manager

yield

for server in SERVER_NAMES:
if bak_server[server] is None and server in application.server:
del application.server[server]
elif bak_server[server] is not None:
application.server[server] = bak_server[server]
application.reporter = bak_reporter
application._cm = bak_cm
application.config = bak_config
application.encoders_decoders = bak_encoder_decoder
application.on_request = bak_on_request
application.on_request_upstream = bak_on_request_upstream
application.on_response = bak_on_response
application.on_response_upstream = bak_on_response_upstream
context.application.socket_io = bak_socketio
context.application.data_manager = bak_dm
7 changes: 6 additions & 1 deletion tests/test_checker.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import pytest
from .utils import FakeSocketio
from lyrebird.event import EventServer
from lyrebird import application
from lyrebird.checker import LyrebirdCheckerServer
from lyrebird.mock import context

CHECKER_A_FILENAME = "checker_a.py"
CHECKER_B_FILENAME = "checker_b.py"
Expand Down Expand Up @@ -35,7 +37,8 @@ def checker_init(tmp_path, tmpdir):
# mock config
application._cm = type('MockedContentManager', (), {'config': config, 'root':tmpdir, 'ROOT':tmpdir})()

return application.checkers
yield application.checkers


@pytest.fixture
def checker_server(checker_init, tmp_path):
Expand All @@ -49,9 +52,11 @@ def checker_server(checker_init, tmp_path):

@pytest.fixture
def event_server():
context.application.socket_io = FakeSocketio()
server = EventServer()
application.server['event'] = server
yield server
server.stop()


def test_rank_valid(event_server, checker_server):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_common_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ def client():
application._cm = ConfigManager()
application._cm.config = _conf
server = LyrebirdMockServer()
client = server.app.test_client()
yield client
with server.app.test_client() as client:
yield client

def test_render_api_without_json(client):
resp = client.put('/api/render')
Expand Down
4 changes: 2 additions & 2 deletions tests/test_conf_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ def client():
application._cm = ConfigManager()
application._cm.config = copy.deepcopy(conf)
server = LyrebirdMockServer()
client = server.app.test_client()
yield client
with server.app.test_client() as client:
yield client


def test_patch_conf_api_with_no_param(client):
Expand Down
10 changes: 5 additions & 5 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest
from lyrebird import config, application, reporter
from lyrebird.task import BackgroundTaskServer
from .utils import FakeEvnetServer
from lyrebird import config, application
from pathlib import Path
import json
import codecs
Expand All @@ -20,12 +20,12 @@
}
}


@pytest.fixture
def cm():
cm = config.ConfigManager()
application.server['task'] = BackgroundTaskServer()
application.reporter = reporter.Reporter()
reporter.start()
application._cm = cm
application.server['event'] = FakeEvnetServer()
yield cm


Expand Down
18 changes: 3 additions & 15 deletions tests/test_db.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
import time
import pytest
from .utils import FakeSocketio, FakeBackgroundTaskServer
from pathlib import Path
from typing import NamedTuple
import lyrebird
from lyrebird import reporter, application
from lyrebird import application
from lyrebird.event import EventServer
from lyrebird.task import BackgroundTaskServer
from lyrebird.config import personal_config_template
from lyrebird.db.database_server import LyrebirdDatabaseServer


MockConfigManager = NamedTuple('MockConfigManager', [('config', dict), ('personal_config', dict), ('ROOT', object), ('root', object)])


class FakeSocketio:

def emit(self, event, *args, **kwargs): {
print(f'Send event {event} args={args} kw={kwargs}')
}


@pytest.fixture
def event_server(tmpdir):
_conf = {
Expand All @@ -38,12 +31,7 @@ def event_server(tmpdir):

@pytest.fixture
def task_server():
lyrebird.application.reporter = reporter.Reporter()
server = BackgroundTaskServer()
server.start()
lyrebird.application.server['task'] = server
yield server
server.stop()
lyrebird.application.server['task'] = FakeBackgroundTaskServer()


@pytest.fixture
Expand Down
10 changes: 2 additions & 8 deletions tests/test_dm.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from copy import deepcopy
from typing import NamedTuple
from urllib.parse import urlparse
from .utils import FakeSocketio
from lyrebird.mock import dm
from lyrebird.mock.dm.file_data_adapter import data_adapter
from lyrebird.mock.handlers.encoder_decoder_handler import EncoderDecoder
Expand Down Expand Up @@ -326,13 +327,6 @@

MockConfigManager = NamedTuple('MockConfigManager', [('config', dict)])

class FakeSocketio:

def emit(self, event, *args, **kwargs): {
print(f'Send event {event} args={args} kw={kwargs}')
}


@pytest.fixture
def root(tmpdir):
with codecs.open(tmpdir / 'dataA-UUID', 'w') as f:
Expand Down Expand Up @@ -373,7 +367,7 @@ def data_manager(root, tmpdir):
_dm.snapshot_workspace = tmpdir
_dm.set_adapter(data_adapter)
_dm.set_root(root)
return _dm
yield _dm


def test_load_from_path(root):
Expand Down
17 changes: 7 additions & 10 deletions tests/test_dm_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
import pytest
import codecs
import lyrebird
from .utils import FakeSocketio
from typing import NamedTuple
from lyrebird import application
from lyrebird.mock import context
from lyrebird.mock.dm import DataManager
from lyrebird.mock.mock_server import LyrebirdMockServer
from lyrebird.mock.dm.file_data_adapter import data_adapter
from lyrebird.mock.handlers.encoder_decoder_handler import EncoderDecoder
Expand Down Expand Up @@ -46,13 +48,6 @@
MockConfigManager = NamedTuple('MockConfigManager', [('config', dict)])


class FakeSocketio:

def emit(self, event, *args, **kwargs): {
print(f'Send event {event} args={args} kw={kwargs}')
}


@pytest.fixture
def root(tmpdir):
with codecs.open(tmpdir / 'dataA-UUID', 'w') as f:
Expand All @@ -73,12 +68,14 @@ def client(root, tmpdir):
application.encoders_decoders = EncoderDecoder()

server = LyrebirdMockServer()
client = server.app.test_client()
_dm = context.application.data_manager
_dm = DataManager()
context.application.data_manager = _dm
_dm.snapshot_workspace = tmpdir
_dm.set_adapter(data_adapter)
_dm.set_root(root)
yield client
with server.app.test_client() as client:
yield client
del server


def test_group_get_with_group_id(client):
Expand Down
18 changes: 6 additions & 12 deletions tests/test_dm_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import json
from copy import deepcopy
import lyrebird
from .utils import FakeSocketio, FakeEvnetServer
from lyrebird.mock import dm
from lyrebird.event import EventServer
from lyrebird.config import ConfigManager
Expand Down Expand Up @@ -69,13 +70,6 @@
}


class FakeSocketio:

def emit(self, event, *args, **kwargs): {
print(f'Send event {event} args={args} kw={kwargs}')
}


@pytest.fixture
def root(tmpdir):
with codecs.open(tmpdir / 'dataA-UUID', 'w') as f:
Expand All @@ -96,8 +90,8 @@ def data_manager(root, tmpdir):
_dm.snapshot_workspace = tmpdir
_dm.set_adapter(data_adapter)
_dm.set_root(root)
return _dm

yield _dm
del _dm


CHECKER_A_FILENAME = 'checker_a.py'
Expand Down Expand Up @@ -134,13 +128,13 @@ def checker_server(checker_init, tmp_path):
application.server['checker'] = server
yield server
server.stop()
del server


@pytest.fixture
def event_server():
server = EventServer()
application.server['event'] = server
yield server
application.server['event'] = FakeEvnetServer()
yield None


def test_mock_data_upgrade_2_14_to_2_15(data_manager):
Expand Down
3 changes: 2 additions & 1 deletion tests/test_dm_label.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ def data_manager(root):
_dm.set_adapter(data_adapter)
_dm.set_root(root)
context.application.data_manager = _dm
return _dm
yield _dm
del _dm


@pytest.fixture
Expand Down
14 changes: 3 additions & 11 deletions tests/test_dm_temp.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import re
import json
import pytest
import codecs
import tarfile
import lyrebird
from pathlib import Path
from copy import deepcopy
from typing import NamedTuple
from urllib.parse import urlparse
from .utils import FakeSocketio
from lyrebird.mock import dm
from lyrebird.mock.dm.file_data_adapter import data_adapter
from lyrebird.mock.handlers.encoder_decoder_handler import EncoderDecoder
Expand Down Expand Up @@ -69,12 +66,6 @@

MockConfigManager = NamedTuple('MockConfigManager', [('config', dict)])

class FakeSocketio:

def emit(self, event, *args, **kwargs): {
print(f'Send event {event} args={args} kw={kwargs}')
}


@pytest.fixture
def root(tmpdir):
Expand Down Expand Up @@ -113,7 +104,8 @@ def data_manager(root, tmpdir):
}
}
_dm.temp_mock_tree.add_data(data)
return _dm

yield _dm


def test_get(data_manager):
Expand Down
15 changes: 2 additions & 13 deletions tests/test_event.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import time
import pytest
import lyrebird
from .utils import FakeSocketio, FakeBackgroundTaskServer
from typing import NamedTuple
from lyrebird import application
from lyrebird import reporter
from lyrebird.event import EventServer
from lyrebird import CustomEventReceiver
from lyrebird.task import BackgroundTaskServer


MockConfigManager = NamedTuple('MockConfigManager', [('config', dict)])
Expand All @@ -21,13 +20,6 @@ def callback(self, msg):
self.history.append(msg)


class FakeSocketio:

def emit(self, event, *args, **kwargs): {
print(f'Send event {event} args={args} kw={kwargs}')
}


@pytest.fixture
def callback_tester():
return CallbackTester()
Expand All @@ -50,12 +42,9 @@ def event_server():

@pytest.fixture
def task_server():
lyrebird.application.reporter = reporter.Reporter()
server = BackgroundTaskServer()
server.start()
server = FakeBackgroundTaskServer()
lyrebird.application.server['task'] = server
yield server
server.stop()


def test_event(callback_tester, event_server, task_server):
Expand Down
Loading
Loading