def test_load_objects(self): """Load an object from the JSON database.""" task_1 = Task("Test1") task_2 = Task("Test2") task_3 = Task("Test3") self.json_handler.write(task_1.to_json()) self.json_handler.write(task_2.to_json()) self.json_handler.write(task_3.to_json()) # Check the first instance json_data = self.json_handler.load_obj("Test1") self.assertEquals(json_data, task_1.__dict__()) # Check the second instance json_data = self.json_handler.load_obj("Test2") self.assertEquals(json_data, task_2.__dict__()) # Check the third instance json_data = self.json_handler.load_obj("Test3") self.assertEquals(json_data, task_3.__dict__())
def test_task_exists_only_once(self): """Verify that task is only written once to the database. The serializer should check that the identifier (name) of the task is in the database, if yes, then don't create the new task; otherwise, yes. """ # Create identical tasks task_1 = Task("Hello") task_2 = Task("Hello") task_3 = Task("Hello") self.json_handler.write(task_1.to_json()) self.json_handler.write(task_2.to_json()) self.json_handler.write(task_3.to_json()) json_data = self.json_handler.load() key_counter = 0 for key in json_data.keys(): key_counter += 1 self.assertTrue(key_counter == 1)
def test_json_handler_writing_multiple_tasks(self): """Save multiple tasks, and assert that they are existing in file.""" task_1 = Task("Hello") task_2 = Task("Hello2") task_3 = Task("Hello3") # Create the test dictionary test_data = {} # Write the tasks to the file self.json_handler.write(task_1.to_json()) self.json_handler.write(task_2.to_json()) self.json_handler.write(task_3.to_json()) # Load the data from the file. json_data = self.json_handler.load() # Check that the key exist in the file. for key in json_data.keys(): self.assertTrue(key in [task_1.get_name(), task_2.get_name(), task_3.get_name()])
class TaskTests(TestCase): def setUp(self): self.task = Task() self.pickler = Pickler() self.unpickler = Unpickler() def test_get_json_representation(self): """Get the json representation for the __dict__ object of the Task object...""" task_dict = self.task.__dict__() json_dict = self.unpickler.restore(self.task.to_json()) self.assertEquals(json_dict, task_dict)
def test_load_object(self): task_1 = Task("Test1") self.json_handler.write(task_1.to_json()) json_data = self.json_handler.load_obj("Test1") # Assert that the data is the same. self.assertEquals(json_data, task_1.__dict__())
class Tracker(object): # Class constants CURRENT = path.expanduser("~/.tt_running") def __init__(self): self.task = Task() self.fh = JSONHandler("test.json") def track(self, name): """Start tracking a task. :param name The name of the task to start. :type name str """ self.task.set_name(name) try: with open(self.CURRENT) as f: d = f.readline() if d: print "Task", d, "already running." exit(0) else: f.close() # Write task-name to ~/.tt_running f = open(self.CURRENT, "w") f.write(name) f.close() except IOError: # Write task-name to ~/.tt_running f = open(self.CURRENT, "w") f.write(name) f.close() self.task.start_task() data = self.task.to_json() self.fh.write(data) # TODO: # Terminate task with given `task_name` #def end(self, task_name): def end(self): # Read ~/.tt_running data = None try: f = open(self.CURRENT) data = f.readline() f.close() except IOError: pass if data: self.task.set_from_dict(self.fh.load_obj(data)) self.task.end_task() self.fh.write(self.task.to_json()) # TODO: print self.task.show_stats() self.reset() else: print "No task running." def reset(self): """Reset the current running task.""" # Empty the self.CURRENT file f = open(self.CURRENT, "w") f.write("") f.close()