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)
    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)
示例#4
0
    def setUp(self):
        path = os.path.join(helpers.SCHEMA_EXAMPLES_DIR, "LCB-exposure.yaml")
        inputs = [("exposure", path)]
        self.job = self.setup_classic_job(inputs=inputs)

        [input] = models.inputs4job(self.job.id,
                                    input_type="exposure",
                                    path=path)
        owner = models.OqUser.objects.get(user_name="openquake")
        emdl = input.model()
        if not emdl:
            emdl = models.ExposureModel(owner=owner,
                                        input=input,
                                        description="LCB exposure model",
                                        category="LCB cars",
                                        stco_unit="peanuts",
                                        stco_type="aggregated")
            emdl.save()

        asset_data = [
            (Site(-118.077721, 33.852034), {
                u'stco': 5.07,
                u'asset_ref': u'a5625',
                u'taxonomy': u'HAZUS_RM1L_LC'
            }),
            (Site(-118.077721, 33.852034), {
                u'stco': 5.63,
                u'asset_ref': u'a5629',
                u'taxonomy': u'HAZUS_URML_LC'
            }),
            (Site(-118.077721, 33.852034), {
                u'stco': 11.26,
                u'asset_ref': u'a5630',
                u'taxonomy': u'HAZUS_URML_LS'
            }),
            (Site(-118.077721, 33.852034), {
                u'stco': 5.5,
                u'asset_ref': u'a5636',
                u'taxonomy': u'HAZUS_C3L_MC'
            }),
        ]
        for idx, (site, adata) in enumerate(asset_data):
            location = GEOSGeometry(site.point.to_wkt())
            asset = models.ExposureData(exposure_model=emdl,
                                        site=location,
                                        **adata)
            asset.save()
            RISK_LOSS_CURVE_DATA[idx][1][1] = asset

        output_path = self.generate_output_path(self.job)
        self.display_name = os.path.basename(output_path)

        self.writer = LossCurveDBWriter(output_path, self.job.id)
        self.reader = LossCurveDBReader()
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))