def setUp(self):
     """Fixture run before all tests."""
     self.building_mixin_blank = BuildingExposureReportMixin()
     self.building_mixin = BuildingExposureReportMixin()
     self.building_mixin.question = ''
     self.building_mixin.buildings = {
         'School': 100,
         'University': 10,
         'Residential': 20000,
         'Religious': 3
     }
     self.building_mixin.affected_buildings = OrderedDict([
         ('Hazard Level 2', {
             'School':
             OrderedDict([('Affected', 50), ('Value', 90000000)]),
             'University':
             OrderedDict([('Affected', 0), ('Value', 0)]),
             'Residential':
             OrderedDict([('Affected', 12000), ('Value', 1234567000)]),
             'Religious':
             OrderedDict([('Affected', 0), ('Value', 0)])
         }),
         ('Hazard Level 1', {
             'School':
             OrderedDict([('Affected', 25), ('Value', 50000000)]),
             'University':
             OrderedDict([('Affected', 1), ('Value', 11111111111)]),
             'Residential':
             OrderedDict([('Affected', 1000), ('Value', 123456000)]),
             'Religious':
             OrderedDict([('Affected', 1), ('Value', 10000000000)])
         })
     ])
Example #2
0
 def __init__(self):
     super(VolcanoPointBuildingFunction, self).__init__()
     BuildingExposureReportMixin.__init__(self)
     # A set of volcano names
     self.volcano_names = set()
     self._affected_categories_volcano = []
     self.hazard_zone_attribute = 'radius'
Example #3
0
    def __init__(self):
        super(ClassifiedPolygonHazardBuildingFunction, self).__init__()
        BuildingExposureReportMixin.__init__(self)

        # Hazard zones are all unique values from the hazard zone attribute
        self.hazard_zones = []
        # Set the question of the IF (as the hazard data is not an event)
        self.question = tr(
            'In each of the hazard zones how many buildings might be '
            'affected?')
Example #4
0
    def __init__(self):
        super(ClassifiedPolygonHazardBuildingFunction, self).__init__()
        BuildingExposureReportMixin.__init__(self)

        # Hazard zones are all unique values from the hazard zone attribute
        self.hazard_zones = []
        # Set the question of the IF (as the hazard data is not an event)
        self.question = tr(
            'In each of the hazard zones how many buildings might be '
            'affected?')
Example #5
0
 def __init__(self):
     """Constructor (calls ctor of base class)."""
     super(TsunamiRasterBuildingFunction, self).__init__()
     BuildingExposureReportMixin.__init__(self)
     self._target_field = 'depth'
     self.hazard_classes = [
         tr('Dry Zone'),
         tr('Low Hazard Zone'),
         tr('Medium Hazard Zone'),
         tr('High Hazard Zone'),
         tr('Very High Hazard Zone'),
     ]
Example #6
0
 def __init__(self):
     """Constructor (calls ctor of base class)."""
     super(TsunamiRasterBuildingFunction, self).__init__()
     BuildingExposureReportMixin.__init__(self)
     self._target_field = 'depth'
     self.hazard_classes = [
         tr('Dry Zone'),
         tr('Low Hazard Zone'),
         tr('Medium Hazard Zone'),
         tr('High Hazard Zone'),
         tr('Very High Hazard Zone'),
     ]
 def setUp(self):
     """Fixture run before all tests."""
     self.building_mixin_blank = BuildingExposureReportMixin()
     self.building_mixin = BuildingExposureReportMixin()
     self.building_mixin.question = ''
     self.building_mixin.buildings = {
         'School': 100,
         'University': 10,
         'Residential': 20000,
         'Religious': 3
     }
     self.building_mixin.affected_buildings = OrderedDict([
         ('Hazard Level 2', {
             'School': OrderedDict([
                 ('Affected', 50),
                 ('Value', 90000000)
             ]),
             'University': OrderedDict([
                 ('Affected', 0),
                 ('Value', 0)
             ]),
             'Residential': OrderedDict([
                 ('Affected', 12000),
                 ('Value', 1234567000)
             ]),
             'Religious': OrderedDict([
                 ('Affected', 0),
                 ('Value', 0)
             ])
         }),
         ('Hazard Level 1', {
             'School': OrderedDict([
                 ('Affected', 25),
                 ('Value', 50000000)
             ]),
             'University': OrderedDict([
                 ('Affected', 1),
                 ('Value', 11111111111)
             ]),
             'Residential': OrderedDict([
                 ('Affected', 1000),
                 ('Value', 123456000)
             ]),
             'Religious': OrderedDict([
                 ('Affected', 1),
                 ('Value', 10000000000)
             ])
         })
     ])
class BuildingExposureReportMixinTest(unittest.TestCase):
    """Test the ReportMixin.

    .. versionadded:: 3.1
    """

    # noinspection PyPep8Naming
    def setUp(self):
        """Fixture run before all tests."""
        self.building_mixin_blank = BuildingExposureReportMixin()
        self.building_mixin = BuildingExposureReportMixin()
        self.building_mixin.question = ''
        self.building_mixin.buildings = {
            'School': 100,
            'University': 10,
            'Residential': 20000,
            'Religious': 3
        }
        self.building_mixin.affected_buildings = OrderedDict([
            ('Hazard Level 2', {
                'School': OrderedDict([
                    ('Affected', 50),
                    ('Value', 90000000)
                ]),
                'University': OrderedDict([
                    ('Affected', 0),
                    ('Value', 0)
                ]),
                'Residential': OrderedDict([
                    ('Affected', 12000),
                    ('Value', 1234567000)
                ]),
                'Religious': OrderedDict([
                    ('Affected', 0),
                    ('Value', 0)
                ])
            }),
            ('Hazard Level 1', {
                'School': OrderedDict([
                    ('Affected', 25),
                    ('Value', 50000000)
                ]),
                'University': OrderedDict([
                    ('Affected', 1),
                    ('Value', 11111111111)
                ]),
                'Residential': OrderedDict([
                    ('Affected', 1000),
                    ('Value', 123456000)
                ]),
                'Religious': OrderedDict([
                    ('Affected', 1),
                    ('Value', 10000000000)
                ])
            })
        ])

    def tearDown(self):
        """Run after each test."""
        del self.building_mixin_blank
        del self.building_mixin

    def test_0004_buildings_breakdown(self):
        """Test the buildings breakdown."""
        buildings_breakdown = self.building_mixin.impact_table()['fields']
        expected_results = [
            ['Religious', 0, 1, 2, 3],
            ['Residential', 12000, 1000, 7000, 20000],
            ['School', 50, 25, 25, 100],
            ['University', 0, 1, 9, 10],
            ['Total', 12050, 1027, 7036, 20113]
        ]
        for expected_result in expected_results:
            self.assertIn(expected_result, buildings_breakdown)

    def test_0005_schools_closed(self):
        """Test schools closed as expected."""
        schools_closed_default = self.building_mixin_blank.schools_closed
        schools_closed = self.building_mixin.schools_closed

        message = 'Default should not have any closed schools.'
        self.assertEqual(schools_closed_default, 0, message)

        message = 'Schools closed in scenario not as expected.'
        self.assertEqual(schools_closed, 75, message)

    def test_0006_hospitals_closed(self):
        """Test hospitals closed as expected."""
        hospitals_closed_default = self.building_mixin_blank.hospitals_closed
        hospitals_closed = self.building_mixin.hospitals_closed
        message = 'Default should not have any closed hospitals.'
        self.assertEqual(hospitals_closed_default, 0, message)
        message = 'Hospitals closed in scenario not as expected.'
        self.assertEqual(hospitals_closed, 0, message)

    def test_0007_general_methods(self):
        """Test general methods."""
        default_count_usage = self.building_mixin_blank._count_usage('School')
        message = 'Default count is not as expected.'
        self.assertEqual(default_count_usage, 0, message)

        count_usage = self.building_mixin._count_usage('School')
        message = 'Count is not as expected.'
        self.assertEqual(count_usage, 75, message)

        default_impact_breakdown = self.building_mixin_blank._impact_breakdown
        message = 'The default impact breakdown should be empty.'
        self.assertListEqual(default_impact_breakdown, [], message)

        impact_breakdown = self.building_mixin._impact_breakdown
        message = 'The default impact breakdown should be empty.'
        self.assertListEqual(impact_breakdown, ['Affected', 'Value'], message)

        default_categories = self.building_mixin_blank._affected_categories
        message = 'The default categories should be empty.'
        self.assertListEqual(default_categories, [], message)

        categories = self.building_mixin._affected_categories
        message = 'The categories are not as expected.'
        self.assertListEqual(
            categories,
            ['Hazard Level 2', 'Hazard Level 1'],
            message)

    def test_0008_building_counts(self):
        """Test the building counts."""
        default_affected = self.building_mixin_blank.total_affected_buildings
        default_unaffected = (
            self.building_mixin_blank.total_unaffected_buildings)
        default_total = self.building_mixin_blank.total_buildings

        message = 'Defaults counts should be 0.'
        self.assertEqual(default_total, 0, message)
        self.assertEqual(default_unaffected, 0, message)
        self.assertEqual(default_affected, 0, message)

        affected = self.building_mixin.total_affected_buildings
        unaffected = self.building_mixin.total_unaffected_buildings
        total = self.building_mixin.total_buildings

        message = (
            'The total number of buildings should equal the sum of affected '
            'and unaffected.')
        self.assertEqual(total, unaffected + affected, message)
        message = 'The total number of buildings is not as expected.'
        self.assertEqual(total, 20113, message)
        message = 'The affected number of buildings is not as expected.'
        self.assertEqual(affected, 13077, message)
        message = 'The unaffected number of buildings is not as expected.'
        self.assertEqual(unaffected, 7036, message)

    def test_0010_generate_data(self):
        """Test generating data."""
        self.maxDiff = None
        data = self.building_mixin.generate_data()
        expected = {
            'exposure': 'building',
            # Since 3.5 the action checklists come from the IF base
            # not the mixin base. Mixin has just a stub for this...
            'action check list': {
                'fields': [],
                'title': u'Action checklist'},
            'impact summary': {
                'attributes': ['category', 'value'],
                'headings': ['Buildings', 'Count'],
                'fields': [
                    [u'Hazard Level 2', 12050, 1324567000],
                    [u'Hazard Level 1', 1027, 21284567111],
                    [u'Affected buildings', 13077],
                    [u'Not affected buildings', 7036],
                    [u'Total', 20113]
                ]
            },
            'impact table': {
                'attributes': [
                    'Building type',
                    u'Hazard Level 2',
                    u'Hazard Level 1',
                    u'Not Affected',
                    u'Total'],
                'fields': [
                    ['Religious', 0, 1, 2, 3],
                    ['Residential', 12000, 1000, 7000, 20000],
                    ['School', 50, 25, 25, 100],
                    ['University', 0, 1, 9, 10],
                    [u'Total', 12050, 1027, 7036, 20113]
                ]
            },
            'notes': {'fields': [], 'title': 'Notes and assumptions'},
            'question': ''}
        self.assertDictEqual(data, expected)
