Exemplo n.º 1
0
    def test_job_launch_calls_record_initial_stats(self):
        """When a job is launched, make sure that
        :py:method:`openquake.engine.CalculationProxy._record_initial_stats`
        is called.
        """
        # Mock out pieces of the test job so it doesn't actually run.
        haz_execute = "openquake.calculators.hazard.event_based.core" ".EventBasedHazardCalculator.execute"
        risk_execute = "openquake.calculators.risk.event_based.core" ".EventBasedRiskCalculator.execute"
        record = "openquake.engine.CalculationProxy._record_initial_stats"

        with patch(haz_execute):
            with patch(risk_execute):
                with patch(record) as record_mock:
                    engine._launch_calculation(self.eb_job, ["general", "HAZARD", "RISK"])

                    self.assertEqual(1, record_mock.call_count)
Exemplo n.º 2
0
    def test__launch_calculation_calls_core_calc_methods(self):
        # The `Calculator` interface defines 4 general methods:
        # - analyze
        # - pre_execute
        # - execute
        # - post_execute
        # When `_launch_calculation` is called, each of these methods should be
        # called once per job type (hazard, risk).

        # Calculation setup:
        cfg_file = demo_file('classical_psha_based_risk/config.gem')

        job_profile, params, sections = engine.import_job_profile(cfg_file)
        calculation = OqCalculation(owner=job_profile.owner,
                                    oq_job_profile=job_profile)
        calculation.save()

        calc_proxy = engine.CalculationProxy(
            params, calculation.id, sections=sections,
            serialize_results_to=['xml', 'db'],
            oq_job_profile=job_profile, oq_calculation=calculation)

        # Mocking setup:
        cls_haz_calc = ('openquake.calculators.hazard.classical.core'
                        '.ClassicalHazardCalculator')
        cls_risk_calc = ('openquake.calculators.risk.classical.core'
                         '.ClassicalRiskCalculator')
        methods = ('analyze', 'pre_execute', 'execute', 'post_execute')
        haz_patchers = [patch('%s.%s' % (cls_haz_calc, m)) for m in methods]
        risk_patchers = [patch('%s.%s' % (cls_risk_calc, m)) for m in methods]

        haz_mocks = [p.start() for p in haz_patchers]
        risk_mocks = [p.start() for p in risk_patchers]

        # Call the function under test:
        engine._launch_calculation(calc_proxy, sections)

        self.assertTrue(all(x.call_count == 1 for x in haz_mocks))
        self.assertTrue(all(x.call_count == 1 for x in risk_mocks))

        # Tear down the mocks:
        for p in haz_patchers:
            p.stop()
        for p in risk_patchers:
            p.stop()