import unittest
from caravan.task import Task
from caravan.tables import Tables


class TestRun(unittest.TestCase):
    def setUp(self):
        self.t = Tables.get()
        self.t.clear()

    def test_task(self):
        t = Task(1234, "echo hello world")
        self.assertEqual(t.id(), 1234)
        self.assertEqual(t.is_finished(), False)
        self.assertEqual(t.command(), "echo hello world")
        t._store_result([1.0, 2.0, 3.0], 0, 3, 111, 222)
        self.assertTrue(t.is_finished())
        self.assertEqual(t.rc(), 0)
        self.assertEqual(t.rank(), 3)
        self.assertEqual(t.start_at(), 111)
        self.assertEqual(t.finish_at(), 222)

    def test_create(self):
        for i in range(10):
            t = Task.create("echo %d" % i)
            self.assertEqual(t.id(), i)
            self.assertEqual(t.is_finished(), False)
        self.assertEqual(len(Task.all()), 10)

    def test_all(self):
        tasks = [Task.create("echo %d" % i) for i in range(10)]
        self.assertEqual(Task.all(), tasks)

    def test_find(self):
        tasks = [Task.create("echo %d" % i) for i in range(10)]
        self.assertEqual(Task.find(5).id(), 5)
        self.assertEqual(Task.find(5), tasks[5])


if __name__ == '__main__':
    unittest.main()

[(41, 'unittest.main', 'unittest.main', False, 'import unittest\n'), (8, 'caravan.tables.Tables.get', 'Tables.get', False, 'from caravan.tables import Tables\n'), (12, 'caravan.task.Task', 'Task', False, 'from caravan.task import Task\n'), (25, 'caravan.task.Task.create', 'Task.create', False, 'from caravan.task import Task\n'), (31, 'caravan.task.Task.create', 'Task.create', False, 'from caravan.task import Task\n'), (32, 'caravan.task.Task.all', 'Task.all', False, 'from caravan.task import Task\n'), (35, 'caravan.task.Task.create', 'Task.create', False, 'from caravan.task import Task\n'), (37, 'caravan.task.Task.find', 'Task.find', False, 'from caravan.task import Task\n'), (28, 'caravan.task.Task.all', 'Task.all', False, 'from caravan.task import Task\n'), (36, 'caravan.task.Task.find', 'Task.find', False, 'from caravan.task import Task\n')]