def test_duplicate_user_species_error(self): """checks that an error is raised when a classifier description is not present in the user value of species mapping """ config = { "species": { "species_classifier": "classifier1", "species_mapping": [ # note "a" is specified more than one time here { "user_species": "a", "default_species": "Spruce" }, { "user_species": "a", "default_species": "Oak" } ] } } ref = Mock(spec=SITCBMDefaults) classifiers, classifier_values = self.get_mock_classifiers() ref.get_afforestation_pre_types = lambda: [] species = pd.Series(["a", "a"]) with self.assertRaises(ValueError): sit_mapping = SITMapping(config, ref) sit_mapping.get_species(species, classifiers, classifier_values)
def test_get_species_error_on_undefined_classifier(self): """checks that an error is thrown if an undefined species classifier is used """ config = { "species": { "species_classifier": "undefined", "species_mapping": [ { "user_species": "a", "default_species": "Spruce" }, { "user_species": "b", "default_species": "Oak" }, { "user_species": "nonforest", "default_species": "Gleysolic" }, ] } } ref = Mock(spec=SITCBMDefaults) classifiers, classifier_values = self.get_mock_classifiers() pd.DataFrame({ "classifier1": ["a", "b"], "classifier2": ["a", "a"], }) species = pd.Series(["a", "b", "a", "b"]) ref.get_afforestation_pre_types.side_effect = lambda: [{ "afforestation_pre_type_name": "Gleysolic" }] sit_mapping = SITMapping(config, ref) with self.assertRaises(ValueError): sit_mapping.get_species(species, classifiers, classifier_values)
def test_get_species_expected_result(self): """checks the expected output of get_species """ config = { "species": { "species_classifier": "classifier1", "species_mapping": [ { "user_species": "a", "default_species": "Spruce" }, { "user_species": "b", "default_species": "Oak" }, { "user_species": "nonforest", "default_species": "Gleysolic" }, ] } } ref = Mock(spec=SITCBMDefaults) classifiers = pd.DataFrame(data=[(1, "classifier1"), (2, "classifier2")], columns=["id", "name"]) classifier_values = pd.DataFrame( data=[(1, "a", "a"), (1, "b", "b"), (1, "nonforest", "nonforest"), (2, "a", "a")], columns=["classifier_id", "name", "description"]) def mock_get_species_id(species_name): if species_name == "Spruce": return 999 if species_name == "Oak": return -999 raise ValueError() species = pd.Series(["a", "b", "a", "b"]) ref.get_species_id.side_effect = mock_get_species_id ref.get_afforestation_pre_types.side_effect = lambda: [{ "afforestation_pre_type_name": "Gleysolic" }] sit_mapping = SITMapping(config, ref) result = sit_mapping.get_species(species, classifiers, classifier_values) self.assertTrue(list(result) == [999, -999, 999, -999])
def test_undefined_user_species_error(self): """checks that an error is raised when a classifier description is not present in the user value of species mapping """ config = { "species": { "species_classifier": "classifier1", "species_mapping": [{ "user_species": "UNDEFINED", "default_species": "Spruce" }, { "user_species": "b", "default_species": "Oak" }] } } ref = Mock(spec=SITCBMDefaults) classifiers, classifier_values = self.get_mock_classifiers() ref.get_afforestation_pre_types = lambda: [] species = pd.Series(["b", "b"]) with self.assertRaises(KeyError): sit_mapping = SITMapping(config, ref) sit_mapping.get_species(species, classifiers, classifier_values)
def test_undefined_default_species_error(self): """Checks that an error is raised when the default mapping of species does not match a defined value in the defaults reference. """ config = { "species": { "species_classifier": "classifier1", "species_mapping": [{ "user_species": "a", "default_species": "Spruce" }, { "user_species": "b", "default_species": "Oak" }] } } ref = Mock(spec=SITCBMDefaults) classifiers, classifier_values = self.get_mock_classifiers() pd.DataFrame({ "classifier1": ["a", "b"], "classifier2": ["a", "a"], }) def mock_get_species_id(species_name): # simulates a key error raised when the specified value is not # present. raise KeyError() species = pd.Series(["a", "b"]) ref.get_species_id.side_effect = mock_get_species_id ref.get_afforestation_pre_types.side_effect = lambda: [] with self.assertRaises(KeyError): sit_mapping = SITMapping(config, ref) sit_mapping.get_species(species, classifiers, classifier_values) self.assertTrue(ref.get_species_id.called)