Commit 430905a7 authored by Hamaker, Alec's avatar Hamaker, Alec
Browse files

Added additional testing to create_logger and split tests

parent 0de470ca
Loading
Loading
Loading
Loading
+45 −34
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@
import unittest
import os
import subprocess
import logging
from common.database import Database
from common.mixins.postgres import PostgresMixin
from common.logz import create_logger
@@ -15,7 +16,6 @@ class DBPG(Database, PostgresMixin):

class PostgresTestCase(unittest.TestCase):
    """Test the Postgres connection case."""

    def test_open(self):
        """
        test_open:
@@ -27,10 +27,12 @@ class PostgresTestCase(unittest.TestCase):

class DBConnTestCase(unittest.TestCase):
    def test_connection(self):
        """test the database connection."""
        status_run = subprocess.call('test-db-conn')
        print(status_run)
        self.assertTrue(status_run == 0)


class LogzTestCase(unittest.TestCase):
    """
    LogzTestCase:
@@ -40,14 +42,36 @@ class LogzTestCase(unittest.TestCase):
    :param unittest: Define this class as an invididual unit of testing
    :type unittest: TestCase
    """
    def test_create_logger_file_creation(self):
        """test_create_logger_file_creation."""
        # 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
        log_str = "THIS IS TEST 5! (info level)"
        logger.info(log_str)
        # 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)

    def test_create_logger(self):
        """
        test_create_logger:
            Method to test the create_logger function
        """
        # 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(log_str in line)
            self.assertTrue("INFO" in line)
            file.close()


    def test_create_logger_db(self):
        # create a DB connection
        with DBPG() as data_base:
            path = "/tmp/test.log"
            self.assertTrue(isinstance(data_base.logger, logging.Logger))
            # use the logger in the db to log some info
            data_base.logger.info("THIS IS TEST 1! (info)")
            data_base.logger.info("<-- Info test.")
@@ -55,16 +79,16 @@ class LogzTestCase(unittest.TestCase):
            data_base.logger.error("<-- Error test.")
            data_base.logger.critical("<-- Critical test.")
            # verify that the log file is indeed a file
            self.assertTrue(os.path.isfile("./test.log"))
            self.assertTrue(os.path.isfile("/tmp/test.log"))
            # verify that the file exists
            self.assertTrue(os.path.exists("./test.log"))
            self.assertTrue(os.path.exists("/tmp/test.log"))
            # get the size of the file
            file_size = os.path.getsize("./test.log")
            file_size = os.path.getsize("/tmp/test.log")
            # verify that there is data within the file
            self.assertTrue(file_size > 0)

        # open the log file
        with open("./test.log", 'r', encoding="utf-8") as file:
        with open("/tmp/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
@@ -73,7 +97,7 @@ class LogzTestCase(unittest.TestCase):
            found_t5 = 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! (info)" in line:
                    found_t1 = True
                elif "<-- Info test." in line and "INFO" in line:
@@ -85,29 +109,16 @@ class LogzTestCase(unittest.TestCase):
                elif "<-- Critical test." in line and "CRITICAL" in line:
                    found_t5 = True
            # validate that all the test logs were found
            self.assertTrue(found_t1 and found_t2 and found_t3 and found_t4 and found_t5)

        # 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
        log_str = "THIS IS TEST 5! (info level)"
        logger.info(log_str)
        # 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)
            self.assertTrue(found_t1 and found_t2 and found_t3 and found_t4
                            and found_t5)

        # 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(log_str in line)
            self.assertTrue("INFO" in line)
            file.close()
    def test_create_logger(self):
        """
        test_create_logger:
            Method to test the create_logger function
        """
        log = create_logger()
        self.assertTrue(isinstance(log, logging.Logger))


if __name__ == "__main__":