import unittest
from caravan.tables import Tables
from caravan.parameter_set import ParameterSet


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

    def test_ps(self):
        ps = ParameterSet(500, (2, 3, 4, 5))
        self.assertEqual(ps.id, 500)
        self.assertEqual(ps.params, (2, 3, 4, 5))
        self.assertEqual(ps.run_ids, [])

    def test_find_or_create(self):
        ps = ParameterSet.find_or_create(0, 1, 2, 3)
        self.assertEqual(ps.id, 0)
        self.assertEqual(ps.params, (0, 1, 2, 3))
        self.assertEqual(len(ParameterSet.all()), 1)
        ps2 = ParameterSet.find_or_create(3, 4, 5, 6)
        self.assertEqual(len(ParameterSet.all()), 2)

    def test_find_or_create(self):
        ps1 = ParameterSet.find_or_create(0, 1, 2, 3)
        ps2 = ParameterSet.find_or_create(0, 1, 2, 3)
        self.assertEqual(ps1, ps2)
        self.assertEqual(len(ParameterSet.all()), 1)

    def test_create_runs(self):
        ps = ParameterSet.find_or_create(0, 1, 2, 3)
        runs = ps.create_runs_upto(3)
        self.assertEqual([r.id for r in runs], [0, 1, 2])
        self.assertEqual([r.seed for r in runs], [0, 1, 2])
        ps2 = ParameterSet.find_or_create(0, 1, 3, 4)
        runs = ps2.create_runs_upto(3)
        self.assertEqual([r.id for r in runs], [3, 4, 5])
        self.assertEqual([r.seed for r in runs], [0, 1, 2])

    def test_is_finished(self):
        ps = ParameterSet.find_or_create(0, 1, 2, 3)
        self.assertEqual(ps.is_finished(), True)
        runs = ps.create_runs_upto(1)
        self.assertFalse(ps.is_finished())
        self.assertEqual(len(ps.finished_runs()), 0)
        runs[0].store_result([1.0, 2.0, 3.0], 0, 3, 111, 222)
        self.assertTrue(ps.is_finished())
        self.assertEqual(len(ps.finished_runs()), 1)

    def test_average_results(self):
        ps = ParameterSet.find_or_create(0, 1, 2, 3)
        runs = ps.create_runs_upto(3)
        self.assertEqual(ps.average_results(), ())
        for (i, r) in enumerate(runs):
            r.store_result([1.0 + i, 2.0 + i, 3.0 + 1], 0, 3, 111, 222)
        self.assertEqual(ps.average_results(), (2.0, 3.0, 4.0))

    def test_all(self):
        ps = ParameterSet.find_or_create(0, 1, 2, 3)
        self.assertEqual(ParameterSet.all(), [ps])
        ps2 = ParameterSet.find_or_create(0, 1, 2, 4)
        self.assertEqual(ParameterSet.all(), [ps, ps2])
        self.assertEqual(len(ParameterSet.all()), 2)

    def test_find(self):
        ps = ParameterSet.find_or_create(0, 1, 2, 3)
        ps2 = ParameterSet.find_or_create(0, 1, 2, 4)
        pid = ps2.id
        self.assertEqual(pid, 1)
        self.assertEqual(ParameterSet.find(1), ps2)


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