Example #9
0
 def __init__(self):
     super(VolcanoPolygonBuildingFunction, self).__init__()
     BuildingExposureReportMixin.__init__(self)
     self.volcano_names = tr('Not specified in data')
     self._target_field = 'Hazard'
Example #10
0
 def __init__(self):
     super(FloodPolygonBuildingFunction, self).__init__()
     BuildingExposureReportMixin.__init__(self)
     # The 'wet' variable
     self.wet = 'wet'
class BuildingExposureReportMixinTest(unittest.TestCase):
    """Test the ReportMixin.

    .. versionadded:: 3.1
    """

    # noinspection PyPep8Naming
    def setUp(self):
        """Fixture run before all tests."""
        self.building_mixin_blank = BuildingExposureReportMixin()
        self.building_mixin = BuildingExposureReportMixin()
        self.building_mixin.buildings = {
            'School': 100,
            'University': 10,
            'Residential': 20000,
            'Religious': 3
        }
        self.building_mixin.affected_buildings = OrderedDict([
            ('Hazard Level 2', {
                'School': OrderedDict([
                    ('Affected', 50),
                    ('Value', 90000000)
                ]),
                'University': OrderedDict([
                    ('Affected', 0),
                    ('Value', 0)
                ]),
                'Residential': OrderedDict([
                    ('Affected', 12000),
                    ('Value', 1234567000)
                ]),
                'Religious': OrderedDict([
                    ('Affected', 0),
                    ('Value', 0)
                ])
            }),
            ('Hazard Level 1', {
                'School': OrderedDict([
                    ('Affected', 25),
                    ('Value', 50000000)
                ]),
                'University': OrderedDict([
                    ('Affected', 1),
                    ('Value', 11111111111)
                ]),
                'Residential': OrderedDict([
                    ('Affected', 1000),
                    ('Value', 123456000)
                ]),
                'Religious': OrderedDict([
                    ('Affected', 1),
                    ('Value', 10000000000)
                ])
            })
        ])

    def tearDown(self):
        """Run after each test."""
        del self.building_mixin_blank
        del self.building_mixin

    def test_0001_generate_report(self):
        """Generate a blank report."""
        blank_report = self.building_mixin_blank.generate_report()
        expected_blank_report = [
            {'content': ''},
            {'content': ''},
            {'content': [u'Hazard Category'], 'header': True},
            {'content': [u'Buildings Not Affected', '0'], 'header': True},
            {'content': [u'All Buildings', '0'], 'header': True},
            {'content': ''},
            {'content': [u'Building type', u'Total'], 'header': True},
            {'content': ''},
            {'content': u'Action Checklist:', 'header': True},
            {'content': u'Are the critical facilities still open?'},
            {'content': (
                u'Which structures have warning capacity (eg. sirens, '
                u'speakers, etc.)?')},
            {'content': u'Which buildings will be evacuation centres?'},
            {'content': u'Where will we locate the operations centre?'},
            {'content': (
                u'Where will we locate warehouse and/or distribution '
                u'centres?')},
            {
                'content': (
                    u'Where will the students from the %s closed schools go '
                    u'to study?'),
                'arguments': ('0',),
                'condition': False
            },
            {
                'content': (
                    u'Where will the patients from the %s closed hospitals '
                    u'go for treatment and how will we transport them?'),
                'arguments': ('0',),
                'condition': False},
            {'content': ''}]
        message = 'Blank report is not as expected.'
        self.assertListEqual(blank_report, expected_blank_report, message)

    def test_0002_action_checklist(self):
        """The default action check list."""
        action_checklist = self.building_mixin_blank.action_checklist()
        expected_action_checklist = [
            {'content': u'Action Checklist:', 'header': True},
            {'content': u'Are the critical facilities still open?'},
            {'content': (
                u'Which structures have warning capacity (eg. sirens, '
                u'speakers, etc.)?')},
            {'content': u'Which buildings will be evacuation centres?'},
            {'content': u'Where will we locate the operations centre?'},
            {'content': (
                u'Where will we locate warehouse and/or distribution '
                u'centres?')},
            {
                'content': (
                    u'Where will the students from the %s closed schools go '
                    u'to study?'),
                'arguments': ('0',),
                'condition': False
            },
            {
                'content': (
                    u'Where will the patients from the %s closed hospitals '
                    u'go for treatment and how will we transport them?'),
                'arguments': ('0',),
                'condition': False}]
        message = 'Default action checklist not as expected.'
        self.assertListEqual(
            action_checklist,
            expected_action_checklist,
            message)

    def test_0003_impact_summary(self):
        """Test the buildings impact summary."""
        impact_summary = self.building_mixin.impact_summary()
        expected_impact_summary = [
            {
                'content': [u'Hazard Category', 'Affected', 'Value'],
                'header': True
            },
            {'content': [u'Hazard Level 2', '12,050', '1,324,567,000']},
            {'content': [u'Hazard Level 1', '1,027', '21,284,567,111']},
            {
                'content': [u'Total Buildings Affected', '13,077'],
                'header': True
            },
            {'content': [u'Buildings Not Affected', '7,036'], 'header': True},
            {'content': [u'All Buildings', '20,113'], 'header': True}]
        message = 'Impact summary is not as expcted.'
        self.assertListEqual(
            impact_summary,
            expected_impact_summary,
            message)

    def test_0004_buildings_breakdown(self):
        """Test the buildings breakdown."""
        buildings_breakdown = self.building_mixin.buildings_breakdown()
        expected_buildings_breakdown = [
            {
                'content': [
                    u'Building type',
                    u'Hazard Level 2',
                    u'Hazard Level 1',
                    u'Total'],
                'header': True
            },
            {'content': ['Religious', '0', '1', '3']},
            {'content': ['Residential', '12,000', '1,000', '20,000']},
            {'content': ['School', '50', '25', '100']},
            {'content': ['University', '0', '1', '10']}]
        message = 'building breakdown is not as expected.'
        self.assertListEqual(
            buildings_breakdown,
            expected_buildings_breakdown,
            message)

    def test_0005_schools_closed(self):
        """Test schools closed as expected."""
        schools_closed_default = self.building_mixin_blank.schools_closed
        schools_closed = self.building_mixin.schools_closed
        message = 'Default should not have any closed schools.'
        self.assertEqual(schools_closed_default, 0, message)
        message = 'Schools closed in scenario not as expected.'
        self.assertEqual(schools_closed, 75, message)

    def test_0006_hospitals_closed(self):
        """Test hospitals closed as expected."""
        hospitals_closed_default = self.building_mixin_blank.hospitals_closed
        hospitals_closed = self.building_mixin.hospitals_closed
        message = 'Default should not have any closed hospitals.'
        self.assertEqual(hospitals_closed_default, 0, message)
        message = 'Hospitals closed in scenario not as expected.'
        self.assertEqual(hospitals_closed, 0, message)

    def test_0007_general_methods(self):
        """Test general methods."""
        default_count_usage = self.building_mixin_blank._count_usage('School')
        message = 'Default count is not as expected.'
        self.assertEqual(default_count_usage, 0, message)
        count_usage = self.building_mixin._count_usage('School')
        message = 'Count is not as expected.'
        self.assertEqual(count_usage, 75, message)
        default_impact_breakdown = self.building_mixin_blank._impact_breakdown
        message = 'The default impact breakdown should be empty.'
        self.assertListEqual(default_impact_breakdown, [], message)
        impact_breakdown = self.building_mixin._impact_breakdown
        message = 'The default impact breakdown should be empty.'
        self.assertListEqual(impact_breakdown, ['Affected', 'Value'], message)
        default_categories = self.building_mixin_blank._affected_categories
        message = 'The default categories should be empty.'
        self.assertListEqual(default_categories, [], message)
        categories = self.building_mixin._affected_categories
        message = 'The categories are not as expected.'
        self.assertListEqual(
            categories,
            ['Hazard Level 2', 'Hazard Level 1'],
            message)

    def test_0008_building_counts(self):
        """Test the building counts."""
        default_affected = self.building_mixin_blank.total_affected_buildings
        default_unaffected = (
            self.building_mixin_blank.total_unaffected_buildings)
        default_total = self.building_mixin_blank.total_buildings
        message = 'Defaults counts should be 0.'
        self.assertEqual(default_total, 0, message)
        self.assertEqual(default_unaffected, 0, message)
        self.assertEqual(default_affected, 0, message)
        affected = self.building_mixin.total_affected_buildings
        unaffected = self.building_mixin.total_unaffected_buildings
        total = self.building_mixin.total_buildings
        message = (
            'The total number of buildings should equal the sum of affected '
            'and unaffected.')
        self.assertEqual(total, unaffected + affected, message)
        message = 'The total number of buildings is not as expected.'
        self.assertEqual(total, 20113, message)
        message = 'The affected number of buildings is not as expected.'
        self.assertEqual(affected, 13077, message)
        message = 'The unaffected number of buildings is not as expected.'
        self.assertEqual(unaffected, 7036, message)

    def test_0009_consolidate_to_other(self):
        """Test consolidating smaller building types to 'other'."""
        total_buildings_before = self.building_mixin.total_buildings
        affected_other_before = self.building_mixin._count_usage('Other')
        message = (
            'There should be no affected buildings of type other before '
            'consolidating.')
        self.assertEqual(affected_other_before, 0, message)
        other_in_buildings_before = (
            'Other' in self.building_mixin.buildings.keys())
        message = (
            'There should be no buildings of type other before '
            'consolidation.')
        self.assertFalse(other_in_buildings_before, message)
        self.building_mixin._consolidate_to_other()
        total_buildings_after = self.building_mixin.total_buildings
        message = (
            'The total number of buildings should remain the same '
            'even after consolidation.')
        self.assertEqual(
            total_buildings_before,
            total_buildings_after,
            message)
        affected_other_after = self.building_mixin._count_usage('Other')
        message = 'The affected other buildings are not as expected.'
        self.assertEqual(affected_other_after, 2, message)
        other_in_buildings_after = (
            'Other' in self.building_mixin.buildings.keys())
        message = 'The type other should be in buildings.'
        self.assertTrue(other_in_buildings_after, message)
        total_other_after = self.building_mixin.buildings['Other']
        message = (
            'The total number of other after consolidation is '
            'not as expected.')
        self.assertEqual(total_other_after, 13, message)
