From 917ab6ec865f55897139240dce33db3321663a37 Mon Sep 17 00:00:00 2001 From: Llewellyn Falco Date: Sun, 16 Jul 2023 18:04:26 +0000 Subject: [PATCH] F!! verify_logging Co-Authored-By: Nazee Hajebi <2491283+NazeeHajebi@users.noreply.github.com> Co-Authored-By: blade290 <43077216+blade290@users.noreply.github.com> Co-Authored-By: Nitsan Avni Co-Authored-By: Lev Konstantinovskiy <1261528+tmylk@users.noreply.github.com> Co-Authored-By: T. E. Green <78671457+Tegsy@users.noreply.github.com> --- approvaltests/scrubbers/date_scrubber.py | 4 ++ approvaltests/utilities/logging/__init__.py | 0 .../utilities/logging/logging_approvals.py | 32 ++++++++++++++++ requirements.prod.extras.txt | 2 + requirements.test.txt | 2 +- tests/logging/test_logging.py | 38 +++++++++++++++++++ ...st_logging.test_basic_logging.approved.txt | 4 ++ ...crubber.test_supported_formats.approved.md | 1 + ...ubber.test_unsupported_format.approved.txt | 1 + 9 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 approvaltests/utilities/logging/__init__.py create mode 100644 approvaltests/utilities/logging/logging_approvals.py create mode 100644 tests/logging/test_logging.py create mode 100644 tests/logging/test_logging.test_basic_logging.approved.txt diff --git a/approvaltests/scrubbers/date_scrubber.py b/approvaltests/scrubbers/date_scrubber.py index 4d52ec5b..eb13efc6 100644 --- a/approvaltests/scrubbers/date_scrubber.py +++ b/approvaltests/scrubbers/date_scrubber.py @@ -52,6 +52,10 @@ def get_supported_formats(): "\\d{4}-\\d{1,2}-\\d{1,2}T\\d{1,2}:\\d{2}\\:\\d{2}\\.\\d{3}Z", ["2020-09-10T01:23:45.678Z"], ), + ( + "\\d{4}-\\d{1,2}-\\d{1,2} \\d{1,2}:\\d{2}\\:\\d{2}\\.\\d{6}", + ["2023-07-16 17:39:03.293919"], + ), ("\\d{8}T\\d{6}Z", ["20210505T091112Z"]), ] diff --git a/approvaltests/utilities/logging/__init__.py b/approvaltests/utilities/logging/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/approvaltests/utilities/logging/logging_approvals.py b/approvaltests/utilities/logging/logging_approvals.py new file mode 100644 index 00000000..5a87e5ad --- /dev/null +++ b/approvaltests/utilities/logging/logging_approvals.py @@ -0,0 +1,32 @@ +import logging +from datetime import datetime +from typing import Optional, ContextManager + +from approvaltests import Options, verify +from testfixtures import LogCapture + +from approvaltests.scrubbers.date_scrubber import DateScrubber + +def verify_logging( + *, # enforce keyword arguments - https://www.python.org/dev/peps/pep-3102/ + options: Optional[Options] = None +) -> ContextManager: + class VerifyLogging: + def __init__(self): + self.l = LogCapture() + self.output = "anything" + self.options = options + if not options: + self.options = Options() + + def __enter__(self): + self.l.__enter__() + + pass + + def __exit__(self, exc_type, exc_val, exc_tb): + self.l.__exit__(exc_type, exc_val, exc_tb) + self.options = self.options.with_scrubber(DateScrubber.get_scrubber_for("2023-07-16 17:39:03.293919")) + verify(self.l, options=self.options) + + return VerifyLogging() \ No newline at end of file diff --git a/requirements.prod.extras.txt b/requirements.prod.extras.txt index 451c96dd..1934aff5 100644 --- a/requirements.prod.extras.txt +++ b/requirements.prod.extras.txt @@ -2,3 +2,5 @@ pyperclip>=1.5.29 # For Clipboard Reporter beautifulsoup4>=4.4.0 # For verify_html allpairspy>=2.1.0 # For PairwiseCombinations mrjob>=0.7.4 # For MrJob +testfixtures >= 7.1.0 # For verify_logging +mock >= 5.1.0 # For verify_logging \ No newline at end of file diff --git a/requirements.test.txt b/requirements.test.txt index 5e6f0c7e..d7c83d1b 100644 --- a/requirements.test.txt +++ b/requirements.test.txt @@ -2,4 +2,4 @@ numpy pylint pytest-asyncio pytest-xdist -# mariadb \ No newline at end of file +# mariadb diff --git a/tests/logging/test_logging.py b/tests/logging/test_logging.py new file mode 100644 index 00000000..c03832b4 --- /dev/null +++ b/tests/logging/test_logging.py @@ -0,0 +1,38 @@ +import logging +from datetime import datetime +from typing import Optional, ContextManager + +from approvaltests import Options, verify +from testfixtures import LogCapture + +from approvaltests.scrubbers.date_scrubber import DateScrubber + + +def verify_logging( + *, # enforce keyword arguments - https://www.python.org/dev/peps/pep-3102/ + options: Optional[Options] = None + ) -> ContextManager: + class VerifyLogging: + def __init__(self): + self.l = LogCapture() + self.output = "anything" + self.options = options + if not options: + self.options = Options() + def __enter__(self): + self.l.__enter__() + + + pass + + def __exit__(self, exc_type, exc_val, exc_tb): + self.l.__exit__(exc_type, exc_val, exc_tb) + self.options = self.options.with_scrubber(DateScrubber.get_scrubber_for("2023-07-16 17:39:03.293919")) + verify(self.l, options=self.options) + + return VerifyLogging() + +def test_basic_logging(): + with verify_logging(): + logging.info("1") + logging.info(datetime.now()) diff --git a/tests/logging/test_logging.test_basic_logging.approved.txt b/tests/logging/test_logging.test_basic_logging.approved.txt new file mode 100644 index 00000000..c7262b77 --- /dev/null +++ b/tests/logging/test_logging.test_basic_logging.approved.txt @@ -0,0 +1,4 @@ +root INFO + 1 +root INFO + diff --git a/tests/scrubbers/test_date_scrubber.test_supported_formats.approved.md b/tests/scrubbers/test_date_scrubber.test_supported_formats.approved.md index ab6b0520..b957d1af 100644 --- a/tests/scrubbers/test_date_scrubber.test_supported_formats.approved.md +++ b/tests/scrubbers/test_date_scrubber.test_supported_formats.approved.md @@ -11,4 +11,5 @@ | 2020-9-10T08:07Z | \d{4}-\d{1,2}-\d{1,2}T\d{1,2}:\d{2}Z | | 2020-09-10T08:07:89Z | \d{4}-\d{1,2}-\d{1,2}T\d{1,2}:\d{2}:\d{2}Z | | 2020-09-10T01:23:45.678Z | \d{4}-\d{1,2}-\d{1,2}T\d{1,2}:\d{2}\:\d{2}\.\d{3}Z | +| 2023-07-16 17:39:03.293919 | \d{4}-\d{1,2}-\d{1,2} \d{1,2}:\d{2}\:\d{2}\.\d{6} | | 20210505T091112Z | \d{8}T\d{6}Z | diff --git a/tests/scrubbers/test_date_scrubber.test_unsupported_format.approved.txt b/tests/scrubbers/test_date_scrubber.test_unsupported_format.approved.txt index 17e9b009..cc8c880c 100644 --- a/tests/scrubbers/test_date_scrubber.test_unsupported_format.approved.txt +++ b/tests/scrubbers/test_date_scrubber.test_unsupported_format.approved.txt @@ -12,4 +12,5 @@ Exception: No match found for 'an unsupported format'. 2020-9-10T08:07Z | \d{4}-\d{1,2}-\d{1,2}T\d{1,2}:\d{2}Z 2020-09-10T08:07:89Z | \d{4}-\d{1,2}-\d{1,2}T\d{1,2}:\d{2}:\d{2}Z 2020-09-10T01:23:45.678Z | \d{4}-\d{1,2}-\d{1,2}T\d{1,2}:\d{2}\:\d{2}\.\d{3}Z + 2023-07-16 17:39:03.293919 | \d{4}-\d{1,2}-\d{1,2} \d{1,2}:\d{2}\:\d{2}\.\d{6} 20210505T091112Z | \d{8}T\d{6}Z