[((75, 4, 75, 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, 13, 12, 44), 'caravan.parameter_set.ParameterSet', 'ParameterSet', ({(12, 26, 12, 29): '500', (12, 31, 12, 43): '(2, 3, 4, 5)'}, {}), '(500, (2, 3, 4, 5))', False, 'from caravan.parameter_set import ParameterSet\n'), ((18, 13, 18, 52), 'caravan.parameter_set.ParameterSet.find_or_create', 'ParameterSet.find_or_create', ({(18, 41, 18, 42): '0', (18, 44, 18, 45): '1', (18, 47, 18, 48): '2', (18, 50, 18, 51): '3'}, {}), '(0, 1, 2, 3)', False, 'from caravan.parameter_set import ParameterSet\n'), ((22, 14, 22, 53), 'caravan.parameter_set.ParameterSet.find_or_create', 'ParameterSet.find_or_create', ({(22, 42, 22, 43): '3', (22, 45, 22, 46): '4', (22, 48, 22, 49): '5', (22, 51, 22, 52): '6'}, {}), '(3, 4, 5, 6)', False, 'from caravan.parameter_set import ParameterSet\n'), ((26, 14, 26, 53), 'caravan.parameter_set.ParameterSet.find_or_create', 'ParameterSet.find_or_create', ({(26, 42, 26, 43): '0', (26, 45, 26, 46): '1', (26, 48, 26, 49): '2', (26, 51, 26, 52): '3'}, {}), '(0, 1, 2, 3)', False, 'from caravan.parameter_set import ParameterSet\n'), ((27, 14, 27, 53), 'caravan.parameter_set.ParameterSet.find_or_create', 'ParameterSet.find_or_create', ({(27, 42, 27, 43): '0', (27, 45, 27, 46): '1', (27, 48, 27, 49): '2', (27, 51, 27, 52): '3'}, {}), '(0, 1, 2, 3)', False, 'from caravan.parameter_set import ParameterSet\n'), ((32, 13, 32, 52), 'caravan.parameter_set.ParameterSet.find_or_create', 'ParameterSet.find_or_create', ({(32, 41, 32, 42): '0', (32, 44, 32, 45): '1', (32, 47, 32, 48): '2', (32, 50, 32, 51): '3'}, {}), '(0, 1, 2, 3)', False, 'from caravan.parameter_set import ParameterSet\n'), ((36, 14, 36, 53), 'caravan.parameter_set.ParameterSet.find_or_create', 'ParameterSet.find_or_create', ({(36, 42, 36, 43): '0', (36, 45, 36, 46): '1', (36, 48, 36, 49): '3', (36, 51, 36, 52): '4'}, {}), '(0, 1, 3, 4)', False, 'from caravan.parameter_set import ParameterSet\n'), ((42, 13, 42, 52), 'caravan.parameter_set.ParameterSet.find_or_create', 'ParameterSet.find_or_create', ({(42, 41, 42, 42): '0', (42, 44, 42, 45): '1', (42, 47, 42, 48): '2', (42, 50, 42, 51): '3'}, {}), '(0, 1, 2, 3)', False, 'from caravan.parameter_set import ParameterSet\n'), ((52, 13, 52, 52), 'caravan.parameter_set.ParameterSet.find_or_create', 'ParameterSet.find_or_create', ({(52, 41, 52, 42): '0', (52, 44, 52, 45): '1', (52, 47, 52, 48): '2', (52, 50, 52, 51): '3'}, {}), '(0, 1, 2, 3)', False, 'from caravan.parameter_set import ParameterSet\n'), ((60, 13, 60, 52), 'caravan.parameter_set.ParameterSet.find_or_create', 'ParameterSet.find_or_create', ({(60, 41, 60, 42): '0', (60, 44, 60, 45): '1', (60, 47, 60, 48): '2', (60, 50, 60, 51): '3'}, {}), '(0, 1, 2, 3)', False, 'from caravan.parameter_set import ParameterSet\n'), ((62, 14, 62, 53), 'caravan.parameter_set.ParameterSet.find_or_create', 'ParameterSet.find_or_create', ({(62, 42, 62, 43): '0', (62, 45, 62, 46): '1', (62, 48, 62, 49): '2', (62, 51, 62, 52): '4'}, {}), '(0, 1, 2, 4)', False, 'from caravan.parameter_set import ParameterSet\n'), ((67, 13, 67, 52), 'caravan.parameter_set.ParameterSet.find_or_create', 'ParameterSet.find_or_create', ({(67, 41, 67, 42): '0', (67, 44, 67, 45): '1', (67, 47, 67, 48): '2', (67, 50, 67, 51): '3'}, {}), '(0, 1, 2, 3)', False, 'from caravan.parameter_set import ParameterSet\n'), ((68, 14, 68, 53), 'caravan.parameter_set.ParameterSet.find_or_create', 'ParameterSet.find_or_create', ({(68, 42, 68, 43): '0', (68, 45, 68, 46): '1', (68, 48, 68, 49): '2', (68, 51, 68, 52): '4'}, {}), '(0, 1, 2, 4)', False, 'from caravan.parameter_set import ParameterSet\n'), ((61, 25, 61, 43), 'caravan.parameter_set.ParameterSet.all', 'ParameterSet.all', ({}, {}), '()', False, 'from caravan.parameter_set import ParameterSet\n'), ((63, 25, 63, 43), 'caravan.parameter_set.ParameterSet.all', 'ParameterSet.all', ({}, {}), '()', False, 'from caravan.parameter_set import ParameterSet\n'), ((71, 25, 71, 45), 'caravan.parameter_set.ParameterSet.find', 'ParameterSet.find', ({(71, 43, 71, 44): '(1)'}, {}), '(1)', False, 'from caravan.parameter_set import ParameterSet\n'), ((21, 29, 21, 47), 'caravan.parameter_set.ParameterSet.all', 'ParameterSet.all', ({}, {}), '()', False, 'from caravan.parameter_set import ParameterSet\n'), ((23, 29, 23, 47), 'caravan.parameter_set.ParameterSet.all', 'ParameterSet.all', ({}, {}), '()', False, 'from caravan.parameter_set import ParameterSet\n'), ((29, 29, 29, 47), 'caravan.parameter_set.ParameterSet.all', 'ParameterSet.all', ({}, {}), '()', False, 'from caravan.parameter_set import ParameterSet\n'), ((64, 29, 64, 47), 'caravan.parameter_set.ParameterSet.all', 'ParameterSet.all', ({}, {}), '()', False, 'from caravan.parameter_set import ParameterSet\n')]