Example #12
0
 def __init__(self):
     super(ClassifiedRasterHazardBuildingFunction, self).__init__()
     BuildingExposureReportMixin.__init__(self)
     self.affected_field = "affected"
     self.target_field = "hazard"
Example #13
0
 def __init__(self):
     """Constructor (calls ctor of base class)."""
     super(FloodRasterBuildingFunction, self).__init__()
     BuildingExposureReportMixin.__init__(self)
class BuildingExposureReportMixinTest(unittest.TestCase):
    """Test the ReportMixin.

    .. versionadded:: 3.1
    """

    # noinspection PyPep8Naming
    def setUp(self):
        """Fixture run before all tests."""
        self.building_mixin_blank = BuildingExposureReportMixin()
        self.building_mixin = BuildingExposureReportMixin()
        self.building_mixin.question = ''
        self.building_mixin.buildings = {
            'School': 100,
            'University': 10,
            'Residential': 20000,
            'Religious': 3
        }
        self.building_mixin.affected_buildings = OrderedDict([
            ('Hazard Level 2', {
                'School':
                OrderedDict([('Affected', 50), ('Value', 90000000)]),
                'University':
                OrderedDict([('Affected', 0), ('Value', 0)]),
                'Residential':
                OrderedDict([('Affected', 12000), ('Value', 1234567000)]),
                'Religious':
                OrderedDict([('Affected', 0), ('Value', 0)])
            }),
            ('Hazard Level 1', {
                'School':
                OrderedDict([('Affected', 25), ('Value', 50000000)]),
                'University':
                OrderedDict([('Affected', 1), ('Value', 11111111111)]),
                'Residential':
                OrderedDict([('Affected', 1000), ('Value', 123456000)]),
                'Religious':
                OrderedDict([('Affected', 1), ('Value', 10000000000)])
            })
        ])

    def tearDown(self):
        """Run after each test."""
        del self.building_mixin_blank
        del self.building_mixin

    def test_0004_buildings_breakdown(self):
        """Test the buildings breakdown."""
        buildings_breakdown = self.building_mixin.impact_table()['fields']
        expected_results = [['Religious', 0, 1, 2, 3],
                            ['Residential', 12000, 1000, 7000, 20000],
                            ['School', 50, 25, 25, 100],
                            ['University', 0, 1, 9, 10],
                            ['Total', 12050, 1027, 7036, 20113]]
        for expected_result in expected_results:
            self.assertIn(expected_result, buildings_breakdown)

    def test_0005_schools_closed(self):
        """Test schools closed as expected."""
        schools_closed_default = self.building_mixin_blank.schools_closed
        schools_closed = self.building_mixin.schools_closed

        message = 'Default should not have any closed schools.'
        self.assertEqual(schools_closed_default, 0, message)

        message = 'Schools closed in scenario not as expected.'
        self.assertEqual(schools_closed, 75, message)

    def test_0006_hospitals_closed(self):
        """Test hospitals closed as expected."""
        hospitals_closed_default = self.building_mixin_blank.hospitals_closed
        hospitals_closed = self.building_mixin.hospitals_closed
        message = 'Default should not have any closed hospitals.'
        self.assertEqual(hospitals_closed_default, 0, message)
        message = 'Hospitals closed in scenario not as expected.'
        self.assertEqual(hospitals_closed, 0, message)

    def test_0007_general_methods(self):
        """Test general methods."""
        default_count_usage = self.building_mixin_blank._count_usage('School')
        message = 'Default count is not as expected.'
        self.assertEqual(default_count_usage, 0, message)

        count_usage = self.building_mixin._count_usage('School')
        message = 'Count is not as expected.'
        self.assertEqual(count_usage, 75, message)

        default_impact_breakdown = self.building_mixin_blank._impact_breakdown
        message = 'The default impact breakdown should be empty.'
        self.assertListEqual(default_impact_breakdown, [], message)

        impact_breakdown = self.building_mixin._impact_breakdown
        message = 'The default impact breakdown should be empty.'
        self.assertListEqual(impact_breakdown, ['Affected', 'Value'], message)

        default_categories = self.building_mixin_blank._affected_categories
        message = 'The default categories should be empty.'
        self.assertListEqual(default_categories, [], message)

        categories = self.building_mixin._affected_categories
        message = 'The categories are not as expected.'
        self.assertListEqual(categories, ['Hazard Level 2', 'Hazard Level 1'],
                             message)

    def test_0008_building_counts(self):
        """Test the building counts."""
        default_affected = self.building_mixin_blank.total_affected_buildings
        default_unaffected = (
            self.building_mixin_blank.total_unaffected_buildings)
        default_total = self.building_mixin_blank.total_buildings

        message = 'Defaults counts should be 0.'
        self.assertEqual(default_total, 0, message)
        self.assertEqual(default_unaffected, 0, message)
        self.assertEqual(default_affected, 0, message)

        affected = self.building_mixin.total_affected_buildings
        unaffected = self.building_mixin.total_unaffected_buildings
        total = self.building_mixin.total_buildings

        message = (
            'The total number of buildings should equal the sum of affected '
            'and unaffected.')
        self.assertEqual(total, unaffected + affected, message)
        message = 'The total number of buildings is not as expected.'
        self.assertEqual(total, 20113, message)
        message = 'The affected number of buildings is not as expected.'
        self.assertEqual(affected, 13077, message)
        message = 'The unaffected number of buildings is not as expected.'
        self.assertEqual(unaffected, 7036, message)

    def test_0010_generate_data(self):
        """Test generating data."""
        self.maxDiff = None
        data = self.building_mixin.generate_data()
        expected = {
            'exposure': 'building',
            # Since 3.5 the action checklists come from the IF base
            # not the mixin base. Mixin has just a stub for this...
            'action check list': {
                'fields': [],
                'title': u'Action checklist'
            },
            'impact summary': {
                'attributes': ['category', 'value'],
                'headings': ['Buildings', 'Count'],
                'fields': [[u'Hazard Level 2', 12050, 1324567000],
                           [u'Hazard Level 1', 1027, 21284567111],
                           [u'Affected buildings', 13077],
                           [u'Not affected buildings', 7036],
                           [u'Total', 20113]]
            },
            'impact table': {
                'attributes': [
                    'Building type', u'Hazard Level 2', u'Hazard Level 1',
                    u'Not Affected', u'Total'
                ],
                'fields': [['Religious', 0, 1, 2, 3],
                           ['Residential', 12000, 1000, 7000, 20000],
                           ['School', 50, 25, 25, 100],
                           ['University', 0, 1, 9, 10],
                           [u'Total', 12050, 1027, 7036, 20113]]
            },
            'notes': {
                'fields': [],
                'title': 'Notes and assumptions'
            },
            'question': ''
        }
        self.assertDictEqual(data, expected)
