Пример #1
0
class ChloroplethMapTestCase(TestCase):
    def setUp(self):
        self.chloropleth_map = NewEnglandPlantDistributionMap()

    def test_get_title(self):
        self.assertEqual('New England Distribution Map',
                         self.chloropleth_map.get_title())

    def test_set_title(self):
        TITLE = 'Test'
        self.chloropleth_map.set_title(TITLE)
        self.assertEqual(TITLE, self.chloropleth_map.get_title())

    def test_tostring(self):
        self.assertEqual(u'<svg xmlns', self.chloropleth_map.tostring()[0:10])
Пример #2
0
class ChloroplethMapTestCase(TestCase):
    def setUp(self):
        self.chloropleth_map = NewEnglandPlantDistributionMap()

    def test_get_title(self):
        self.assertEqual('New England Distribution Map',
                         self.chloropleth_map.get_title())

    def test_set_title(self):
        TITLE = 'Test'
        self.chloropleth_map.set_title(TITLE)
        self.assertEqual(TITLE, self.chloropleth_map.get_title())

    def test_tostring(self):
        self.assertEqual(u'<svg xmlns', self.chloropleth_map.tostring()[0:10])
Пример #3
0
class NewEnglandPlantDistributionMapTestCase(TestCase):
    def setUp(self):
        self.distribution_map = NewEnglandPlantDistributionMap()
        create_distribution_records()

    def test_is_correct_map(self):
        self.assertEqual('New England Distribution Map',
                         self.distribution_map.get_title())

    def test_get_distribution_records(self):
        SCIENTIFIC_NAME = 'Dendrolycopodium dendroideum'
        self.distribution_map.set_plant(SCIENTIFIC_NAME)
        records = (self.distribution_map._get_distribution_records(
                   SCIENTIFIC_NAME))
        self.assertTrue(len(records) > 0)
Пример #4
0
class NewEnglandPlantDistributionMapTestCase(TestCase):
    def setUp(self):
        self.distribution_map = NewEnglandPlantDistributionMap()
        create_distribution_records()

    def test_is_correct_map(self):
        self.assertEqual('New England Distribution Map',
                         self.distribution_map.get_title())

    def test_get_distribution_records(self):
        NEW_ENGLAND_STATES = ['CT', 'MA', 'ME', 'NH', 'RI', 'VT']
        SCIENTIFIC_NAME = 'Dendrolycopodium dendroideum'
        self.distribution_map.set_plant(SCIENTIFIC_NAME)
        records = (self.distribution_map._get_distribution_records(
                   SCIENTIFIC_NAME))
        self.assertTrue(len(records) > 0)
        [self.assertTrue(record.state in NEW_ENGLAND_STATES)
         for record in records]
Пример #5
0
def new_england_distribution_map(request, genus, epithet):
    """Return a vector map of New England showing county-level
    distribution data for a plant.
    """
    distribution_map = NewEnglandPlantDistributionMap()
    return _distribution_map(request, distribution_map, genus, epithet)
Пример #6
0
 def setUp(self):
     self.chloropleth_map = NewEnglandPlantDistributionMap()
Пример #7
0
 def setUp(self):
     self.distribution_map = NewEnglandPlantDistributionMap()
     create_distribution_records()
