diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 00000000..dda89ace --- /dev/null +++ b/tests/conftest.py @@ -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 diff --git a/tests/test_checker.py b/tests/test_checker.py index 4df6544f..83f0c36f 100644 --- a/tests/test_checker.py +++ b/tests/test_checker.py @@ -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" @@ -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): @@ -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): diff --git a/tests/test_common_api.py b/tests/test_common_api.py index d34c33f2..32309515 100644 --- a/tests/test_common_api.py +++ b/tests/test_common_api.py @@ -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') diff --git a/tests/test_conf_api.py b/tests/test_conf_api.py index 6587cfc0..72fae78b 100644 --- a/tests/test_conf_api.py +++ b/tests/test_conf_api.py @@ -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): diff --git a/tests/test_config.py b/tests/test_config.py index 01d5b3cf..10a5a7d3 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -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 @@ -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 diff --git a/tests/test_db.py b/tests/test_db.py index 2cdac9b3..461a83d5 100644 --- a/tests/test_db.py +++ b/tests/test_db.py @@ -1,11 +1,11 @@ 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 @@ -13,13 +13,6 @@ 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 = { @@ -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 diff --git a/tests/test_dm.py b/tests/test_dm.py index 647059e4..893ca024 100644 --- a/tests/test_dm.py +++ b/tests/test_dm.py @@ -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 @@ -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: @@ -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): diff --git a/tests/test_dm_api.py b/tests/test_dm_api.py index dbb37680..28d4376d 100644 --- a/tests/test_dm_api.py +++ b/tests/test_dm_api.py @@ -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 @@ -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: @@ -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): diff --git a/tests/test_dm_config.py b/tests/test_dm_config.py index 4cdcfb4e..b0f68712 100644 --- a/tests/test_dm_config.py +++ b/tests/test_dm_config.py @@ -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 @@ -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: @@ -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' @@ -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): diff --git a/tests/test_dm_label.py b/tests/test_dm_label.py index 29fdb6b5..09f8d9d8 100644 --- a/tests/test_dm_label.py +++ b/tests/test_dm_label.py @@ -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 diff --git a/tests/test_dm_temp.py b/tests/test_dm_temp.py index 3ee77087..59e83f5a 100644 --- a/tests/test_dm_temp.py +++ b/tests/test_dm_temp.py @@ -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 @@ -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): @@ -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): diff --git a/tests/test_event.py b/tests/test_event.py index 06f0857a..cab390e8 100644 --- a/tests/test_event.py +++ b/tests/test_event.py @@ -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)]) @@ -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() @@ -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): diff --git a/tests/test_extension_encoder_decoder.py b/tests/test_extension_encoder_decoder.py index 08aebd9b..0900b627 100644 --- a/tests/test_extension_encoder_decoder.py +++ b/tests/test_extension_encoder_decoder.py @@ -1,6 +1,5 @@ import pytest from copy import deepcopy -from lyrebird.event import EventServer from lyrebird import application from lyrebird.checker import LyrebirdCheckerServer from lyrebird.mock.handlers.encoder_decoder_handler import EncoderDecoder @@ -45,7 +44,6 @@ def checker_init(tmp_path, tmpdir): } # init file dir - encoder_decoder_file = tmp_path / FILENAME encoder_decoder_file.write_text(CONTENT) @@ -66,26 +64,19 @@ def checker_server(checker_init, tmp_path): server.stop() -@pytest.fixture -def event_server(): - server = EventServer() - application.server['event'] = server - yield server - - -def test_encoder_handler_match_and_no_output(event_server, checker_server): +def test_encoder_handler_match_and_no_output(checker_server): flow = deepcopy(FLOW_DATA_MATCH) application.encoders_decoders.encoder_handler(flow) assert flow['request']['data'] == 'encode' -def test_encoder_handler_not_match_and_no_output(event_server, checker_server): +def test_encoder_handler_not_match_and_no_output(checker_server): flow = deepcopy(FLOW_DATA_NO_MATCH) application.encoders_decoders.encoder_handler(flow) assert flow['request']['data'] == '' -def test_encoder_handler_match_and_exist_output(event_server, checker_server): +def test_encoder_handler_match_and_exist_output(checker_server): flow = deepcopy(FLOW_DATA_MATCH) output = {} application.encoders_decoders.encoder_handler(flow, output) @@ -95,7 +86,7 @@ def test_encoder_handler_match_and_exist_output(event_server, checker_server): assert flow['request']['data'] == '' -def test_encoder_handler_not_match_and_exist_output(event_server, checker_server): +def test_encoder_handler_not_match_and_exist_output(checker_server): flow = deepcopy(FLOW_DATA_NO_MATCH) output = {} application.encoders_decoders.encoder_handler(flow, output) @@ -105,19 +96,19 @@ def test_encoder_handler_not_match_and_exist_output(event_server, checker_server assert flow['request']['data'] == '' -def test_decoder_handler_match_and_no_output(event_server, checker_server): +def test_decoder_handler_match_and_no_output(checker_server): flow = deepcopy(FLOW_DATA_MATCH) application.encoders_decoders.decoder_handler(flow) assert flow['request']['data'] == 'decode' -def test_decoder_handler_not_match_and_no_output(event_server, checker_server): +def test_decoder_handler_not_match_and_no_output(checker_server): flow = deepcopy(FLOW_DATA_NO_MATCH) application.encoders_decoders.decoder_handler(flow) assert flow['request']['data'] == '' -def test_decoder_handler_match_and_exist_output(event_server, checker_server): +def test_decoder_handler_match_and_exist_output(checker_server): flow = deepcopy(FLOW_DATA_MATCH) output = {} application.encoders_decoders.decoder_handler(flow, output) @@ -127,7 +118,7 @@ def test_decoder_handler_match_and_exist_output(event_server, checker_server): assert flow['request']['data'] == '' -def test_decoder_handler_not_match_and_exist_output(event_server, checker_server): +def test_decoder_handler_not_match_and_exist_output(checker_server): flow = deepcopy(FLOW_DATA_NO_MATCH) output = {} application.encoders_decoders.decoder_handler(flow, output) diff --git a/tests/test_extra_mock_server.py b/tests/test_extra_mock_server.py index e9524798..d146c556 100644 --- a/tests/test_extra_mock_server.py +++ b/tests/test_extra_mock_server.py @@ -12,7 +12,7 @@ }} -async def test_proxy_args_in_path(aiohttp_client, loop): +async def test_proxy_args_in_path(aiohttp_client): app = init_app(config) client = await aiohttp_client(app) resp = await client.get('/http://www.bing.com') @@ -21,7 +21,7 @@ async def test_proxy_args_in_path(aiohttp_client, loop): assert 'bing' in text -async def test_proxy_args_in_headers(aiohttp_client, loop): +async def test_proxy_args_in_headers(aiohttp_client): app = init_app(config) client = await aiohttp_client(app) resp = await client.get('/http://www.bing.com', headers={ @@ -33,7 +33,7 @@ async def test_proxy_args_in_headers(aiohttp_client, loop): assert 'bing' in text -async def test_proxy_args_in_query_v1(aiohttp_client, loop): +async def test_proxy_args_in_query_v1(aiohttp_client): app = init_app(config) client = await aiohttp_client(app) resp = await client.get('/?proxy=http%3A//www.bing.com') @@ -42,7 +42,7 @@ async def test_proxy_args_in_query_v1(aiohttp_client, loop): assert 'bing' in text -async def test_proxy_args_in_query_v2(aiohttp_client, loop): +async def test_proxy_args_in_query_v2(aiohttp_client): app = init_app(config) client = await aiohttp_client(app) resp = await client.get('/?proxyscheme=http&proxyhost=www.bing.com') diff --git a/tests/test_flow_api.py b/tests/test_flow_api.py index 5738d037..5d2528af 100644 --- a/tests/test_flow_api.py +++ b/tests/test_flow_api.py @@ -107,10 +107,13 @@ def get_server(): return server -with get_server().app.test_client() as init_client: - context.application.cache._cache.clear() - for url in REQUEST_URL: - init_client.get(url) +@pytest.fixture(scope='module', autouse=True) +def setup_and_teardown_environment(): + with get_server().app.test_client() as init_client: + context.application.cache._cache.clear() + for url in REQUEST_URL: + init_client.get(url) + yield @pytest.fixture @@ -133,8 +136,8 @@ def checker_server(tmp_path, tmpdir): @pytest.fixture def client(checker_server): server = get_server() - client = server.app.test_client() - yield client + with server.app.test_client() as client: + yield client def test_flow_list_with_get(client): diff --git a/tests/test_mock_server.py b/tests/test_mock_server.py index 4a438635..9770085e 100644 --- a/tests/test_mock_server.py +++ b/tests/test_mock_server.py @@ -1,10 +1,8 @@ from lyrebird.mock.handlers.encoder_decoder_handler import EncoderDecoder from lyrebird.mock.mock_server import LyrebirdMockServer from lyrebird.mock import context -from lyrebird.task import BackgroundTaskServer -from lyrebird.event import EventServer from lyrebird import application -from lyrebird import reporter +from .utils import FakeEvnetServer, FakeReportor, FakeBackgroundTaskServer import json import pytest @@ -32,15 +30,15 @@ @pytest.fixture def client(): - # application.config = conf application._cm = MockConfigManager(config=conf) - application.server['event'] = EventServer() - application.reporter = reporter.Reporter() - application.server['task'] = BackgroundTaskServer() + application.server['event'] = FakeEvnetServer() + application.reporter = FakeReportor() + application.server['task'] = FakeBackgroundTaskServer() application.encoders_decoders = EncoderDecoder() server = LyrebirdMockServer() - client = server.app.test_client() - yield client + server.app.testing = True + with server.app.test_client() as client: + yield client @pytest.fixture diff --git a/tests/test_notice_center.py b/tests/test_notice_center.py index 0c11231c..60d2eac4 100644 --- a/tests/test_notice_center.py +++ b/tests/test_notice_center.py @@ -1,10 +1,8 @@ from typing import NamedTuple +from .utils import FakeEvnetServer from lyrebird.mock import context from lyrebird.notice_center import NoticeCenter -from lyrebird import reporter from lyrebird import application -from lyrebird.event import EventServer -from lyrebird.task import BackgroundTaskServer import pytest @@ -33,36 +31,17 @@ def emit(*args, **kwargs):pass @pytest.fixture def event_server(): - _conf = { - 'ip': '127.0.0.1', - 'mock.port': 9090 - } - application._cm = MockConfigManager(config=_conf) - server = EventServer() - server.start() - application.server['event'] = server - yield server - server.stop() + application.server['event'] = FakeEvnetServer() -@pytest.fixture -def task_server(): - application.reporter = reporter.Reporter() - server = BackgroundTaskServer() - server.start() - application.server['task'] = server - yield server - server.stop() - - -def test_new_notice(event_server, task_server, notice_center): +def test_new_notice(event_server, notice_center): notice_center.notice_hashmap = {} notice_center.new_notice(NOTICE) test_str = NOTICE.get('title') assert bool(notice_center.notice_hashmap.get(test_str)) == True -def test_new_notices(event_server, task_server, notice_center): +def test_new_notices(event_server, notice_center): notice_center.notice_hashmap = {} times = 10 for _ in range(times): @@ -75,7 +54,7 @@ def test_new_notices(event_server, task_server, notice_center): assert len_notice_list == times -def test_change_notice_status(event_server, task_server, notice_center): +def test_change_notice_status(event_server, notice_center): notice_center.notice_hashmap = {} notice_center.new_notice(NOTICE) test_str = '1st title' @@ -84,7 +63,7 @@ def test_change_notice_status(event_server, task_server, notice_center): assert notice_center.notice_hashmap[test_str].get('alert') == test_status -def test_delete_notice(event_server, task_server, notice_center): +def test_delete_notice(event_server, notice_center): notice_center.notice_hashmap = {} notice_center.new_notice(NOTICE) test_str = '1st title' diff --git a/tests/test_plugins.py b/tests/test_plugins.py index 0fc01faf..5d67dda8 100644 --- a/tests/test_plugins.py +++ b/tests/test_plugins.py @@ -1,5 +1,5 @@ import pytest -from lyrebird.event import EventServer +from .utils import FakeEvnetServer from lyrebird.mock.mock_server import LyrebirdMockServer from lyrebird import application from lyrebird.plugins import PluginManager @@ -121,13 +121,12 @@ def mock_server(): server = LyrebirdMockServer() application.server['mock'] = server yield server + del server @pytest.fixture def event_server(): - server = EventServer() - application.server['event'] = server - yield server + application.server['event'] = FakeEvnetServer() @pytest.fixture diff --git a/tests/utils.py b/tests/utils.py new file mode 100644 index 00000000..b949bb7b --- /dev/null +++ b/tests/utils.py @@ -0,0 +1,59 @@ +class FakeSocketio: + + def emit(self, event, *args, **kwargs): + print(f'Send event {event} args={args} kw={kwargs}') + + def run(self, *args, **kwargs): + pass + + def stop(self, *args, **kwargs): + pass + + +class FakeEvnetServer: + + def run(self, *args, **kwargs): + pass + + def stop(self, *args, **kwargs): + pass + + def terminate(self, *args, **kwargs): + pass + + def publish(self, *args, **kwargs): + pass + + def subscribe(self, *args, **kwargs): + pass + + def unsubscribe(self, *args, **kwargs): + pass + +class FakeReportor: + + def run(self, *args, **kwargs): + pass + + def stop(self, *args, **kwargs): + pass + + def terminate(self, *args, **kwargs): + pass + + def report(self, *args, **kwargs): + pass + +class FakeBackgroundTaskServer: + + def run(self, *args, **kwargs): + pass + + def stop(self, *args, **kwargs): + pass + + def terminate(self, *args, **kwargs): + pass + + def add_task(self, *args, **kwargs): + pass