class BuildingExposureReportMixinTest(unittest.TestCase):
    """Test the ReportMixin.

    .. versionadded:: 3.1
    """

    # noinspection PyPep8Naming
    def setUp(self):
        """Fixture run before all tests."""
        self.building_mixin_blank = BuildingExposureReportMixin()
        self.building_mixin = BuildingExposureReportMixin()
        self.building_mixin.buildings = {
            'School': 100,
            'University': 10,
            'Residential': 20000,
            'Religious': 3
        }
        self.building_mixin.affected_buildings = OrderedDict([
            ('Hazard Level 2', {
                'School':
                OrderedDict([('Affected', 50), ('Value', 90000000)]),
                'University':
                OrderedDict([('Affected', 0), ('Value', 0)]),
                'Residential':
                OrderedDict([('Affected', 12000), ('Value', 1234567000)]),
                'Religious':
                OrderedDict([('Affected', 0), ('Value', 0)])
            }),
            ('Hazard Level 1', {
                'School':
                OrderedDict([('Affected', 25), ('Value', 50000000)]),
                'University':
                OrderedDict([('Affected', 1), ('Value', 11111111111)]),
                'Residential':
                OrderedDict([('Affected', 1000), ('Value', 123456000)]),
                'Religious':
                OrderedDict([('Affected', 1), ('Value', 10000000000)])
            })
        ])

    def tearDown(self):
        """Run after each test."""
        del self.building_mixin_blank
        del self.building_mixin

    def test_0001_generate_report(self):
        """Generate a blank report."""
        blank_report = self.building_mixin_blank.generate_report()
        expected_blank_report = [{
            'content': ''
        }, {
            'content': ''
        }, {
            'content': [u'Hazard Category'],
            'header': True
        }, {
            'content': [u'Buildings Not Affected', '0'],
            'header': True
        }, {
            'content': [u'All Buildings', '0'],
            'header': True
        }, {
            'content': ''
        }, {
            'content': [u'Building type', u'Total'],
            'header': True
        }, {
            'content': ''
        }, {
            'content': u'Action Checklist:',
            'header': True
        }, {
            'content':
            u'Are the critical facilities still open?'
        }, {
            'content': (u'Which structures have warning capacity (eg. sirens, '
                        u'speakers, etc.)?')
        }, {
            'content':
            u'Which buildings will be evacuation centres?'
        }, {
            'content':
            u'Where will we locate the operations centre?'
        }, {
            'content': (u'Where will we locate warehouse and/or distribution '
                        u'centres?')
        }, {
            'content':
            (u'Where will the students from the %s closed schools go '
             u'to study?'),
            'arguments': ('0', ),
            'condition':
            False
        }, {
            'content':
            (u'Where will the patients from the %s closed hospitals '
             u'go for treatment and how will we transport them?'),
            'arguments': ('0', ),
            'condition':
            False
        }, {
            'content': ''
        }]
        message = 'Blank report is not as expected.'
        self.assertListEqual(blank_report, expected_blank_report, message)

    def test_0002_action_checklist(self):
        """The default action check list."""
        action_checklist = self.building_mixin_blank.action_checklist()
        expected_action_checklist = [{
            'content': u'Action Checklist:',
            'header': True
        }, {
            'content':
            u'Are the critical facilities still open?'
        }, {
            'content': (u'Which structures have warning capacity (eg. sirens, '
                        u'speakers, etc.)?')
        }, {
            'content':
            u'Which buildings will be evacuation centres?'
        }, {
            'content':
            u'Where will we locate the operations centre?'
        }, {
            'content': (u'Where will we locate warehouse and/or distribution '
                        u'centres?')
        }, {
            'content':
            (u'Where will the students from the %s closed schools go '
             u'to study?'),
            'arguments': ('0', ),
            'condition':
            False
        }, {
            'content':
            (u'Where will the patients from the %s closed hospitals '
             u'go for treatment and how will we transport them?'),
            'arguments': ('0', ),
            'condition':
            False
        }]
        message = 'Default action checklist not as expected.'
        self.assertListEqual(action_checklist, expected_action_checklist,
                             message)

    def test_0003_impact_summary(self):
        """Test the buildings impact summary."""
        impact_summary = self.building_mixin.impact_summary()
        expected_impact_summary = [{
            'content': [u'Hazard Category', 'Affected', 'Value'],
            'header':
            True
        }, {
            'content': [u'Hazard Level 2', '12,050', '1,324,567,000']
        }, {
            'content': [u'Hazard Level 1', '1,027', '21,284,567,111']
        }, {
            'content': [u'Total Buildings Affected', '13,077'],
            'header':
            True
        }, {
            'content': [u'Buildings Not Affected', '7,036'],
            'header':
            True
        }, {
            'content': [u'All Buildings', '20,113'],
            'header': True
        }]
        message = 'Impact summary is not as expcted.'
        self.assertListEqual(impact_summary, expected_impact_summary, message)

    def test_0004_buildings_breakdown(self):
        """Test the buildings breakdown."""
        buildings_breakdown = self.building_mixin.buildings_breakdown()
        expected_buildings_breakdown = [{
            'content':
            [u'Building type', u'Hazard Level 2', u'Hazard Level 1', u'Total'],
            'header':
            True
        }, {
            'content': ['Religious', '0', '1', '1']
        }, {
            'content': ['Residential', '12,000', '1,000', '13,000']
        }, {
            'content': ['School', '50', '25', '75']
        }, {
            'content': ['University', '0', '1', '1']
        }]
        message = 'building breakdown is not as expected.'
        self.assertListEqual(buildings_breakdown, expected_buildings_breakdown,
                             message)

    def test_0005_schools_closed(self):
        """Test schools closed as expected."""
        schools_closed_default = self.building_mixin_blank.schools_closed
        schools_closed = self.building_mixin.schools_closed
        message = 'Default should not have any closed schools.'
        self.assertEqual(schools_closed_default, 0, message)
        message = 'Schools closed in scenario not as expected.'
        self.assertEqual(schools_closed, 75, message)

    def test_0006_hospitals_closed(self):
        """Test hospitals closed as expected."""
        hospitals_closed_default = self.building_mixin_blank.hospitals_closed
        hospitals_closed = self.building_mixin.hospitals_closed
        message = 'Default should not have any closed hospitals.'
        self.assertEqual(hospitals_closed_default, 0, message)
        message = 'Hospitals closed in scenario not as expected.'
        self.assertEqual(hospitals_closed, 0, message)

    def test_0007_general_methods(self):
        """Test general methods."""
        default_count_usage = self.building_mixin_blank._count_usage('School')
        message = 'Default count is not as expected.'
        self.assertEqual(default_count_usage, 0, message)
        count_usage = self.building_mixin._count_usage('School')
        message = 'Count is not as expected.'
        self.assertEqual(count_usage, 75, message)
        default_impact_breakdown = self.building_mixin_blank._impact_breakdown
        message = 'The default impact breakdown should be empty.'
        self.assertListEqual(default_impact_breakdown, [], message)
        impact_breakdown = self.building_mixin._impact_breakdown
        message = 'The default impact breakdown should be empty.'
        self.assertListEqual(impact_breakdown, ['Affected', 'Value'], message)
        default_categories = self.building_mixin_blank._affected_categories
        message = 'The default categories should be empty.'
        self.assertListEqual(default_categories, [], message)
        categories = self.building_mixin._affected_categories
        message = 'The categories are not as expected.'
        self.assertListEqual(categories, ['Hazard Level 2', 'Hazard Level 1'],
                             message)

    def test_0008_building_counts(self):
        """Test the building counts."""
        default_affected = self.building_mixin_blank.total_affected_buildings
        default_unaffected = (
            self.building_mixin_blank.total_unaffected_buildings)
        default_total = self.building_mixin_blank.total_buildings
        message = 'Defaults counts should be 0.'
        self.assertEqual(default_total, 0, message)
        self.assertEqual(default_unaffected, 0, message)
        self.assertEqual(default_affected, 0, message)
        affected = self.building_mixin.total_affected_buildings
        unaffected = self.building_mixin.total_unaffected_buildings
        total = self.building_mixin.total_buildings
        message = (
            'The total number of buildings should equal the sum of affected '
            'and unaffected.')
        self.assertEqual(total, unaffected + affected, message)
        message = 'The total number of buildings is not as expected.'
        self.assertEqual(total, 20113, message)
        message = 'The affected number of buildings is not as expected.'
        self.assertEqual(affected, 13077, message)
        message = 'The unaffected number of buildings is not as expected.'
        self.assertEqual(unaffected, 7036, message)

    def test_0009_consolidate_to_other(self):
        """Test consolidating smaller building types to 'other'."""
        total_buildings_before = self.building_mixin.total_buildings
        affected_other_before = self.building_mixin._count_usage('Other')
        message = (
            'There should be no affected buildings of type other before '
            'consolidating.')
        self.assertEqual(affected_other_before, 0, message)
        other_in_buildings_before = ('Other'
                                     in self.building_mixin.buildings.keys())
        message = ('There should be no buildings of type other before '
                   'consolidation.')
        self.assertFalse(other_in_buildings_before, message)
        self.building_mixin._consolidate_to_other()
        total_buildings_after = self.building_mixin.total_buildings
        message = ('The total number of buildings should remain the same '
                   'even after consolidation.')
        self.assertEqual(total_buildings_before, total_buildings_after,
                         message)
        affected_other_after = self.building_mixin._count_usage('Other')
        message = 'The affected other buildings are not as expected.'
        self.assertEqual(affected_other_after, 2, message)
        other_in_buildings_after = ('Other'
                                    in self.building_mixin.buildings.keys())
        message = 'The type other should be in buildings.'
        self.assertTrue(other_in_buildings_after, message)
        total_other_after = self.building_mixin.buildings['Other']
        message = ('The total number of other after consolidation is '
                   'not as expected.')
        self.assertEqual(total_other_after, 13, message)