Пример #8
0
class PlantDistributionMapTestCase(TestCase):
    def setUp(self):
        self.distribution_map = NewEnglandPlantDistributionMap()
        create_distribution_records()

    def test_map_init(self):
        self.assertTrue(self.distribution_map)

    def test_get_label_for_status_native(self):
        statuses = [
            'Species present in state and native',
            'Species present and not rare',
            'Species native, but adventive in state',
            ]
        for status in statuses:
            self.assertEqual('native',
                self.distribution_map._get_label_for_status(status))

    def test_get_label_for_status_rare(self):
        statuses = [
            'Species present and rare',
            ]
        for status in statuses:
            self.assertEqual('native',
                self.distribution_map._get_label_for_status(status))

    def test_get_label_for_status_introduced(self):
        statuses = [
            'Species present in state and exotic',
            'Species exotic and present',
            'Species waif',
            ]
        for status in statuses:
            self.assertEqual('non-native',
                self.distribution_map._get_label_for_status(status))

    def test_get_label_for_status_invasive(self):
        statuses = [
            'Species noxious',
            ]
        for status in statuses:
            self.assertEqual('non-native',
                self.distribution_map._get_label_for_status(status))

    def test_get_label_for_status_historic(self):
        statuses = [
            'Species extirpated (historic)',
            'Species extinct',
            ]
        for status in statuses:
            self.assertEqual('absent',
                self.distribution_map._get_label_for_status(status))

    def test_get_label_for_status_absent(self):
        statuses = [
            'Species not present in state',
            'Species eradicated',
            'Questionable Presence (cross-hatched)',
            ]
        for status in statuses:
            self.assertEqual('absent',
                self.distribution_map._get_label_for_status(status))

    def test_add_name_to_title(self):
        SCIENTIFIC_NAME = 'Tsuga canadensis'
        self.distribution_map._add_name_to_title(SCIENTIFIC_NAME)
        self.assertEqual('%s: New England Distribution Map' % SCIENTIFIC_NAME,
                         self.distribution_map.get_title())

    def test_get_distribution_records(self):
        SCIENTIFIC_NAME = 'Dendrolycopodium dendroideum'
        self.distribution_map.set_plant(SCIENTIFIC_NAME)
        records = (self.distribution_map._get_distribution_records(
                   SCIENTIFIC_NAME))
        self.assertTrue(len(records) > 0)

    def test_set_plant(self):
        SCIENTIFIC_NAME = 'Dendrolycopodium dendroideum'
        self.distribution_map.set_plant(SCIENTIFIC_NAME)
        self.assertEqual(SCIENTIFIC_NAME,
                         self.distribution_map.scientific_name)
        self.assertEqual('%s: New England Distribution Map' % SCIENTIFIC_NAME,
                         self.distribution_map.get_title())
        self.assertTrue(len(self.distribution_map.distribution_records) > 0)

    def _check_equal(self, list1, list2):
        return list1[1:] == list2[:-1]

    def _verify_shaded_counties(self, legend_labels_found):
        path_nodes = self.distribution_map.svg_map.findall(
            '{http://www.w3.org/2000/svg}path')
        paths = [Path(path_node) for path_node in path_nodes]
        statuses_verified = []
        for path in paths:
            style = path.get_style()
            status = None
            if style.find('fill:#78bf47') > -1:
                status = 'native'
            elif style.find('fill:#fff') > -1:
                status = 'absent'
            if status and status not in statuses_verified:
                statuses_verified.append(status)
            if statuses_verified == legend_labels_found:
                break
        self.assertEqual(statuses_verified.sort(), legend_labels_found.sort())

    def test_shade_counties(self):
        SCIENTIFIC_NAME = 'Dendrolycopodium dendroideum'
        self.distribution_map.set_plant(SCIENTIFIC_NAME)
        legend_labels_found = self.distribution_map._shade_areas()
        self._verify_shaded_counties(legend_labels_found)

    def test_shade(self):
        SCIENTIFIC_NAME = 'Vaccinium vitis-idaea ssp. minus'
        self.distribution_map.set_plant(SCIENTIFIC_NAME)
        self.distribution_map.shade()
        self._verify_shaded_counties(['native', 'absent'])
        labels = [label_node.text for label_node in
            self.distribution_map.legend.svg_map.xpath('svg:text/svg:tspan',
            namespaces=NAMESPACES)]
        self.assertEqual('native', labels[0])
        self.assertEqual('absent', labels[1])
        [self.assertEqual('', label) for label in labels[2:]]

    def test_plant_with_distribution_data_has_plant_name_in_title(self):
        SCIENTIFIC_NAME = 'Dendrolycopodium dendroideum'
        self.distribution_map.set_plant(SCIENTIFIC_NAME)
        self.distribution_map.shade()
        self.assertEqual('%s: New England Distribution Map' % SCIENTIFIC_NAME,
                         self.distribution_map.get_title())

    def test_plant_with_no_distribution_data_returns_blank_map(self):
        SCIENTIFIC_NAME = 'Foo bar'
        self.distribution_map.set_plant(SCIENTIFIC_NAME)
        self.distribution_map.shade()
        # Verify that the map is not shaded.
        path_nodes = self.distribution_map.svg_map.findall(
            '{http://www.w3.org/2000/svg}path')
        paths = [Path(path_node) for path_node in path_nodes]
        for path in paths:
            style = path.get_style()
            self.assertTrue(style.find('fill:#fff') > -1 or
                            style.find('fill:none') > -1)
        # Verify that the legend contains only a 'no data' label.
        labels = [label_node.text for label_node in
            self.distribution_map.legend.svg_map.xpath('svg:text/svg:tspan',
            namespaces=NAMESPACES)]
        self.assertEqual(['no data', '', '', '', ''], labels)

    def test_plant_with_no_distribution_data_has_no_plant_name_in_title(self):
        SCIENTIFIC_NAME = 'Foo bar'
        self.distribution_map.set_plant(SCIENTIFIC_NAME)
        self.distribution_map.shade()
        self.assertEqual('New England Distribution Map',
                         self.distribution_map.get_title())

    def test_plant_with_data_only_under_synonym_returns_shaded_map(self):
        # This plant's distribution data is listed only under the
        # synonym Vaccinium vitis-idaea ssp. minus.
        SCIENTIFIC_NAME = 'Vaccinium vitis-idaea'
        self.distribution_map.set_plant(SCIENTIFIC_NAME)
        self.distribution_map.shade()
        self._verify_shaded_counties(['present', 'absent'])
        labels = [label_node.text for label_node in
            self.distribution_map.legend.svg_map.xpath('svg:text/svg:tspan',
            namespaces=NAMESPACES)]
        self.assertEqual(['native', 'absent', '', '', ''], labels)
        self.assertEqual('%s: New England Distribution Map' % SCIENTIFIC_NAME,
                         self.distribution_map.get_title())
