From: Eduardo Sousa Date: Thu, 3 May 2018 14:47:41 +0000 (+0100) Subject: Adding tests for MsgLocal X-Git-Tag: v4.0.0~8 X-Git-Url: https://osm.etsi.org/gitweb/?p=osm%2Fcommon.git;a=commitdiff_plain;h=acbbdf2e24f53353f18b60fbb67b33f616bb3aae;hp=e74238f281baa37a6ec32f9118cce37190a24403 Adding tests for MsgLocal Change-Id: Id9a2136593636bd07302bbb60b0fd8f1d327dae2 Signed-off-by: Eduardo Sousa --- diff --git a/osm_common/tests/test_dbmemory.py b/osm_common/tests/test_dbmemory.py index f73c317..55c900f 100644 --- a/osm_common/tests/test_dbmemory.py +++ b/osm_common/tests/test_dbmemory.py @@ -23,8 +23,6 @@ def db_memory_with_data(): return db - - def empty_exception_message(): return 'database exception ' @@ -68,8 +66,7 @@ def test_db_connect(): ("test", {}), ("test", {"_id": 1}), ("test", {"data": 1}), - ("test", {"_id": 1, "data": 1}), -]) + ("test", {"_id": 1, "data": 1})]) def test_get_list_with_empty_db(db_memory, table, filter): result = db_memory.get_list(table, filter) @@ -89,8 +86,7 @@ def test_get_list_with_empty_db(db_memory, table, filter): ("test_table", {}, []), ("test_table", {"_id": 1}, []), ("test_table", {"data": 1}, []), - ("test_table", {"_id": 1, "data": 1}, []), -]) + ("test_table", {"_id": 1, "data": 1}, [])]) def test_get_list_with_non_empty_db(db_memory_with_data, table, filter, expected_data): result = db_memory_with_data.get_list(table, filter) @@ -118,8 +114,7 @@ def test_get_list_exception(db_memory_with_data): ("test", {"data": 3}, {"_id": 3, "data": 3}), ("test", {"_id": 1, "data": 1}, {"_id": 1, "data": 1}), ("test", {"_id": 2, "data": 2}, {"_id": 2, "data": 2}), - ("test", {"_id": 3, "data": 3}, {"_id": 3, "data": 3}), -]) + ("test", {"_id": 3, "data": 3}, {"_id": 3, "data": 3})]) def test_get_one(db_memory_with_data, table, filter, expected_data): result = db_memory_with_data.get_one(table, filter) @@ -130,8 +125,7 @@ def test_get_one(db_memory_with_data, table, filter, expected_data): assert result in db_memory_with_data.db[table] @pytest.mark.parametrize("table, filter, expected_data", [ - ("test", {}, {"_id": 1, "data": 1}), -]) + ("test", {}, {"_id": 1, "data": 1})]) def test_get_one_with_multiple_results(db_memory_with_data, table, filter, expected_data): result = db_memory_with_data.get_one(table, filter, fail_on_more=False) @@ -157,8 +151,7 @@ def test_get_one_with_multiple_results_exception(db_memory_with_data): ("test", {"_id": 4, "data": 4}), ("test_table", {"_id": 4}), ("test_table", {"data": 4}), - ("test_table", {"_id": 4, "data": 4}), -]) + ("test_table", {"_id": 4, "data": 4})]) def test_get_one_with_non_empty_db_exception(db_memory_with_data, table, filter): with pytest.raises(DbException) as excinfo: db_memory_with_data.get_one(table, filter) @@ -171,8 +164,7 @@ def test_get_one_with_non_empty_db_exception(db_memory_with_data, table, filter) ("test", {"_id": 4, "data": 4}), ("test_table", {"_id": 4}), ("test_table", {"data": 4}), - ("test_table", {"_id": 4, "data": 4}), -]) + ("test_table", {"_id": 4, "data": 4})]) def test_get_one_with_non_empty_db_none(db_memory_with_data, table, filter): result = db_memory_with_data.get_one(table, filter, fail_on_empty=False) @@ -184,8 +176,7 @@ def test_get_one_with_non_empty_db_none(db_memory_with_data, table, filter): ("test", {"_id": 4, "data": 4}), ("test_table", {"_id": 4}), ("test_table", {"data": 4}), - ("test_table", {"_id": 4, "data": 4}), -]) + ("test_table", {"_id": 4, "data": 4})]) def test_get_one_with_empty_db_exception(db_memory, table, filter): with pytest.raises(DbException) as excinfo: db_memory.get_one(table, filter) @@ -198,8 +189,7 @@ def test_get_one_with_empty_db_exception(db_memory, table, filter): ("test", {"_id": 4, "data": 4}), ("test_table", {"_id": 4}), ("test_table", {"data": 4}), - ("test_table", {"_id": 4, "data": 4}), -]) + ("test_table", {"_id": 4, "data": 4})]) def test_get_one_with_empty_db_none(db_memory, table, filter): result = db_memory.get_one(table, filter, fail_on_empty=False) @@ -221,8 +211,7 @@ def test_get_one_generic_exception(db_memory_with_data): ("test", {"_id": 1}, [{"_id": 2, "data": 2}, {"_id": 3, "data": 3}]), ("test", {"_id": 2}, [{"_id": 1, "data": 1}, {"_id": 3, "data": 3}]), ("test", {"_id": 1, "data": 1}, [{"_id": 2, "data": 2}, {"_id": 3, "data": 3}]), - ("test", {"_id": 2, "data": 2}, [{"_id": 1, "data": 1}, {"_id": 3, "data": 3}]), -]) + ("test", {"_id": 2, "data": 2}, [{"_id": 1, "data": 1}, {"_id": 3, "data": 3}])]) def test_del_list_with_non_empty_db(db_memory_with_data, table, filter, expected_data): result = db_memory_with_data.del_list(table, filter) @@ -240,8 +229,7 @@ def test_del_list_with_non_empty_db(db_memory_with_data, table, filter, expected ("test", {"data": 1}), ("test", {"data": 2}), ("test", {"_id": 1, "data": 1}), - ("test", {"_id": 2, "data": 2}), -]) + ("test", {"_id": 2, "data": 2})]) def test_del_list_with_empty_db(db_memory, table, filter): result = db_memory.del_list(table, filter) assert result['deleted'] == 0 @@ -264,8 +252,7 @@ def test_del_list_generic_exception(db_memory_with_data): ("test", {"_id": 1, "data": 1}, {"_id": 1, "data": 1}), ("test", {"_id": 2}, {"_id": 2, "data": 2}), ("test", {"data": 2}, {"_id": 2, "data": 2}), - ("test", {"_id": 2, "data": 2}, {"_id": 2, "data": 2}), -]) + ("test", {"_id": 2, "data": 2}, {"_id": 2, "data": 2})]) def test_del_one(db_memory_with_data, table, filter, data): result = db_memory_with_data.del_one(table, filter) @@ -289,8 +276,7 @@ def test_del_one(db_memory_with_data, table, filter, data): ("test_table", {"data": 1}), ("test_table", {"data": 2}), ("test_table", {"_id": 1, "data": 1}), - ("test_table", {"_id": 2, "data": 2}), -]) + ("test_table", {"_id": 2, "data": 2})]) def test_del_one_with_empty_db_exception(db_memory, table, filter): with pytest.raises(DbException) as excinfo: db_memory.del_one(table, filter) @@ -311,8 +297,7 @@ def test_del_one_with_empty_db_exception(db_memory, table, filter): ("test_table", {"data": 1}), ("test_table", {"data": 2}), ("test_table", {"_id": 1, "data": 1}), - ("test_table", {"_id": 2, "data": 2}), -]) + ("test_table", {"_id": 2, "data": 2})]) def test_del_one_with_empty_db_none(db_memory, table, filter): result = db_memory.del_one(table, filter, fail_on_empty=False) @@ -331,8 +316,7 @@ def test_del_one_with_empty_db_none(db_memory, table, filter): ("test_table", {"data": 1}), ("test_table", {"data": 2}), ("test_table", {"_id": 1, "data": 1}), - ("test_table", {"_id": 2, "data": 2}), -]) + ("test_table", {"_id": 2, "data": 2})]) def test_del_one_with_non_empty_db_exception(db_memory_with_data, table, filter): with pytest.raises(DbException) as excinfo: db_memory_with_data.del_one(table, filter) @@ -352,8 +336,7 @@ def test_del_one_with_non_empty_db_exception(db_memory_with_data, table, filter) ("test_table", {"data": 1}), ("test_table", {"data": 2}), ("test_table", {"_id": 1, "data": 1}), - ("test_table", {"_id": 2, "data": 2}), -]) + ("test_table", {"_id": 2, "data": 2})]) def test_del_one_with_non_empty_db_none(db_memory_with_data, table, filter): result = db_memory_with_data.del_one(table, filter, fail_on_empty=False) @@ -361,8 +344,7 @@ def test_del_one_with_non_empty_db_none(db_memory_with_data, table, filter): @pytest.mark.parametrize("fail_on_empty", [ (True), - (False), -]) + (False)]) def test_del_one_generic_exception(db_memory_with_data, fail_on_empty): table = 'test' filter = {} @@ -382,8 +364,7 @@ def test_del_one_generic_exception(db_memory_with_data, fail_on_empty): ("test", {"data": 1}, {"_id": 3, "data": 42}), ("test", {"data": 3}, {"_id": 3, "data": 42}), ("test", {"_id": 1, "data": 1}, {"_id": 3, "data": 42}), - ("test", {"_id": 3, "data": 3}, {"_id": 3, "data": 42}), -]) + ("test", {"_id": 3, "data": 3}, {"_id": 3, "data": 42})]) def test_replace(db_memory_with_data, table, filter, indata): result = db_memory_with_data.replace(table, filter, indata) @@ -403,8 +384,7 @@ def test_replace(db_memory_with_data, table, filter, indata): ("test_table", {}, {'_id': 2, 'data': 1}), ("test_table", {}, {'_id': 1, 'data': 2}), ("test_table", {'_id': 1}, {'_id': 1, 'data': 1}), - ("test_table", {'_id': 1, 'data': 1}, {'_id': 1, 'data': 1}), -]) + ("test_table", {'_id': 1, 'data': 1}, {'_id': 1, 'data': 1})]) def test_replace_without_data_exception(db_memory, table, filter, indata): with pytest.raises(DbException) as excinfo: db_memory.replace(table, filter, indata, fail_on_empty=True) @@ -421,8 +401,7 @@ def test_replace_without_data_exception(db_memory, table, filter, indata): ("test_table", {}, {'_id': 2, 'data': 1}), ("test_table", {}, {'_id': 1, 'data': 2}), ("test_table", {'_id': 1}, {'_id': 1, 'data': 1}), - ("test_table", {'_id': 1, 'data': 1}, {'_id': 1, 'data': 1}), -]) + ("test_table", {'_id': 1, 'data': 1}, {'_id': 1, 'data': 1})]) def test_replace_without_data_none(db_memory, table, filter, indata): result = db_memory.replace(table, filter, indata, fail_on_empty=False) assert result == None @@ -432,8 +411,7 @@ def test_replace_without_data_none(db_memory, table, filter, indata): ("test_table", {}, {'_id': 2, 'data': 1}), ("test_table", {}, {'_id': 1, 'data': 2}), ("test_table", {'_id': 1}, {'_id': 1, 'data': 1}), - ("test_table", {'_id': 1, 'data': 1}, {'_id': 1, 'data': 1}), -]) + ("test_table", {'_id': 1, 'data': 1}, {'_id': 1, 'data': 1})]) def test_replace_with_data_exception(db_memory_with_data, table, filter, indata): with pytest.raises(DbException) as excinfo: db_memory_with_data.replace(table, filter, indata, fail_on_empty=True) @@ -445,16 +423,14 @@ def test_replace_with_data_exception(db_memory_with_data, table, filter, indata) ("test_table", {}, {'_id': 2, 'data': 1}), ("test_table", {}, {'_id': 1, 'data': 2}), ("test_table", {'_id': 1}, {'_id': 1, 'data': 1}), - ("test_table", {'_id': 1, 'data': 1}, {'_id': 1, 'data': 1}), -]) + ("test_table", {'_id': 1, 'data': 1}, {'_id': 1, 'data': 1})]) def test_replace_with_data_none(db_memory_with_data, table, filter, indata): result = db_memory_with_data.replace(table, filter, indata, fail_on_empty=False) assert result == None @pytest.mark.parametrize("fail_on_empty", [ (True), - (False), -]) + (False)]) def test_replace_generic_exception(db_memory_with_data, fail_on_empty): table = 'test' filter = {} @@ -483,8 +459,7 @@ def test_replace_generic_exception(db_memory_with_data, fail_on_empty): ("test_table", "1", {"data_1": 1, "data_2": 2}), ("test_table", "1", {"data_1": 2, "data_2": 1}), ("test_table", "2", {"data_1": 1, "data_2": 2}), - ("test_table", "2", {"data_1": 2, "data_2": 1}), -]) + ("test_table", "2", {"data_1": 2, "data_2": 1})]) def test_create_with_empty_db_with_id(db_memory, table, id, data): data_to_insert = data data_to_insert['_id'] = id @@ -513,8 +488,7 @@ def test_create_with_empty_db_with_id(db_memory, table, id, data): ("test_table", "4", {"data_1": 1, "data_2": 2}), ("test_table", "5", {"data_1": 2, "data_2": 1}), ("test_table", "4", {"data_1": 1, "data_2": 2}), - ("test_table", "5", {"data_1": 2, "data_2": 1}), -]) + ("test_table", "5", {"data_1": 2, "data_2": 1})]) def test_create_with_non_empty_db_with_id(db_memory_with_data, table, id, data): data_to_insert = data data_to_insert['_id'] = id @@ -543,8 +517,7 @@ def test_create_with_non_empty_db_with_id(db_memory_with_data, table, id, data): ("test_table", {"data_1": 1, "data_2": 2}), ("test_table", {"data_1": 2, "data_2": 1}), ("test_table", {"data_1": 1, "data_2": 2}), - ("test_table", {"data_1": 2, "data_2": 1}), -]) + ("test_table", {"data_1": 2, "data_2": 1})]) def test_create_with_empty_db_without_id(db_memory, table, data): returned_id = db_memory.create(table, data) @@ -573,8 +546,7 @@ def test_create_with_empty_db_without_id(db_memory, table, data): ("test_table", {"data_1": 1, "data_2": 2}), ("test_table", {"data_1": 2, "data_2": 1}), ("test_table", {"data_1": 1, "data_2": 2}), - ("test_table", {"data_1": 2, "data_2": 1}), -]) + ("test_table", {"data_1": 2, "data_2": 1})]) def test_create_with_non_empty_db_without_id(db_memory_with_data, table, data): returned_id = db_memory_with_data.create(table, data) diff --git a/osm_common/tests/test_fslocal.py b/osm_common/tests/test_fslocal.py index 6a8e620..77886d8 100644 --- a/osm_common/tests/test_fslocal.py +++ b/osm_common/tests/test_fslocal.py @@ -66,8 +66,7 @@ def test_constructor_with_logger(): ({'logger_name': 'fs_local', 'path': valid_path()}, 'fs_local', valid_path()), ({'logger_name': 'fs_local', 'path': valid_path()[:-1]}, 'fs_local', valid_path()), ({'path': valid_path()}, 'fs', valid_path()), - ({'path': valid_path()[:-1]}, 'fs', valid_path()), -]) + ({'path': valid_path()[:-1]}, 'fs', valid_path())]) def test_fs_connect_with_valid_config(config, exp_logger, exp_path): fs = FsLocal() fs.fs_connect(config) @@ -79,8 +78,7 @@ def test_fs_connect_with_valid_config(config, exp_logger, exp_path): ({'logger_name': 'fs_local', 'path': invalid_path()}, fs_connect_exception_message(invalid_path())), ({'logger_name': 'fs_local', 'path': invalid_path()[:-1]}, fs_connect_exception_message(invalid_path()[:-1])), ({'path': invalid_path()}, fs_connect_exception_message(invalid_path())), - ({'path': invalid_path()[:-1]}, fs_connect_exception_message(invalid_path()[:-1])), -]) + ({'path': invalid_path()[:-1]}, fs_connect_exception_message(invalid_path()[:-1]))]) def test_fs_connect_with_invalid_path(config, exp_exception_message): fs = FsLocal() @@ -113,8 +111,7 @@ def test_mkdir_with_exception(fs_local): (str(uuid.uuid4()), 'file', False), ([str(uuid.uuid4())], 'file', False), (str(uuid.uuid4()), 'dir', False), - ([str(uuid.uuid4())], 'dir', False), -]) + ([str(uuid.uuid4())], 'dir', False)]) def test_file_exists_returns_false(fs_local, storage, mode, expected): assert fs_local.file_exists(storage, mode) == expected @@ -122,8 +119,7 @@ def test_file_exists_returns_false(fs_local, storage, mode, expected): (str(uuid.uuid4()), 'file', True), ([str(uuid.uuid4())], 'file', True), (str(uuid.uuid4()), 'dir', True), - ([str(uuid.uuid4())], 'dir', True), -]) + ([str(uuid.uuid4())], 'dir', True)]) def test_file_exists_returns_true(fs_local, storage, mode, expected): path = valid_path() + storage if isinstance(storage, str) else valid_path() + storage[0] @@ -143,8 +139,7 @@ def test_file_exists_returns_true(fs_local, storage, mode, expected): (str(uuid.uuid4()), 'file'), ([str(uuid.uuid4())], 'file'), (str(uuid.uuid4()), 'dir'), - ([str(uuid.uuid4())], 'dir'), -]) + ([str(uuid.uuid4())], 'dir')]) def test_file_size(fs_local, storage, mode): path = valid_path() + storage if isinstance(storage, str) else valid_path() + storage[0] @@ -164,8 +159,7 @@ def test_file_size(fs_local, storage, mode): @pytest.mark.parametrize("files, path", [ (['foo', 'bar', 'foobar'], str(uuid.uuid4())), - (['foo', 'bar', 'foobar'], [str(uuid.uuid4())]), -]) + (['foo', 'bar', 'foobar'], [str(uuid.uuid4())])]) def test_file_extract(fs_local, files, path): for f in files: os.mknod(valid_path() + f) @@ -204,8 +198,7 @@ def test_file_extract(fs_local, files, path): ([str(uuid.uuid4())], 'a'), ([str(uuid.uuid4())], 'rb'), ([str(uuid.uuid4())], 'wb'), - ([str(uuid.uuid4())], 'ab'), -]) + ([str(uuid.uuid4())], 'ab')]) def test_file_open(fs_local, storage, mode): path = valid_path() + storage if isinstance(storage, str) else valid_path() + storage[0] @@ -222,8 +215,7 @@ def test_file_open(fs_local, storage, mode): (str(uuid.uuid4()), 'r'), (str(uuid.uuid4()), 'rb'), ([str(uuid.uuid4())], 'r'), - ([str(uuid.uuid4())], 'rb'), -]) + ([str(uuid.uuid4())], 'rb')]) def test_file_open_file_not_found_exception(fs_local, storage, mode): with pytest.raises(FsException) as excinfo: fs_local.file_open(storage, mode) @@ -242,8 +234,7 @@ def test_file_open_file_not_found_exception(fs_local, storage, mode): ([str(uuid.uuid4())], 'a'), ([str(uuid.uuid4())], 'rb'), ([str(uuid.uuid4())], 'wb'), - ([str(uuid.uuid4())], 'ab'), -]) + ([str(uuid.uuid4())], 'ab')]) def test_file_open_io_error(fs_local, storage, mode): path = valid_path() + storage if isinstance(storage, str) else valid_path() + storage[0] @@ -261,8 +252,7 @@ def test_file_open_io_error(fs_local, storage, mode): (str(uuid.uuid4()), True), (str(uuid.uuid4()), False), ([str(uuid.uuid4())], True), - ([str(uuid.uuid4())], False), -]) + ([str(uuid.uuid4())], False)]) def test_dir_ls(fs_local, storage, with_files): path = valid_path() + storage if isinstance(storage, str) else valid_path() + storage[0] @@ -285,8 +275,7 @@ def test_dir_ls(fs_local, storage, with_files): @pytest.mark.parametrize("storage", [ (str(uuid.uuid4())), - ([str(uuid.uuid4())]), -]) + ([str(uuid.uuid4())])]) def test_dir_ls_with_not_a_directory_error(fs_local, storage): path = valid_path() + storage if isinstance(storage, str) else valid_path() + storage[0] @@ -301,8 +290,7 @@ def test_dir_ls_with_not_a_directory_error(fs_local, storage): @pytest.mark.parametrize("storage", [ (str(uuid.uuid4())), - ([str(uuid.uuid4())]), -]) + ([str(uuid.uuid4())])]) def test_dir_ls_with_io_error(fs_local, storage): path = valid_path() + storage if isinstance(storage, str) else valid_path() + storage[0] @@ -324,8 +312,7 @@ def test_dir_ls_with_io_error(fs_local, storage): ([str(uuid.uuid4())], True, True), ([str(uuid.uuid4())], False, True), ([str(uuid.uuid4())], True, False), - ([str(uuid.uuid4())], False, False), -]) + ([str(uuid.uuid4())], False, False)]) def test_file_delete_with_dir(fs_local, storage, with_files, ignore_non_exist): path = valid_path() + storage if isinstance(storage, str) else valid_path() + storage[0] @@ -341,8 +328,7 @@ def test_file_delete_with_dir(fs_local, storage, with_files, ignore_non_exist): @pytest.mark.parametrize("storage", [ (str(uuid.uuid4())), - ([str(uuid.uuid4())]), -]) + ([str(uuid.uuid4())])]) def test_file_delete_expect_exception(fs_local, storage): with pytest.raises(FsException) as excinfo: fs_local.file_delete(storage) @@ -351,12 +337,10 @@ def test_file_delete_expect_exception(fs_local, storage): @pytest.mark.parametrize("storage", [ (str(uuid.uuid4())), - ([str(uuid.uuid4())]), -]) + ([str(uuid.uuid4())])]) def test_file_delete_no_exception(fs_local, storage): path = valid_path() + storage if isinstance(storage, str) else valid_path() + storage[0] fs_local.file_delete(storage, ignore_non_exist=True) assert os.path.exists(path) == False - \ No newline at end of file diff --git a/osm_common/tests/test_msglocal.py b/osm_common/tests/test_msglocal.py new file mode 100644 index 0000000..93cd19b --- /dev/null +++ b/osm_common/tests/test_msglocal.py @@ -0,0 +1,372 @@ +import http +import logging +import pytest +import tempfile +import shutil +import uuid +import os +import yaml +import time +import threading + +from unittest.mock import MagicMock +from osm_common.msgbase import MsgException +from osm_common.msglocal import MsgLocal + +__author__ = "Eduardo Sousa " + +def valid_path(): + return tempfile.gettempdir() + '/' + +def invalid_path(): + return '/#tweeter/' + +@pytest.fixture +def msg_local(): + msg = MsgLocal() + + yield msg + + if msg.path and msg.path != invalid_path() and msg.path != valid_path(): + msg.disconnect() + shutil.rmtree(msg.path) + +@pytest.fixture +def msg_local_config(): + msg = MsgLocal() + msg.connect({"path": valid_path() + str(uuid.uuid4())}) + + yield msg + + msg.disconnect() + if msg.path != invalid_path(): + shutil.rmtree(msg.path) + +@pytest.fixture +def msg_local_with_data(): + msg = MsgLocal() + msg.connect({"path": valid_path() + str(uuid.uuid4())}) + + msg.write("topic1", "key1", "msg1") + msg.write("topic1", "key2", "msg1") + msg.write("topic2", "key1", "msg1") + msg.write("topic2", "key2", "msg1") + msg.write("topic1", "key1", "msg2") + msg.write("topic1", "key2", "msg2") + msg.write("topic2", "key1", "msg2") + msg.write("topic2", "key2", "msg2") + + yield msg + + msg.disconnect() + if msg.path != invalid_path(): + shutil.rmtree(msg.path) + +def empty_exception_message(): + return "messaging exception " + +def test_constructor(): + msg = MsgLocal() + + assert msg.logger == logging.getLogger('msg') + assert msg.path == None + assert len(msg.files) == 0 + +def test_constructor_with_logger(): + logger_name = 'msg_local' + + msg = MsgLocal(logger_name=logger_name) + + assert msg.logger == logging.getLogger(logger_name) + assert msg.path == None + assert len(msg.files) == 0 + +@pytest.mark.parametrize("config, logger_name, path", [ + ({"logger_name": "msg_local", "path": valid_path()}, "msg_local", valid_path()), + ({"logger_name": "msg_local", "path": valid_path()[:-1]}, "msg_local", valid_path()), + ({"logger_name": "msg_local", "path": valid_path() + "test_it/"}, "msg_local", valid_path() + "test_it/"), + ({"logger_name": "msg_local", "path": valid_path() + "test_it"}, "msg_local", valid_path() + "test_it/"), + ({"path": valid_path()}, "msg", valid_path()), + ({"path": valid_path()[:-1]}, "msg", valid_path()), + ({"path": valid_path() + "test_it/"}, "msg", valid_path() + "test_it/"), + ({"path": valid_path() + "test_it"}, "msg", valid_path() + "test_it/")]) +def test_connect(msg_local, config, logger_name, path): + msg_local.connect(config) + + assert msg_local.logger == logging.getLogger(logger_name) + assert msg_local.path == path + assert len(msg_local.files) == 0 + +@pytest.mark.parametrize("config", [ + ({"logger_name": "msg_local", "path": invalid_path()}), + ({"path": invalid_path()})]) +def test_connect_with_exception(msg_local, config): + with pytest.raises(MsgException) as excinfo: + msg_local.connect(config) + assert str(excinfo.value).startswith(empty_exception_message()) + assert excinfo.value.http_code == http.HTTPStatus.INTERNAL_SERVER_ERROR + +def test_disconnect(): + pass + +@pytest.mark.parametrize("topic, key, msg", [ + ("test_topic", "test_key", "test_msg"), + ("test", "test_key", "test_msg"), + ("test_topic", "test", "test_msg"), + ("test_topic", "test_key", "test"), + ("test_topic", "test_list", ["a", "b", "c"]), + ("test_topic", "test_tuple", ("c", "b", "a")), + ("test_topic", "test_dict", {"a": 1, "b": 2, "c": 3}), + ("test_topic", "test_number", 123), + ("test_topic", "test_float", 1.23), + ("test_topic", "test_boolean", True), + ("test_topic", "test_none", None)]) +def test_write(msg_local_config, topic, key, msg): + file_path = msg_local_config.path + topic + + msg_local_config.write(topic, key, msg) + + assert os.path.exists(file_path) + + with open(file_path, 'r') as stream: + assert yaml.load(stream) == {key: msg if not isinstance(msg, tuple) else list(msg)} + +@pytest.mark.parametrize("topic, key, msg, times", [ + ("test_topic", "test_key", "test_msg", 2), + ("test", "test_key", "test_msg", 3), + ("test_topic", "test", "test_msg", 4), + ("test_topic", "test_key", "test", 2), + ("test_topic", "test_list", ["a", "b", "c"], 3), + ("test_topic", "test_tuple", ("c", "b", "a"), 4), + ("test_topic", "test_dict", {"a": 1, "b": 2, "c": 3}, 2), + ("test_topic", "test_number", 123, 3), + ("test_topic", "test_float", 1.23, 4), + ("test_topic", "test_boolean", True, 2), + ("test_topic", "test_none", None, 3)]) +def test_write_with_multiple_calls(msg_local_config, topic, key, msg, times): + file_path = msg_local_config.path + topic + + for _ in range(times): + msg_local_config.write(topic, key, msg) + + assert os.path.exists(file_path) + + with open(file_path, 'r') as stream: + for _ in range(times): + data = stream.readline() + assert yaml.load(data) == {key: msg if not isinstance(msg, tuple) else list(msg)} + +def test_write_exception(msg_local_config): + msg_local_config.files = MagicMock() + msg_local_config.files.__contains__.side_effect = Exception() + + with pytest.raises(MsgException) as excinfo: + msg_local_config.write("test", "test", "test") + assert str(excinfo.value).startswith(empty_exception_message()) + assert excinfo.value.http_code == http.HTTPStatus.INTERNAL_SERVER_ERROR + +@pytest.mark.parametrize("topics, datas", [ + (["topic"], [{"key": "value"}]), + (["topic1"], [{"key": "value"}]), + (["topic2"], [{"key": "value"}]), + (["topic", "topic1"], [{"key": "value"}]), + (["topic", "topic2"], [{"key": "value"}]), + (["topic1", "topic2"], [{"key": "value"}]), + (["topic", "topic1", "topic2"], [{"key": "value"}]), + (["topic"], [{"key": "value"}, {"key1": "value1"}]), + (["topic1"], [{"key": "value"}, {"key1": "value1"}]), + (["topic2"], [{"key": "value"}, {"key1": "value1"}]), + (["topic", "topic1"], [{"key": "value"}, {"key1": "value1"}]), + (["topic", "topic2"], [{"key": "value"}, {"key1": "value1"}]), + (["topic1", "topic2"], [{"key": "value"}, {"key1": "value1"}]), + (["topic", "topic1", "topic2"], [{"key": "value"}, {"key1": "value1"}])]) +def test_read(msg_local_with_data, topics, datas): + def write_to_topic(topics, datas): + time.sleep(2) + for topic in topics: + for data in datas: + with open(msg_local_with_data.path + topic, "a+") as fp: + yaml.safe_dump(data, fp, default_flow_style=True, width=20000) + fp.flush() + + # If file is not opened first, the messages written won't be seen + for topic in topics: + if topic not in msg_local_with_data.files: + msg_local_with_data.read(topic, blocks=False) + + t = threading.Thread(target=write_to_topic, args=(topics, datas)) + t.start() + + for topic in topics: + for data in datas: + recv_topic, recv_key, recv_msg = msg_local_with_data.read(topic) + + key = list(data.keys())[0] + val = data[key] + + assert recv_topic == topic + assert recv_key == key + assert recv_msg == val + + t.join() + +@pytest.mark.parametrize("topics, datas", [ + (["topic"], [{"key": "value"}]), + (["topic1"], [{"key": "value"}]), + (["topic2"], [{"key": "value"}]), + (["topic", "topic1"], [{"key": "value"}]), + (["topic", "topic2"], [{"key": "value"}]), + (["topic1", "topic2"], [{"key": "value"}]), + (["topic", "topic1", "topic2"], [{"key": "value"}]), + (["topic"], [{"key": "value"}, {"key1": "value1"}]), + (["topic1"], [{"key": "value"}, {"key1": "value1"}]), + (["topic2"], [{"key": "value"}, {"key1": "value1"}]), + (["topic", "topic1"], [{"key": "value"}, {"key1": "value1"}]), + (["topic", "topic2"], [{"key": "value"}, {"key1": "value1"}]), + (["topic1", "topic2"], [{"key": "value"}, {"key1": "value1"}]), + (["topic", "topic1", "topic2"], [{"key": "value"}, {"key1": "value1"}])]) +def test_read_non_block(msg_local_with_data, topics, datas): + def write_to_topic(topics, datas): + for topic in topics: + for data in datas: + with open(msg_local_with_data.path + topic, "a+") as fp: + yaml.safe_dump(data, fp, default_flow_style=True, width=20000) + fp.flush() + + # If file is not opened first, the messages written won't be seen + for topic in topics: + if topic not in msg_local_with_data.files: + msg_local_with_data.read(topic, blocks=False) + + t = threading.Thread(target=write_to_topic, args=(topics, datas)) + t.start() + + time.sleep(2) + + for topic in topics: + for data in datas: + recv_topic, recv_key, recv_msg = msg_local_with_data.read(topic, blocks=False) + + key = list(data.keys())[0] + val = data[key] + + assert recv_topic == topic + assert recv_key == key + assert recv_msg == val + + t.join() + +@pytest.mark.parametrize("topics, datas", [ + (["topic"], [{"key": "value"}]), + (["topic1"], [{"key": "value"}]), + (["topic2"], [{"key": "value"}]), + (["topic", "topic1"], [{"key": "value"}]), + (["topic", "topic2"], [{"key": "value"}]), + (["topic1", "topic2"], [{"key": "value"}]), + (["topic", "topic1", "topic2"], [{"key": "value"}]), + (["topic"], [{"key": "value"}, {"key1": "value1"}]), + (["topic1"], [{"key": "value"}, {"key1": "value1"}]), + (["topic2"], [{"key": "value"}, {"key1": "value1"}]), + (["topic", "topic1"], [{"key": "value"}, {"key1": "value1"}]), + (["topic", "topic2"], [{"key": "value"}, {"key1": "value1"}]), + (["topic1", "topic2"], [{"key": "value"}, {"key1": "value1"}]), + (["topic", "topic1", "topic2"], [{"key": "value"}, {"key1": "value1"}])]) +def test_read_non_block_none(msg_local_with_data, topics, datas): + def write_to_topic(topics, datas): + time.sleep(2) + for topic in topics: + for data in datas: + with open(msg_local_with_data.path + topic, "a+") as fp: + yaml.safe_dump(data, fp, default_flow_style=True, width=20000) + fp.flush() + + # If file is not opened first, the messages written won't be seen + for topic in topics: + if topic not in msg_local_with_data.files: + msg_local_with_data.read(topic, blocks=False) + + t = threading.Thread(target=write_to_topic, args=(topics, datas)) + t.start() + + for topic in topics: + recv_data = msg_local_with_data.read(topic, blocks=False) + + assert recv_data == None + + t.join() + +@pytest.mark.parametrize("blocks", [ + (True), + (False)]) +def test_read_exception(msg_local_with_data, blocks): + msg_local_with_data.files = MagicMock() + msg_local_with_data.files.__contains__.side_effect = Exception() + + with pytest.raises(MsgException) as excinfo: + msg_local_with_data.read("topic1", blocks=blocks) + assert str(excinfo.value).startswith(empty_exception_message()) + assert excinfo.value.http_code == http.HTTPStatus.INTERNAL_SERVER_ERROR + +@pytest.mark.parametrize("topics, datas", [ + (["topic"], [{"key": "value"}]), + (["topic1"], [{"key": "value"}]), + (["topic2"], [{"key": "value"}]), + (["topic", "topic1"], [{"key": "value"}]), + (["topic", "topic2"], [{"key": "value"}]), + (["topic1", "topic2"], [{"key": "value"}]), + (["topic", "topic1", "topic2"], [{"key": "value"}]), + (["topic"], [{"key": "value"}, {"key1": "value1"}]), + (["topic1"], [{"key": "value"}, {"key1": "value1"}]), + (["topic2"], [{"key": "value"}, {"key1": "value1"}]), + (["topic", "topic1"], [{"key": "value"}, {"key1": "value1"}]), + (["topic", "topic2"], [{"key": "value"}, {"key1": "value1"}]), + (["topic1", "topic2"], [{"key": "value"}, {"key1": "value1"}]), + (["topic", "topic1", "topic2"], [{"key": "value"}, {"key1": "value1"}])]) +def test_aioread(msg_local_with_data, event_loop, topics, datas): + def write_to_topic(topics, datas): + time.sleep(2) + for topic in topics: + for data in datas: + with open(msg_local_with_data.path + topic, "a+") as fp: + yaml.safe_dump(data, fp, default_flow_style=True, width=20000) + fp.flush() + + # If file is not opened first, the messages written won't be seen + for topic in topics: + if topic not in msg_local_with_data.files: + msg_local_with_data.read(topic, blocks=False) + + t = threading.Thread(target=write_to_topic, args=(topics, datas)) + t.start() + + for topic in topics: + for data in datas: + recv = event_loop.run_until_complete(msg_local_with_data.aioread(topic, event_loop)) + recv_topic, recv_key, recv_msg = recv + + key = list(data.keys())[0] + val = data[key] + + assert recv_topic == topic + assert recv_key == key + assert recv_msg == val + + t.join() + +def test_aioread_exception(msg_local_with_data, event_loop): + msg_local_with_data.files = MagicMock() + msg_local_with_data.files.__contains__.side_effect = Exception() + + with pytest.raises(MsgException) as excinfo: + event_loop.run_until_complete(msg_local_with_data.aioread("topic1", event_loop)) + assert str(excinfo.value).startswith(empty_exception_message()) + assert excinfo.value.http_code == http.HTTPStatus.INTERNAL_SERVER_ERROR + +def test_aioread_general_exception(msg_local_with_data, event_loop): + msg_local_with_data.read = MagicMock() + msg_local_with_data.read.side_effect = Exception() + + with pytest.raises(MsgException) as excinfo: + event_loop.run_until_complete(msg_local_with_data.aioread("topic1", event_loop)) + assert str(excinfo.value).startswith(empty_exception_message()) + assert excinfo.value.http_code == http.HTTPStatus.INTERNAL_SERVER_ERROR \ No newline at end of file diff --git a/tox.ini b/tox.ini index a36b6ed..baf649b 100644 --- a/tox.ini +++ b/tox.ini @@ -4,7 +4,7 @@ toxworkdir={homedir}/.tox [testenv] basepython = python3 -deps = pytest +deps = pytest pytest-asyncio commands = pytest [testenv:flake8]