-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: unit tests for json_message and log_util (#29)
* add unit tests for json_message utility * add test_log_util.py * remove unused path_builder code * test_path_builder.py: more concise dict definition Co-authored-by: Geary-Layne <[email protected]>
- Loading branch information
1 parent
4313c22
commit b896095
Showing
6 changed files
with
228 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
"""Test suite for json_message.py""" | ||
# ---------------------------------------------------------------------------------- | ||
# Created on Fri Oct 20 2023 | ||
# | ||
# Copyright (c) 2023 Regents of the University of Colorado. All rights reserved. (1) | ||
# Copyright (c) 2023 Colorado State University. All rights reserved. (2) | ||
# | ||
# Contributors: | ||
# Mackenzie Grimes (2) | ||
# | ||
# ---------------------------------------------------------------------------------- | ||
# pylint: disable=missing-function-docstring | ||
|
||
import json | ||
from datetime import datetime, UTC | ||
from uuid import uuid4 as uuid | ||
|
||
from idsse.common.json_message import get_corr_id, add_corr_id | ||
from idsse.common.utils import to_iso | ||
|
||
# test data | ||
EXAMPLE_ORIGINATOR = 'idsse' | ||
EXAMPLE_UUID = str(uuid()) | ||
EXAMPLE_ISSUE_DT = to_iso(datetime.now(UTC)) | ||
|
||
EXAMPLE_MESSAGE = { | ||
'corrId': { | ||
'originator': EXAMPLE_ORIGINATOR, 'uuid': EXAMPLE_UUID, 'issueDt': EXAMPLE_ISSUE_DT | ||
} | ||
} | ||
|
||
|
||
def test_get_corr_id_dict(): | ||
result = get_corr_id(EXAMPLE_MESSAGE) | ||
assert result == (EXAMPLE_ORIGINATOR, EXAMPLE_UUID, EXAMPLE_ISSUE_DT) | ||
|
||
|
||
def test_get_corr_id_str(): | ||
result = get_corr_id(json.dumps(EXAMPLE_MESSAGE)) | ||
assert result == (EXAMPLE_ORIGINATOR, EXAMPLE_UUID, EXAMPLE_ISSUE_DT) | ||
|
||
|
||
def test_get_corr_id_empty_corr_id(): | ||
result = get_corr_id({'other_data': 123}) | ||
assert result is None | ||
|
||
|
||
def test_get_corr_id_originator(): | ||
result = get_corr_id({'corrId': {'originator': EXAMPLE_ORIGINATOR}}) | ||
assert result == (EXAMPLE_ORIGINATOR, None, None) | ||
|
||
|
||
def test_get_corr_id_uuid(): | ||
result = get_corr_id({'corrId': {'uuid': EXAMPLE_UUID}}) | ||
assert result == (None, EXAMPLE_UUID, None) | ||
|
||
|
||
def test_get_corr_id_issue_dt(): | ||
result = get_corr_id({'corrId': {'issueDt': EXAMPLE_ISSUE_DT}}) | ||
assert result == (None, None, EXAMPLE_ISSUE_DT) | ||
|
||
|
||
def test_json_get_corr_id_failure(): | ||
bad_message = {'invalid': 'message'} | ||
result = get_corr_id(bad_message) | ||
assert result is None | ||
|
||
|
||
def test_add_corr_id(): | ||
new_originator = 'different_app' | ||
new_message = add_corr_id(EXAMPLE_MESSAGE, new_originator) | ||
|
||
# blank issueDt and random uuid should have been returned | ||
result = new_message['corrId'] | ||
assert result['originator'] == new_originator | ||
assert result['issueDt'] == '_' | ||
assert result['uuid'] != EXAMPLE_UUID | ||
|
||
|
||
def test_add_corr_id_str(): | ||
new_originator = 'different_app' | ||
new_message = add_corr_id(json.dumps(EXAMPLE_MESSAGE), new_originator) | ||
assert new_message['corrId']['originator'] == new_originator | ||
|
||
|
||
def test_add_corr_id_uuid(): | ||
new_uuid = uuid() | ||
new_message = add_corr_id(EXAMPLE_MESSAGE, EXAMPLE_ORIGINATOR, uuid_=new_uuid) | ||
|
||
# blank issueDt and random uuid should have been returned | ||
assert new_message['corrId']['uuid'] == str(new_uuid) | ||
assert new_message['corrId']['originator'] == EXAMPLE_ORIGINATOR | ||
|
||
|
||
def test_add_corr_id_issue_dt(): | ||
new_issue = to_iso(datetime.now(UTC)) | ||
new_message = add_corr_id(EXAMPLE_MESSAGE, EXAMPLE_ORIGINATOR, issue_dt=new_issue) | ||
assert new_message['corrId']['issueDt'] == new_issue |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
"""Test suite for json_message.py""" | ||
# ---------------------------------------------------------------------------------- | ||
# Created on Fri Oct 20 2023 | ||
# | ||
# Copyright (c) 2023 Regents of the University of Colorado. All rights reserved. (1) | ||
# Copyright (c) 2023 Colorado State University. All rights reserved. (2) | ||
# | ||
# Contributors: | ||
# Mackenzie Grimes (2) | ||
# | ||
# ---------------------------------------------------------------------------------- | ||
# pylint: disable=missing-function-docstring,redefined-outer-name,invalid-name,unused-argument | ||
|
||
import logging | ||
import logging.config | ||
from datetime import datetime, UTC | ||
from uuid import uuid4 as uuid | ||
|
||
from idsse.common.log_util import ( | ||
set_corr_id_context_var, | ||
get_corr_id_context_var_parts, | ||
get_corr_id_context_var_str, | ||
get_default_log_config, | ||
AddCorrelationIdFilter | ||
) | ||
from idsse.common.utils import to_iso | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
# test data | ||
EXAMPLE_ORIGINATOR = 'idsse' | ||
EXAMPLE_UUID = uuid() | ||
EXAMPLE_ISSUE_DT = datetime.now(UTC) | ||
EXAMPLE_ISSUE_STR = to_iso(EXAMPLE_ISSUE_DT) | ||
EXAMPLE_LOG_MESSAGE = 'hello world' | ||
|
||
|
||
def test_add_correlation_id_filter(): | ||
filter_object = AddCorrelationIdFilter() | ||
test_record = logging.LogRecord( | ||
name=EXAMPLE_ORIGINATOR, | ||
level=logging.INFO, | ||
pathname='./some/path/to/file.py', | ||
lineno=123, | ||
msg='hello world', | ||
args=None, | ||
exc_info=(None, None, None) | ||
) | ||
|
||
# corr_id does not exist yet, so filter fails | ||
assert not filter_object.filter(test_record) | ||
|
||
# set corr_id so filter does match | ||
set_corr_id_context_var(EXAMPLE_ORIGINATOR) | ||
assert filter_object.filter(test_record) | ||
|
||
|
||
def test_set_corr_id(): | ||
set_corr_id_context_var(EXAMPLE_ORIGINATOR, EXAMPLE_UUID, EXAMPLE_ISSUE_STR) | ||
|
||
expected_result = [EXAMPLE_ORIGINATOR, str(EXAMPLE_UUID), EXAMPLE_ISSUE_STR] | ||
assert get_corr_id_context_var_parts() == expected_result | ||
assert get_corr_id_context_var_str() == ';'.join(expected_result) | ||
|
||
|
||
def test_set_corr_id_datetime(): | ||
set_corr_id_context_var(EXAMPLE_ORIGINATOR, key=EXAMPLE_UUID, issue_dt=EXAMPLE_ISSUE_DT) | ||
|
||
assert get_corr_id_context_var_parts() == [ | ||
EXAMPLE_ORIGINATOR, str(EXAMPLE_UUID), EXAMPLE_ISSUE_STR | ||
] | ||
|
||
|
||
def test_get_default_log_config_with_corr_id(capsys): | ||
logging.config.dictConfig(get_default_log_config('INFO')) | ||
corr_id = get_corr_id_context_var_str() | ||
|
||
logger.debug(msg=EXAMPLE_LOG_MESSAGE) | ||
stdout = capsys.readouterr().out # capture std output from test run | ||
|
||
# should not be logging DEBUG if default log config handled level correctly | ||
assert stdout == '' | ||
logger.info(msg=EXAMPLE_LOG_MESSAGE) | ||
stdout = capsys.readouterr().out | ||
|
||
assert EXAMPLE_LOG_MESSAGE in stdout | ||
assert corr_id in stdout | ||
|
||
|
||
def test_get_default_log_config_no_corr_id(capsys): | ||
logging.config.dictConfigClass(get_default_log_config('DEBUG', False)) | ||
corr_id = get_corr_id_context_var_str() | ||
|
||
logger.debug('hello world') | ||
stdout = capsys.readouterr().out | ||
assert corr_id not in stdout |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters