<commit_before>import unittest
import kayvee as kv
import logger
import json
import os
import StringIO

class TestLogger(unittest.TestCase):

  def assertEqualJson(self, a, b):
    """ Given two strings, assert they are the same json dict """
    actual = json.loads(a)
    expected = json.loads(b)
    self.assertEqual(actual, expected)

  def assertNotEqualJson(self, a, b):
    """ Given two strings, assert they are the same json dict """
    actual = json.loads(a)
    expected = json.loads(b)
    self.assertNotEqual(actual, expected)

  def test_constructor(self):
    formatter = lambda data: data["level"] + "." + data["source"] + "." + data["title"]
    outputIO = StringIO.StringIO()
    logObj = logger.Logger("logger-test", logger.LOG_LEVELS["Info"], formatter, outputIO)
    logObj.debug("testlogdebug")
    expected = ""
    self.assertEqual(outputIO.getvalue(), expected)

    logObj.info("testloginfo")
    expected = logger.LOG_LEVELS["Info"] + ".logger-test.testloginfo\n"
    self.assertEqual(outputIO.getvalue(), expected)
    outputIO.close()

  def test_validateLog(self):
    # Explicit validation checks
    logObj = logger.Logger("logger-tester")
    outputIO = StringIO.StringIO()
    logObj.setOutput(outputIO)

    logLvl = logObj._validateLogLvl("debug")
    self.assertEqual(logLvl, logger.LOG_LEVELS["Debug"])

    logLvl = logObj._validateLogLvl("Debug")
    self.assertEqual(logLvl, logger.LOG_LEVELS["Debug"])

    logLvl = logObj._validateLogLvl("info")
    self.assertEqual(logLvl, logger.LOG_LEVELS["Info"])
    logLvl = logObj._validateLogLvl("Info")
    self.assertEqual(logLvl, logger.LOG_LEVELS["Info"])

    logLvl = logObj._validateLogLvl("sometest")
    self.assertEqual(logLvl, logger.LOG_LEVELS["Debug"])
    outputIO.close()

  def test_invalidLog(self):
    # Invalid log levels will default to debug
    logObj = logger.Logger("logger-tester")
    outputIO = StringIO.StringIO()
    logObj.setOutput(outputIO)

    logObj.setLogLevel("debu")
    logObj.debug("testlogdebug")
    expected = "{\"source\": \"logger-tester\", \"level\": \"" + logger.LOG_LEVELS["Debug"] + "\", \"title\": \"testlogdebug\"}"
    self.assertEqualJson(outputIO.getvalue(), expected)

    # Recreate to clear
    outputIO = StringIO.StringIO()
    logObj.setOutput(outputIO)
    logObj.setLogLevel("sometest")
    logObj.info("testloginfo")
    expected = "{\"source\": \"logger-tester\", \"level\": \"" + logger.LOG_LEVELS["Info"] + "\", \"title\": \"testloginfo\"}"
    self.assertEqualJson(outputIO.getvalue(), expected)
    outputIO.close()

  def test_debug(self):
    logObj = logger.Logger("logger-tester")
    outputIO = StringIO.StringIO()
    logObj.setOutput(outputIO)

    logObj.debug("testlogdebug")
    expected = "{\"source\": \"logger-tester\", \"level\": \"" + logger.LOG_LEVELS["Debug"] + "\", \"title\": \"testlogdebug\"}"
    self.assertEqualJson(outputIO.getvalue(), expected)
    outputIO.close()
  def test_debugD(self):
    logObj = logger.Logger("logger-tester")
    outputIO = StringIO.StringIO()
    logObj.setOutput(outputIO)

    logObj.debugD("testlogdebug", {"key1":"val1","key2":"val2"})
    expected = "{\"source\": \"logger-tester\", \"level\": \"" + logger.LOG_LEVELS["Debug"] + "\", \"title\": \"testlogdebug\",\"key1\": \"val1\", \"key2\": \"val2\"}"
    self.assertEqualJson(outputIO.getvalue(), expected)
    outputIO.close()

  def test_info(self):
    logObj = logger.Logger("logger-tester")
    outputIO = StringIO.StringIO()
    logObj.setOutput(outputIO)

    logObj.info("testloginfo")
    expected = "{\"source\": \"logger-tester\", \"level\": \"" + logger.LOG_LEVELS["Info"] + "\", \"title\": \"testloginfo\"}"
    self.assertEqualJson(outputIO.getvalue(), expected)
    outputIO.close()
  def test_infoD(self):
    logObj = logger.Logger("logger-tester")
    outputIO = StringIO.StringIO()
    logObj.setOutput(outputIO)

    logObj.infoD("testloginfo", {"key1":"val1","key2":"val2"})
    expected = "{\"source\": \"logger-tester\", \"level\": \"" + logger.LOG_LEVELS["Info"] + "\", \"title\": \"testloginfo\",\"key1\": \"val1\", \"key2\": \"val2\"}"
    self.assertEqualJson(outputIO.getvalue(), expected)
    outputIO.close()

  def test_warn(self):
    logObj = logger.Logger("logger-tester")
    outputIO = StringIO.StringIO()
    logObj.setOutput(outputIO)

    logObj.warn("testlogwarning")
    expected = "{\"source\": \"logger-tester\", \"level\": \"" + logger.LOG_LEVELS["Warning"] + "\", \"title\": \"testlogwarning\"}"
    self.assertEqualJson(outputIO.getvalue(), expected)
    outputIO.close()
  def test_warnD(self):
    logObj = logger.Logger("logger-tester")
    outputIO = StringIO.StringIO()
    logObj.setOutput(outputIO)

    logObj.warnD("testlogwarning", {"key1":"val1","key2":"val2"})
    expected = "{\"source\": \"logger-tester\", \"level\": \"" + logger.LOG_LEVELS["Warning"] + "\", \"title\": \"testlogwarning\",\"key1\": \"val1\", \"key2\": \"val2\"}"
    self.assertEqualJson(outputIO.getvalue(), expected)
    outputIO.close()

  def test_error(self):
    logObj = logger.Logger("logger-tester")
    outputIO = StringIO.StringIO()
    logObj.setOutput(outputIO)

    logObj.error("testlogerror")
    expected = "{\"source\": \"logger-tester\", \"level\": \"" + logger.LOG_LEVELS["Error"] + "\", \"title\": \"testlogerror\"}"
    self.assertEqualJson(outputIO.getvalue(), expected)
    outputIO.close()
  def test_errorD(self):
    logObj = logger.Logger("logger-tester")
    outputIO = StringIO.StringIO()
    logObj.setOutput(outputIO)

    logObj.errorD("testlogerror", {"key1":"val1","key2":"val2"})
    expected = "{\"source\": \"logger-tester\", \"level\": \"" + logger.LOG_LEVELS["Error"] + "\", \"title\": \"testlogerror\",\"key1\": \"val1\", \"key2\": \"val2\"}"
    self.assertEqualJson(outputIO.getvalue(), expected)
    outputIO.close()

  def test_critical(self):
    logObj = logger.Logger("logger-tester")
    outputIO = StringIO.StringIO()
    logObj.setOutput(outputIO)

    logObj.critical("testlogcritical")
    expected = "{\"source\": \"logger-tester\", \"level\": \"" + logger.LOG_LEVELS["Critical"] + "\", \"title\": \"testlogcritical\"}"
    self.assertEqualJson(outputIO.getvalue(), expected)
    outputIO.close()
  def test_criticalD(self):
    logObj = logger.Logger("logger-tester")
    outputIO = StringIO.StringIO()
    logObj.setOutput(outputIO)

    logObj.criticalD("testlogcritical", {"key1":"val1","key2":"val2"})
    expected = "{\"source\": \"logger-tester\", \"level\": \"" + logger.LOG_LEVELS["Critical"] + "\", \"title\": \"testlogcritical\",\"key1\": \"val1\", \"key2\": \"val2\"}"
    self.assertEqualJson(outputIO.getvalue(), expected)
    outputIO.close()

  def test_counter(self):
    logObj = logger.Logger("logger-tester")
    outputIO = StringIO.StringIO()<commit_msg>Move log function testing into loop except counter/gauge since they have slightly different structure. Adding additional comments for clarity<commit_after>import unittest
import kayvee as kv
import logger
import json
import os
import StringIO

class TestLogger(unittest.TestCase):

  def assertEqualJson(self, a, b):
    """ Given two strings, assert they are the same json dict """
    actual = json.loads(a)
    expected = json.loads(b)
    self.assertEqual(actual, expected)

  def assertNotEqualJson(self, a, b):
    """ Given two strings, assert they are the same json dict """
    actual = json.loads(a)
    expected = json.loads(b)
    self.assertNotEqual(actual, expected)

  def test_constructor(self):
    formatter = lambda data: data["level"] + "." + data["source"] + "." + data["title"]
    outputIO = StringIO.StringIO()
    logObj = logger.Logger("logger-test", logger.LOG_LEVELS["Info"], formatter, outputIO)
    logObj.debug("testlogdebug")
    expected = ""
    self.assertEqual(outputIO.getvalue(), expected)

    logObj.info("testloginfo")
    expected = logger.LOG_LEVELS["Info"] + ".logger-test.testloginfo\n"
    self.assertEqual(outputIO.getvalue(), expected)
    outputIO.close()

  def test_validateLogLvl(self):
    # Explicit validation checks
    logObj = logger.Logger("logger-tester")
    outputIO = StringIO.StringIO()
    logObj.setOutput(outputIO)

    # Test case-insensitive in log level name
    logLvl = logObj._validateLogLvl("debug")
    self.assertEqual(logLvl, logger.LOG_LEVELS["Debug"])
    logLvl = logObj._validateLogLvl("Debug")
    self.assertEqual(logLvl, logger.LOG_LEVELS["Debug"])

    # Test non-default log levels
    logLvl = logObj._validateLogLvl("info")
    self.assertEqual(logLvl, logger.LOG_LEVELS["Info"])
    logLvl = logObj._validateLogLvl("critical")
    self.assertEqual(logLvl, logger.LOG_LEVELS["Critical"])
    # TODO: add for each possible level

    # Test sets level to debug if given invalid log level
    logLvl = logObj._validateLogLvl("sometest")
    self.assertEqual(logLvl, logger.LOG_LEVELS["Debug"])
    outputIO.close()

  def test_invalidLog(self):
    # Invalid log levels will default to debug
    logObj = logger.Logger("logger-tester")
    outputIO = StringIO.StringIO()
    logObj.setOutput(outputIO)

    logObj.setLogLevel("invalidloglvl")
    logObj.debug("testlogdebug")
    expected = "{\"source\": \"logger-tester\", \"level\": \"" + logger.LOG_LEVELS["Debug"] + "\", \"title\": \"testlogdebug\"}"
    self.assertEqualJson(outputIO.getvalue(), expected)

    # Recreate to clear
    outputIO = StringIO.StringIO()
    logObj.setOutput(outputIO)
    logObj.setLogLevel("sometest")
    logObj.info("testloginfo")
    expected = "{\"source\": \"logger-tester\", \"level\": \"" + logger.LOG_LEVELS["Info"] + "\", \"title\": \"testloginfo\"}"
    self.assertEqualJson(outputIO.getvalue(), expected)
    outputIO.close()

  def test_logFuncs(self):
    logObj = logger.Logger("logger-tester")

    # LOG_LEVEL: (simpleLogFunc, addlDataLogFunc)
    tests = {
        logger.LOG_LEVELS["Debug"]: (logObj.debug, logObj.debugD),
        logger.LOG_LEVELS["Info"]: (logObj.info, logObj.infoD),
        logger.LOG_LEVELS["Warning"]: (logObj.warn, logObj.warnD),
        logger.LOG_LEVELS["Error"]: (logObj.error, logObj.errorD),
        logger.LOG_LEVELS["Critical"]: (logObj.critical, logObj.criticalD)
    }

    for logLvl in tests:
        outputIO = StringIO.StringIO()
        logObj.setOutput(outputIO)

        simpleLogFunc = tests[logLvl][0]
        simpleLogFunc("testlog"+logLvl)
        simpleExpected = "{\"source\": \"logger-tester\", \"level\": \"" + logLvl + "\", \"title\": \"testlog" + logLvl + "\"}"
        self.assertEqualJson(outputIO.getvalue(), simpleExpected)

        outputIO = StringIO.StringIO()
        logObj.setOutput(outputIO)

        addlDataLogFunc = tests[logLvl][1]
        addlDataLogFunc("testlog"+logLvl, {"key1":"val1","key2":"val2"})
        addlDataExpected = "{\"source\": \"logger-tester\", \"level\": \"" + logLvl + "\", \"title\": \"testlog" + logLvl + "\",\"key1\": \"val1\", \"key2\": \"val2\"}"
        self.assertEqualJson(outputIO.getvalue(), addlDataExpected)

    outputIO.close()

  def test_counter(self):
    logObj = logger.Logger("logger-tester")
    outputIO = StringIO.StringIO()
{'fraction_non_alphanumeric': 0.17928930366116297, 'fraction_numerical': 0.004486719310839914, 'mean_word_length': 3.8731963270660255, 'pattern_counts': {'":': 72, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '192048', 'n_tokens_mistral': 3635, 'n_tokens_neox': 3575, 'n_words': 632}