def test_exposure_data_with_no_number_and_coco_reco_not_aggregated(self): # the 'number_of_units' datum must be present when the contents # and retrofitting cost type is not 'aggregated'. self.mdl.area_type = "per_asset" self.mdl.area_unit = "sqm" self.mdl.reco_type = "per_area" self.mdl.reco_unit = "TJR" self.mdl.coco_type = "per_asset" self.mdl.coco_unit = "MVR" self.mdl.save() site = shapes.Site(-121.9000, 38.1000) edata = models.ExposureData(exposure_model=self.mdl, asset_ref=helpers.random_string(), taxonomy=helpers.random_string(), stco=16.0, coco=17.0, site=site.point.to_wkt()) try: edata.save() except DatabaseError, de: self.assertEqual( "Exception: number_of_units is mandatory for " "<coco_type=per_asset, reco_type=per_area> (exposure_data)", de.args[0].split('\n', 1)[0]) transaction.rollback()
def setUpClass(cls): default_user = helpers.default_user() cls.job = models.OqJob(owner=default_user) cls.job.save() # dmg dist per asset cls.ddpa_output = models.Output( owner=default_user, oq_job=cls.job, display_name='Test dmg dist per asset', output_type='dmg_dist_per_asset', db_backed=True) cls.ddpa_output.save() cls.ddpa = models.DmgDistPerAsset( output=cls.ddpa_output, dmg_states=cls.DMG_STATES) cls.ddpa.save() # We also need some sample exposure data records (to satisfy the dmg # dist per asset FK). test_input = models.Input( owner=default_user, input_type='exposure', path='fake', size=0) test_input.save() i2j = models.Input2job(input=test_input, oq_job=cls.job) i2j.save() exp_model = models.ExposureModel( owner=default_user, input=test_input, name='test-exp-model', category='economic loss', stco_type='per_asset', stco_unit='CHF') exp_model.save() test_site = shapes.Site(3.14, 2.17) cls.exp_data = models.ExposureData( # Asset exposure_model=exp_model, asset_ref=helpers.random_string(), taxonomy=helpers.random_string(), number_of_units=37, site=test_site.point.to_wkt(), stco=1234.56) cls.exp_data.save() # dmg dist per taxonomy cls.ddpt_output = models.Output( owner=default_user, oq_job=cls.job, display_name='Test dmg dist per taxonomy', output_type='dmg_dist_per_taxonomy', db_backed=True) cls.ddpt_output.save() cls.ddpt = models.DmgDistPerTaxonomy( output=cls.ddpt_output, dmg_states=cls.DMG_STATES) cls.ddpt.save() # total dmg dist cls.ddt_output = models.Output( owner=default_user, oq_job=cls.job, display_name='Test dmg dist total', output_type='dmg_dist_total', db_backed=True) cls.ddt_output.save() cls.ddt = models.DmgDistTotal( output=cls.ddt_output, dmg_states=cls.DMG_STATES) cls.ddt.save()
def test_exposure_data_with_coco_and_unit_type_count(self): # the content cost may be present when unit_type is set to "count" site = shapes.Site(-122.5000, 37.5000) edata = models.ExposureData( exposure_model=self.mdl, asset_ref=helpers.random_string(), taxonomy=helpers.random_string(), number_of_units=111, site=site.point.to_wkt(), coco=112) edata.save()
def test_exposure_data_unit_type_count(self): # the unit_type is set to "count" and we can insert an `exposure_data` # row as long as we omit the area, reco and stco properties. site = shapes.Site(-122.5000, 37.5000) edata = models.ExposureData( exposure_model=self.mdl, asset_ref=helpers.random_string(), taxonomy=helpers.random_string(), number_of_units=111, site=site.point.to_wkt()) edata.save()
def test_exposure_data_with_no_stco_and_population(self): # the structural cost needs not be present when we calculate exposure # in terms of population self.mdl.stco_type = None self.mdl.stco_unit = None self.mdl.category = "population" self.mdl.save() site = shapes.Site(-122.5000, 37.5000) edata = models.ExposureData( exposure_model=self.mdl, asset_ref=helpers.random_string(), taxonomy=helpers.random_string(), number_of_units=111, site=site.point.to_wkt()) edata.save()
def test_exposure_data_with_coco_type_but_no_value(self): # if the content cost type is set the value must be set as well. site = shapes.Site(-122.5000, 37.5000) edata = models.ExposureData( exposure_model=self.mdl, asset_ref=helpers.random_string(), taxonomy=helpers.random_string(), number_of_units=111, site=site.point.to_wkt()) try: edata.save() except DatabaseError, de: self.assertEqual( "Exception: contents cost is mandatory for " "<coco_type=per_asset> (exposure_data)", de.args[0].split('\n', 1)[0]) transaction.rollback()
def test_exposure_data_with_area_and_unit_type_count(self): # the area cost must not be present when unit_type is set to "count" site = shapes.Site(-122.5000, 37.5000) edata = models.ExposureData( exposure_model=self.mdl, asset_ref=helpers.random_string(), taxonomy=helpers.random_string(), number_of_units=111, site=site.point.to_wkt(), area=112) try: edata.save() except DatabaseError, de: self.assertEqual( "Exception: We are in counting mode: neither of these must " "be set ['area'] (exposure_data)", de.args[0].split('\n', 1)[0]) transaction.rollback()
def test_ddpt_update_invalid_dmg_state(self): dd = models.DmgDistPerTaxonomy( taxonomy=helpers.random_string(), dmg_state=self.dmg_states['extensive'], mean=0.0, stddev=0.0) dd.save() self._test_insert_update_invalid(dd, 'dmg_dist_per_taxonomy')
def setUpClass(cls): cls.job = engine.prepare_job() jp, _, _ = engine.import_job_profile(RISK_DEMO_CONFIG_FILE, cls.job) cls.job_ctxt = helpers.create_job({}, job_id=cls.job.id, oq_job_profile=jp, oq_job=cls.job) calc = ClassicalRiskCalculator(cls.job_ctxt) calc.store_exposure_assets() [input] = models.inputs4job(cls.job.id, input_type="exposure") model = input.model() assets = model.exposuredata_set.filter(taxonomy="af/ctc-D/LR") # Add some more assets. coos = [(10.000155392289116, 46.546194318563), (10.222034128255, 46.0071299176413), (10.520376165581, 46.247463385278)] for lat, lon in coos: site = shapes.Site(lat, lon) cls.sites.append(site) if assets: continue location = geos.GEOSGeometry(site.point.to_wkt()) asset = models.ExposureData(exposure_model=model, taxonomy="af/ctc-D/LR", asset_ref=helpers.random_string(6), stco=lat * 2, site=location, reco=1.1 * lon) asset.save()
def test_ddpt_insert_valid_dmg_state(self): for ds in self.dmg_states.itervalues(): dd = models.DmgDistPerTaxonomy( taxonomy=helpers.random_string(), dmg_state=ds, mean=0.0, stddev=0.0) dd.save()
def setUpClass(cls): cls.job = engine.prepare_job() jp, _, _ = engine.import_job_profile(RISK_DEMO_CONFIG_FILE, cls.job) cls.job_ctxt = helpers.create_job({}, job_id=cls.job.id, oq_job_profile=jp, oq_job=cls.job) calc = ClassicalRiskCalculator(cls.job_ctxt) calc.store_exposure_assets() [input] = models.inputs4job(cls.job.id, input_type="exposure") model = input.model() assets = model.exposuredata_set.filter(taxonomy="af/ctc-D/LR") # Add some more assets. coos = [(10.000155392289116, 46.546194318563), (10.222034128255, 46.0071299176413), (10.520376165581, 46.247463385278)] for lat, lon in coos: site = shapes.Site(lat, lon) cls.sites.append(site) if assets: continue location = geos.GEOSGeometry(site.point.to_wkt()) asset = models.ExposureData( exposure_model=model, taxonomy="af/ctc-D/LR", asset_ref=helpers.random_string(6), stco=lat * 2, site=location, reco=1.1 * lon) asset.save()
def test_ddpt_insert_valid_dmg_state(self): for ds in self.DMG_STATES: dd = models.DmgDistPerTaxonomyData( dmg_dist_per_taxonomy=self.ddpt, taxonomy=helpers.random_string(), dmg_state=ds, mean=0.0, stddev=0.0) dd.save()
def test_ddpt_update_valid_dmg_state(self): dd = models.DmgDistPerTaxonomy( taxonomy=helpers.random_string(), dmg_state=self.dmg_states['extensive'], mean=0.0, stddev=0.0) dd.save() dd.dmg_state = self.dmg_states['complete'] dd.save()
def test_ddpt_update_invalid_dmg_state(self): dd = models.DmgDistPerTaxonomyData( dmg_dist_per_taxonomy=self.ddpt, taxonomy=helpers.random_string(), dmg_state='extensive', mean=0.0, stddev=0.0) dd.save() self._test_insert_update_invalid(dd, 'dmg_dist_per_taxonomy_data')
def test_exposure_data_with_no_stco_and_category_not_population(self): # the structural cost must be present when we calculate exposure # in terms other than population. self.mdl.save() site = shapes.Site(-122.4000, 37.6000) edata = models.ExposureData( exposure_model=self.mdl, asset_ref=helpers.random_string(), taxonomy=helpers.random_string(), number_of_units=111, site=site.point.to_wkt()) try: edata.save() except DatabaseError, de: self.assertEqual( "Exception: structural cost is mandatory for category " "<economic loss> (exposure_data)", de.args[0].split('\n', 1)[0]) transaction.rollback()
def test_exposure_data_with_no_number_and_stco_type_not_aggregated(self): # the 'number_of_units' datum must be present when the structural # cost type is not 'aggregated'. self.mdl.stco_type = "per_asset" self.mdl.save() site = shapes.Site(-122.2000, 37.8000) edata = models.ExposureData( exposure_model=self.mdl, asset_ref=helpers.random_string(), taxonomy=helpers.random_string(), stco=11.0, site=site.point.to_wkt()) try: edata.save() except DatabaseError, de: self.assertEqual( "Exception: number_of_units is mandatory for " "<stco_type=per_asset> (exposure_data)", de.args[0].split('\n', 1)[0]) transaction.rollback()
def test_ddpt_update_valid_dmg_state(self): dd = models.DmgDistPerTaxonomyData( dmg_dist_per_taxonomy=self.ddpt, taxonomy=helpers.random_string(), dmg_state='extensive', mean=0.0, stddev=0.0) dd.save() dd.dmg_state = 'complete' dd.save()
def test_exposure_stco_type_but_no_stco_value(self): # the structural cost must be present if the structural cost type # is set. self.mdl.category = "population" self.mdl.save() site = shapes.Site(-121.6000, 38.4000) edata = models.ExposureData( exposure_model=self.mdl, asset_ref=helpers.random_string(), taxonomy=helpers.random_string(), number_of_units=24, site=site.point.to_wkt()) try: edata.save() except DatabaseError, de: self.assertEqual( "Exception: structural cost is mandatory for " "<stco_type=aggregated> (exposure_data)", de.args[0].split('\n', 1)[0]) transaction.rollback()
def test_del_haz_calc_no_access(self): # Test the case where we try to delete a hazard calculation which does # not belong to current user. # In this case, deletion is now allowed and should raise an exception. hazard_job = helpers.get_hazard_job(self.hazard_cfg, username=helpers.random_string()) hazard_calc = hazard_job.hazard_calculation self.assertRaises(RuntimeError, engine.del_haz_calc, hazard_calc.id)
def test_exposure_data_with_no_number_and_population(self): # the 'number_of_units' datum must be present when we calculate # exposure in terms of population self.mdl.category = "population" self.mdl.save() site = shapes.Site(-122.3000, 37.7000) edata = models.ExposureData(exposure_model=self.mdl, asset_ref=helpers.random_string(), taxonomy=helpers.random_string(), site=site.point.to_wkt()) try: edata.save() except DatabaseError, de: self.assertEqual( "Exception: number_of_units is mandatory for " "<category=population> (exposure_data)", de.args[0].split('\n', 1)[0]) transaction.rollback()
def test_exposure_data_with_no_number_and_population(self): # the 'number_of_units' datum must be present when we calculate # exposure in terms of population self.mdl.category = "population" self.mdl.save() site = shapes.Site(-122.3000, 37.7000) edata = models.ExposureData( exposure_model=self.mdl, asset_ref=helpers.random_string(), taxonomy=helpers.random_string(), site=site.point.to_wkt()) try: edata.save() except DatabaseError, de: self.assertEqual( "Exception: number_of_units is mandatory for " "<category=population> (exposure_data)", de.args[0].split('\n', 1)[0]) transaction.rollback()
def test_prepare_user_exists(self): user_name = helpers.random_string() existing_user = models.OqUser( user_name=user_name, full_name=user_name, organization_id=1 ) existing_user.save() user = engine2.prepare_user(user_name) self.assertEqual(existing_user.id, user.id)
def test_exposure_coco_type_but_no_coco_value(self): # the contents cost must be present if the contents cost type # is set. self.mdl.coco_type = "per_asset" self.mdl.coco_unit = "MUR" self.mdl.save() site = shapes.Site(-121.7000, 38.3000) edata = models.ExposureData( exposure_model=self.mdl, asset_ref=helpers.random_string(), taxonomy=helpers.random_string(), stco=19.0, number_of_units=23, site=site.point.to_wkt()) try: edata.save() except DatabaseError, de: self.assertEqual( "Exception: contents cost is mandatory for " "<coco_type=per_asset> (exposure_data)", de.args[0].split('\n', 1)[0]) transaction.rollback()
def test_del_risk_calc_no_access(self): # Test the case where we try to delete a risk calculation which does # not belong to current user. # In this case, deletion is now allowed and should raise an exception. risk_job, _ = helpers.get_fake_risk_job( self.risk_cfg, self.hazard_cfg, output_type='curves', username=helpers.random_string() ) risk_calc = risk_job.risk_calculation self.assertRaises(RuntimeError, engine.del_risk_calc, risk_calc.id)
def test_exposure_stco_type_per_area_but_no_area_value(self): # the area must be set if the structural cost type is 'per_area' self.mdl.stco_type = "per_area" self.mdl.stco_unit = "IDR" self.mdl.area_type = "aggregated" self.mdl.area_unit = "ATS" self.mdl.save() site = shapes.Site(-121.3000, 38.7000) edata = models.ExposureData(exposure_model=self.mdl, asset_ref=helpers.random_string(), taxonomy=helpers.random_string(), stco=22.0, site=site.point.to_wkt()) try: edata.save() except DatabaseError, de: self.assertEqual( "Exception: area is mandatory for <stco_type=per_area> " "(exposure_data)", de.args[0].split('\n', 1)[0]) transaction.rollback()
def test_exposure_stco_type_per_area_but_no_area_value(self): # the area must be set if the structural cost type is 'per_area' self.mdl.stco_type = "per_area" self.mdl.stco_unit = "IDR" self.mdl.area_type = "aggregated" self.mdl.area_unit = "ATS" self.mdl.save() site = shapes.Site(-121.3000, 38.7000) edata = models.ExposureData( exposure_model=self.mdl, asset_ref=helpers.random_string(), taxonomy=helpers.random_string(), stco=22.0, site=site.point.to_wkt()) try: edata.save() except DatabaseError, de: self.assertEqual( "Exception: area is mandatory for <stco_type=per_area> " "(exposure_data)", de.args[0].split('\n', 1)[0]) transaction.rollback()
def setUpClass(cls): default_user = helpers.default_user() cls.job = models.OqJob(owner=default_user) rc = engine2.create_risk_calculation( cls.job.owner, dict(calculation_mode='scenario_damage', base_path='/'), []) cls.job.risk_calculation = rc cls.job.save() # We also need some sample exposure data records (to satisfy the dmg # dist per asset FK). test_input = models.Input( owner=default_user, input_type='exposure', path='fake', size=0) test_input.save() i2j = models.Input2job(input=test_input, oq_job=cls.job) i2j.save() exp_model = models.ExposureModel( owner=default_user, input=test_input, name='test-exp-model', category='economic loss', stco_type='per_asset', stco_unit='CHF') exp_model.save() test_site = Point(3.14, 2.17) cls.exp_data = models.ExposureData( # Asset exposure_model=exp_model, asset_ref=helpers.random_string(), taxonomy=helpers.random_string(), number_of_units=37, site=test_site.to_wkt(), stco=1234.56) cls.exp_data.save() cls.dmg_states = {} for lsi, dmg_state in enumerate(cls.DMG_STATES): dstate = models.DmgState( risk_calculation=cls.job.risk_calculation, dmg_state=dmg_state, lsi=lsi) cls.dmg_states[dmg_state] = dstate dstate.save() cls.invalid_state = models.DmgState( risk_calculation=cls.job.risk_calculation, dmg_state='invalid state', lsi=0)
def test_prepare_job_specified_user(self): user_name = helpers.random_string() job = engine.prepare_job(user_name=user_name) self.assertEqual(user_name, job.user_name) self.assertEqual('pre_executing', job.status) self.assertEqual('progress', job.log_level) try: models.OqJob.objects.get(id=job.id) except exceptions.ObjectDoesNotExist: self.fail('Job was not found in the database')
def test_prepare_job_specified_user(self): user_name = helpers.random_string() job = engine2.prepare_job(user_name=user_name) self.assertEqual(user_name, job.owner.user_name) self.assertEqual('pre_executing', job.status) self.assertEqual('progress', job.log_level) try: models.OqJob.objects.get(id=job.id) except exceptions.ObjectDoesNotExist: self.fail('Job was not found in the database')
def test_exposure_data_with_no_number_and_coco_reco_not_aggregated(self): # the 'number_of_units' datum must be present when the contents # and retrofitting cost type is not 'aggregated'. self.mdl.area_type = "per_asset" self.mdl.area_unit = "sqm" self.mdl.reco_type = "per_area" self.mdl.reco_unit = "TJR" self.mdl.coco_type = "per_asset" self.mdl.coco_unit = "MVR" self.mdl.save() site = shapes.Site(-121.9000, 38.1000) edata = models.ExposureData( exposure_model=self.mdl, asset_ref=helpers.random_string(), taxonomy=helpers.random_string(), stco=16.0, coco=17.0, site=site.point.to_wkt()) try: edata.save() except DatabaseError, de: self.assertEqual( "Exception: number_of_units is mandatory for " "<coco_type=per_asset, reco_type=per_area> (exposure_data)", de.args[0].split('\n', 1)[0]) transaction.rollback()
def test_prepare_user_does_not_exist(self): user_name = helpers.random_string() # Sanity check; make sure the user doesn't exist yet. self.assertEqual( 0, len(models.OqUser.objects.filter(user_name=user_name)) ) engine2.prepare_user(user_name) # Now the user should exist. self.assertEqual( 1, len(models.OqUser.objects.filter(user_name=user_name)) )
def test_log_file(self): # Test logging to a file when running bin/oqscript.py. uhs_cfg = helpers.demo_file('uhs/config.gem') log_file = './%s.log' % helpers.random_string() self.assertFalse(os.path.exists(log_file)) ret_code = helpers.run_job( uhs_cfg, ['--log-level', 'debug', '--log-file', log_file]) self.assertEqual(0, ret_code) self.assertTrue(os.path.exists(log_file)) # Make sure there is something in it. self.assertTrue(os.path.getsize(log_file) > 0) os.unlink(log_file)
def test_ddpt_insert_invalid_dmg_state(self): dd = models.DmgDistPerTaxonomyData( dmg_dist_per_taxonomy=self.ddpt, taxonomy=helpers.random_string(), mean=0.0, stddev=0.0) self._test_insert_update_invalid(dd, 'dmg_dist_per_taxonomy_data')