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()

[((2776, 2791), 'unittest.main', 'unittest.main', ([], {}), '()\n', (2789, 2791), False, 'import unittest\n'), ((180, 192), 'caravan.tables.Tables.get', 'Tables.get', ([], {}), '()\n', (190, 192), False, 'from caravan.tables import Tables\n'), ((253, 284), 'caravan.parameter_set.ParameterSet', 'ParameterSet', (['(500)', '(2, 3, 4, 5)'], {}), '(500, (2, 3, 4, 5))\n', (265, 284), False, 'from caravan.parameter_set import ParameterSet\n'), ((462, 501), 'caravan.parameter_set.ParameterSet.find_or_create', 'ParameterSet.find_or_create', (['(0)', '(1)', '(2)', '(3)'], {}), '(0, 1, 2, 3)\n', (489, 501), False, 'from caravan.parameter_set import ParameterSet\n'), ((654, 693), 'caravan.parameter_set.ParameterSet.find_or_create', 'ParameterSet.find_or_create', (['(3)', '(4)', '(5)', '(6)'], {}), '(3, 4, 5, 6)\n', (681, 693), False, 'from caravan.parameter_set import ParameterSet\n'), ((797, 836), 'caravan.parameter_set.ParameterSet.find_or_create', 'ParameterSet.find_or_create', (['(0)', '(1)', '(2)', '(3)'], {}), '(0, 1, 2, 3)\n', (824, 836), False, 'from caravan.parameter_set import ParameterSet\n'), ((851, 890), 'caravan.parameter_set.ParameterSet.find_or_create', 'ParameterSet.find_or_create', (['(0)', '(1)', '(2)', '(3)'], {}), '(0, 1, 2, 3)\n', (878, 890), False, 'from caravan.parameter_set import ParameterSet\n'), ((1025, 1064), 'caravan.parameter_set.ParameterSet.find_or_create', 'ParameterSet.find_or_create', (['(0)', '(1)', '(2)', '(3)'], {}), '(0, 1, 2, 3)\n', (1052, 1064), False, 'from caravan.parameter_set import ParameterSet\n'), ((1235, 1274), 'caravan.parameter_set.ParameterSet.find_or_create', 'ParameterSet.find_or_create', (['(0)', '(1)', '(3)', '(4)'], {}), '(0, 1, 3, 4)\n', (1262, 1274), False, 'from caravan.parameter_set import ParameterSet\n'), ((1478, 1517), 'caravan.parameter_set.ParameterSet.find_or_create', 'ParameterSet.find_or_create', (['(0)', '(1)', '(2)', '(3)'], {}), '(0, 1, 2, 3)\n', (1505, 1517), False, 'from caravan.parameter_set import ParameterSet\n'), ((1908, 1947), 'caravan.parameter_set.ParameterSet.find_or_create', 'ParameterSet.find_or_create', (['(0)', '(1)', '(2)', '(3)'], {}), '(0, 1, 2, 3)\n', (1935, 1947), False, 'from caravan.parameter_set import ParameterSet\n'), ((2250, 2289), 'caravan.parameter_set.ParameterSet.find_or_create', 'ParameterSet.find_or_create', (['(0)', '(1)', '(2)', '(3)'], {}), '(0, 1, 2, 3)\n', (2277, 2289), False, 'from caravan.parameter_set import ParameterSet\n'), ((2355, 2394), 'caravan.parameter_set.ParameterSet.find_or_create', 'ParameterSet.find_or_create', (['(0)', '(1)', '(2)', '(4)'], {}), '(0, 1, 2, 4)\n', (2382, 2394), False, 'from caravan.parameter_set import ParameterSet\n'), ((2543, 2582), 'caravan.parameter_set.ParameterSet.find_or_create', 'ParameterSet.find_or_create', (['(0)', '(1)', '(2)', '(3)'], {}), '(0, 1, 2, 3)\n', (2570, 2582), False, 'from caravan.parameter_set import ParameterSet\n'), ((2597, 2636), 'caravan.parameter_set.ParameterSet.find_or_create', 'ParameterSet.find_or_create', (['(0)', '(1)', '(2)', '(4)'], {}), '(0, 1, 2, 4)\n', (2624, 2636), False, 'from caravan.parameter_set import ParameterSet\n'), ((2315, 2333), 'caravan.parameter_set.ParameterSet.all', 'ParameterSet.all', ([], {}), '()\n', (2331, 2333), False, 'from caravan.parameter_set import ParameterSet\n'), ((2420, 2438), 'caravan.parameter_set.ParameterSet.all', 'ParameterSet.all', ([], {}), '()\n', (2436, 2438), False, 'from caravan.parameter_set import ParameterSet\n'), ((2716, 2736), 'caravan.parameter_set.ParameterSet.find', 'ParameterSet.find', (['(1)'], {}), '(1)\n', (2733, 2736), False, 'from caravan.parameter_set import ParameterSet\n'), ((616, 634), 'caravan.parameter_set.ParameterSet.all', 'ParameterSet.all', ([], {}), '()\n', (632, 634), False, 'from caravan.parameter_set import ParameterSet\n'), ((723, 741), 'caravan.parameter_set.ParameterSet.all', 'ParameterSet.all', ([], {}), '()\n', (739, 741), False, 'from caravan.parameter_set import ParameterSet\n'), ((955, 973), 'caravan.parameter_set.ParameterSet.all', 'ParameterSet.all', ([], {}), '()\n', (971, 973), False, 'from caravan.parameter_set import ParameterSet\n'), ((2480, 2498), 'caravan.parameter_set.ParameterSet.all', 'ParameterSet.all', ([], {}), '()\n', (2496, 2498), False, 'from caravan.parameter_set import ParameterSet\n')]