Example #16
0
 def __init__(self):
     super(EarthquakeBuildingFunction, self).__init__()
     BuildingExposureReportMixin.__init__(self)
     self.is_nexis = False
     self.structure_class_field = None
class BuildingExposureReportMixinTest(unittest.TestCase):
    """Test the ReportMixin.

    .. versionadded:: 3.1
    """

    # noinspection PyPep8Naming
    def setUp(self):
        """Fixture run before all tests."""
        self.building_mixin_blank = BuildingExposureReportMixin()
        self.building_mixin = BuildingExposureReportMixin()
        self.building_mixin.buildings = {
            'School': 100,
            'University': 10,
            'Residential': 20000,
            'Religious': 3
        }
        self.building_mixin.affected_buildings = OrderedDict([
            ('Hazard Level 2', {
                'School':
                OrderedDict([('Affected', 50), ('Value', 90000000)]),
                'University':
                OrderedDict([('Affected', 0), ('Value', 0)]),
                'Residential':
                OrderedDict([('Affected', 12000), ('Value', 1234567000)]),
                'Religious':
                OrderedDict([('Affected', 0), ('Value', 0)])
            }),
            ('Hazard Level 1', {
                'School':
                OrderedDict([('Affected', 25), ('Value', 50000000)]),
                'University':
                OrderedDict([('Affected', 1), ('Value', 11111111111)]),
                'Residential':
                OrderedDict([('Affected', 1000), ('Value', 123456000)]),
                'Religious':
                OrderedDict([('Affected', 1), ('Value', 10000000000)])
            })
        ])

    def tearDown(self):
        """Run after each test."""
        del self.building_mixin_blank
        del self.building_mixin

    def test_0001_generate_report(self):
        """Generate a blank report."""
        blank_report = self.building_mixin_blank.generate_report().to_text()
        self.assertIn('**Not affected buildings**, 0', blank_report)
        self.assertIn('**Building type**, **Not Affected**, **Total**',
                      blank_report)

    def test_0002_action_checklist(self):
        """The default action check list."""
        action_checklist = self.building_mixin_blank.action_checklist()
        action_checklist = action_checklist.to_text()
        self.assertIn(
            u'Which structures have warning capacity (eg. sirens, '
            u'speakers, etc.)?', action_checklist)
        self.assertIn(
            u'Are the water and electricity services still operating?',
            action_checklist)
        self.assertIn(u'Are the health centres still open?', action_checklist)
        self.assertIn(u'Are the other public services accessible',
                      action_checklist)
        self.assertIn(u'Which buildings will be evacuation centres?',
                      action_checklist)
        self.assertIn(u'Where will we locate the operations centre?',
                      action_checklist)
        self.assertIn(
            u'Where will we locate warehouse and/or distribution '
            u'centres?', action_checklist)
        self.assertNotIn(
            u'Where will the students from the 0 closed schools go to study?',
            action_checklist)
        self.assertNotIn(
            u'Where will the patients from the %s closed hospitals '
            u'go for treatment and how will we transport them?',
            action_checklist)

    def test_0003_impact_summary(self):
        """Test the buildings impact summary."""
        impact_summary = self.building_mixin.impact_summary()
        impact_summary = impact_summary.to_text()

        self.assertIn(u'**Hazard Level 2**, 12,050, 1,324,567,000',
                      impact_summary)
        self.assertIn(u'**Hazard Level 1**, 1,027, 21,284,567,111',
                      impact_summary)
        self.assertIn(u'**Affected buildings**, 13,077', impact_summary)
        self.assertIn(u'**Not affected buildings**, 7,036', impact_summary)
        self.assertIn(u'**Total**, 20,113', impact_summary)

    def test_0004_buildings_breakdown(self):
        """Test the buildings breakdown."""
        buildings_breakdown = self.building_mixin.buildings_breakdown()
        buildings_breakdown = buildings_breakdown.to_text()
        self.assertIn(u'**Religious**, 0, 1, 2, **3**', buildings_breakdown)
        self.assertIn(u'**Residential**, 12,000, 1,000, 7,000, **20,000**',
                      buildings_breakdown)
        self.assertIn(u'**School**, 50, 25, 25, **100**', buildings_breakdown)
        self.assertIn(u'**University**, 0, 1, 9, **10**', buildings_breakdown)

    def test_0005_schools_closed(self):
        """Test schools closed as expected."""
        schools_closed_default = self.building_mixin_blank.schools_closed
        schools_closed = self.building_mixin.schools_closed
        message = 'Default should not have any closed schools.'
        self.assertEqual(schools_closed_default, 0, message)
        message = 'Schools closed in scenario not as expected.'
        self.assertEqual(schools_closed, 75, message)

    def test_0006_hospitals_closed(self):
        """Test hospitals closed as expected."""
        hospitals_closed_default = self.building_mixin_blank.hospitals_closed
        hospitals_closed = self.building_mixin.hospitals_closed
        message = 'Default should not have any closed hospitals.'
        self.assertEqual(hospitals_closed_default, 0, message)
        message = 'Hospitals closed in scenario not as expected.'
        self.assertEqual(hospitals_closed, 0, message)

    def test_0007_general_methods(self):
        """Test general methods."""
        default_count_usage = self.building_mixin_blank._count_usage('School')
        message = 'Default count is not as expected.'
        self.assertEqual(default_count_usage, 0, message)
        count_usage = self.building_mixin._count_usage('School')
        message = 'Count is not as expected.'
        self.assertEqual(count_usage, 75, message)
        default_impact_breakdown = self.building_mixin_blank._impact_breakdown
        message = 'The default impact breakdown should be empty.'
        self.assertListEqual(default_impact_breakdown, [], message)
        impact_breakdown = self.building_mixin._impact_breakdown
        message = 'The default impact breakdown should be empty.'
        self.assertListEqual(impact_breakdown, ['Affected', 'Value'], message)
        default_categories = self.building_mixin_blank._affected_categories
        message = 'The default categories should be empty.'
        self.assertListEqual(default_categories, [], message)
        categories = self.building_mixin._affected_categories
        message = 'The categories are not as expected.'
        self.assertListEqual(categories, ['Hazard Level 2', 'Hazard Level 1'],
                             message)

    def test_0008_building_counts(self):
        """Test the building counts."""
        default_affected = self.building_mixin_blank.total_affected_buildings
        default_unaffected = (
            self.building_mixin_blank.total_unaffected_buildings)
        default_total = self.building_mixin_blank.total_buildings
        message = 'Defaults counts should be 0.'
        self.assertEqual(default_total, 0, message)
        self.assertEqual(default_unaffected, 0, message)
        self.assertEqual(default_affected, 0, message)
        affected = self.building_mixin.total_affected_buildings
        unaffected = self.building_mixin.total_unaffected_buildings
        total = self.building_mixin.total_buildings
        message = (
            'The total number of buildings should equal the sum of affected '
            'and unaffected.')
        self.assertEqual(total, unaffected + affected, message)
        message = 'The total number of buildings is not as expected.'
        self.assertEqual(total, 20113, message)
        message = 'The affected number of buildings is not as expected.'
        self.assertEqual(affected, 13077, message)
        message = 'The unaffected number of buildings is not as expected.'
        self.assertEqual(unaffected, 7036, message)

    def test_0009_consolidate_to_other(self):
        """Test consolidating smaller building types to 'other'."""
        total_buildings_before = self.building_mixin.total_buildings
        affected_other_before = self.building_mixin._count_usage('Other')
        message = (
            'There should be no affected buildings of type other before '
            'consolidating.')
        self.assertEqual(affected_other_before, 0, message)
        other_in_buildings_before = ('Other'
                                     in self.building_mixin.buildings.keys())
        message = ('There should be no buildings of type other before '
                   'consolidation.')
        self.assertFalse(other_in_buildings_before, message)
        self.building_mixin._consolidate_to_other()
        total_buildings_after = self.building_mixin.total_buildings
        message = ('The total number of buildings should remain the same '
                   'even after consolidation.')
        self.assertEqual(total_buildings_before, total_buildings_after,
                         message)
        affected_other_after = self.building_mixin._count_usage('Other')
        message = 'The affected other buildings are not as expected.'
        self.assertEqual(affected_other_after, 2, message)
        other_in_buildings_after = ('Other'
                                    in self.building_mixin.buildings.keys())
        message = 'The type other should be in buildings.'
        self.assertTrue(other_in_buildings_after, message)
        total_other_after = self.building_mixin.buildings['Other']
        message = ('The total number of other after consolidation is '
                   'not as expected.')
        self.assertEqual(total_other_after, 13, message)
