def test_classical_config_file_generation_no_serialize_results_to_db(self):
        """
        By default, config files are generated with the [general] parameter
        SERIALIZE_RESULTS_TO_DB set to True. This test exercises config file
        creation with the parameter set to False.
        """
        expected_config = tests.test_data_path('config_no_serialize_to_db.gem')

        cfg_writer = config_writer.JobConfigWriter(
            self.oqjob.id, serialize_results_to_db=False)

        path_to_new_cfg_file = cfg_writer.serialize()

        self._test_config_files_are_the_same(expected_config,
                                             path_to_new_cfg_file,
                                             self.oqjob.id)
    def test_classical_config_file_generation_with_vuln_imls(self):
        """
        Test that the config file output has the proper IML values,
        as determined by the job's vulnerability model.
        """
        expected_config = tests.test_data_path('config_with_vuln_imls.gem')

        cfg_writer = config_writer.JobConfigWriter(self.oqjob.id,
                                                   derive_imls_from_vuln=True,
                                                   num_of_derived_imls=15)

        path_to_new_cfg_file = cfg_writer.serialize()

        self._test_config_files_are_the_same(expected_config,
                                             path_to_new_cfg_file,
                                             oqjob_id=self.oqjob.id)
def create_inputs(upload_uuid):
    """
    Create some sample :py:class:`geonode.mtapi.models.Input` objects for the
    test case.

    This function will copy the input files from the base data directory
    to a unique temporary directory for the test.
    """
    exposure_path = upload_file_path(upload_uuid, 'exposure.xml')
    exposure = mt_models.Input(owner_id=TEST_OWNER_ID,
                               path=exposure_path,
                               input_type='exposure')

    vuln_path = upload_file_path(upload_uuid, 'vulnerability.xml')
    vuln = mt_models.Input(owner_id=TEST_OWNER_ID,
                           path=vuln_path,
                           input_type='vulnerability')

    src_ltree_path = upload_file_path(upload_uuid,
                                      'source-model-logic-tree.xml')
    src_ltree = mt_models.Input(owner_id=TEST_OWNER_ID,
                                path=src_ltree_path,
                                input_type='lt_source')

    gmpe_ltree_path = upload_file_path(upload_uuid, 'gmpe-logic-tree.xml')
    gmpe_ltree = mt_models.Input(owner_id=TEST_OWNER_ID,
                                 path=gmpe_ltree_path,
                                 input_type='lt_gmpe')

    source_path = upload_file_path(upload_uuid, 'source-model.xml')
    source = mt_models.Input(owner_id=TEST_OWNER_ID,
                             path=source_path,
                             input_type='source')

    inputs = (exposure, vuln, src_ltree, gmpe_ltree, source)

    # copy the files to the test location and get the file size
    for i in inputs:
        file_name = os.path.basename(i.path)
        file_path = tests.test_data_path(file_name)
        shutil.copy(file_path, i.path)
        i.size = os.path.getsize(i.path)

    return inputs
    def test_classical_config_file_generation(self):
        """
        This is somewhat of a 'blackbox' test. We have an existing 'expected'
        config file; given our sample data (which this test suite has loaded
        into the database), we simply generate a config file and compare it to
        the expected file.
        """
        out_path = os.path.join(self.job_dir, 'config.gem')
        expected_config = tests.test_data_path('expected_config.gem')

        cfg_writer = config_writer.JobConfigWriter(self.oqjob.id)

        path_to_new_cfg_file = cfg_writer.serialize()

        # check that the result directory is what we specified
        self.assertEqual(os.path.abspath(out_path),
                         os.path.abspath(path_to_new_cfg_file))

        self._test_config_files_are_the_same(expected_config,
                                             path_to_new_cfg_file,
                                             oqjob_id=self.oqjob.id)
예제 #5
0
class VulnerabilityIMLsTestCase(unittest.TestCase):

    TEST_VULN_GOOD = tests.test_data_path('vulnerability.xml')

    # Doesn't not contain enough IML values
    TEST_VULN_NOT_ENOUGH_IMLS = tests.test_fail_data_path(
        'vuln_not_enough_imls.xml')

    # Contains improperly ordered IML values
    TEST_VULN_BAD_IML_ORDER = tests.test_fail_data_path(
        'vuln_bad_iml_order.xml')

    # Contains negative and 0.0 IML values
    TEST_VULN_BAD_IMLS = tests.test_fail_data_path('vuln_bad_imls.xml')

    def test_lower_bound(self):
        """
        Test _lower_bound with known-good inputs.
        """
        expected_lower_bound = 0.0657675

        lower_bound = _lower_bound(0.0672981496848, 0.0703593786689)

        self.assertEqual(expected_lower_bound, lower_bound)

    def test_lower_bound_raises(self):
        """
        Test that the _lower_bound function raises an AssertionError when the
        computed lower_bound value is <= 0.0.
        """
        # gives a lower bound of 0.0
        self.assertRaises(AssertionError, _lower_bound, 1, 3)
        # gives a negative lower bound
        self.assertRaises(AssertionError, _lower_bound, 1, 5)

    def test_upper_bound(self):
        """
        Test _upper_bound with known-good inputs.
        """
        expected_upper_bound = 5.62235

        upper_bound = _upper_bound(5.50264416787, 5.26323253716)

        self.assertEqual(expected_upper_bound, upper_bound)

    def test_upper_bound_raises(self):
        """
        Test that the _upper_bound function raises an AssertionError when the
        computed upper_bound value is <= 0.0.
        """
        # gives an upper bound of 0.0
        self.assertRaises(AssertionError, _upper_bound, 2, 6)
        # gives a negative upper bound
        self.assertRaises(AssertionError, _upper_bound, 1, 5)

    def test_get_iml_bounds_with_good_vuln_file(self):
        """
        Test calculation of IML bounds from a known-good vulnerability file.
        """
        exp_lb = 0.07414
        exp_ub = 1.62586

        actual_lb, actual_ub = _get_iml_bounds_from_vuln_file(
            self.TEST_VULN_GOOD)

        self.assertEqual(exp_lb, actual_lb)
        self.assertEqual(exp_ub, actual_ub)

    def test_get_iml_bounds_raises_when_not_enough_imls(self):
        """
        If a vulnerability file contains less than 2 IMLs values in a given IML
        set, an AssertionError should be raised.
        """
        self.assertRaises(
            AssertionError, _get_iml_bounds_from_vuln_file,
            self.TEST_VULN_NOT_ENOUGH_IMLS)

    def test_get_iml_bounds_raises_when_imls_are_not_in_asc_order(self):
        """
        If the IMLs in a given IML set are not arranged in ascending order
        (where no two values are equal), an AssertionError should be raised.
        """
        self.assertRaises(
            AssertionError, _get_iml_bounds_from_vuln_file,
            self.TEST_VULN_BAD_IML_ORDER)

    def test_get_iml_bounds_raises_on_invalid_imls(self):
        """
        If a vulnerability file contains IML values <= 0.0, an AssertionError
        should be raised.
        """
        self.assertRaises(
            AssertionError, _get_iml_bounds_from_vuln_file,
            self.TEST_VULN_BAD_IMLS)