Пример #1
0
    def test_undefined_user_admin_error(self):
        """checks that an error is raised when a classifier description is
        not present in the user value of admin mapping
        """
        mapping = {
            "spatial_units": {
                "mapping_mode":
                "SeparateAdminEcoClassifiers",
                "admin_classifier":
                "classifier1",
                "eco_classifier":
                "classifier2",
                "admin_mapping": [{
                    "user_admin_boundary": "missing",
                    "default_admin_boundary": "Alberta"
                }],
                "eco_mapping": [{
                    "user_eco_boundary": "a",
                    "default_eco_boundary": "Montane Cordillera"
                }]
            }
        }
        ref = Mock(spec=SITCBMDefaults)
        classifiers, classifier_values = self.get_mock_classifiers()
        inventory = pd.DataFrame({
            "classifier1": ["a", "b"],
            "classifier2": ["a", "a"],
        })

        sit_mapping = SITMapping(mapping, ref)
        with self.assertRaises(KeyError):
            sit_mapping.get_spatial_unit(inventory, classifiers,
                                         classifier_values)
Пример #2
0
    def test_undefined_single_default_spatial_unit_error(self):
        """Checks that an error is raised when the default mapping of spatial
        unit does not match a defined value in the defaults reference.
        """
        mapping = {
            "spatial_units": {
                "mapping_mode": "SingleDefaultSpatialUnit",
                "default_spuid": 10
            }
        }
        ref = Mock(spec=SITCBMDefaults)

        def mock_get_spatial_unit(spatial_unit_id):
            raise KeyError

        ref.get_spatial_unit.side_effect = mock_get_spatial_unit
        classifiers, classifier_values = self.get_mock_classifiers()
        inventory = pd.DataFrame({
            "classifier1": ["a", "b"],
            "classifier2": ["a", "a"],
        })
        sit_mapping = SITMapping(mapping, ref)
        with self.assertRaises(KeyError):
            sit_mapping.get_spatial_unit(inventory, classifiers,
                                         classifier_values)
        self.assertTrue(ref.get_spatial_unit.called)
Пример #3
0
    def test_single_default_spatial_unit_admin_eco_specified(self):
        """Checks that an error is raised when the default mapping of spatial
        unit does not match a defined value in the defaults reference.
        """
        mapping = {
            "spatial_units": {
                "mapping_mode": "SingleDefaultSpatialUnit",
                "admin_boundary": "a1",
                "eco_boundary": "e1"
            }
        }
        ref = Mock(spec=SITCBMDefaults)

        def mock_get_spatial_unit_id(admin_boundary, eco_boundary):
            self.assertTrue(admin_boundary == "a1")
            self.assertTrue(eco_boundary == "e1")
            return 1

        ref.get_spatial_unit_id.side_effect = mock_get_spatial_unit_id
        classifiers, classifier_values = self.get_mock_classifiers()
        inventory = pd.DataFrame({
            "classifier1": ["a", "b"],
            "classifier2": ["a", "a"],
        })
        sit_mapping = SITMapping(mapping, ref)
        result = sit_mapping.get_spatial_unit(inventory, classifiers,
                                              classifier_values)
        self.assertTrue((result == 1).all())
        self.assertTrue(len(result) == inventory.shape[0])
        ref.get_spatial_unit_id.assert_called_once()
Пример #4
0
    def test_undefined_mapped_default_spatial_unit_error(self):
        """Checks that an error is raised when the default mapping of spatial
        unit does not match a defined value in the defaults reference in
        spu classifier mode
        """
        mapping = {
            "spatial_units": {
                "mapping_mode":
                "JoinedAdminEcoClassifier",
                "spu_classifier":
                "classifier1",
                "spu_mapping": [{
                    "user_spatial_unit": "a",
                    "default_spatial_unit": {
                        "admin_boundary": "British Columbia",
                        "eco_boundary": "Montane Cordillera"
                    }
                }, {
                    "user_spatial_unit": "b",
                    "default_spatial_unit": {
                        "admin_boundary": "Alberta",
                        "eco_boundary": "Montane Cordillera"
                    }
                }]
            }
        }
        ref = Mock(spec=SITCBMDefaults)
        classifiers, classifier_values = self.get_mock_classifiers()
        inventory = pd.DataFrame({
            "classifier1": ["a", "b"],
            "classifier2": ["a", "a"],
        })

        def mock_get_spatial_unit_id(admin, eco):
            # simulates a key error raised when the specified value is not
            # present.
            raise KeyError

        ref.get_spatial_unit_id.side_effect = mock_get_spatial_unit_id
        sit_mapping = SITMapping(mapping, ref)
        with self.assertRaises(KeyError):
            sit_mapping.get_spatial_unit(inventory, classifiers,
                                         classifier_values)
        self.assertTrue(ref.get_spatial_unit_id.called)
