Skip to content

Commit

Permalink
F!! verify_logging
Browse files Browse the repository at this point in the history
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 <nitsanav@gmail.com>
Co-Authored-By: Lev Konstantinovskiy <1261528+tmylk@users.noreply.github.com>
Co-Authored-By: T. E. Green <78671457+Tegsy@users.noreply.github.com>
  • Loading branch information
6 people committed Jul 16, 2023
1 parent 0ae8d37 commit 917ab6e
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 1 deletion.
4 changes: 4 additions & 0 deletions approvaltests/scrubbers/date_scrubber.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]),
]

Expand Down
Empty file.
32 changes: 32 additions & 0 deletions approvaltests/utilities/logging/logging_approvals.py
Original file line number Diff line number Diff line change
@@ -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()
2 changes: 2 additions & 0 deletions requirements.prod.extras.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion requirements.test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ numpy
pylint
pytest-asyncio
pytest-xdist
# mariadb
# mariadb
38 changes: 38 additions & 0 deletions tests/logging/test_logging.py
Original file line number Diff line number Diff line change
@@ -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())
4 changes: 4 additions & 0 deletions tests/logging/test_logging.test_basic_logging.approved.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
root INFO
1
root INFO
<date0>
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 917ab6e

Please sign in to comment.