test_configuration.py 7.03 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/env python3

# Copyright (C) 2022:
#   Helmholtz-Zentrum Potsdam Deutsches GeoForschungsZentrum GFZ
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
# General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see http://www.gnu.org/licenses/.

import os
import pytest
from gdecore.configuration import Configuration


def test_Configuration():
    # Test the non-trivial case using a test data file
    returned_config = Configuration(
        os.path.join(os.path.dirname(__file__), "data", "config_for_testing_good.yml"),
        force_config_over_hierarchies=True,
    )
    assert returned_config.model_name == "esrm20"
    assert len(returned_config.database_gde_tiles.keys()) == 4
    assert returned_config.database_gde_tiles["host"] == "host.somewhere.xx"
    assert returned_config.database_gde_tiles["dbname"] == "some_database_name"
    assert returned_config.database_gde_tiles["username"] == "some_username"
    assert returned_config.database_gde_tiles["password"] == "some_password"
36
37
38
39
40
41
42
43
44
    assert returned_config.database_obm_buildings["host"] == "host.somewhere.xx"
    assert returned_config.database_obm_buildings["dbname"] == "some_database_name"
    assert returned_config.database_obm_buildings["username"] == "some_username"
    assert returned_config.database_obm_buildings["password"] == "some_password"
    assert returned_config.database_completeness["host"] == "host.somewhere.xx"
    assert returned_config.database_completeness["dbname"] == "some_database_name"
    assert returned_config.database_completeness["username"] == "some_username"
    assert returned_config.database_completeness["password"] == "some_password"
    assert returned_config.database_completeness["sourceid"] == 1
45
46
47
    assert len(returned_config.exposure_entities_to_run) == 1
    assert returned_config.exposure_entities_to_run[0] == "Italy"
    assert returned_config.exposure_entities_code == "ISO3"
48
49
50
    assert len(returned_config.occupancies_to_run) == 2
    assert returned_config.occupancies_to_run[0] == "residential"
    assert returned_config.occupancies_to_run[1] == "commercial"
51
    assert returned_config.number_cores == 1
52
53
54
55
56
57
58
59

    # Test case in which the file is not found
    with pytest.raises(OSError) as excinfo:
        returned_config = Configuration(
            os.path.join(os.path.dirname(__file__), "data", "doesnotexist.yml"),
            force_config_over_hierarchies=True,
        )
    assert "OSError" in str(excinfo.type)
60

61
62
63
64
65
66
67
68
    # Test case in which a parameter is missing
    with pytest.raises(OSError) as excinfo:
        returned_config = Configuration(
            os.path.join(os.path.dirname(__file__), "data", "config_for_testing_missing.yml"),
            force_config_over_hierarchies=True,
        )
    assert "OSError" in str(excinfo.type)

69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102

def test_Configuration_interpret_exposure_entities_to_run(test_db):
    returned_config = Configuration(
        os.path.join(os.path.dirname(__file__), "data", "config_for_testing_good.yml"),
        force_config_over_hierarchies=False,
    )

    # One country name provided, 'exposure_entities_code' is ISO3
    returned_config.interpret_exposure_entities_to_run()
    assert len(returned_config.exposure_entities_to_run) == 1
    assert returned_config.exposure_entities_to_run[0] == "ITA"

    # Several country names provided, 'exposure_entities_code' is ISO3
    returned_config.exposure_entities_to_run = ["Italy", "France", "Portugal"]
    returned_config.interpret_exposure_entities_to_run()
    assert len(returned_config.exposure_entities_to_run) == 3
    assert returned_config.exposure_entities_to_run[0] == "ITA"
    assert returned_config.exposure_entities_to_run[1] == "FRA"
    assert returned_config.exposure_entities_to_run[2] == "PRT"

    # Several country names provided, one does not exist, 'exposure_entities_code' is ISO3
    returned_config.exposure_entities_to_run = ["Italy", "France", "England"]
    returned_config.interpret_exposure_entities_to_run()
    assert len(returned_config.exposure_entities_to_run) == 2
    assert returned_config.exposure_entities_to_run[0] == "ITA"
    assert returned_config.exposure_entities_to_run[1] == "FRA"

    # A name of a TXT file is provided, 'exposure_entities_code' is ISO3
    returned_config.exposure_entities_to_run = [
        os.path.join(os.path.dirname(__file__), "data", "exposure_entities.txt")
    ]
    returned_config.interpret_exposure_entities_to_run()
    assert len(returned_config.exposure_entities_to_run) == 3
    assert returned_config.exposure_entities_to_run[0] == "ESP"
103
    assert returned_config.exposure_entities_to_run[1] == "CHE"
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
    assert returned_config.exposure_entities_to_run[2] == "DEU"

    # A name of a CSV file is provided, 'exposure_entities_code' is ISO3
    returned_config.exposure_entities_to_run = [
        os.path.join(os.path.dirname(__file__), "data", "exposure_entities.csv")
    ]
    returned_config.interpret_exposure_entities_to_run()
    assert len(returned_config.exposure_entities_to_run) == 3
    assert returned_config.exposure_entities_to_run[0] == "GBR"
    assert returned_config.exposure_entities_to_run[1] == "LTU"
    assert returned_config.exposure_entities_to_run[2] == "GRC"

    # "all" is provided, 'exposure_entities_code' is ISO3
    returned_config.exposure_entities_to_run = ["all"]
    returned_config.interpret_exposure_entities_to_run(2)
    assert len(returned_config.exposure_entities_to_run) == 2
    assert returned_config.exposure_entities_to_run[0] == "ABC"
    assert returned_config.exposure_entities_to_run[1] == "DEF"

    # A name of a TXT file is provided, 'exposure_entities_code' is a dictionary
    returned_config.exposure_entities_to_run = [
        os.path.join(os.path.dirname(__file__), "data", "exposure_entities_non_countries.txt")
    ]
    returned_config.exposure_entities_code = {
        "Entity1": "EN1",
        "Entity2": "ET2",
    }
    returned_config.interpret_exposure_entities_to_run()
    assert len(returned_config.exposure_entities_to_run) == 2
    assert returned_config.exposure_entities_to_run[0] == "EN1"
    assert returned_config.exposure_entities_to_run[1] == "ET2"

    # A name of a TXT file is provided, 'exposure_entities_code' is a dictionary, one missing
    returned_config.exposure_entities_to_run = [
        os.path.join(os.path.dirname(__file__), "data", "exposure_entities_non_countries.txt")
    ]
    returned_config.exposure_entities_code = {
        "Entity1": "EN1",
        "Entity3": "ET3",
    }
    returned_config.interpret_exposure_entities_to_run()
    assert len(returned_config.exposure_entities_to_run) == 1
    assert returned_config.exposure_entities_to_run[0] == "EN1"