From 112b4983d3a90dbf6f5b83563925b50d54ae8859 Mon Sep 17 00:00:00 2001 From: Steph Merritt Date: Fri, 10 May 2024 14:33:46 +0100 Subject: [PATCH] Unit test for log creation. --- tests/adler/utilities/test_adler_logging.py | 40 +++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 tests/adler/utilities/test_adler_logging.py diff --git a/tests/adler/utilities/test_adler_logging.py b/tests/adler/utilities/test_adler_logging.py new file mode 100644 index 0000000..eefd91a --- /dev/null +++ b/tests/adler/utilities/test_adler_logging.py @@ -0,0 +1,40 @@ +import glob +import os +import pytest +import tempfile + + +def test_setup_adler_logging(): + from adler.utilities.adler_logging import setup_adler_logging + + with tempfile.TemporaryDirectory() as dir_name: + logger = setup_adler_logging(dir_name) + + # Check that the files get created. + errlog = glob.glob(os.path.join(dir_name, "*-adler.err")) + datalog = glob.glob(os.path.join(dir_name, "*-adler.log")) + + assert os.path.exists(errlog[0]) + assert os.path.exists(datalog[0]) + + # Log some information. + logger.info("Test1") + logger.info("Test2") + logger.error("Error1") + logger.info("Test3") + + # Check that all five lines exist in the INFO file. + with open(datalog[0], "r") as f_info: + log_data = f_info.read() + assert "Test1" in log_data + assert "Test2" in log_data + assert "Error1" in log_data + assert "Test3" in log_data + + # Check that only error and critical lines exist in the ERROR file. + with open(errlog[0], "r") as f_err: + log_data = f_err.read() + assert "Test1" not in log_data + assert "Test2" not in log_data + assert "Error1" in log_data + assert "Test3" not in log_data