Commit 10e4403c authored by Hamaker, Alec's avatar Hamaker, Alec
Browse files

Finished tests for standalone create_logger

parent bce4a3fd
Loading
Loading
Loading
Loading
+53 −21
Original line number Diff line number Diff line
@@ -3,7 +3,6 @@
"""Unit test common functionality."""
import unittest
import os
from unittest.case import _AssertWarnsContext
from common.database import Database
from common.mixins.postgres import PostgresMixin
from common.logz import create_logger
@@ -11,38 +10,61 @@ from common.logz import create_logger

class DBPG(Database, PostgresMixin):
    """Create the Database and PostgresMixin class."""
    pass


class PostgresTestCase(unittest.TestCase):
    """Test the Postgres connection case."""
    def setUp(self) -> None:
        return super().setUp()

    def test_open(self):
        with DBPG() as db:
            print(db.query("SELECT * FROM test_table"))
        """
        test_open:
            Test that the DB can successfully be opened
        """
        with DBPG() as data_base:
            print(data_base.query("SELECT * FROM test_table"))


class LogzTestCase(unittest.TestCase):
    def setUp(self) -> None:
        return super().setUp()
    """
    LogzTestCase:
        Test case for testing the Logz.py file. This class tests both,
        create_logger's use with a database and standalone.

    :param unittest: Define this class as an invididual unit of testing
    :type unittest: TestCase
    """

    def test_create_logger(self):
        with DBPG() as db:
            db.logger.info("THIS IS TEST 1!")
            db.logger.info("THIS IS TEST 2!")
            db.logger.debug("THIS IS TEST 3!")
            db.logger.error("THIS IS TEST 4!")
        """
        test_create_logger:
            Method to test the create_logger function
        """
        # create a DB connection
        with DBPG() as data_base:
            # use the logger in the db to log some info
            data_base.logger.info("THIS IS TEST 1!")
            data_base.logger.info("THIS IS TEST 2!")
            data_base.logger.debug("THIS IS TEST 3!")
            data_base.logger.error("THIS IS TEST 4!")
            # verify that the log file is indeed a file
            self.assertTrue(os.path.isfile("./test.log"))
            # verify that the file exists
            self.assertTrue(os.path.exists("./test.log"))
            # get the size of the file
            file_size = os.path.getsize("./test.log")
            # verify that there is data within the file
            self.assertTrue(file_size > 0)
        with open("./test.log") as file:

        # open the log file
        with open("./test.log", 'r', encoding="utf-8") as file:
            # create bools to track if the info was found in the file
            found_t1 = False
            found_t2 = False
            found_t3 = False
            found_t4 = False
            # iterate through the file's lines
            for line in file.readlines():
                # if any of the test logs are found, record their existance
                if "THIS IS TEST 1!" in line:
                    found_t1 = True
                elif "THIS IS TEST 2!" in line and "INFO" in line:
@@ -51,19 +73,29 @@ class LogzTestCase(unittest.TestCase):
                    found_t3 = True
                elif "THIS IS TEST 4!" in line and "ERROR" in line:
                    found_t4 = True
            # validate that all the test logs were found
            self.assertTrue(found_t1 and found_t2 and found_t3 and found_t4)
        path = "standalone.log"
        logger = create_logger(file_out=path, mode='w', encoding="utf-8")

        # create a path for the standalone log file
        path = "./standalone.log"
        # create a logger to test its abilities standalone
        logger = create_logger(path, 'a', "utf-8")
        # log some info
        logger.info("THIS IS TEST 5!")
        logger.info("THIS IS TEST 5!")
        logger.info("THIS IS TEST 5!")
        # verify that a the path is a file
        self.assertTrue(os.path.isfile(path))
        # verify that the file exists
        self.assertTrue(os.path.exists(path))
        # verify that there is data in the file
        file_size = os.path.getsize(path)
        self.assertGreater(file_size, 0)


        # verify that the data from the logger is in the file
        with open(path, 'r', encoding="utf-8") as file:
            line = file.readline()
            self.assertTrue("THIS IS TEST 5" in line)
            self.assertTrue("INFO" in line)
            file.close()


if __name__ == "__main__":