Skip to content

Commit

Permalink
Added basic unit tests for _log, minor improvements in _strings and _log
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Fernandes Vieira committed Jan 5, 2019
1 parent e0c46f8 commit c510e92
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
/History/
/Private/
/__pycache__/
/Tests/Temp/
96 changes: 96 additions & 0 deletions Tests/Unit/test_log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import unittest

import _log
import _defaults
import _about
import _strings

class TestLog(unittest.TestCase):
def setUp(self):
pass

def test_init(self):
log = _log.Log(".")

self.assertTrue(log.path.startswith("./"))
self.assertTrue(log.path.endswith(".txt"))

self.assertFalse(log.error_occurred)
self.assertFalse(log.warning_occurred)
self.assertFalse(log.sync_occurred)

self.assertEqual(log.content, [])
self.assertEqual(log.summary, [])

self.assertEqual(log.repeated_line, "")
self.assertEqual(log.repeated_count, 0)

def test_report(self):
# will test all strings in all languages
for language in _strings.languages:
_strings.strings = _strings.languages[language]

for string in _strings.strings.keys():
if string.startswith("email"):
continue

log = _log.Log(".")
detail = "test"
log.report(string, detail=detail)

if string.startswith("ok"):
self.assertFalse(log.error_occurred)
self.assertFalse(log.warning_occurred)
self.assertFalse(log.sync_occurred)

elif string.startswith("warning"):
self.assertFalse(log.error_occurred)
self.assertTrue(log.warning_occurred)
self.assertFalse(log.sync_occurred)

elif string.startswith("error"):
self.assertTrue(log.error_occurred)
self.assertFalse(log.warning_occurred)
self.assertFalse(log.sync_occurred)

elif string.startswith("message"):
self.assertFalse(log.error_occurred)
self.assertFalse(log.warning_occurred)
self.assertFalse(log.sync_occurred)

elif string.startswith("prefix"):
self.assertFalse(log.error_occurred)
self.assertFalse(log.warning_occurred)
self.assertFalse(log.sync_occurred)

def test_insert(self):
# will test all strings in all languages
for language in _strings.languages:
_strings.strings = _strings.languages[language]

for string in _strings.strings.keys():
if string.startswith("email"):
continue

log = _log.Log(".")
detail = "test"
log.insert(string, detail=detail)
self.assertEqual(log.content[0][18:], _strings.strings[string] + detail)

def test_get_content(self):
log = _log.Log(".")
log.content = ["test"]
self.assertEqual(log.get_content(), _strings.strings["prefix_timestamp_spacing"] + _strings.strings["prefix_spacing"] + _about.name + " " + _about.version + " \"" + _about.codename + "\" build date:" + _about.build_date + "\n\ntest\n")

def test_write(self):
log = _log.Log("Tests/Temp")
log.content = ["test"]
log.write()
fl = open(log.path, "r")
text = fl.read()
fl.close()
self.assertEqual(text, _strings.strings["prefix_timestamp_spacing"] + _strings.strings["prefix_spacing"] + _about.name + " " + _about.version + " \"" + _about.codename + "\" build date:" + _about.build_date + "\n\ntest\n")


if __name__ == '__main__':
unittest.main()
19 changes: 13 additions & 6 deletions _log.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
import datetime

class Log:
def __init__(self, root): # initializes an empty log
# initializes an empty log
def __init__(self, root): # test in Tests/Unit/test_log.test_init
self.path = os.path.join(root, _paths.logs_folder, time.strftime("%Y-%m-%d %H-%M-%S") + ".txt")

self.error_occurred = False
Expand All @@ -27,7 +28,8 @@ def __init__(self, root): # initializes an empty log
self.repeated_line = ""
self.repeated_count = 0

def report(self, id, detail="", critical=False): # reports events in the log
# reports events in the log
def report(self, id, detail="", critical=False): # test in Tests/Unit/test_log.test_report
if id.startswith("error"):
self.error_occurred = True

Expand All @@ -54,7 +56,8 @@ def report(self, id, detail="", critical=False): # reports events in the log

return True

def insert(self, id, detail):
# will insert a line in the log file
def insert(self, id, detail): # test in Tests/Unit/test_log.test_insert
timestamp = datetime.datetime.now().strftime("[%H:%M:%S]")

if id.startswith("ok"):
Expand All @@ -63,14 +66,17 @@ def insert(self, id, detail):
self.content.append(timestamp + _strings.strings["prefix_error"] + _strings.strings[id] + detail)
elif id.startswith("warning"):
self.content.append(timestamp + _strings.strings["prefix_warning"] + _strings.strings[id] + detail)
elif id.startswith("prefix"):
self.content.append(timestamp + _strings.strings["prefix_spacing"] + _strings.strings[id] + detail)
elif id.startswith("message"):
self.content.append(timestamp + _strings.strings[id] + detail)
self.content.append(timestamp + _strings.strings["prefix_spacing"] + _strings.strings[id] + detail)

else:
# skip timestamp in this case but include some spacing for indentation
self.content.append(_strings.strings["prefix_timestamp_spacing"] + _strings.strings["prefix_spacing"] + id.replace("\n", "\n" + _strings.strings["prefix_timestamp_spacing"] + _strings.strings["prefix_spacing"]) + detail)

def get_content(self):
# returns the full log file in the correct format
def get_content(self): # test in Tests/Unit/test_log.test_get_content
string = ""

string += _strings.strings["prefix_timestamp_spacing"] + _strings.strings["prefix_spacing"] + _about.name + " " + _about.version + " \"" + _about.codename + "\" build date:" + _about.build_date + "\n\n"
Expand All @@ -79,7 +85,8 @@ def get_content(self):

return string

def write(self): # writes the message to the file system
# writes the message to the file system
def write(self): # test in Tests/Unit/test_log.test_write
try:
self.file = open(self.path, "w")
self.file.write(self.get_content())
Expand Down
2 changes: 1 addition & 1 deletion _strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
"warning_file_evaluate_mathematical_comparison_not_identified": "\tmathematical comparison not identified: ",

# Log
"message_repeated_lines": " \tthe previous line was repeated this more times: "
"message_repeated_lines": "\tthe previous line was repeated this more times: "
}

languages = {
Expand Down

0 comments on commit c510e92

Please sign in to comment.