Пример #9
0
 def setUp(self):
     self.dist_map = NewEnglandPlantDistributionMap()
     self.legend = Legend(self.dist_map.svg_map,
                          maximum_categories=2,
                          maximum_items=5)
Пример #10
0
 def setUp(self):
     self.distribution_map = NewEnglandPlantDistributionMap()
     create_distribution_records()
Пример #11
0
class NewEnglandPlantDistributionMapTestCase(TestCase):
    def setUp(self):
        self.distribution_map = NewEnglandPlantDistributionMap()
        create_distribution_records()

    def test_is_correct_map(self):
        self.assertEqual('New England Distribution Map',
                         self.distribution_map.get_title())

    def test_get_distribution_records(self):
        SCIENTIFIC_NAME = 'Dendrolycopodium dendroideum'
        self.distribution_map.set_plant(SCIENTIFIC_NAME)
        records = (
            self.distribution_map._get_distribution_records(SCIENTIFIC_NAME))
        self.assertTrue(len(records) > 0)

    def _get_shaded_paths(self, distribution_map):
        path_nodes = distribution_map.svg_map.xpath('svg:path',
                                                    namespaces=NAMESPACES)
        paths = [Path(path_node) for path_node in path_nodes]
        shaded_paths = []
        for path in paths:
            style = path.get_style()
            if style.find('fill:#') > -1 and style.find('fill:#fff') == -1:
                shaded_paths.append(path)
        return shaded_paths

    def _verify_number_expected_shaded_areas(self, expected_shaded_areas,
                                             shaded_paths):
        # Verify that the number of expected shaded areas is found, at a
        # minimum, among the full list of shaded paths.
        # There can be more shaded paths than expected shaded areas.
        # Example: BC has two paths, one for the mainland and one for
        # Vancouver Island.
        #
        # To debug a failing test, uncomment the following line and run
        # the failing test alone:
        #print 'shaded_paths: %d  expected_shaded_areas: %d' % (
        #    len(shaded_paths), len(expected_shaded_areas))
        self.assertTrue(len(shaded_paths) >= len(expected_shaded_areas))

    def _verify_expected_shaded_areas(self, expected_shaded_areas,
                                      shaded_paths):
        # Check that each shaded area and its color is expected.
        for path in shaded_paths:
            path_id = path.path_node.get('id')
            area_key = path_id[0:2]
            self.assertTrue(area_key in expected_shaded_areas.keys())
            label = expected_shaded_areas[area_key]
            # To debug a failing test, uncomment the following line and
            # run the failing test alone:
            #print 'area_key: %s  label: %s' % (area_key, label)
            fill_declaration = 'fill:%s' % Legend.COLORS[label]
            self.assertTrue(path.get_style().find(fill_declaration) > -1)

    def test_plants_with_similar_names_do_not_conflate_records_1of2(self):
        # Bug fix from Issue #595: ensure that two plants whose names
        # only differ at the very end do not mistakenly get combined.
        # First plant:
        SCIENTIFIC_NAME = 'Carex arcta'
        EXPECTED_SHADED_AREAS = {
            'ME': 'county documented na',  # From test data, expect only ME
        }
        self.distribution_map.set_plant(SCIENTIFIC_NAME)
        records = self.distribution_map._get_distribution_records(
            SCIENTIFIC_NAME)
        self.assertTrue(len(records) == 1)
        self.distribution_map.shade()
        shaded_paths = self._get_shaded_paths(self.distribution_map)
        self._verify_number_expected_shaded_areas(EXPECTED_SHADED_AREAS,
                                                  shaded_paths)
        self._verify_expected_shaded_areas(EXPECTED_SHADED_AREAS, shaded_paths)

    def test_plants_with_similar_names_do_not_conflate_records_2of2(self):
        # Bug fix from Issue #595: ensure that two plants whose names
        # only differ at the very end do not mistakenly get combined.
        # Second plant:
        SCIENTIFIC_NAME = 'Carex arctata'
        EXPECTED_SHADED_AREAS = {
            'MA': 'county documented na',  # From test data, expect only MA
        }
        self.distribution_map.set_plant(SCIENTIFIC_NAME)
        records = self.distribution_map._get_distribution_records(
            SCIENTIFIC_NAME)
        self.assertTrue(len(records) == 1)
        self.distribution_map.shade()
        shaded_paths = self._get_shaded_paths(self.distribution_map)
        self._verify_number_expected_shaded_areas(EXPECTED_SHADED_AREAS,
                                                  shaded_paths)
        self._verify_expected_shaded_areas(EXPECTED_SHADED_AREAS, shaded_paths)
