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, 4, 41, 19), 'unittest.main', 'unittest.main', ({}, {}), '()', False, 'import unittest\n'), ((8, 17, 8, 29), 'caravan.tables.Tables.get', 'Tables.get', ({}, {}), '()', False, 'from caravan.tables import Tables\n'), ((12, 12, 12, 42), 'caravan.task.Task', 'Task', ({(12, 17, 12, 21): '1234', (12, 23, 12, 41): '"""echo hello world"""'}, {}), "(1234, 'echo hello world')", False, 'from caravan.task import Task\n'), ((25, 16, 25, 42), 'caravan.task.Task.create', 'Task.create', ({(25, 28, 25, 41): "'echo %d' % i"}, {}), "('echo %d' % i)", False, 'from caravan.task import Task\n'), ((31, 17, 31, 43), 'caravan.task.Task.create', 'Task.create', ({(31, 29, 31, 42): "('echo %d' % i)"}, {}), "('echo %d' % i)", False, 'from caravan.task import Task\n'), ((32, 25, 32, 35), 'caravan.task.Task.all', 'Task.all', ({}, {}), '()', False, 'from caravan.task import Task\n'), ((35, 17, 35, 43), 'caravan.task.Task.create', 'Task.create', ({(35, 29, 35, 42): "('echo %d' % i)"}, {}), "('echo %d' % i)", False, 'from caravan.task import Task\n'), ((37, 25, 37, 37), 'caravan.task.Task.find', 'Task.find', ({(37, 35, 37, 36): '(5)'}, {}), '(5)', False, 'from caravan.task import Task\n'), ((28, 29, 28, 39), 'caravan.task.Task.all', 'Task.all', ({}, {}), '()', False, 'from caravan.task import Task\n'), ((36, 25, 36, 37), 'caravan.task.Task.find', 'Task.find', ({(36, 35, 36, 36): '(5)'}, {}), '(5)', False, 'from caravan.task import Task\n')]