Example #18
0
 def __init__(self):
     super(EarthquakeBuildingFunction, self).__init__()
     BuildingExposureReportMixin.__init__(self)
     self.is_nexis = False
     self.structure_class_field = None
class BuildingExposureReportMixinTest(unittest.TestCase):
    """Test the ReportMixin.

    .. versionadded:: 3.1
    """

    # noinspection PyPep8Naming
    def setUp(self):
        """Fixture run before all tests."""
        self.building_mixin_blank = BuildingExposureReportMixin()
        self.building_mixin = BuildingExposureReportMixin()
        self.building_mixin.buildings = {
            'School': 100,
            'University': 10,
            'Residential': 20000,
            'Religious': 3
        }
        self.building_mixin.affected_buildings = OrderedDict([
            ('Hazard Level 2', {
                'School': OrderedDict([
                    ('Affected', 50),
                    ('Value', 90000000)
                ]),
                'University': OrderedDict([
                    ('Affected', 0),
                    ('Value', 0)
                ]),
                'Residential': OrderedDict([
                    ('Affected', 12000),
                    ('Value', 1234567000)
                ]),
                'Religious': OrderedDict([
                    ('Affected', 0),
                    ('Value', 0)
                ])
            }),
            ('Hazard Level 1', {
                'School': OrderedDict([
                    ('Affected', 25),
                    ('Value', 50000000)
                ]),
                'University': OrderedDict([
                    ('Affected', 1),
                    ('Value', 11111111111)
                ]),
                'Residential': OrderedDict([
                    ('Affected', 1000),
                    ('Value', 123456000)
                ]),
                'Religious': OrderedDict([
                    ('Affected', 1),
                    ('Value', 10000000000)
                ])
            })
        ])

    def tearDown(self):
        """Run after each test."""
        del self.building_mixin_blank
        del self.building_mixin

    def test_0002_action_checklist(self):
        """The default action check list."""
        action_checklist = self.building_mixin_blank.action_checklist()[
            'fields']
        expected = [
            'Which structures have warning capacity (eg. sirens, speakers, '
            'etc.)?',
            'Are the water and electricity services still operating?',
            'Are the health centres still open?',
            'Are the other public services accessible?',
            'Which buildings will be evacuation centres?',
            'Where will we locate the operations centre?',
            'Where will we locate warehouse and/or distribution centres?',
        ]
        for item in expected:
            self.assertIn(item, action_checklist)

        not_expected = [
            'Where will the students from the 0 closed schools go to study?',
            'Where will the patients from the 0 closed hospitals go for '
            'treatment and how will we transport them?',
        ]
        for item in not_expected:
            self.assertNotIn(item, action_checklist)

    def test_0004_buildings_breakdown(self):
        """Test the buildings breakdown."""
        buildings_breakdown = self.building_mixin.buildings_breakdown()[
            'fields']
        expected_results = [
            ['Religious', 0, 1, 2, 3],
            ['Residential', 12000, 1000, 7000, 20000],
            ['School', 50, 25, 25, 100],
            ['University', 0, 1, 9, 10],
            ['Total', 12050, 1027, 7036, 20113]
        ]
        for expected_result in expected_results:
            self.assertIn(expected_result, buildings_breakdown)

    def test_0005_schools_closed(self):
        """Test schools closed as expected."""
        schools_closed_default = self.building_mixin_blank.schools_closed
        schools_closed = self.building_mixin.schools_closed

        message = 'Default should not have any closed schools.'
        self.assertEqual(schools_closed_default, 0, message)

        message = 'Schools closed in scenario not as expected.'
        self.assertEqual(schools_closed, 75, message)

    def test_0006_hospitals_closed(self):
        """Test hospitals closed as expected."""
        hospitals_closed_default = self.building_mixin_blank.hospitals_closed
        hospitals_closed = self.building_mixin.hospitals_closed
        message = 'Default should not have any closed hospitals.'
        self.assertEqual(hospitals_closed_default, 0, message)
        message = 'Hospitals closed in scenario not as expected.'
        self.assertEqual(hospitals_closed, 0, message)

    def test_0007_general_methods(self):
        """Test general methods."""
        default_count_usage = self.building_mixin_blank._count_usage('School')
        message = 'Default count is not as expected.'
        self.assertEqual(default_count_usage, 0, message)

        count_usage = self.building_mixin._count_usage('School')
        message = 'Count is not as expected.'
        self.assertEqual(count_usage, 75, message)

        default_impact_breakdown = self.building_mixin_blank._impact_breakdown
        message = 'The default impact breakdown should be empty.'
        self.assertListEqual(default_impact_breakdown, [], message)

        impact_breakdown = self.building_mixin._impact_breakdown
        message = 'The default impact breakdown should be empty.'
        self.assertListEqual(impact_breakdown, ['Affected', 'Value'], message)

        default_categories = self.building_mixin_blank._affected_categories
        message = 'The default categories should be empty.'
        self.assertListEqual(default_categories, [], message)

        categories = self.building_mixin._affected_categories
        message = 'The categories are not as expected.'
        self.assertListEqual(
            categories,
            ['Hazard Level 2', 'Hazard Level 1'],
            message)

    def test_0008_building_counts(self):
        """Test the building counts."""
        default_affected = self.building_mixin_blank.total_affected_buildings
        default_unaffected = (
            self.building_mixin_blank.total_unaffected_buildings)
        default_total = self.building_mixin_blank.total_buildings

        message = 'Defaults counts should be 0.'
        self.assertEqual(default_total, 0, message)
        self.assertEqual(default_unaffected, 0, message)
        self.assertEqual(default_affected, 0, message)

        affected = self.building_mixin.total_affected_buildings
        unaffected = self.building_mixin.total_unaffected_buildings
        total = self.building_mixin.total_buildings

        message = (
            'The total number of buildings should equal the sum of affected '
            'and unaffected.')
        self.assertEqual(total, unaffected + affected, message)
        message = 'The total number of buildings is not as expected.'
        self.assertEqual(total, 20113, message)
        message = 'The affected number of buildings is not as expected.'
        self.assertEqual(affected, 13077, message)
        message = 'The unaffected number of buildings is not as expected.'
        self.assertEqual(unaffected, 7036, message)

    def test_0009_consolidate_to_other(self):
        """Test consolidating smaller building types to 'other'."""
        total_buildings_before = self.building_mixin.total_buildings
        affected_other_before = self.building_mixin._count_usage('Other')
        message = (
            'There should be no affected buildings of type other before '
            'consolidating.')
        self.assertEqual(affected_other_before, 0, message)
        other_in_buildings_before = (
            'Other' in self.building_mixin.buildings.keys())
        message = (
            'There should be no buildings of type other before '
            'consolidation.')
        self.assertFalse(other_in_buildings_before, message)
        self.building_mixin._consolidate_to_other()
        total_buildings_after = self.building_mixin.total_buildings
        message = (
            'The total number of buildings should remain the same '
            'even after consolidation.')
        self.assertEqual(
            total_buildings_before,
            total_buildings_after,
            message)
        affected_other_after = self.building_mixin._count_usage('Other')
        message = 'The affected other buildings are not as expected.'
        self.assertEqual(affected_other_after, 2, message)
        other_in_buildings_after = (
            'Other' in self.building_mixin.buildings.keys())
        message = 'The type other should be in buildings.'
        self.assertTrue(other_in_buildings_after, message)
        total_other_after = self.building_mixin.buildings['Other']
        message = (
            'The total number of other after consolidation is '
            'not as expected.')
        self.assertEqual(total_other_after, 13, message)

    def test_0010_generate_data(self):
        """Test generating data."""
        self.maxDiff = None
        data = self.building_mixin.generate_data()
        expected = {
            'exposure': 'building',
            'action check list': {'fields': [
                u'Which structures have warning capacity (eg. sirens, '
                u'speakers, etc.)?',
                u'Are the water and electricity services still operating?',
                u'Are the health centres still open?',
                u'Are the other public services accessible?',
                u'Which buildings will be evacuation centres?',
                u'Where will we locate the operations centre?',
                u'Where will we locate warehouse and/or distribution centres?',
                u'Are the schools and hospitals still active?',
                u'Where will the students from the 75 closed schools go to '
                u'study?'],
                'title': u'Action checklist'},
            'impact summary': {'attributes': ['category', 'value'],
                               'fields': [[u'Hazard Level 2', 12050,
                                           1324567000],
                                          [u'Hazard Level 1', 1027,
                                           21284567111],
                                          [u'Affected buildings',
                                           13077],
                                          [u'Not affected buildings',
                                           7036],
                                          [u'Total', 20113]]},
            'impact table': {'attributes': ['Building type',
                                            u'Hazard Level 2',
                                            u'Hazard Level 1',
                                            u'Not Affected',
                                            u'Total'],
                             'fields': [
                                 ['Religious', 0, 1, 2, 3],
                                 ['Residential', 12000, 1000, 7000, 20000],
                                 ['School', 50, 25, 25, 100],
                                 ['University', 0, 1, 9, 10],
                                 [u'Total', 12050, 1027, 7036, 20113]]},
            'notes': {'fields': [], 'title': 'Notes'},
            'question': ''}
        self.assertEquals(data, expected)