Пример #12
0
class PlantDistributionMapTestCase(TestCase):
    def setUp(self):
        self.distribution_map = NewEnglandPlantDistributionMap()
        create_distribution_records()

    def test_map_init(self):
        self.assertTrue(self.distribution_map)

    def test_get_label_absent(self):
        self.assertEqual('absent',
                         self.distribution_map._get_label(False, False))

    def test_get_label_native(self):
        self.assertEqual('native',
                         self.distribution_map._get_label(True, True))

    def test_get_label_non_native(self):
        self.assertEqual('non-native',
                         self.distribution_map._get_label(True, False))

    def test_add_name_to_title(self):
        SCIENTIFIC_NAME = 'Tsuga canadensis'
        self.distribution_map._add_name_to_title(SCIENTIFIC_NAME)
        self.assertEqual('%s: New England Distribution Map' % SCIENTIFIC_NAME,
                         self.distribution_map.get_title())

    def test_get_distribution_records(self):
        SCIENTIFIC_NAME = 'Dendrolycopodium dendroideum'
        self.distribution_map.set_plant(SCIENTIFIC_NAME)
        records = (
            self.distribution_map._get_distribution_records(SCIENTIFIC_NAME))
        self.assertTrue(len(records) > 0)

    def test_set_plant(self):
        SCIENTIFIC_NAME = 'Dendrolycopodium dendroideum'
        self.distribution_map.set_plant(SCIENTIFIC_NAME)
        self.assertEqual(SCIENTIFIC_NAME,
                         self.distribution_map.scientific_name)
        self.assertEqual('%s: New England Distribution Map' % SCIENTIFIC_NAME,
                         self.distribution_map.get_title())
        self.assertTrue(len(self.distribution_map.distribution_records) > 0)

    def _check_equal(self, list1, list2):
        return list1[1:] == list2[:-1]

    def _verify_shaded_counties(self, legend_labels_found):
        path_nodes = self.distribution_map.svg_map.findall(
            '{http://www.w3.org/2000/svg}path')
        paths = [Path(path_node) for path_node in path_nodes]
        statuses_verified = []
        for path in paths:
            style = path.get_style()
            status = None
            if style.find('fill:#78bf47') > -1:
                status = 'native'
            elif style.find('fill:#fff') > -1:
                status = 'absent'
            if status and status not in statuses_verified:
                statuses_verified.append(status)
            if statuses_verified == legend_labels_found:
                break
        self.assertEqual(statuses_verified.sort(), legend_labels_found.sort())

    def test_shade_counties(self):
        SCIENTIFIC_NAME = 'Dendrolycopodium dendroideum'
        self.distribution_map.set_plant(SCIENTIFIC_NAME)
        legend_labels_found = self.distribution_map._shade_areas()
        self._verify_shaded_counties(legend_labels_found)

    def test_shade(self):
        SCIENTIFIC_NAME = 'Vaccinium vitis-idaea ssp. minus'
        self.distribution_map.set_plant(SCIENTIFIC_NAME)
        self.distribution_map.shade()
        self._verify_shaded_counties(['county documented', 'absent'])
        labels = get_legend_labels(self.distribution_map.legend)
        self.assertEqual('county documented', labels[0])
        self.assertEqual('state documented', labels[1])

    def test_plant_with_distribution_data_has_plant_name_in_title(self):
        SCIENTIFIC_NAME = 'Dendrolycopodium dendroideum'
        self.distribution_map.set_plant(SCIENTIFIC_NAME)
        self.distribution_map.shade()
        self.assertEqual('%s: New England Distribution Map' % SCIENTIFIC_NAME,
                         self.distribution_map.get_title())

    def test_plant_with_no_distribution_data_returns_blank_map(self):
        SCIENTIFIC_NAME = 'Foo bar'
        self.distribution_map.set_plant(SCIENTIFIC_NAME)
        self.distribution_map.shade()
        # Verify that the map is not shaded.
        path_nodes = self.distribution_map.svg_map.findall(
            '{http://www.w3.org/2000/svg}path')
        paths = [Path(path_node) for path_node in path_nodes]
        for path in paths:
            style = path.get_style()
            self.assertTrue(
                style.find('fill:#fff') > -1 or style.find('fill:none') > -1)
        # Verify that the legend contains only a 'no data' label.
        labels = get_legend_labels(self.distribution_map.legend)
        self.assertEqual(['no data', '', '', '', '', ''], labels)

    def test_plant_with_no_distribution_data_has_no_plant_name_in_title(self):
        SCIENTIFIC_NAME = 'Foo bar'
        self.distribution_map.set_plant(SCIENTIFIC_NAME)
        self.distribution_map.shade()
        self.assertEqual('New England Distribution Map',
                         self.distribution_map.get_title())

    def test_plant_with_data_only_under_synonym_returns_shaded_map(self):
        # This plant's distribution data is listed only under the
        # synonym Vaccinium vitis-idaea ssp. minus.
        SCIENTIFIC_NAME = 'Vaccinium vitis-idaea'
        self.distribution_map.set_plant(SCIENTIFIC_NAME)
        self.distribution_map.shade()
        self._verify_shaded_counties(['county documented', 'absent'])
        labels = get_legend_labels(self.distribution_map.legend)
        self.assertEqual(
            ['county documented', 'state documented', '', '', 'Native', ''],
            labels)
        self.assertEqual('%s: New England Distribution Map' % SCIENTIFIC_NAME,
                         self.distribution_map.get_title())

    def test_legend_correct_with_conflicting_state_and_county_records(self):
        # Ensure that if all of a plant's county-level records override
        # its state-level record, that the map legend lists only those
        # items that are visible on the final map.
        SCIENTIFIC_NAME = 'Sambucus nigra'
        self.distribution_map.set_plant(SCIENTIFIC_NAME)
        self.distribution_map.shade()
        labels = get_legend_labels(self.distribution_map.legend)
        legend_shows_non_native = ('non-native' in labels)
        self.assertFalse(legend_shows_non_native)

    def test_species_and_infraspecific_taxa_shaded_together(self):
        # Ensure that the distribution records for a species and any
        # associated infraspecific taxa are shaded together on the map,
        # with native overriding non-native.
        SCIENTIFIC_NAME = 'Sambucus nigra'
        self.distribution_map.set_plant(SCIENTIFIC_NAME)
        self.distribution_map.shade()
        labels = get_legend_labels(self.distribution_map.legend)
        legend_shows_native = ('native, st.' or 'native, co.' in labels)
        self.assertTrue(legend_shows_native)
