###############################################################################
# File:        UnitTest_zxLog.py
# Appointment: unittests for log 
# Project:     
# Author:      AR
# History:
# 12.04.2014 Created
###############################################################################
import unittest
import sys
sys.path.append("../")

import zxLog

###############################################################################
class TestZXLog(unittest.TestCase):
    #>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    class CTestLogTarget(zxLog.CLogTarget):
        def __init__(self):
            self.Severity = zxLog.LOG_UNKNOWN
            self.Message = ""

        # Redefined virtual
        def Log(self, aSeverity, aMessage):
            self.Message = aMessage
            self.Severity = aSeverity
    #<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    
    def setUp(self):
        pass

    #--------------------------------------------------------------------------
    def test_LogTarget(self):
        target1 = self.CTestLogTarget()
        target2 = self.CTestLogTarget()
        
        log = zxLog.CLog()
        self.assertEqual(0, log.CountTargets())
        log.AddTarget(target1)
        self.assertEqual(1, log.CountTargets())
        
        # Do some log
        self.assertEqual(zxLog.LOG_UNKNOWN, target1.Severity)
        self.assertEqual('', target1.Message)
        self.assertEqual(zxLog.LOG_UNKNOWN, target2.Severity)
        self.assertEqual('', target2.Message)
        log.Log(zxLog.LOG_INFO, 'hello')
        self.assertEqual(zxLog.LOG_INFO, target1.Severity)
        self.assertEqual('hello', target1.Message)
        self.assertEqual(zxLog.LOG_UNKNOWN, target2.Severity)
        self.assertEqual('', target2.Message)
        
        # Register 2nd target and do more log
        log.AddTarget(target2)
        self.assertEqual(2, log.CountTargets())
        log.Log(zxLog.LOG_ERROR, 'hi')
        self.assertEqual(zxLog.LOG_ERROR, target1.Severity)
        self.assertEqual('hi', target1.Message)
        self.assertEqual(zxLog.LOG_ERROR, target2.Severity)
        self.assertEqual('hi', target2.Message)


    #--------------------------------------------------------------------------
    def test_LogLevels(self):
        log = zxLog.CLog()
        
        # Normal level by default
        self.assertEqual(zxLog.LEVEL_NORMAL, log.Level)

        target = self.CTestLogTarget()
        log.AddTarget(target)
        self.assertEqual('', target.Message)
        self.assertEqual(zxLog.LOG_UNKNOWN, target.Severity)

        # Log with level NO or below - no effect
        log.Log(zxLog.LOG_INFO, 'hello', zxLog.LEVEL_NO)
        self.assertEqual('', target.Message)
        self.assertEqual(zxLog.LOG_UNKNOWN, target.Severity)
        log.Log(zxLog.LOG_INFO, 'hello', -1)
        self.assertEqual('', target.Message)
        self.assertEqual(zxLog.LOG_UNKNOWN, target.Severity)
        
        # Logs by default with normal level
        log.Log(zxLog.LOG_INFO, 'hello')
        self.assertEqual('hello', target.Message)
        self.assertEqual(zxLog.LOG_INFO, target.Severity)
        
        # Detailed or debug level should not be logged
        log.Log(zxLog.LOG_ERROR, 'detailed', zxLog.LEVEL_DETAILED)
        self.assertEqual('hello', target.Message)
        self.assertEqual(zxLog.LOG_INFO, target.Severity)
        log.Log(zxLog.LOG_WARNING, 'debug', zxLog.LEVEL_DEBUG)
        self.assertEqual('hello', target.Message)
        self.assertEqual(zxLog.LOG_INFO, target.Severity)
        
        # Increase log level
        log.Level = zxLog.LEVEL_DETAILED
        
        # Now detailed should be logged
        log.Log(zxLog.LOG_ERROR, 'detailed', zxLog.LEVEL_DETAILED)
        self.assertEqual('detailed', target.Message)
        self.assertEqual(zxLog.LOG_ERROR, target.Severity)
        #...but not debug
        log.Log(zxLog.LOG_WARNING, 'debug', zxLog.LEVEL_DEBUG)
        self.assertEqual('detailed', target.Message)
        self.assertEqual(zxLog.LOG_ERROR, target.Severity)
        # Normal should be logged
        log.Log(zxLog.LOG_INFO, 'hello')
        self.assertEqual('hello', target.Message)
        self.assertEqual(zxLog.LOG_INFO, target.Severity)
        
        # Increase level again
        log.Level = zxLog.LEVEL_DEBUG
        # Now debug should be fine
        log.Log(zxLog.LOG_WARNING, 'debug', zxLog.LEVEL_DEBUG)
        self.assertEqual('debug', target.Message)
        self.assertEqual(zxLog.LOG_WARNING, target.Severity)
        # All below as well
        log.Log(zxLog.LOG_ERROR, 'detailed', zxLog.LEVEL_DETAILED)
        self.assertEqual('detailed', target.Message)
        self.assertEqual(zxLog.LOG_ERROR, target.Severity)
        log.Log(zxLog.LOG_INFO, 'hello')
        self.assertEqual('hello', target.Message)
        self.assertEqual(zxLog.LOG_INFO, target.Severity)
        
        # Set log level to NO
        log.Level = zxLog.LEVEL_NO
        # Reset target
        target.Severity = zxLog.LOG_UNKNOWN
        target.Message = ""
        # Should be no log
        log.Log(zxLog.LOG_INFO, 'hello')
        self.assertEqual('', target.Message)
        self.assertEqual(zxLog.LOG_UNKNOWN, target.Severity)
        log.Log(zxLog.LOG_WARNING, 'debug', zxLog.LEVEL_DEBUG)
        self.assertEqual('', target.Message)
        self.assertEqual(zxLog.LOG_UNKNOWN, target.Severity)
        log.Log(zxLog.LOG_WARNING, 'debug', zxLog.LEVEL_DEBUG)
        self.assertEqual('', target.Message)
        self.assertEqual(zxLog.LOG_UNKNOWN, target.Severity)
        log.Log(zxLog.LOG_WARNING, 'debug', 0)
        self.assertEqual('', target.Message)
        self.assertEqual(zxLog.LOG_UNKNOWN, target.Severity)
        log.Log(zxLog.LOG_WARNING, 'debug', -12)
        self.assertEqual('', target.Message)
        self.assertEqual(zxLog.LOG_UNKNOWN, target.Severity)
        

################################################################################
if __name__ == "__main__":
    unittest.main()
