X-Git-Url: https://osm.etsi.org/gitweb/?p=osm%2Fcommon.git;a=blobdiff_plain;f=osm_common%2Ftests%2Ftest_msglocal.py;fp=osm_common%2Ftests%2Ftest_msglocal.py;h=95488859aee32008c42761bb5c78edb302eeac06;hp=41f6eb8d0d4c6ebcaec4dfff00855837809d50e0;hb=2644b76248a1b96f7a47013b414e31b4e3feecf8;hpb=fee977a304dbfadee797e25906df2db3cf51f1e3 diff --git a/osm_common/tests/test_msglocal.py b/osm_common/tests/test_msglocal.py index 41f6eb8..9548885 100644 --- a/osm_common/tests/test_msglocal.py +++ b/osm_common/tests/test_msglocal.py @@ -36,11 +36,11 @@ __author__ = "Eduardo Sousa " def valid_path(): - return tempfile.gettempdir() + '/' + return tempfile.gettempdir() + "/" def invalid_path(): - return '/#tweeter/' + return "/#tweeter/" @pytest.fixture(scope="function", params=[True, False]) @@ -90,7 +90,7 @@ def empty_exception_message(): def test_constructor(): msg = MsgLocal() - assert msg.logger == logging.getLogger('msg') + assert msg.logger == logging.getLogger("msg") assert msg.path is None assert len(msg.files_read) == 0 assert len(msg.files_write) == 0 @@ -98,7 +98,7 @@ def test_constructor(): def test_constructor_with_logger(): - logger_name = 'msg_local' + logger_name = "msg_local" msg = MsgLocal(logger_name=logger_name) assert msg.logger == logging.getLogger(logger_name) assert msg.path is None @@ -107,15 +107,31 @@ def test_constructor_with_logger(): assert len(msg.buffer) == 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/")]) +@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) @@ -125,9 +141,13 @@ def test_connect(msg_local, config, logger_name, path): assert len(msg_local.buffer) == 0 -@pytest.mark.parametrize("config", [ - ({"logger_name": "msg_local", "path": invalid_path()}), - ({"path": invalid_path()})]) +@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) @@ -146,8 +166,8 @@ def test_disconnect(msg_local_config): def test_disconnect_with_read(msg_local_config): - msg_local_config.read('topic1', blocks=False) - msg_local_config.read('topic2', blocks=False) + msg_local_config.read("topic1", blocks=False) + msg_local_config.read("topic2", blocks=False) files_read = msg_local_config.files_read.copy() files_write = msg_local_config.files_write.copy() msg_local_config.disconnect() @@ -164,14 +184,14 @@ def test_disconnect_with_write(msg_local_with_data): for f in files_read.values(): assert f.closed - + for f in files_write.values(): assert f.closed def test_disconnect_with_read_and_write(msg_local_with_data): - msg_local_with_data.read('topic1', blocks=False) - msg_local_with_data.read('topic2', blocks=False) + msg_local_with_data.read("topic1", blocks=False) + msg_local_with_data.read("topic2", blocks=False) files_read = msg_local_with_data.files_read.copy() files_write = msg_local_with_data.files_write.copy() @@ -182,77 +202,93 @@ def test_disconnect_with_read_and_write(msg_local_with_data): assert f.closed -@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)]) +@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.safe_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)]) + with open(file_path, "r") as stream: + assert yaml.safe_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: + with open(file_path, "r") as stream: for _ in range(times): data = stream.readline() - assert yaml.safe_load(data) == {key: msg if not isinstance(msg, tuple) else list(msg)} + assert yaml.safe_load(data) == { + key: msg if not isinstance(msg, tuple) else list(msg) + } def test_write_exception(msg_local_config): msg_local_config.files_write = MagicMock() msg_local_config.files_write.__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"}])]) +@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): # Allow msglocal to block while waiting @@ -282,21 +318,25 @@ def test_read(msg_local_with_data, topics, datas): 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"}])]) +@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: @@ -316,7 +356,9 @@ def test_read_non_block(msg_local_with_data, topics, datas): for topic in topics: for data in datas: - recv_topic, recv_key, recv_msg = msg_local_with_data.read(topic, blocks=False) + 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 @@ -324,21 +366,25 @@ def test_read_non_block(msg_local_with_data, topics, datas): assert recv_msg == val -@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"}])]) +@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) @@ -347,6 +393,7 @@ def test_read_non_block_none(msg_local_with_data, topics, 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_read: @@ -360,9 +407,7 @@ def test_read_non_block_none(msg_local_with_data, topics, datas): t.join() -@pytest.mark.parametrize("blocks", [ - (True), - (False)]) +@pytest.mark.parametrize("blocks", [(True), (False)]) def test_read_exception(msg_local_with_data, blocks): msg_local_with_data.files_read = MagicMock() msg_local_with_data.files_read.__contains__.side_effect = Exception() @@ -373,21 +418,25 @@ def test_read_exception(msg_local_with_data, blocks): 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"}])]) +@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) @@ -396,6 +445,7 @@ def test_aioread(msg_local_with_data, event_loop, topics, 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_read: @@ -405,7 +455,9 @@ def test_aioread(msg_local_with_data, event_loop, 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 = 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] @@ -435,55 +487,69 @@ def test_aioread_general_exception(msg_local_with_data, event_loop): assert excinfo.value.http_code == http.HTTPStatus.INTERNAL_SERVER_ERROR -@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)]) +@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_aiowrite(msg_local_config, event_loop, topic, key, msg): file_path = msg_local_config.path + topic event_loop.run_until_complete(msg_local_config.aiowrite(topic, key, msg)) assert os.path.exists(file_path) - with open(file_path, 'r') as stream: - assert yaml.safe_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_aiowrite_with_multiple_calls(msg_local_config, event_loop, topic, key, msg, times): + with open(file_path, "r") as stream: + assert yaml.safe_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_aiowrite_with_multiple_calls( + msg_local_config, event_loop, topic, key, msg, times +): file_path = msg_local_config.path + topic for _ in range(times): event_loop.run_until_complete(msg_local_config.aiowrite(topic, key, msg)) assert os.path.exists(file_path) - with open(file_path, 'r') as stream: + with open(file_path, "r") as stream: for _ in range(times): data = stream.readline() - assert yaml.safe_load(data) == {key: msg if not isinstance(msg, tuple) else list(msg)} + assert yaml.safe_load(data) == { + key: msg if not isinstance(msg, tuple) else list(msg) + } def test_aiowrite_exception(msg_local_config, event_loop): msg_local_config.files_write = MagicMock() msg_local_config.files_write.__contains__.side_effect = Exception() - + with pytest.raises(MsgException) as excinfo: event_loop.run_until_complete(msg_local_config.aiowrite("test", "test", "test")) assert str(excinfo.value).startswith(empty_exception_message())