Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: 1deterministic/1D-Sync
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v2.5
Choose a base ref
...
head repository: 1deterministic/1D-Sync
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
  • 4 commits
  • 10 files changed
  • 1 contributor

Commits on Jan 4, 2019

  1. Added basic unit tests for the _validations file

    Lucas Fernandes Vieira committed Jan 4, 2019
    Copy the full SHA
    e0c46f8 View commit details

Commits on Jan 5, 2019

  1. Copy the full SHA
    c510e92 View commit details
  2. Added basic unit tests for _email

    Lucas Fernandes Vieira committed Jan 5, 2019
    Copy the full SHA
    3cce278 View commit details
  3. Copy the full SHA
    1ac74cb View commit details
Showing with 331 additions and 20 deletions.
  1. +1 −0 .gitignore
  2. +92 −0 Tests/Unit/test_control.py
  3. +39 −0 Tests/Unit/test_email.py
  4. +107 −0 Tests/Unit/test_log.py
  5. +60 −0 Tests/Unit/test_validations.py
  6. +9 −5 _control.py
  7. +4 −3 _email.py
  8. +13 −6 _log.py
  9. +1 −1 _strings.py
  10. +5 −5 _validations.py
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -4,3 +4,4 @@
/History/
/Private/
/__pycache__/
/Tests/Temp/
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()
39 changes: 39 additions & 0 deletions Tests/Unit/test_email.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import unittest
import os

import _email
import _log

class TestEmail(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/Logs"):
os.mkdir("Tests/Temp/Logs")


def test_init(self):
email = _email.Email()
self.assertEqual(email.message, "")

def test_send(self):
email = _email.Email()
log = _log.Log("Tests/Temp")
self.assertFalse(email.send("#####@gmail.com", "strong and complicated password", "#####@gmail.com", log))
self.assertTrue(log.error_occurred)

def test_append_message(self):
email = _email.Email()
email.append_message("test")
self.assertEqual(email.message, "test")


if __name__ == '__main__':
unittest.main()
107 changes: 107 additions & 0 deletions Tests/Unit/test_log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import unittest
import os

import _log
import _defaults
import _about
import _strings

class TestLog(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/Logs"):
os.mkdir("Tests/Temp/Logs")

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

self.assertTrue(log.path.startswith("Tests/Temp"))
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()
60 changes: 60 additions & 0 deletions Tests/Unit/test_validations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import unittest

import _validations

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

def test_validate_path(self):
valid_paths = ["Tests/Unit"]
invalid_paths = ["Tests/UnitWRONG"]

for valid_path in valid_paths:
self.assertTrue(_validations.validate_path(valid_path))

for invalid_path in invalid_paths:
self.assertFalse(_validations.validate_path(invalid_path))

def test_validate_boolean_value(self):
valid_booleans = ["True"]
invalid_booleans = ["TrueWRONG"]

for valid_boolean in valid_booleans:
self.assertTrue(_validations.validate_boolean_value(valid_boolean))

for invalid_boolean in invalid_booleans:
self.assertFalse(_validations.validate_boolean_value(invalid_boolean))

def test_validate_integer_greater_than_or_equal_to_zero(self):
valid_integers = ["15"]
invalid_integers = ["-41"]

for valid_integer in valid_integers:
self.assertTrue(_validations.validate_integer_greater_than_or_equal_to_zero(valid_integer))

for invalid_integer in invalid_integers:
self.assertFalse(_validations.validate_integer_greater_than_or_equal_to_zero(invalid_integer))

def test_validate_integer_greater_than_zero(self):
valid_integers = ["15"]
invalid_integers = ["0"]

for valid_integer in valid_integers:
self.assertTrue(_validations.validate_integer_greater_than_zero(valid_integer))

for invalid_integer in invalid_integers:
self.assertFalse(_validations.validate_integer_greater_than_zero(invalid_integer))

def test_validate_selection_condition(self):
valid_conditions = ["a ^ b | ~{c ^ ~d}"]
invalid_conditions = ["a ^ b | ~{c ^ ~d"]

for valid_condition in valid_conditions:
self.assertTrue(_validations.validate_selection_condition(valid_condition))

for invalid_condition in invalid_conditions:
self.assertFalse(_validations.validate_selection_condition(invalid_condition))

if __name__ == '__main__':
unittest.main()
14 changes: 9 additions & 5 deletions _control.py
Original file line number Diff line number Diff line change
@@ -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)
7 changes: 4 additions & 3 deletions _email.py
Original file line number Diff line number Diff line change
@@ -10,10 +10,11 @@
from email.message import EmailMessage

class Email:
def __init__(self):
def __init__(self): # test in Tests/Unit/test_email.test_init
self.message = "" # initializes the email with an empty message

def send(self, sender, password, addressee, log): # sends the email
# sends the email
def send(self, sender, password, addressee, log): # test in Tests/Unit/test_email.test_send
server_address = "smtp.gmail.com: 587"

try:
@@ -40,5 +41,5 @@ def send(self, sender, password, addressee, log): # sends the email
log.report("error_email_send", detail=addressee)
return False

def append_message(self, text):
def append_message(self, text): # test in Tests/Unit/test_email.test_append_message
self.message += text
Loading