base structure for logging and logging management via session, closes #2
This commit is contained in:
@@ -3,7 +3,7 @@ from __future__ import annotations
|
||||
import json
|
||||
import tomllib
|
||||
from pathlib import Path
|
||||
from typing import Any, cast
|
||||
from typing import cast
|
||||
from unittest.mock import patch
|
||||
|
||||
import pandas as pd
|
||||
|
||||
@@ -4,6 +4,7 @@ from unittest.mock import patch
|
||||
import pytest
|
||||
|
||||
import delta_barth.session
|
||||
from delta_barth import logging
|
||||
from delta_barth.constants import (
|
||||
DEFAULT_API_ERR_CODE,
|
||||
HTTP_BASE_CONTENT_HEADERS,
|
||||
@@ -55,6 +56,8 @@ def test_session_setup_db_management(tmp_path):
|
||||
assert db_path.parent == target_db_dir
|
||||
assert not db_path.exists()
|
||||
session.setup()
|
||||
db_path2 = session.db_path
|
||||
assert db_path2 == db_path
|
||||
assert session._db_engine is not None
|
||||
assert db_path.exists()
|
||||
|
||||
@@ -66,6 +69,30 @@ def test_session_setup_logging(tmp_path):
|
||||
foldername: str = "logging_test"
|
||||
target_log_dir = tmp_path / foldername
|
||||
|
||||
session = delta_barth.session.Session(
|
||||
HTTP_BASE_CONTENT_HEADERS, logging_folder=foldername
|
||||
)
|
||||
session.set_data_path(str_path)
|
||||
log_dir = session.logging_dir
|
||||
|
||||
assert log_dir.exists()
|
||||
assert log_dir == target_log_dir
|
||||
# write file
|
||||
target_file = target_log_dir / LOG_FILENAME
|
||||
assert not target_file.exists()
|
||||
session.setup() # calls setup code for logging
|
||||
log_dir2 = session.logging_dir
|
||||
assert log_dir2 == log_dir
|
||||
assert target_file.exists()
|
||||
|
||||
|
||||
@patch("delta_barth.logging.ENABLE_LOGGING", True)
|
||||
@patch("delta_barth.logging.LOGGING_TO_FILE", True)
|
||||
def test_session_disable_logging(tmp_path):
|
||||
str_path = str(tmp_path)
|
||||
foldername: str = "logging_test"
|
||||
target_log_dir = tmp_path / foldername
|
||||
|
||||
session = delta_barth.session.Session(
|
||||
HTTP_BASE_CONTENT_HEADERS, logging_folder=foldername
|
||||
)
|
||||
@@ -78,6 +105,21 @@ def test_session_setup_logging(tmp_path):
|
||||
assert not target_file.exists()
|
||||
session.setup() # calls setup code for logging
|
||||
assert target_file.exists()
|
||||
# provoke entry
|
||||
msg = "this is a test"
|
||||
logging.logger_base.critical(msg)
|
||||
session.disable_logging()
|
||||
with open(target_file, "r") as file:
|
||||
content = file.readlines()
|
||||
last_line = content[-1]
|
||||
assert msg in last_line.lower()
|
||||
# log new entry which should not be added as logging is disabled
|
||||
msg = "this is a second test"
|
||||
logging.logger_base.critical(msg)
|
||||
with open(target_file, "r") as file:
|
||||
content = file.readlines()
|
||||
last_line = content[-1]
|
||||
assert msg not in last_line.lower()
|
||||
|
||||
|
||||
def test_session_set_ApiInfo_LoggedOut(credentials, api_base_url):
|
||||
|
||||
Reference in New Issue
Block a user