Commit b5224f85 authored by Bishnoi, Bhaskar's avatar Bishnoi, Bhaskar
Browse files

removed unittest and added pytest

parent c49ee675
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -10,8 +10,8 @@ services:
      - ./test_envfile
    #command: ["sh", "-c", "pytest --cov=common /test/test.py"]
    #command: ["sh", "-c", "test-db-conn && pytest -v --cov=common /test/test.py"]
    command: ["sh", "-c", "test-db-conn && python3 /test/test.py"]
    #command: tail -f /dev/null
    #command: ["sh", "-c", "test-db-conn && python3 /test/test.py"]
    command: tail -f /dev/null
    depends_on:
      - postgres

+7 −2
Original line number Diff line number Diff line
@@ -60,14 +60,13 @@ def create_rich_logger(file_out=None,

    try:
        from rich.logging import RichHandler
        from rich.console import Console
        from rich.traceback import install
    except ImportError:
        raise ImportError("Failed to load rich logger")

    install()
    log_level = os.environ.get("LOGLEVEL", "INFO").upper()
    rich_handler = RichHandler(rich_tracebacks=True, markup=True, console=Console(stderr=True))
    rich_handler = RichHandler(rich_tracebacks=True, markup=True)
    file_handler, handlers = create_handler(file_out, mode, encoding, rich_handler)
    logging.basicConfig(
        level=log_level,
@@ -148,6 +147,12 @@ def create_python_logger(file_out=None,

    log_level = os.environ.get("LOGLEVEL", "INFO").upper()
    file_handler, handlers = create_handler(file_out, mode, encoding)
    stdout_handler = logging.StreamHandler(sys.stdout)
    stdout_handler_fmt = logging.Formatter(
            "[%(asctime)s]" + "%(levelname)8s - " + " - %(message)s"
    )
    stdout_handler.setFormatter(stdout_handler_fmt)
    handlers.append(stdout_handler)
    if file_handler:
        logging.basicConfig(
            level=log_level,
+27 −70
Original line number Diff line number Diff line
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import unittest
import pytest
import os
import io
import sys
from common.logz import create_logger
from common.file_operations import path_exists, read_file, delete_file

class TestLogger(unittest.TestCase):

    def setUp(self):
        self.stream_io = io.StringIO()
        self.system_stdr = sys.stderr
        sys.stderr = self.stream_io
        
    def tearDown(self):
        sys.stderr = self.system_stdr
        # remove log files
@pytest.fixture(autouse=True)
def cleanup_log_files():
    log_files = ["test_rich.log", "test_loguru.log", "test_python_logging.log"]
    yield 
    for file in log_files:
        if (path_exists(file)):
            delete_file(file)

    def test_rich_logger(self):
        log_file = "test_rich.log"
        logger = create_logger(file_out=log_file)
        custom_rich_text = "Testing rich logger 1"
        logger.info(custom_rich_text)

        stderr_output = self.stream_io.getvalue()
        self.assertIn(custom_rich_text, stderr_output)


        file_contents = read_file(log_file)
        if file_contents == None:
            return f"An error occured with rich file {log_file}"
        self.assertIn(custom_rich_text, file_contents)
@pytest.mark.parametrize("logger_type, log_file", [
    ("rich", "test_rich.log"),
    ("logger", "test_loguru.log"),
    ("logging", "test_python_logging.log")
])
def test_loggers(logger_type, log_file, capsys):
    logger = create_logger(file_out=log_file, logger_type=logger_type)
    custom_text = f"Test {logger_type} logger"
    logger.info(custom_text)

    def test_loguru(self):
        log_file = "test_loguru.log"
        logger = create_logger(logger_type="logger")
        custom_loguru_text = "Testing Loguru logger 2"
        logger.info(custom_loguru_text)
    captured = capsys.readouterr()
    stdout_output = captured.out
    assert custom_text in stdout_output + 'adadda', f"Expected '{custom_text}' in stdout, but got {stdout_output}"

        stderr_output = self.stream_io.getvalue()
        self.assertIn(custom_loguru_text, stderr_output)
        logger = None

        logger = create_logger(file_out=log_file, logger_type="logger")
        custom_loguru_text = "Testing Loguru logger 2.1"
        logger.info(custom_loguru_text)
    file_contents = read_file(log_file)
    if file_contents == None:
            return f"An error occured with loguru file {log_file}"
        self.assertIn(custom_loguru_text, file_contents)

    def test_python_logging(self):
        log_file = "test_python_logging.log"
        logger = create_logger(logger_type="logging")
        custom_python_logging_text = "Testing Python Logging logger 3"
        logger.info(custom_python_logging_text)

        stderr_output = self.stream_io.getvalue()
        self.assertIn(custom_python_logging_text, stderr_output)
        logger = None


        logger = create_logger(file_out=log_file, logger_type="logging")
        custom_python_logging_text = "Testing Loguru logger 3.1"
        logger.info(custom_python_logging_text)
        file_contents = read_file(log_file)
        if file_contents == None:
            return f"An error occured with Python logging file {log_file}"
        self.assertIn(custom_python_logging_text, file_contents)


if __name__ == "__main__":
    unittest.main(verbosity=2)
        return f"An error occured with file {log_file} for {logger_type} logger"
    assert custom_text in file_contents, f"Expected '{custom_text}' in log file, but got {file_contents}"