Пример #5
0
    def test_invalid_spatial_unit_mapping_mode_error(self):
        """checks that a non-supported mapping mode results in error
        """
        mapping = {
            "spatial_units": {
                "mapping_mode": "UNSUPPORTED",
                "default_spuid": 10
            }
        }
        ref = Mock(spec=SITCBMDefaults)

        classifiers, classifier_values = self.get_mock_classifiers()
        inventory = pd.DataFrame({
            "classifier1": ["a", "b"],
            "classifier2": ["a", "a"],
        })
        sit_mapping = SITMapping(mapping, ref)
        with self.assertRaises(ValueError):
            sit_mapping.get_spatial_unit(inventory, classifiers,
                                         classifier_values)
Пример #6
0
    def test_undefined_classifier_joined_admin_eco_error(self):
        """checks that an error is raised when any mapping references a
        non-existant classifier name
        """
        mapping = {
            "spatial_units": {
                "mapping_mode":
                "JoinedAdminEcoClassifier",
                "spu_classifier":
                "NOT_DEFINED",
                "spu_mapping": [{
                    "user_spatial_unit": "a",
                    "default_spatial_unit": {
                        "admin_boundary": "British Columbia",
                        "eco_boundary": "Montane Cordillera"
                    }
                }, {
                    "user_spatial_unit": "b",
                    "default_spatial_unit": {
                        "admin_boundary": "Alberta",
                        "eco_boundary": "Montane Cordillera"
                    }
                }]
            }
        }
        ref = Mock(spec=SITCBMDefaults)
        classifiers, classifier_values = self.get_mock_classifiers()
        inventory = pd.DataFrame({
            "classifier1": ["a", "b"],
            "classifier2": ["a", "a"],
        })

        with self.assertRaises(KeyError):
            sit_mapping = SITMapping(mapping, ref)
            sit_mapping.get_spatial_unit(inventory, classifiers,
                                         classifier_values)
Пример #7
0
    def test_admin_eco_mapping_returns_expected_value(self):
        """Checks that an expected value is returned when admin-eco
        classifier mapping is used.
        """
        mapping = {
            "spatial_units": {
                "mapping_mode":
                "SeparateAdminEcoClassifiers",
                "admin_classifier":
                "classifier1",
                "eco_classifier":
                "classifier2",
                "admin_mapping": [{
                    "user_admin_boundary": "a",
                    "default_admin_boundary": "British Columbia"
                }, {
                    "user_admin_boundary": "b",
                    "default_admin_boundary": "Alberta"
                }],
                "eco_mapping": [{
                    "user_eco_boundary": "a",
                    "default_eco_boundary": "Montane Cordillera"
                }]
            }
        }
        ref = Mock(spec=SITCBMDefaults)
        classifiers, classifier_values = self.get_mock_classifiers()
        inventory = pd.DataFrame({
            "classifier1": ["a", "b"],
            "classifier2": ["a", "a"],
        })

        def mock_get_spatial_unit_id(admin, eco):
            if admin == "British Columbia" and eco == "Montane Cordillera":
                return 1000
            elif admin == "Alberta" and eco == "Montane Cordillera":
                return 2000
            else:
                raise ValueError

        ref.get_spatial_unit_id.side_effect = mock_get_spatial_unit_id
        sit_mapping = SITMapping(mapping, ref)
        result = sit_mapping.get_spatial_unit(inventory, classifiers,
                                              classifier_values)
        self.assertTrue(list(result) == [1000, 2000])