def setUp(self): self.db_patch = patch( 'openquake.engine.utils.monitor._db_cnode_status') self.live_patch = ( patch('openquake.engine.utils.monitor._live_cnode_status')) self.db_mock = self.db_patch.start() self.live_mock = self.live_patch.start()
def test_initialize_site_model_no_site_model(self): patch_path = 'openquake.engine.calculators.hazard.general.\ store_site_model' with helpers.patch(patch_path) as store_sm_patch: self.calc.initialize_site_model() # We should never try to store a site model in this case. self.assertEqual(0, store_sm_patch.call_count)
def test_get_with_unknown_key(self): """config.get() returns `None` if the `key` is not known.""" with patch('openquake.engine.utils.config.get_section') as mock: mock.return_value = dict(b=1) self.assertTrue(config.get("arghh", "c") is None) self.assertEqual(1, mock.call_count) self.assertEqual([("arghh",), {}], mock.call_args)
def test_get_with_unknown_key(self): """config.get() returns `None` if the `key` is not known.""" with patch('openquake.engine.config.get_section') as mock: mock.return_value = dict(b=1) self.assertTrue(config.get("arghh", "c") is None) self.assertEqual(1, mock.call_count) self.assertEqual([("arghh", ), {}], mock.call_args)
def test_get_with_empty_section_data(self): # config.get() returns `None` if the section data dict is empty with patch('openquake.engine.utils.config.get_section') as mock: mock.return_value = dict() self.assertTrue(config.get("whatever", "key") is None) self.assertEqual(1, mock.call_count) self.assertEqual([("whatever",), {}], mock.call_args)
def test_get_with_empty_section_data(self): # config.get() returns `None` if the section data dict is empty with patch('openquake.engine.config.get_section') as mock: mock.return_value = dict() self.assertTrue(config.get("whatever", "key") is None) self.assertEqual(1, mock.call_count) self.assertEqual([("whatever", ), {}], mock.call_args)
def test_get_with_nonempty_section_data_and_known_key(self): # config.get() correctly returns the configuration datum for known # sections/keys with patch('openquake.engine.config.get_section') as mock: mock.return_value = dict(a=11) self.assertEqual(11, config.get("hmmm", "a")) self.assertEqual(1, mock.call_count) self.assertEqual([("hmmm", ), {}], mock.call_args)
def test_get_with_nonempty_section_data_and_known_key(self): # config.get() correctly returns the configuration datum for known # sections/keys with patch('openquake.engine.utils.config.get_section') as mock: mock.return_value = dict(a=11) self.assertEqual(11, config.get("hmmm", "a")) self.assertEqual(1, mock.call_count) self.assertEqual([("hmmm",), {}], mock.call_args)
def test(self): # check that if risk models are provided, then the ``points to # compute`` and the imls are got from there username = helpers.default_user() job = engine.prepare_job(username) cfg = helpers.get_data_path('classical_job-sd-imt.ini') params = engine.parse_config(open(cfg, 'r')) haz_calc = engine.create_calculation(models.HazardCalculation, params) haz_calc = models.HazardCalculation.objects.get(id=haz_calc.id) job.hazard_calculation = haz_calc job.is_running = True job.save() base_path = ('openquake.engine.calculators.hazard.classical.core' '.ClassicalHazardCalculator') init_src_patch = helpers.patch( '%s.%s' % (base_path, 'initialize_sources')) init_rlz_patch = helpers.patch( '%s.%s' % (base_path, 'initialize_realizations')) patches = (init_src_patch, init_rlz_patch) mocks = [p.start() for p in patches] get_calculator_class( 'hazard', job.hazard_calculation.calculation_mode)(job).pre_execute() self.assertEqual([(1.0, -1.0), (0.0, 0.0)], [(point.latitude, point.longitude) for point in haz_calc.points_to_compute()]) self.assertEqual(['PGA'], haz_calc.get_imts()) self.assertEqual( 3, haz_calc.oqjob.exposuremodel.exposuredata_set.count()) for i, m in enumerate(mocks): m.stop() patches[i].stop() return job
def test_save_only_nonzero_gmvs(self): ses = mock.Mock() gmvs = numpy.matrix([[0.0, 0, 1]]) gmf_dict = {PGA: dict(rupture_ids=[1, 2, 3], gmvs=gmvs)} points = make_mock_points(1) with helpers.patch('openquake.engine.writer.CacheInserter') as m: core._save_gmfs(ses, gmf_dict, points) self.assertEqual(1, m.add.call_count)
def test_pre_execute(self): base_path = ('openquake.engine.calculators.hazard.disaggregation.core' '.DisaggHazardCalculator') init_src_patch = helpers.patch( '%s.%s' % (base_path, 'initialize_sources')) init_rlz_patch = helpers.patch( '%s.%s' % (base_path, 'initialize_realizations')) patches = (init_src_patch, init_rlz_patch) mocks = [p.start() for p in patches] self.calc.pre_execute() # make sure the site_collection is loaded: self.assertIsNotNone(self.calc.hc.site_collection) for i, m in enumerate(mocks): self.assertEqual(1, m.call_count) m.stop() patches[i].stop()
def test_pre_execute(self): # Most of the pre-execute functionality is implement in other methods. # For this test, just make sure each method gets called. base_path = ('openquake.engine.calculators.hazard.classical.core' '.ClassicalHazardCalculator') init_src_patch = helpers.patch( '%s.%s' % (base_path, 'initialize_sources')) init_rlz_patch = helpers.patch( '%s.%s' % (base_path, 'initialize_realizations')) patches = (init_src_patch, init_rlz_patch) mocks = [p.start() for p in patches] self.calc.pre_execute() # make sure the site_collection is loaded: self.assertIsNotNone(self.calc.hc.site_collection) for i, m in enumerate(mocks): self.assertEqual(1, m.call_count) m.stop() patches[i].stop()
def test_celery_task(self): # Test that the celery task when called properly call the # specific method to write loss map data. patch_dbwriter = helpers.patch( 'openquake.engine.calculators.risk.writers.loss_map',) try: write_lossmap_mock = patch_dbwriter.start() scenario.scenario(*self.calculator.task_arg_gen().next()) finally: patch_dbwriter.stop() self.assertEqual(1, write_lossmap_mock.call_count)
def test_celery_task(self): # Test that the celery task when called properly call the # specific method to write loss curves base_path = 'openquake.engine.calculators.risk.writers' patches = [ helpers.patch('%s.loss_curve' % base_path), helpers.patch('%s.event_loss_curve' % base_path)] try: mocked_loss_writer, mocked_event_loss_writer = [ p.start() for p in patches] event_based.event_based( *self.calculator.task_arg_gen().next()) # we expect 1 asset being filtered out by the region # constraint, so there are only four loss curves (2 of them # are insured) to be written self.assertEqual(0, mocked_loss_writer.call_count) self.assertEqual(2, mocked_event_loss_writer.call_count) finally: [p.stop() for p in patches]
def test_donot_save_trivial_gmf(self): ses = mock.Mock() # setup two ground motion fields on a region made by three # locations. On the first two locations the values are # nonzero, in the third one is zero. Then, we will expect the # cache inserter to add only two entries. gmvs = numpy.matrix([[1., 1.], [1., 1.], [0., 0.]]) gmf_dict = {PGA: dict(rupture_ids=[1, 2], gmvs=gmvs)} points = make_mock_points(3) with helpers.patch('openquake.engine.writer.CacheInserter') as m: core._save_gmfs( ses, gmf_dict, points) self.assertEqual(2, m.add.call_count)
def test_celery_task(self): self.calculator.pre_execute() self.job.is_running = True self.job.status = 'executing' self.job.save() patch = helpers.patch( 'openquake.engine.calculators.risk.writers.loss_curve') try: mocked_writer = patch.start() classical.classical(*self.calculator.task_arg_gen().next()) finally: patch.stop() self.assertEqual(1, mocked_writer.call_count)
def setUp(self): self.output_mock = mock.Mock() self.output_patch = helpers.patch( 'openquake.engine.db.models.Output.objects.get') m = self.output_patch.start() m.return_value = self.output_mock self.output_mock.hazard_metadata.investigation_time = 30 self.output_mock.hazard_metadata.statistics = "mean" self.output_mock.hazard_metadata.quantile = None self.output_mock.hazard_metadata.sm_path = None self.output_mock.hazard_metadata.gsim_path = None rc = self.output_mock.oq_job.risk_calculation rc.exposure_model.category = "air" rc.exposure_model.unit = mock.Mock(return_value="bucks") rc.interest_rate = 0.3 rc.asset_life_expectancy = 10
def setUp(self): self.output_mock = mock.Mock() self.output_patch = helpers.patch( 'openquake.engine.db.models.Output.objects.get') m = self.output_patch.start() m.return_value = self.output_mock self.output_mock.hazard_metadata.investigation_time = 30 self.output_mock.hazard_metadata.statistics = "mean" self.output_mock.hazard_metadata.quantile = None self.output_mock.hazard_metadata.sm_path = None self.output_mock.hazard_metadata.gsim_path = None exposure_model = self.output_mock.oq_job.exposure_model exposure_model.category = "air" exposure_model.unit = mock.Mock(return_value="bucks") rc = self.output_mock.oq_job.get_oqparam() rc.interest_rate = 0.3 rc.asset_life_expectancy = 10
def test_run_scripts_with_failing_upgrades(self): """Upgrades are available but the second one will fail.""" def fail_on_first_even_script( config, script=None, cmd=None, ignore_dryrun=False, runner=None): """Pretend that the second SQL script failed on execution.""" if script and script.find("02-d.sql") >= 0: return(1, "", '02-d.sql:1: ERROR: relation "admin.dbm_test" ') else: return(0, "All goood", "") artefact = "openquake/hzrdi" rev_info = {"step": "2", "id": "3", "revision": "0.3.9-1"} config = {"dryrun": False, "path": "/tmp", "host": "localhost", "db": "openquake", "user": "******"} scripts = ["0.3.9-1/3/01-c.sql", "0.3.9-1/3/02-d.sql", "0.4.2/1/01-a.sql"] with patch('tools.dbmaint.psql') as mock_psql: # Make all the calls pass. mock_psql.side_effect = fail_on_first_even_script # Run the actual function that is to be tested. run_scripts(artefact, rev_info, scripts, config) # The mock was called thrice. self.assertEqual(3, mock_psql.call_count) # The first call executed an SQL script. self.assertEqual({"script": "0.3.9-1/3/01-c.sql"}, mock_psql.call_args_list[0][1]) # The second call executed the second SQL script. self.assertEqual({"script": "0.3.9-1/3/02-d.sql"}, mock_psql.call_args_list[1][1]) # Please note how the step is assigned a -1 value which indicates # a database upgrade failure. self.assertEqual( {"cmd": "UPDATE admin.revision_info SET step=-1, " "revision='0.3.9-1', " "last_update=timezone('UTC'::text, now()) WHERE " "artefact='openquake/hzrdi' AND revision = '0.3.9-1'"}, mock_psql.call_args_list[2][1])
def test_run_scripts_with_available_upgrades(self): """ The `psql` function is called for every upgrade script and at the very end to update the revision step. """ artefact = "openquake/hzrdi" rev_info = {"step": "2", "id": "3", "revision": "0.3.9-1"} config = {"dryrun": True, "path": "/tmp", "host": "localhost", "db": "openquake", "user": "******"} scripts = ["0.3.9-1/3/01-c.sql", "0.3.9-1/3/02-d.sql", "0.4.2/2/01-a.sql"] with patch('tools.dbmaint.psql') as mock_psql: # Make all the calls pass. mock_psql.return_value = (0, "", "") # Run the actual function that is to be tested. run_scripts(artefact, rev_info, scripts, config) # The mock was called four times. self.assertEqual(4, mock_psql.call_count) # The first call executed an SQL script. self.assertEqual({"script": "0.3.9-1/3/01-c.sql"}, mock_psql.call_args_list[0][1]) # The second call executed the second SQL script. self.assertEqual({"script": "0.3.9-1/3/02-d.sql"}, mock_psql.call_args_list[1][1]) # The third call executed the second SQL script. self.assertEqual({"script": "0.4.2/2/01-a.sql"}, mock_psql.call_args_list[2][1]) # The last call executed the command to update the revision step. self.assertEqual( {"cmd": "UPDATE admin.revision_info SET step=2, " "revision='0.4.2', " "last_update=timezone('UTC'::text, now()) WHERE " "artefact='openquake/hzrdi' AND revision = '0.3.9-1'"}, mock_psql.call_args_list[3][1])