Example #1
0
 def setUp(self):
     file_name = "unit_test"
     self.database = file_name + ".json"
     self.json_handler = JSONHandler(self.database)
Example #2
0
 def __init__(self):
     self.task = Task()
     self.fh = JSONHandler("test.json")
Example #3
0
class HandlerTests(TestCase):
    def setUp(self):
        file_name = "unit_test"
        self.database = file_name + ".json"
        self.json_handler = JSONHandler(self.database)
    
    def tearDown(self):
        if path.exists("unit_test.json"): remove("unit_test.json")

    def test_json_handler_writing(self):
        """Check the data integrity of the data in the files of the
        JSONHandler."""
        # Write information to database
        test_data = {"name": "Blabla"}
        self.json_handler.write(test_data)
        json_data = self.json_handler.load()
        self.assertEquals(json_data, test_data)
        # Be sure the handler doesn't tell us shits...
        test_data = {"name": "Blabladf"} # fake the data
        self.assertNotEqual(json_data, test_data)

    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()])
    
    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_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__())

    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__())
Example #4
0
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()