please help me to write test-cases for that two functions log_from_api and check_and_update_15min_count
Published: 2020-03-30 12:01:04

import random
import unittest
import xmlrunner
import logging
from datetime import *
from pytz import *
import pytz
class TestSequenceFunctions(unittest.TestCase):
    global token_dict 
    token_dict= {
            'after_15_mins_timestamp': datetime.strptime("March 28, 2020 11:54:58 PM", '%B %d, %Y %H:%M:%S %p').replace(
                tzinfo=pytz.UTC),
            'disabled_reason_code': "",
            'email_address': "tomc94@gmail.com",
            'expired_timestamp': '',
            'ip_addresses_used': ["127.0.0.1"],
            'is_disabled': False,
            'is_expired': False,
            'last_used_timestamp': datetime.strptime("March 28, 2020 11:54:58 PM", '%B %d, %Y %H:%M:%S %p').replace(
                tzinfo=pytz.UTC),
            'token_granted_timestamp': datetime.strptime("March 28, 2020 11:54:58 PM", '%B %d, %Y %H:%M:%S %p').replace(
                tzinfo=pytz.UTC),
            'token_type': "access",
            'used_in_last_15_mins': 7,
            'userid': 78}
    def log_from_api(self, log_type, content):
         if log_type == 'INFO':
            logging.info(content)
         elif log_type == 'DEBUG':
            logging.debug(content)
         elif log_type == 'WARN':
            logging.warning(content)
         elif log_type == 'CRITICAL':
            logging.critical(content)
         elif log_type == 'ERROR':
            logging.error(content)
            
    def check_and_update_15min_count(token_dict):
        if token_dict[u"after_15_mins_timestamp"] < datetime.now().replace(tzinfo=pytz.UTC):
            def test_assert_logs_info(self):
                with self.assertLogs(' ', level='INFO') as cm:
                    
                    self.assertEqual(cm.output, ['INFO: :updating after 15 mins timestamp token...',
                                                 'ERROR: :updating after 15 mins timestamp token...'])
                    token_dict[u"after_15_mins_timestamp"] = datetime.now().replace(tzinfo=pytz.UTC) + timedelta(minutes=15)
                    token_dict[u"used_in_last_15_mins"] = 1
                    token_dict[u"last_used_timestamp"] = datetime.now().replace(tzinfo=pytz.UTC)


            def test_assert_logs_debug(self):
               with self.assertLogs(' ', level='INFO') as cm1:
                    self.log_from_api('DEBUG', 'updating after 15 mins timestamp token...')
                    self.assertEqual(cm1.output, ["INFO: :updating after 15 mins timestamp token...",
                                                  "ERROR: :updating after 15 mins timestamp token..."])
                    token_dict[u"after_15_mins_timestamp"] = datetime.now().replace(tzinfo=pytz.UTC) + timedelta(minutes=15)
                    token_dict[u"used_in_last_15_mins"] = 1
                    token_dict[u"last_used_timestamp"] = datetime.now().replace(tzinfo=pytz.UTC)

            def test_assert_logs_warning(self):
                with self.assertLogs(' ', level='INFO') as cm2:
                    self.log_from_api('WARN', 'updating after 15 mins timestamp token...')
                    self.assertEqual(cm2.output, ["INFO: :updating after 15 mins timestamp token...",
                                              "ERROR: :updating after 15 mins timestamp token..."])
                    token_dict[u"after_15_mins_timestamp"] = datetime.now().replace(tzinfo=pytz.UTC) + timedelta(minutes=15)
                    token_dict[u"used_in_last_15_mins"] = 1
                    token_dict[u"last_used_timestamp"] = datetime.now().replace(tzinfo=pytz.UTC)


            def test_assert_logs_critical(self):
                with self.assertLogs(' ', level='INFO') as cm3:
                    self.log_from_api('CRITICAL', 'updating after 15 mins timestamp token...')
                    self.assertEqual(cm3.output, ["INFO: :updating after 15 mins timestamp token...",
                                                  "ERROR: :updating after 15 mins timestamp token..."])
                    token_dict[u"after_15_mins_timestamp"] = datetime.now().replace(tzinfo=pytz.UTC) + timedelta(minutes=15)
                    token_dict[u"used_in_last_15_mins"] = 1
                    token_dict[u"last_used_timestamp"] = datetime.now().replace(tzinfo=pytz.UTC)

            def test_assert_logs_error(self):
                with self.assertLogs(' ', level='INFO') as cm4:
                    self.log_from_api('ERROR', 'updating after 15 mins timestamp token...')
                    self.assertEqual(cm4.output, ["INFO: :updating after 15 mins timestamp token...",
                                                  "ERROR: :updating after 15 mins timestamp token..."])

                    token_dict[u"after_15_mins_timestamp"] = datetime.now().replace(tzinfo=pytz.UTC) + timedelta(minutes=15)
                    token_dict[u"used_in_last_15_mins"] = 1
                    token_dict[u"last_used_timestamp"] = datetime.now().replace(tzinfo=pytz.UTC)

        else:
            token_dict[u"used_in_last_15_mins"] = Increment(1)
            token_dict[u"last_used_timestamp"] = datetime.now().replace(tzinfo=pytz.UTC)

    #    @unittest.skip("demonstrating skipping")


if __name__ == '__main__':
    c=TestSequenceFunctions()
    c.log_from_api('INFO', 'updating after 15 mins timestamp token...')
    c.check_and_update_15min_count()
    c.log_from_api('DEBUG', 'updating after 15 mins timestamp token...')
    c.log_from_api('WARN', 'updating after 15 mins timestamp token...')
    c.log_from_api('CRITICAL', 'updating after 15 mins timestamp token...')
    c.log_from_api('ERROR', 'updating after 15 mins timestamp token...')
   
    unittest.main(

        testRunner=xmlrunner.XMLTestRunner(output='tests_fail-reports'),
        failfast=False, buffer=False, catchbreak=False)