Example #1
0
    def test_missing_keys(self):
        """CBC Validate: assert missing required keys."""
        from natcap.invest.coastal_blue_carbon import coastal_blue_carbon
        from natcap.invest import validation

        validation_errors = coastal_blue_carbon.validate({})  # empty args dict.
        invalid_keys = validation.get_invalid_keys(validation_errors)
        expected_missing_keys = set(self.base_required_keys)
        self.assertEqual(invalid_keys, expected_missing_keys)
    def test_missing_keys_transition_maps(self):
        """CBC Validate: assert years required if maps given."""
        from natcap.invest.coastal_blue_carbon import coastal_blue_carbon
        from natcap.invest import validation

        validation_errors = coastal_blue_carbon.validate(
            {'lulc_transition_maps_list': ['foo.tif']})
        invalid_keys = validation.get_invalid_keys(validation_errors)
        expected_missing_keys = set(self.base_required_keys +
                                    ['lulc_transition_years_list'])
        self.assertEqual(invalid_keys, expected_missing_keys)
    def test_missing_keys_do_valuation(self):
        """CBC Validate: assert missing required for valuation."""
        from natcap.invest.coastal_blue_carbon import coastal_blue_carbon
        from natcap.invest import validation

        validation_errors = coastal_blue_carbon.validate(
            {'do_economic_analysis': True})
        invalid_keys = validation.get_invalid_keys(validation_errors)
        expected_missing_keys = set(
            self.base_required_keys +
            ['price', 'inflation_rate', 'discount_rate'])
        self.assertEqual(invalid_keys, expected_missing_keys)
Example #4
0
    def test_validation(self):
        """CBC: Test custom validation."""
        args = TestCBC2._create_model_args(self.workspace_dir)
        args['workspace_dir'] = self.workspace_dir

        # verify validation passes on basic set of arguments.
        validation_warnings = coastal_blue_carbon.validate(args)
        self.assertEqual([], validation_warnings)

        # Now work through the extra validation warnings.
        # Create an invalid transitions table.
        invalid_raster_path = os.path.join(self.workspace_dir,
                                           'invalid_raster.tif')
        with open(invalid_raster_path, 'w') as raster:
            raster.write('not a raster')

        # Write over the landcover snapshot CSV
        prior_snapshots = coastal_blue_carbon._extract_snapshots_from_table(
            args['landcover_snapshot_csv'])
        baseline_year = min(prior_snapshots)
        with open(args['landcover_snapshot_csv'], 'w') as snapshot_table:
            snapshot_table.write('snapshot_year,raster_path\n')
            snapshot_table.write(
                f'{baseline_year},{prior_snapshots[baseline_year]}\n')
            snapshot_table.write(f"{baseline_year + 10},{invalid_raster_path}")

        # analysis year must be >= the last transition year.
        args['analysis_year'] = baseline_year

        validation_warnings = coastal_blue_carbon.validate(args)
        self.assertEqual(len(validation_warnings), 2)
        self.assertIn(
            coastal_blue_carbon.INVALID_SNAPSHOT_RASTER_MSG.format(
                snapshot_year=baseline_year + 10), validation_warnings[0][1])
        self.assertIn(
            coastal_blue_carbon.INVALID_ANALYSIS_YEAR_MSG.format(
                analysis_year=2000, latest_year=2010),
            validation_warnings[1][1])
Example #5
0
    def test_missing_transitions_map_year_mismatch(self):
        """CBC Validate: assert transition maps and years are equal length."""
        from natcap.invest.coastal_blue_carbon import coastal_blue_carbon
        from natcap.invest import validation

        validation_warnings = coastal_blue_carbon.validate(
            {'lulc_transition_years_list': [1999],
             'lulc_transition_maps_list': ['foo.tif', 'bar.tif']})
        invalid_keys = validation.get_invalid_keys(validation_warnings)
        expected_missing_keys = set(
            self.base_required_keys +
            ['lulc_transition_maps_list',
             'lulc_transition_years_list'])
        self.assertEqual(invalid_keys, expected_missing_keys)
        expected_message = 'Must have the same number of elements.'
        actual_messages = set()
        for keys, error_strings in validation_warnings:
            actual_messages.add(error_strings)
        self.assertTrue(expected_message in actual_messages)