From 1ac74cb1df3a1e2ab6dbb6bad81458540c2aae22 Mon Sep 17 00:00:00 2001 From: Lucas Fernandes Vieira Date: Sat, 5 Jan 2019 15:17:57 -0200 Subject: [PATCH] Added basic unit tests for _control, improved testing in _email and _log --- Tests/Unit/test_control.py | 92 ++++++++++++++++++++++++++++++++++++++ Tests/Unit/test_email.py | 14 +++++- Tests/Unit/test_log.py | 17 +++++-- _control.py | 14 +++--- 4 files changed, 128 insertions(+), 9 deletions(-) create mode 100644 Tests/Unit/test_control.py diff --git a/Tests/Unit/test_control.py b/Tests/Unit/test_control.py new file mode 100644 index 0000000..9f56d7d --- /dev/null +++ b/Tests/Unit/test_control.py @@ -0,0 +1,92 @@ +import unittest +import os +import datetime + +import _control +import _log +import _paths + +class TestControl(unittest.TestCase): + def setUp(self): + for (dirpath, dirnames, filenames) in os.walk("Tests/Temp"): + for f in filenames: + os.remove(os.path.join(dirpath, f)) + + for (dirpath, dirnames, filenames) in os.walk("Tests/Temp"): + for d in dirnames: + if not dirpath == d: + os.rmdir(os.path.join(dirpath, d)) + + if not os.path.isdir("Tests/Temp/Config"): + os.mkdir("Tests/Temp/Config") + + if not os.path.isdir("Tests/Temp/Logs"): + os.mkdir("Tests/Temp/Logs") + + control = open("Tests/Temp/Config/control.json", "w") + control.write("""{ + "itstime.json": "2000-01-01 00-00-00", + "notyet.json": "3000-01-01 00-00-00" + }""") + control.close() + + def test_init(self): + control = _control.Control("Tests/Temp") + self.assertEqual(control.path, os.path.join("Tests/Temp", _paths.config_folder, _paths.control_file)) + + + def test_load(self): + log = _log.Log("Tests/Temp") + control = _control.Control("Tests/Temp") + self.assertTrue(control.load(log)) + self.assertFalse(log.error_occurred) + self.assertFalse(log.warning_occurred) + self.assertFalse(log.sync_occurred) + + control = _control.Control("Tests/TempWRONG") + self.assertFalse(control.load(log)) + self.assertTrue(log.error_occurred) + self.assertFalse(log.warning_occurred) + self.assertFalse(log.sync_occurred) + + def test_its_time(self): + log = _log.Log("Tests/Temp") + control = _control.Control("Tests/Temp") + self.assertTrue(control.load(log)) + self.assertTrue(control.its_time("itstime.json")) + self.assertFalse(control.its_time("notyet.json")) + + def test_schedule(self): + log = _log.Log("Tests/Temp") + control = _control.Control("Tests/Temp") + self.assertTrue(control.load(log)) + now = datetime.datetime.now() + control.schedule("itstime.json", 1) + self.assertEqual(control.properties["itstime.json"], (now + datetime.timedelta(seconds=1)).strftime("%Y-%m-%d %H-%M-%S")) + + def test_write(self): + log = _log.Log("Tests/Temp") + control = _control.Control("Tests/Temp") + self.assertTrue(control.load(log)) + now = datetime.datetime.now() + control.schedule("itstime.json", 1) + self.assertTrue(control.write(log)) + self.assertFalse(log.error_occurred) + self.assertFalse(log.warning_occurred) + self.assertFalse(log.sync_occurred) + self.assertTrue(os.path.isfile(control.path)) + + log = _log.Log("Tests/Temp") + log.path = "Tests/Temp/Logs/Error.txt" + control = _control.Control("Tests/TempWRONG") + self.assertFalse(control.load(log)) + self.assertFalse(control.write(log)) + self.assertTrue(log.error_occurred) + self.assertFalse(log.warning_occurred) + self.assertFalse(log.sync_occurred) + # ensures that the log is written right away when needed (critical=True) + self.assertTrue(os.path.isfile("Tests/Temp/Logs/Error.txt")) + + +if __name__ == '__main__': + unittest.main() diff --git a/Tests/Unit/test_email.py b/Tests/Unit/test_email.py index f37739d..2028016 100644 --- a/Tests/Unit/test_email.py +++ b/Tests/Unit/test_email.py @@ -1,11 +1,23 @@ import unittest +import os import _email import _log class TestEmail(unittest.TestCase): def setUp(self): - pass + for (dirpath, dirnames, filenames) in os.walk("Tests/Temp"): + for f in filenames: + os.remove(os.path.join(dirpath, f)) + + for (dirpath, dirnames, filenames) in os.walk("Tests/Temp"): + for d in dirnames: + if not dirpath == d: + os.rmdir(os.path.join(dirpath, d)) + + if not os.path.isdir("Tests/Temp/Logs"): + os.mkdir("Tests/Temp/Logs") + def test_init(self): email = _email.Email() diff --git a/Tests/Unit/test_log.py b/Tests/Unit/test_log.py index 5b18b41..24482b5 100644 --- a/Tests/Unit/test_log.py +++ b/Tests/Unit/test_log.py @@ -1,4 +1,5 @@ import unittest +import os import _log import _defaults @@ -7,12 +8,22 @@ class TestLog(unittest.TestCase): def setUp(self): - pass + for (dirpath, dirnames, filenames) in os.walk("Tests/Temp"): + for f in filenames: + os.remove(os.path.join(dirpath, f)) + + for (dirpath, dirnames, filenames) in os.walk("Tests/Temp"): + for d in dirnames: + if not dirpath == d: + os.rmdir(os.path.join(dirpath, d)) + + if not os.path.isdir("Tests/Temp/Logs"): + os.mkdir("Tests/Temp/Logs") def test_init(self): - log = _log.Log(".") + log = _log.Log("Tests/Temp") - self.assertTrue(log.path.startswith("./")) + self.assertTrue(log.path.startswith("Tests/Temp")) self.assertTrue(log.path.endswith(".txt")) self.assertFalse(log.error_occurred) diff --git a/_control.py b/_control.py index b13a998..942e339 100644 --- a/_control.py +++ b/_control.py @@ -11,10 +11,11 @@ import datetime class Control: - def __init__(self, root): + def __init__(self, root): # test in Tests/Unit/test_control.test_init self.path = os.path.join(root, _paths.config_folder, _paths.control_file) # stores the path to the schedule file - def load(self, log): # loads the schedule file + # loads the schedule file + def load(self, log): # test in Tests/Unit/test_control.test_load try: self.properties = json.loads(open(self.path, "r", encoding="utf-8").read()) log.report("ok_control_json_load") @@ -23,14 +24,17 @@ def load(self, log): # loads the schedule file log.report("error_control_opening", critical=True) return False - def its_time(self, name): # given a sync name, tells if its time to run it + # given a sync name, tells if its time to run it + def its_time(self, name): # test in Tests/Unit/test_control.test_its_time return datetime.datetime.now() > datetime.datetime.strptime(self.properties[name], "%Y-%m-%d %H-%M-%S") - def schedule(self, name, time): # schedules a sync to occur in time hours from now + # schedules a sync to occur in time seconds from now + def schedule(self, name, time): # test in Tests/Unit/test_control.test_schedule self.properties[name] = (datetime.datetime.now() + datetime.timedelta(seconds=time)).strftime("%Y-%m-%d %H-%M-%S") return True - def write(self, log): # writes the changes back to the schedule file + # writes the changes back to the schedule file + def write(self, log): # test in Tests/Unit/test_control.test_write try: with open(self.path, "w") as file_to_write: json.dump(self.properties, file_to_write, indent=4, ensure_ascii=False)