Пример #13
0
 def setUp(self):
     dist_map = NewEnglandPlantDistributionMap()
     path_nodes = dist_map.svg_map.findall(
         '{http://www.w3.org/2000/svg}path')
     self.path = Path(path_nodes[0])
Пример #14
0
 def setUp(self):
     self.chloropleth_map = NewEnglandPlantDistributionMap()
Пример #15
0
class NewEnglandPlantDistributionMapTestCase(TestCase):
    def setUp(self):
        self.distribution_map = NewEnglandPlantDistributionMap()
        create_distribution_records()

    def test_is_correct_map(self):
        self.assertEqual('New England Distribution Map',
                         self.distribution_map.get_title())

    def test_get_distribution_records(self):
        SCIENTIFIC_NAME = 'Dendrolycopodium dendroideum'
        self.distribution_map.set_plant(SCIENTIFIC_NAME)
        records = (self.distribution_map._get_distribution_records(
                   SCIENTIFIC_NAME))
        self.assertTrue(len(records) > 0)

    def _get_shaded_paths(self, distribution_map):
        path_nodes = distribution_map.svg_map.xpath(
            'svg:path', namespaces=NAMESPACES)
        paths = [Path(path_node) for path_node in path_nodes]
        shaded_paths = []
        for path in paths:
            style = path.get_style()
            if style.find('fill:#') > -1 and style.find('fill:#fff') == -1:
                shaded_paths.append(path)
        return shaded_paths

    def _verify_number_expected_shaded_areas(self, expected_shaded_areas,
            shaded_paths):
        # Verify that the number of expected shaded areas is found, at a
        # minimum, among the full list of shaded paths.
        # There can be more shaded paths than expected shaded areas.
        # Example: BC has two paths, one for the mainland and one for
        # Vancouver Island.
        #
        # To debug a failing test, uncomment the following line and run
        # the failing test alone:
        #print 'shaded_paths: %d  expected_shaded_areas: %d' % (
        #    len(shaded_paths), len(expected_shaded_areas))
        self.assertTrue(len(shaded_paths) >= len(expected_shaded_areas))

    def _verify_expected_shaded_areas(self, expected_shaded_areas,
            shaded_paths):
        # Check that each shaded area and its color is expected.
        for path in shaded_paths:
            path_id = path.path_node.get('id')
            area_key = path_id[0:2]
            self.assertTrue(area_key in expected_shaded_areas.keys())
            label = expected_shaded_areas[area_key]
            # To debug a failing test, uncomment the following line and
            # run the failing test alone:
            #print 'area_key: %s  label: %s' % (area_key, label)
            fill_declaration = 'fill:%s' % Legend.COLORS[label]
            self.assertTrue(path.get_style().find(fill_declaration) > -1)

    def test_plants_with_similar_names_do_not_conflate_records_1of2(self):
        # Bug fix from Issue #595: ensure that two plants whose names
        # only differ at the very end do not mistakenly get combined.
        # First plant:
        SCIENTIFIC_NAME = 'Carex arcta'
        EXPECTED_SHADED_AREAS = {
            'ME': 'county documented na',   # From test data, expect only ME
        }
        self.distribution_map.set_plant(SCIENTIFIC_NAME)
        records = self.distribution_map._get_distribution_records(
            SCIENTIFIC_NAME)
        self.assertTrue(len(records) == 1)
        self.distribution_map.shade()
        shaded_paths = self._get_shaded_paths(self.distribution_map)
        self._verify_number_expected_shaded_areas(EXPECTED_SHADED_AREAS,
            shaded_paths)
        self._verify_expected_shaded_areas(EXPECTED_SHADED_AREAS,
            shaded_paths)

    def test_plants_with_similar_names_do_not_conflate_records_2of2(self):
        # Bug fix from Issue #595: ensure that two plants whose names
        # only differ at the very end do not mistakenly get combined.
        # Second plant:
        SCIENTIFIC_NAME = 'Carex arctata'
        EXPECTED_SHADED_AREAS = {
            'MA': 'county documented na',   # From test data, expect only MA
        }
        self.distribution_map.set_plant(SCIENTIFIC_NAME)
        records = self.distribution_map._get_distribution_records(
            SCIENTIFIC_NAME)
        self.assertTrue(len(records) == 1)
        self.distribution_map.shade()
        shaded_paths = self._get_shaded_paths(self.distribution_map)
        self._verify_number_expected_shaded_areas(EXPECTED_SHADED_AREAS,
            shaded_paths)
        self._verify_expected_shaded_areas(EXPECTED_SHADED_AREAS,
            shaded_paths)
