class LoadSpeciesTests(unittest.TestCase): """Tests for the function convert_species().""" def setUp(self): super(LoadSpeciesTests, self).setUp() self.obj = PortugalAvesHandler() location_file = pkg_resources.resource_filename("ebimport", "data/portugalaves/species.csv") self.obj.load_species(self.obj.species, location_file) def test_convert_known_species(self): """Species is updated with table values.""" record = {"BirdLife common name": "Manx Shearwater"} result = self.obj.convert_species(record) self.assertTrue(result["Species Converted"]) def test_rename_species(self): """Species are renamed where appropriate.""" record = {"BirdLife common name": "Cinereous Vulture"} result = self.obj.convert_species(record) self.assertEquals(result["Common Name"], "Black Vulture") def test_convert_unknown_species(self): """Species not in the table are unchanged.""" record = {"BirdLife common name": "Dodo"} result = self.obj.convert_species(record) self.assertEquals(result["Common Name"], "Dodo") self.assertFalse(result["Species Converted"])
def test_load_locations(self): """Locations file can be loaded from package resources.""" obj = PortugalAvesHandler() location_file = pkg_resources.resource_filename( 'ebimport', 'data/portugalaves/locations.csv') obj.load_locations(obj.locations, location_file) self.assertTrue(obj.locations)
def setUp(self): super(ConvertLocationTestCase, self).setUp() self.obj = PortugalAvesHandler() self.obj.locations = { ('location a', '1.1', '1.1'): { 'eBird Location': 'Location A', 'eBird Region Code': 'Region A', 'eBird Country Code': 'CC', 'eBird Latitude': '1.0000', 'eBird Longitude': '1.0000', }, ('location b', '2.0001', '2.0001'): { 'eBird Location': 'Location B', 'eBird Region Code': 'Region B', 'eBird Country Code': 'CC', 'eBird Latitude': '2.0000', 'eBird Longitude': '2.0000', } }
def setUp(self): super(LoadSpeciesTests, self).setUp() self.obj = PortugalAvesHandler() location_file = pkg_resources.resource_filename("ebimport", "data/portugalaves/species.csv") self.obj.load_species(self.obj.species, location_file)
class ConvertLocationTestCase(unittest.TestCase): """Test for the convert_location() function.""" def setUp(self): super(ConvertLocationTestCase, self).setUp() self.obj = PortugalAvesHandler() self.obj.locations = { ('location a', '1.1', '1.1'): { 'eBird Location': 'Location A', 'eBird Region Code': 'Region A', 'eBird Country Code': 'CC', 'eBird Latitude': '1.0000', 'eBird Longitude': '1.0000', }, ('location b', '2.0001', '2.0001'): { 'eBird Location': 'Location B', 'eBird Region Code': 'Region B', 'eBird Country Code': 'CC', 'eBird Latitude': '2.0000', 'eBird Longitude': '2.0000', } } def test_convert_location_name(self): """The location name is updated""" rin = { 'Location': 'location a', 'Region/State': 'region a', 'Latitude': '1.1', 'Longitude': '1.1', } actual = self.obj.convert_location(rin) self.assertEqual(actual['Location Name'], 'Location A') def test_convert_region(self): """The region is updated""" rin = { 'Location': 'location a', 'Region/State': 'region a', 'Latitude': '1.1', 'Longitude': '1.1', } actual = self.obj.convert_location(rin) self.assertEqual(actual['State/Province'], 'Region A') def test_convert_country(self): """The region is updated""" rin = { 'Location': 'location a', 'Region/State': 'region a', 'Latitude': '1.1', 'Longitude': '1.1', } actual = self.obj.convert_location(rin) self.assertEqual(actual['Country Code'], 'CC') def test_convert_latitude(self): """The latitude is updated""" rin = { 'Location': 'location a', 'Region/State': 'region a', 'Latitude': '1.1', 'Longitude': '1.1', } actual = self.obj.convert_location(rin) self.assertEqual(actual['Latitude'], '1.0000') def test_convert_longitude(self): """The longitude is updated""" rin = { 'Location': 'location a', 'Region/State': 'region a', 'Latitude': '1.1', 'Longitude': '1.1', } actual = self.obj.convert_location(rin) self.assertEqual(actual['Longitude'], '1.0000') def test_result_is_marked_as_converted(self): """Location Converted is True for converted locations""" rin = { 'Location': 'location a', 'Region/State': 'region a', 'Latitude': '1.1', 'Longitude': '1.1', } actual = self.obj.convert_location(rin) self.assertTrue(actual['Location Converted']) def test_unknownlocation_is_not_marked_as_converted(self): """Location Converted is False for unknown locations""" rin = { 'Location': 'location z', 'Region/State': 'region z', 'Latitude': '1.1', 'Longitude': '1.1', } actual = self.obj.convert_location(rin) self.assertFalse(actual['Location Converted'])
class ReadHeaderTests(unittest.TestCase): """Tests for the function read_header().""" def setUp(self): super(ReadHeaderTests, self).setUp() self.obj = PortugalAvesHandler() def test_load_file_with_bom(self): """Find headers when file contents has a UTF-16 byte order mark.""" contents = StringIO(u"""\xfe\xffThese data have been extracted. Search conditions used Purpose Species Region/State Location Start date End date Total no. records 1 Data Family name Family sequence Species sequence Scientific name 1 Procellariidae 18 6900 Puffinus mauretanicus """) result = self.obj.read_header(contents) self.assertTrue(result) def test_load_file_without_bom(self): """Find headers when UTF-16 byte order mark is missing.""" contents = StringIO(u"""These data have been extracted. Search conditions used Purpose Species Region/State Location Start date End date Total no. records 1 Data Family name Family sequence Species sequence Scientific name 1 Procellariidae 18 6900 Puffinus mauretanicus """) result = self.obj.read_header(contents) self.assertTrue(result) def test_load_file_with_dos_line_endings(self): """Find headers when UTF-16 byte order mark is missing.""" contents = StringIO(u"""These data have been extracted.\r \r Search conditions used\r Purpose Species Region/State Location Start date End date\r \r \r Total no. records 1\r \r Data\r Family name Family sequence Species sequence Scientific name\r 1 Procellariidae 18 6900 Puffinus mauretanicus\r """) result = self.obj.read_header(contents) self.assertTrue(result) def test_load_file_without_data_section(self): """An exception is raised when data section is missing.""" contents = StringIO(u"""These data have been extracted. Search conditions used Purpose Species Region/State Location Start date End date Total no. records 1 """) self.assertRaises(ValueError, self.obj.read_header, contents) def test_load_file_with_empty_data_section(self): """An exception is raised when data section is empty.""" contents = StringIO(u"""These data have been extracted. Search conditions used Purpose Species Region/State Location Start date End date Total no. records 1 Data """) self.assertRaises(ValueError, self.obj.read_header, contents) def test_load_file_with_truncated_data_section(self): """Function completes when records are missing.""" contents = StringIO(u"""These data have been extracted. Search conditions used Purpose Species Region/State Location Start date End date Total no. records 1 Data Family name Family sequence Species sequence Scientific name """) result = self.obj.read_header(contents) self.assertTrue(result) def test_load_file_with_missing_headers(self): """An exception is raised when headers are missing.""" contents = StringIO(u"""These data have been extracted. Search conditions used Purpose Species Region/State Location Start date End date Total no. records 1 Data 1 Procellariidae 18 6900 Puffinus mauretanicus """) self.assertRaises(ValueError, self.obj.read_header, contents) def test_data_with_whitespace(self): """Find headers when data section has extra whitespace""" contents = StringIO(u"""These data have been extracted. Search conditions used Purpose Species Region/State Location Start date End date Total no. records 1 Data Family name Family sequence Species sequence Scientific name 1 Procellariidae 18 6900 Puffinus mauretanicus """) result = self.obj.read_header(contents) self.assertTrue(result) def test_data_lowercase(self): """Find headers when data section is lower case""" contents = StringIO(u"""These data have been extracted. Search conditions used Purpose Species Region/State Location Start date End date Total no. records 1 data Family name Family sequence Species sequence Scientific name 1 Procellariidae 18 6900 Puffinus mauretanicus """) result = self.obj.read_header(contents) self.assertTrue(result)
def setUp(self): super(ReadHeaderTests, self).setUp() self.obj = PortugalAvesHandler()