/
test_osm_downloader_dialog.py
165 lines (134 loc) · 6.23 KB
/
test_osm_downloader_dialog.py
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
103
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# coding=utf-8
"""
InaSAFE Disaster risk assessment tool developed by AusAid and World Bank
- **Import Dialog Test Cases.**
Contact : ole.moller.nielsen@gmail.com
.. note:: This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
"""
__author__ = 'bungcip@gmail.com'
__date__ = '05/02/2013'
__copyright__ = ('Copyright 2013, Australia Indonesia Facility for '
'Disaster Reduction')
# noinspection PyUnresolvedReferences
import unittest
import logging
import os
import tempfile
import shutil
# This import is to enable SIP API V2
# noinspection PyUnresolvedReferences
import qgis # pylint: disable=unused-import
from safe.gui.tools.osm_downloader_dialog import OsmDownloaderDialog
from safe.test.utilities import get_qgis_app
QGIS_APP, CANVAS, IFACE, PARENT = get_qgis_app()
LOGGER = logging.getLogger('InaSAFE')
class OsmDownloaderDialogTest(unittest.TestCase):
"""Test Osm Downloader Dialog widget
.. versionchanged:: 3.2
"""
# noinspection PyPep8Naming
def setUp(self):
"""Runs before each test."""
self.dialog = OsmDownloaderDialog(PARENT, IFACE)
def test_checked_features(self):
"""Test checked features"""
self.dialog.roads_checkBox.setChecked(False)
self.dialog.buildings_checkBox.setChecked(False)
self.dialog.building_points_checkBox.setChecked(False)
self.dialog.potential_idp_checkBox.setChecked(False)
self.dialog.boundary_checkBox.setChecked(False)
expected = []
get = self.dialog.get_checked_features()
self.assertItemsEqual(expected, get)
self.dialog.roads_checkBox.setChecked(True)
self.dialog.buildings_checkBox.setChecked(True)
self.dialog.building_points_checkBox.setChecked(True)
self.dialog.potential_idp_checkBox.setChecked(True)
self.dialog.boundary_checkBox.setChecked(False)
expected = ['roads', 'buildings', 'building-points', 'potential-idp']
get = self.dialog.get_checked_features()
self.assertItemsEqual(expected, get)
admin_level = 6
self.dialog.admin_level_comboBox.setCurrentIndex(admin_level - 1)
self.dialog.roads_checkBox.setChecked(False)
self.dialog.buildings_checkBox.setChecked(True)
self.dialog.building_points_checkBox.setChecked(True)
self.dialog.potential_idp_checkBox.setChecked(True)
self.dialog.boundary_checkBox.setChecked(True)
expected = [
'buildings', 'building-points', 'potential-idp', 'boundary-6']
get = self.dialog.get_checked_features()
self.assertItemsEqual(expected, get)
def test_detect_country(self):
"""Test if the country is well detected according to the extent."""
# Extent in Zimbabwe.
self.dialog.update_extent([29.4239, -18.2391, 29.4676, -18.2068])
index = self.dialog.country_comboBox.currentIndex()
country = self.dialog.country_comboBox.itemText(index)
self.assertTrue(country == 'Zimbabwe')
# Extent in Indonesia.
self.dialog.update_extent([106.7741, -6.2609, 106.8874, -6.1859])
index = self.dialog.country_comboBox.currentIndex()
country = self.dialog.country_comboBox.itemText(index)
self.assertTrue(country == 'Indonesia')
# Extent in the middle of nowhere in the Indian Ocean, default value.
self.dialog.update_extent([75.0586, -31.7477, 75.8867, -30.9022])
index = self.dialog.country_comboBox.currentIndex()
country = self.dialog.country_comboBox.itemText(index)
self.assertTrue(country == 'Afghanistan')
def test_populate_countries(self):
"""Test if items are in the combobox.
For instance every admin_level from 1 to 11 and
the first and last country (alphabetical order)."""
self.assertTrue(self.dialog.admin_level_comboBox.count() == 11)
self.assertTrue(
self.dialog.country_comboBox.itemText(0) == 'Afghanistan')
nb_items = self.dialog.country_comboBox.count()
self.assertTrue(
self.dialog.country_comboBox.itemText(nb_items - 1) == 'Zimbabwe')
def test_admin_level_helper(self):
"""Test the helper by setting a country and an admin level."""
admin_level = 8
country = 'Indonesia'
expected = \
'<span style=" font-size:12pt; font-style:italic;">' \
'level 8 is : Community Group (Rukun Warga)</span>'
self.dialog.admin_level_comboBox.setCurrentIndex(admin_level - 1)
index = self.dialog.country_comboBox.findText(country)
self.dialog.country_comboBox.setCurrentIndex(index)
self.assertEquals(expected, self.dialog.boundary_helper.text())
admin_level = 6
country = 'Madagascar'
expected = \
'<span style=" font-size:12pt; font-style:italic;">' \
'level 6 is : Distrika (districts)</span>'
self.dialog.admin_level_comboBox.setCurrentIndex(admin_level - 1)
index = self.dialog.country_comboBox.findText(country)
self.dialog.country_comboBox.setCurrentIndex(index)
self.assertEquals(expected, self.dialog.boundary_helper.text())
def test_suffix_extracting_shapefile(self):
"""Test existing files method."""
path = tempfile.mkdtemp('existing-files')
# Create some files
one_file = os.path.join(path, 'a.txt')
open(one_file, 'a').close()
other_file = os.path.join(path, 'a-1.txt')
open(other_file, 'a').close()
message = "Index for existing files is wrong."
result = self.dialog.get_unique_file_path_suffix(one_file)
assert result == 2, message
os.remove(other_file)
result = self.dialog.get_unique_file_path_suffix(one_file)
assert result == 1, message
os.remove(one_file)
result = self.dialog.get_unique_file_path_suffix(one_file)
assert result == 0, message
# cleanup
shutil.rmtree(path)
if __name__ == '__main__':
suite = unittest.makeSuite(OsmDownloaderDialogTest, 'test')
runner = unittest.TextTestRunner(verbosity=2)
runner.run(suite)