Skip to content

Commit

Permalink
Added basic unit tests for _control, improved testing in _email 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 3cce278 commit 1ac74cb
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 9 deletions.
92 changes: 92 additions & 0 deletions Tests/Unit/test_control.py
Original file line number Diff line number Diff line change
@@ -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()
14 changes: 13 additions & 1 deletion Tests/Unit/test_email.py
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
17 changes: 14 additions & 3 deletions Tests/Unit/test_log.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import unittest
import os

import _log
import _defaults
Expand All @@ -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)
Expand Down
14 changes: 9 additions & 5 deletions _control.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)
Expand Down

0 comments on commit 1ac74cb

Please sign in to comment.