def test_invalid_form(self): rc = models.RiskCalculation( calculation_mode="event_based", region_constraint=( 'POLYGON((-122.0 38.113, -122.114 38.113, -122.57 38.111, ' '-122.0 38.113))'), hazard_output=self.job.risk_calculation.hazard_output, sites_disagg='-180.1 38.113, -122.114 38.113', coordinate_bin_width=0.0, loss_curve_resolution=0, mag_bin_width=0.0, ) expected_errors = { 'coordinate_bin_width': ['Coordinate bin width must be > 0.0'], 'distance_bin_width': ['Distance bin width must be > 0.0'], 'loss_curve_resolution': ['Loss Curve Resolution must be >= 1'], 'mag_bin_width': ['Magnitude bin width must be > 0.0'], 'sites_disagg': [ 'Longitude values must in the range [-180, 180]', 'disaggregation requires mag_bin_width, ' 'coordinate_bin_width, distance_bin_width' ], } form = validation.EventBasedRiskForm(instance=rc, files=None) self.assertFalse(form.is_valid()) self.assertEqual(expected_errors, dict(form.errors))
def test_invalid_form(self): rc = models.RiskCalculation( calculation_mode='scenario', region_constraint=( 'POLYGON((-122.0 38.113, -122.114 38.113, -122.57 38.111, ' '-122.0 38.113))'), maximum_distance=100, master_seed=666, asset_correlation='foo', insured_losses=True, ) form = validation.ScenarioRiskForm( instance=rc, files=dict(occupants_vulnerability=object())) expected_errors = { 'asset_correlation': [u'Enter a number.', u'Asset Correlation must be >= 0 and <= 1'], 'time_event': [ 'Scenario Risk requires time_event when an ' 'occupants vulnerability model is given' ], } self.assertFalse(form.is_valid()) self.assertEqual(expected_errors, dict(form.errors))
def test_valid_form(self): args = dict(self.compulsory_arguments.items()) args.update(self.other_args) rc = models.RiskCalculation(**args) form = validation.ClassicalBCRRiskForm(instance=rc, files=None) self.assertTrue(form.is_valid(), dict(form.errors))
def test_valid_form_with_default_resolution(self): rc = models.RiskCalculation( calculation_mode="event_based", region_constraint=( 'POLYGON((-122.0 38.113, -122.114 38.113, -122.57 38.111, ' '-122.0 38.113))'), hazard_output=self.job.risk_calculation.hazard_output) form = validation.EventBasedRiskForm(instance=rc, files=None) self.assertTrue(form.is_valid(), dict(form.errors))
def test_invalid_form(self): region_constraint = ('POLYGON((-122.0 38.113, -122.114 38.113, ' '-122.57 38.111, -122.0 38.113))') rc = models.RiskCalculation( calculation_mode="event_based_bcr", region_constraint=region_constraint, hazard_output=self.job.risk_calculation.hazard_output, ) form = validation.EventBasedBCRRiskForm(instance=rc, files=None) self.assertFalse(form.is_valid())
def test_valid_form(self): region_constraint = ('POLYGON((-122.0 38.113, -122.114 38.113, ' '-122.57 38.111, -122.0 38.113))') rc = models.RiskCalculation( calculation_mode="event_based_bcr", region_constraint=region_constraint, hazard_output=self.job.risk_calculation.hazard_output, interest_rate=0.05, asset_life_expectancy=40, ) form = validation.EventBasedBCRRiskForm(instance=rc, files=None) self.assertTrue(form.is_valid(), dict(form.errors))
def test_invalid_form(self): def powerset(iterable): s = list(iterable) return itertools.chain.from_iterable( itertools.combinations(s, r) for r in range(len(s) + 1)) for fields in list(powerset(self.compulsory_arguments))[1:]: compulsory_arguments = dict(self.compulsory_arguments.items()) for field in fields: compulsory_arguments[field] = None compulsory_arguments.update(self.other_args) rc = models.RiskCalculation(**compulsory_arguments) form = validation.ClassicalBCRRiskForm(instance=rc, files=None) self.assertFalse(form.is_valid(), fields)