diff options
author | Michal Jagiello <michal.jagiello@t-mobile.pl> | 2020-07-02 12:34:25 +0000 |
---|---|---|
committer | Michal Jagiello <michal.jagiello@t-mobile.pl> | 2020-07-13 08:57:03 +0000 |
commit | b437b2c38ec0d5bb234fef0459b0e0533d08b7af (patch) | |
tree | a234cd12effde9854b77ea3ceaa6848a80a241b0 /pylog/tests/test_mdc_formatter.py | |
parent | d80e67cc8994fb0b92c8c1b5e3f51bde21b4a791 (diff) |
Pylog test suite
Prepare test suite for onaplogging library.
Add utils to check Python version.
Issue-ID: LOG-1229
Signed-off-by: Michal Jagiello <michal.jagiello@t-mobile.pl>
Change-Id: I12c431c10f61fceaf70d5ae36581dfd5a876ef72
Diffstat (limited to 'pylog/tests/test_mdc_formatter.py')
-rw-r--r-- | pylog/tests/test_mdc_formatter.py | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/pylog/tests/test_mdc_formatter.py b/pylog/tests/test_mdc_formatter.py new file mode 100644 index 0000000..6866b9d --- /dev/null +++ b/pylog/tests/test_mdc_formatter.py @@ -0,0 +1,108 @@ +# Copyright (c) 2020 Deutsche Telekom. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at: +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + +import sys +import unittest + +if sys.version_info[0] < 3: + from mock import MagicMock, patch +if sys.version_info[0] >= 3: + from unittest.mock import MagicMock, patch + +import pytest + +from onaplogging.mdcformatter import MDCFormatter + + +class TestMdcFormatter(unittest.TestCase): + + def test_mdc_formatter_init(self): + mdc_formatter = MDCFormatter() + self.assertEqual(mdc_formatter.style, "%") + self.assertEqual(mdc_formatter._mdc_tag, "%(mdc)s") + self.assertEqual(mdc_formatter._mdcFmt, "{reqeustID}") + + mdc_formatter = MDCFormatter(mdcfmt="{test}") + self.assertEqual(mdc_formatter.style, "%") + self.assertEqual(mdc_formatter._mdc_tag, "%(mdc)s") + self.assertEqual(mdc_formatter._mdcFmt, "{test}") + + if sys.version_info[0] >= 3: + mdc_formatter = MDCFormatter(style="{") + self.assertEqual(mdc_formatter.style, "{") + self.assertEqual(mdc_formatter._mdc_tag, "{mdc}") + self.assertEqual(mdc_formatter._mdcFmt, "{reqeustID}") + + mdc_formatter = MDCFormatter(style="$") + self.assertEqual(mdc_formatter.style, "$") + self.assertEqual(mdc_formatter._mdc_tag, "${mdc}") + self.assertEqual(mdc_formatter._mdcFmt, "{reqeustID}") + + with pytest.raises(ValueError): + MDCFormatter(style="*") + + def test_mdc_fmt_key(self): + mdc_formatter = MDCFormatter() + brace, not_brace = mdc_formatter._mdcfmtKey() + self.assertEqual(brace, ["{reqeustID}"]) + self.assertEqual(list(not_brace), ["reqeustID"]) + + mdc_formatter = MDCFormatter(mdcfmt="{test} {value} {anything}") + brace, not_brace = mdc_formatter._mdcfmtKey() + self.assertEqual(brace, ["{test}", "{value}", "{anything}"]) + self.assertEqual(list(not_brace), ["test", "value", "anything"]) + + mdc_formatter = MDCFormatter(mdcfmt="no_braces") + brace, not_brace = mdc_formatter._mdcfmtKey() + self.assertEqual(brace, []) + self.assertIsNone(not_brace) + + mdc_formatter = MDCFormatter(mdcfmt="}what?{") + brace, not_brace = mdc_formatter._mdcfmtKey() + self.assertEqual(brace, []) + self.assertIsNone(not_brace) + + mdc_formatter = MDCFormatter(mdcfmt="}{hello}{") + brace, not_brace = mdc_formatter._mdcfmtKey() + self.assertEqual(brace, ["{hello}"]) + self.assertEqual(list(not_brace), ["hello"]) + + mdc_formatter = MDCFormatter(mdcfmt="}{}{hel{lo}{") + brace, not_brace = mdc_formatter._mdcfmtKey() + self.assertEqual(brace, []) + self.assertIsNone(not_brace) + + def test_format(self): + record = MagicMock() + with patch("onaplogging.mdcformatter.MarkerFormatter.format") as mock_marker_formatter_format: + mdc_formatter = MDCFormatter() + mdc_formatter.format(record) + mock_marker_formatter_format.assert_called_once_with(record) + self.assertEqual(mdc_formatter._fmt, "%(message)s") + + if sys.version_info[0] >= 3: + with patch("onaplogging.mdcformatter.MarkerFormatter.format") as mock_marker_formatter_format: + mdc_formatter = MDCFormatter(fmt="{mdc}", style="{", mdcfmt="{key}") + mdc_formatter.format(record) + mock_marker_formatter_format.assert_called_once_with(record) + self.assertEqual(mdc_formatter._fmt, "key=") + + record.mdc = {"key": 123} + with patch("onaplogging.mdcformatter.MarkerFormatter.format") as mock_marker_formatter_format: + mdc_formatter = MDCFormatter(fmt="{mdc}", style="{", mdcfmt="no_braces") + mdc_formatter.format(record) + mock_marker_formatter_format.assert_called_once_with(record) + self.assertEqual(mdc_formatter._fmt, "") + + with patch("onaplogging.mdcformatter.MarkerFormatter.format") as mock_marker_formatter_format: + mdc_formatter = MDCFormatter(fmt="{mdc}", style="{", mdcfmt="{key}") + mdc_formatter.format(record) + mock_marker_formatter_format.assert_called_once_with(record) + self.assertEqual(mdc_formatter._fmt, "key=123") |