def test_serialize_small(self):
        data = LOSS_CURVE_DATA(20, 4)

        self.job = self.setup_classic_job()
        output_path = self.generate_output_path(self.job)

        for i in xrange(0, 20):
            lcw = LossCurveDBWriter(output_path + str(i), self.job.id)

            # Call the function under test.
            lcw.serialize(data)
    def test_serialize_small(self):
        data = LOSS_CURVE_DATA(20, 4)

        self.job = self.setup_classic_job()
        output_path = self.generate_output_path(self.job)

        for i in xrange(0, 20):
            lcw = LossCurveDBWriter(output_path + str(i), self.job.id)

            # Call the function under test.
            lcw.serialize(data)
class LossCurveDBWriterTestCase(unittest.TestCase, helpers.DbTestMixin):
    """
    Unit tests for the LossCurveDBWriter class, which serializes
    loss curves to the database.
    """
    def tearDown(self):
        if hasattr(self, "job") and self.job:
            self.teardown_job(self.job)
        if hasattr(self, "output") and self.output:
            self.teardown_output(self.output)

    def setUp(self):
        self.job = self.setup_classic_job()
        self.session = get_uiapi_writer_session()
        output_path = self.generate_output_path(self.job)
        self.display_name = os.path.basename(output_path)

        self.writer = LossCurveDBWriter(self.session, output_path, self.job.id)

    def test_insert(self):
        """All the records are inserted correctly."""
        output = self.writer.output

        # Call the function under test.
        data = RISK_LOSS_CURVE_DATA
        self.writer.serialize(data)

        # After calling the function under test we see the expected output.
        self.assertEqual(1, len(self.job.output_set))

        # Make sure the inserted output record has the right data.
        [output] = self.job.output_set
        self.assertTrue(output.db_backed)
        self.assertTrue(output.path is None)
        self.assertEqual(self.display_name, output.display_name)
        self.assertEqual("loss_curve", output.output_type)
        self.assertTrue(self.job is output.oq_job)

        # After calling the function under test we see the expected loss asset
        # data.
        self.assertEqual(4, len(output.lossassetdata_set))

        inserted_data = []

        for lad in output.lossassetdata_set:
            pos = lad.pos.coords(self.session)

            for curve in lad.losscurvedata_set:
                data = (Site(pos[0], pos[1]),
                        (Curve(zip(curve.abscissae, curve.poes)),
                        {u'assetID': lad.asset_id}))

                inserted_data.append(data)

        def normalize(values):
            result = []
            for value in values:
                result.append((value[0],
                               (value[1][0],
                                {'assetID': value[1][1]['assetID']})))

            return sorted(result, key=lambda v: v[1][1]['assetID'])

        self.assertEquals(normalize(RISK_LOSS_CURVE_DATA),
                          normalize(inserted_data))