Skip to content

Commit

Permalink
Add tests to cover base logging behaviour.
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeZiminski committed May 2, 2024
1 parent ea9b144 commit d0fb327
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions tests/tests_integration/test_logging.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import glob
import logging
import os
import re
from pathlib import Path
Expand All @@ -8,13 +9,85 @@

from datashuttle import DataShuttle
from datashuttle.configs.canonical_tags import tags
from datashuttle.utils import ds_logger
from datashuttle.utils.custom_exceptions import (
ConfigError,
NeuroBlueprintError,
)


class TestLogging:

@pytest.fixture(scope="function")
def teardown_logger(self):
"""
Ensure the logger is deleted at the end of each test.
"""
yield
if "datashuttle" in logging.root.manager.loggerDict:
logging.root.manager.loggerDict.pop("datashuttle")

# -------------------------------------------------------------------------
# Basic Functionality Tests
# -------------------------------------------------------------------------

def test_logger_name(self):
"""
Check the canonical logger name.
"""
assert ds_logger.get_logger_name() == "datashuttle"

def test_start_logging(self, tmp_path, teardown_logger):
"""
Test that the central `start` logging function
starts the named logger with the expected handlers.
"""
assert ds_logger.logging_is_active() is False

ds_logger.start(tmp_path, "test-command", variables=[])

# test logger exists and is as expected
assert "datashuttle" in logging.root.manager.loggerDict
assert ds_logger.logging_is_active() is True

logger = logging.getLogger("datashuttle")
assert logger.propagate is False
assert len(logger.handlers) == 1
assert isinstance(logger.handlers[0], logging.FileHandler)

def test_shutdown_logger(self, tmp_path, teardown_logger):
"""
Check the log handler remover indeed removes the handles.
"""
assert ds_logger.logging_is_active() is False

ds_logger.start(tmp_path, "test-command", variables=[])

logger = logging.getLogger("datashuttle")

ds_logger.close_log_filehandler()

assert len(logger.handlers) == 0
assert ds_logger.logging_is_active() is False

def test_logging_an_error(self, project, teardown_logger):
"""
Check that errors are caught and logged properly.
"""
try:
project.create_folders("rawdata", "sob-001")
except:
pass

log = test_utils.read_log_file(project.cfg.logging_path)

assert "ERROR" in log
assert "Problem with name:" in log

# -------------------------------------------------------------------------
# Functional Tests
# -------------------------------------------------------------------------

@pytest.fixture(scope="function")
def clean_project_name(self):
"""
Expand Down

0 comments on commit d0fb327

Please sign in to comment.