Пример #16
0
class PlantDistributionMapTestCase(TestCase):
    def setUp(self):
        self.distribution_map = NewEnglandPlantDistributionMap()
        create_distribution_records()

    def test_map_init(self):
        self.assertTrue(self.distribution_map)

    def test_get_label_absent(self):
        self.assertEqual('absent',
            self.distribution_map._get_label(False, False))

    def test_get_label_native(self):
        self.assertEqual('native',
            self.distribution_map._get_label(True, True))

    def test_get_label_non_native(self):
        self.assertEqual('non-native',
            self.distribution_map._get_label(True, False))

    def test_add_name_to_title(self):
        SCIENTIFIC_NAME = 'Tsuga canadensis'
        self.distribution_map._add_name_to_title(SCIENTIFIC_NAME)
        self.assertEqual('%s: New England Distribution Map' % SCIENTIFIC_NAME,
                         self.distribution_map.get_title())

    def test_get_distribution_records(self):
        SCIENTIFIC_NAME = 'Dendrolycopodium dendroideum'
        self.distribution_map.set_plant(SCIENTIFIC_NAME)
        records = (self.distribution_map._get_distribution_records(
                   SCIENTIFIC_NAME))
        self.assertTrue(len(records) > 0)

    def test_set_plant(self):
        SCIENTIFIC_NAME = 'Dendrolycopodium dendroideum'
        self.distribution_map.set_plant(SCIENTIFIC_NAME)
        self.assertEqual(SCIENTIFIC_NAME,
                         self.distribution_map.scientific_name)
        self.assertEqual('%s: New England Distribution Map' % SCIENTIFIC_NAME,
                         self.distribution_map.get_title())
        self.assertTrue(len(self.distribution_map.distribution_records) > 0)

    def _check_equal(self, list1, list2):
        return list1[1:] == list2[:-1]

    def _verify_shaded_counties(self, legend_labels_found):
        path_nodes = self.distribution_map.svg_map.findall(
            '{http://www.w3.org/2000/svg}path')
        paths = [Path(path_node) for path_node in path_nodes]
        statuses_verified = []
        for path in paths:
            style = path.get_style()
            status = None
            if style.find('fill:#78bf47') > -1:
                status = 'native'
            elif style.find('fill:#fff') > -1:
                status = 'absent'
            if status and status not in statuses_verified:
                statuses_verified.append(status)
            if statuses_verified == legend_labels_found:
                break
        self.assertEqual(statuses_verified.sort(), legend_labels_found.sort())

    def test_shade_counties(self):
        SCIENTIFIC_NAME = 'Dendrolycopodium dendroideum'
        self.distribution_map.set_plant(SCIENTIFIC_NAME)
        legend_labels_found = self.distribution_map._shade_areas()
        self._verify_shaded_counties(legend_labels_found)

    def test_shade(self):
        SCIENTIFIC_NAME = 'Vaccinium vitis-idaea ssp. minus'
        self.distribution_map.set_plant(SCIENTIFIC_NAME)
        self.distribution_map.shade()
        self._verify_shaded_counties(['county documented', 'absent'])
        labels = get_legend_labels(self.distribution_map.legend)
        self.assertEqual('county documented', labels[0])
        self.assertEqual('state documented', labels[1])

    def test_plant_with_distribution_data_has_plant_name_in_title(self):
        SCIENTIFIC_NAME = 'Dendrolycopodium dendroideum'
        self.distribution_map.set_plant(SCIENTIFIC_NAME)
        self.distribution_map.shade()
        self.assertEqual('%s: New England Distribution Map' % SCIENTIFIC_NAME,
                         self.distribution_map.get_title())

    def test_plant_with_no_distribution_data_returns_blank_map(self):
        SCIENTIFIC_NAME = 'Foo bar'
        self.distribution_map.set_plant(SCIENTIFIC_NAME)
        self.distribution_map.shade()
        # Verify that the map is not shaded.
        path_nodes = self.distribution_map.svg_map.findall(
            '{http://www.w3.org/2000/svg}path')
        paths = [Path(path_node) for path_node in path_nodes]
        for path in paths:
            style = path.get_style()
            self.assertTrue(style.find('fill:#fff') > -1 or
                            style.find('fill:none') > -1)
        # Verify that the legend contains only a 'no data' label.
        labels = get_legend_labels(self.distribution_map.legend)
        self.assertEqual(['no data', '', '', '', '', ''], labels)

    def test_plant_with_no_distribution_data_has_no_plant_name_in_title(self):
        SCIENTIFIC_NAME = 'Foo bar'
        self.distribution_map.set_plant(SCIENTIFIC_NAME)
        self.distribution_map.shade()
        self.assertEqual('New England Distribution Map',
                         self.distribution_map.get_title())

    def test_plant_with_data_only_under_synonym_returns_shaded_map(self):
        # This plant's distribution data is listed only under the
        # synonym Vaccinium vitis-idaea ssp. minus.
        SCIENTIFIC_NAME = 'Vaccinium vitis-idaea'
        self.distribution_map.set_plant(SCIENTIFIC_NAME)
        self.distribution_map.shade()
        self._verify_shaded_counties(['county documented', 'absent'])
        labels = get_legend_labels(self.distribution_map.legend)
        self.assertEqual(['county documented', 'state documented', '', '',
            'Native', ''], labels)
        self.assertEqual('%s: New England Distribution Map' % SCIENTIFIC_NAME,
                         self.distribution_map.get_title())

    def test_legend_correct_with_conflicting_state_and_county_records(self):
        # Ensure that if all of a plant's county-level records override
        # its state-level record, that the map legend lists only those
        # items that are visible on the final map.
        SCIENTIFIC_NAME = 'Sambucus nigra'
        self.distribution_map.set_plant(SCIENTIFIC_NAME)
        self.distribution_map.shade()
        labels = get_legend_labels(self.distribution_map.legend)
        legend_shows_non_native = ('non-native' in labels)
        self.assertFalse(legend_shows_non_native)

    def test_species_and_infraspecific_taxa_shaded_together(self):
        # Ensure that the distribution records for a species and any
        # associated infraspecific taxa are shaded together on the map,
        # with native overriding non-native.
        SCIENTIFIC_NAME = 'Sambucus nigra'
        self.distribution_map.set_plant(SCIENTIFIC_NAME)
        self.distribution_map.shade()
        labels = get_legend_labels(self.distribution_map.legend)
        legend_shows_native = ('native, st.' or 'native, co.' in labels)
        self.assertTrue(legend_shows_native)