Example #20
0
 def __init__(self):
     super(FloodPolygonBuildingFunction, self).__init__()
     BuildingExposureReportMixin.__init__(self)
     # The 'wet' variable
     self.wet = 'wet'
class BuildingExposureReportMixinTest(unittest.TestCase):
    """Test the ReportMixin.

    .. versionadded:: 3.1
    """

    # noinspection PyPep8Naming
    def setUp(self):
        """Fixture run before all tests."""
        self.building_mixin_blank = BuildingExposureReportMixin()
        self.building_mixin = BuildingExposureReportMixin()
        self.building_mixin.buildings = {
            'School': 100,
            'University': 10,
            'Residential': 20000,
            'Religious': 3
        }
        self.building_mixin.affected_buildings = OrderedDict([
            ('Hazard Level 2', {
                'School': OrderedDict([
                    ('Affected', 50),
                    ('Value', 90000000)
                ]),
                'University': OrderedDict([
                    ('Affected', 0),
                    ('Value', 0)
                ]),
                'Residential': OrderedDict([
                    ('Affected', 12000),
                    ('Value', 1234567000)
                ]),
                'Religious': OrderedDict([
                    ('Affected', 0),
                    ('Value', 0)
                ])
            }),
            ('Hazard Level 1', {
                'School': OrderedDict([
                    ('Affected', 25),
                    ('Value', 50000000)
                ]),
                'University': OrderedDict([
                    ('Affected', 1),
                    ('Value', 11111111111)
                ]),
                'Residential': OrderedDict([
                    ('Affected', 1000),
                    ('Value', 123456000)
                ]),
                'Religious': OrderedDict([
                    ('Affected', 1),
                    ('Value', 10000000000)
                ])
            })
        ])

    def tearDown(self):
        """Run after each test."""
        del self.building_mixin_blank
        del self.building_mixin

    def test_0001_generate_report(self):
        """Generate a blank report."""
        blank_report = self.building_mixin_blank.generate_report().to_text()
        self.assertIn('**Not affected buildings**, 0', blank_report)
        self.assertIn(
            '**Building type**, **Not Affected**, **Total**',
            blank_report
        )

    def test_0002_action_checklist(self):
        """The default action check list."""
        action_checklist = self.building_mixin_blank.action_checklist()
        action_checklist = action_checklist.to_text()
        self.assertIn(
            u'Which structures have warning capacity (eg. sirens, '
            u'speakers, etc.)?',
            action_checklist)
        self.assertIn(
            u'Are the water and electricity services still operating?',
            action_checklist)
        self.assertIn(u'Are the health centres still open?', action_checklist)
        self.assertIn(
                u'Are the other public services accessible', action_checklist)
        self.assertIn(
            u'Which buildings will be evacuation centres?',
            action_checklist)
        self.assertIn(
            u'Where will we locate the operations centre?',
            action_checklist
        )
        self.assertIn(
            u'Where will we locate warehouse and/or distribution '
            u'centres?',
            action_checklist
        )
        self.assertNotIn(
            u'Where will the students from the 0 closed schools go to study?',
            action_checklist
        )
        self.assertNotIn(
            u'Where will the patients from the %s closed hospitals '
            u'go for treatment and how will we transport them?',
            action_checklist)

    def test_0003_impact_summary(self):
        """Test the buildings impact summary."""
        impact_summary = self.building_mixin.impact_summary()
        impact_summary = impact_summary.to_text()

        self.assertIn(
            u'**Hazard Level 2**, 12,050, 1,324,567,000', impact_summary)
        self.assertIn(
            u'**Hazard Level 1**, 1,027, 21,284,567,111', impact_summary)
        self.assertIn(u'**Affected buildings**, 13,077', impact_summary)
        self.assertIn(u'**Not affected buildings**, 7,036', impact_summary)
        self.assertIn(u'**Total**, 20,113', impact_summary)

    def test_0004_buildings_breakdown(self):
        """Test the buildings breakdown."""
        buildings_breakdown = self.building_mixin.buildings_breakdown()
        buildings_breakdown = buildings_breakdown.to_text()
        self.assertIn(u'**Religious**, 0, 1, 2, **3**', buildings_breakdown)
        self.assertIn(
            u'**Residential**, 12,000, 1,000, 7,000, **20,000**',
            buildings_breakdown)
        self.assertIn(u'**School**, 50, 25, 25, **100**', buildings_breakdown)
        self.assertIn(u'**University**, 0, 1, 9, **10**', buildings_breakdown)

    def test_0005_schools_closed(self):
        """Test schools closed as expected."""
        schools_closed_default = self.building_mixin_blank.schools_closed
        schools_closed = self.building_mixin.schools_closed
        message = 'Default should not have any closed schools.'
        self.assertEqual(schools_closed_default, 0, message)
        message = 'Schools closed in scenario not as expected.'
        self.assertEqual(schools_closed, 75, message)

    def test_0006_hospitals_closed(self):
        """Test hospitals closed as expected."""
        hospitals_closed_default = self.building_mixin_blank.hospitals_closed
        hospitals_closed = self.building_mixin.hospitals_closed
        message = 'Default should not have any closed hospitals.'
        self.assertEqual(hospitals_closed_default, 0, message)
        message = 'Hospitals closed in scenario not as expected.'
        self.assertEqual(hospitals_closed, 0, message)

    def test_0007_general_methods(self):
        """Test general methods."""
        default_count_usage = self.building_mixin_blank._count_usage('School')
        message = 'Default count is not as expected.'
        self.assertEqual(default_count_usage, 0, message)
        count_usage = self.building_mixin._count_usage('School')
        message = 'Count is not as expected.'
        self.assertEqual(count_usage, 75, message)
        default_impact_breakdown = self.building_mixin_blank._impact_breakdown
        message = 'The default impact breakdown should be empty.'
        self.assertListEqual(default_impact_breakdown, [], message)
        impact_breakdown = self.building_mixin._impact_breakdown
        message = 'The default impact breakdown should be empty.'
        self.assertListEqual(impact_breakdown, ['Affected', 'Value'], message)
        default_categories = self.building_mixin_blank._affected_categories
        message = 'The default categories should be empty.'
        self.assertListEqual(default_categories, [], message)
        categories = self.building_mixin._affected_categories
        message = 'The categories are not as expected.'
        self.assertListEqual(
            categories,
            ['Hazard Level 2', 'Hazard Level 1'],
            message)

    def test_0008_building_counts(self):
        """Test the building counts."""
        default_affected = self.building_mixin_blank.total_affected_buildings
        default_unaffected = (
            self.building_mixin_blank.total_unaffected_buildings)
        default_total = self.building_mixin_blank.total_buildings
        message = 'Defaults counts should be 0.'
        self.assertEqual(default_total, 0, message)
        self.assertEqual(default_unaffected, 0, message)
        self.assertEqual(default_affected, 0, message)
        affected = self.building_mixin.total_affected_buildings
        unaffected = self.building_mixin.total_unaffected_buildings
        total = self.building_mixin.total_buildings
        message = (
            'The total number of buildings should equal the sum of affected '
            'and unaffected.')
        self.assertEqual(total, unaffected + affected, message)
        message = 'The total number of buildings is not as expected.'
        self.assertEqual(total, 20113, message)
        message = 'The affected number of buildings is not as expected.'
        self.assertEqual(affected, 13077, message)
        message = 'The unaffected number of buildings is not as expected.'
        self.assertEqual(unaffected, 7036, message)

    def test_0009_consolidate_to_other(self):
        """Test consolidating smaller building types to 'other'."""
        total_buildings_before = self.building_mixin.total_buildings
        affected_other_before = self.building_mixin._count_usage('Other')
        message = (
            'There should be no affected buildings of type other before '
            'consolidating.')
        self.assertEqual(affected_other_before, 0, message)
        other_in_buildings_before = (
            'Other' in self.building_mixin.buildings.keys())
        message = (
            'There should be no buildings of type other before '
            'consolidation.')
        self.assertFalse(other_in_buildings_before, message)
        self.building_mixin._consolidate_to_other()
        total_buildings_after = self.building_mixin.total_buildings
        message = (
            'The total number of buildings should remain the same '
            'even after consolidation.')
        self.assertEqual(
            total_buildings_before,
            total_buildings_after,
            message)
        affected_other_after = self.building_mixin._count_usage('Other')
        message = 'The affected other buildings are not as expected.'
        self.assertEqual(affected_other_after, 2, message)
        other_in_buildings_after = (
            'Other' in self.building_mixin.buildings.keys())
        message = 'The type other should be in buildings.'
        self.assertTrue(other_in_buildings_after, message)
        total_other_after = self.building_mixin.buildings['Other']
        message = (
            'The total number of other after consolidation is '
            'not as expected.')
        self.assertEqual(total_other_after, 13, message)
Example #22
0
 def __init__(self):
     super(ClassifiedRasterHazardBuildingFunction, self).__init__()
     BuildingExposureReportMixin.__init__(self)
     self.affected_field = 'affected'
     self.target_field = 'hazard'
Example #23
0
 def __init__(self):
     """Constructor (calls ctor of base class)."""
     super(FloodRasterBuildingFunction, self).__init__()
     BuildingExposureReportMixin.__init__(self)
Example #24
0
 def __init__(self):
     super(VolcanoPolygonBuildingFunction, self).__init__()
     BuildingExposureReportMixin.__init__(self)
     # A set of volcano names
     self.volcano_names = set()
     self._target_field = 'Hazard'