|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""QGIS Unit tests for QgsMetadataWidget. |
| 3 | +
|
| 4 | +Run with: ctest -V -R QgsMetadataWidget |
| 5 | +
|
| 6 | +.. note:: This program is free software; you can redistribute it and/or modify |
| 7 | +it under the terms of the GNU General Public License as published by |
| 8 | +the Free Software Foundation; either version 2 of the License, or |
| 9 | +(at your option) any later version. |
| 10 | +""" |
| 11 | +__author__ = 'Nyall Dawson' |
| 12 | +__date__ = '20/03/2018' |
| 13 | +__copyright__ = 'Copyright 2018, The QGIS Project' |
| 14 | +# This will get replaced with a git SHA1 when you do a git archive |
| 15 | +__revision__ = '$Format:%H$' |
| 16 | + |
| 17 | +import qgis # NOQA |
| 18 | + |
| 19 | +from qgis.PyQt.QtXml import QDomDocument |
| 20 | + |
| 21 | +from qgis.core import (QgsCoordinateReferenceSystem, |
| 22 | + QgsAbstractMetadataBase, |
| 23 | + QgsLayerMetadata, |
| 24 | + QgsProjectMetadata, |
| 25 | + QgsBox3d, |
| 26 | + QgsDateTimeRange) |
| 27 | +from qgis.gui import (QgsMetadataWidget) |
| 28 | +from qgis.PyQt.QtCore import (QDate, |
| 29 | + QTime, |
| 30 | + QDateTime) |
| 31 | +from qgis.testing import start_app, unittest |
| 32 | + |
| 33 | +start_app() |
| 34 | + |
| 35 | + |
| 36 | +class TestQgsMetadataWidget(unittest.TestCase): |
| 37 | + |
| 38 | + def testLayerMode(self): |
| 39 | + """ |
| 40 | + Create a fully populated QgsLayerMetadata object, then set it to the widget and re-read back |
| 41 | + the generated metadata to ensure that no content is lost. |
| 42 | + """ |
| 43 | + w = QgsMetadataWidget() |
| 44 | + |
| 45 | + m = QgsLayerMetadata() |
| 46 | + m.setIdentifier('1234') |
| 47 | + m.setParentIdentifier('xyz') |
| 48 | + m.setLanguage('en-CA') |
| 49 | + m.setType('dataset') |
| 50 | + m.setTitle('roads') |
| 51 | + m.setAbstract('my roads') |
| 52 | + m.setFees('None') |
| 53 | + m.setConstraints([QgsLayerMetadata.Constraint('None', 'access')]) |
| 54 | + m.setRights(['Copyright foo 2017']) |
| 55 | + m.setLicenses(['WTFPL']) |
| 56 | + m.setHistory(['history a', 'history b']) |
| 57 | + m.setKeywords({ |
| 58 | + 'GEMET': ['kw1', 'kw2'], |
| 59 | + 'gmd:topicCategory': ['natural'], |
| 60 | + }) |
| 61 | + #m.setEncoding('utf-8') |
| 62 | + m.setCrs(QgsCoordinateReferenceSystem.fromOgcWmsCrs('EPSG:4326')) |
| 63 | + |
| 64 | + e = QgsLayerMetadata.Extent() |
| 65 | + se = QgsLayerMetadata.SpatialExtent() |
| 66 | + se.extentCrs = QgsCoordinateReferenceSystem.fromOgcWmsCrs('EPSG:4326') |
| 67 | + se.bounds = QgsBox3d(-180, -90, 0, 180, 90, 0) |
| 68 | + e.setSpatialExtents([se]) |
| 69 | + dates = [ |
| 70 | + QgsDateTimeRange( |
| 71 | + QDateTime(QDate(2001, 12, 17), QTime(9, 30, 47)), |
| 72 | + QDateTime(QDate(2001, 12, 17), QTime(9, 30, 47))) |
| 73 | + ] |
| 74 | + e.setTemporalExtents(dates) |
| 75 | + m.setExtent(e) |
| 76 | + |
| 77 | + c = QgsLayerMetadata.Contact() |
| 78 | + c.name = 'John Smith' |
| 79 | + c.organization = 'ACME' |
| 80 | + c.position = 'staff' |
| 81 | + c.voice = '1500 515 555' |
| 82 | + c.fax = 'xx.xxx.xxx.xxxx' |
| 83 | + c.email = 'foo@example.org' |
| 84 | + c.role = 'pointOfContact' |
| 85 | + address = QgsLayerMetadata.Address() |
| 86 | + address.type = 'postal' |
| 87 | + address.address = '123 Main Street' |
| 88 | + address.city = 'anycity' |
| 89 | + address.administrativeArea = 'anyprovince' |
| 90 | + address.postalCode = '90210' |
| 91 | + address.country = 'Canada' |
| 92 | + c.addresses = [address] |
| 93 | + m.setContacts([c]) |
| 94 | + |
| 95 | + l = QgsLayerMetadata.Link() |
| 96 | + l.name = 'geonode:roads' |
| 97 | + l.type = 'OGC:WMS' |
| 98 | + l.description = 'my GeoNode road layer' |
| 99 | + l.url = 'http://example.org/wms' |
| 100 | + |
| 101 | + l2 = QgsLayerMetadata.Link() |
| 102 | + l2.name = 'geonode:roads' |
| 103 | + l2.type = 'OGC:WFS' |
| 104 | + l2.description = 'my GeoNode road layer' |
| 105 | + l2.url = 'http://example.org/wfs' |
| 106 | + |
| 107 | + l3 = QgsLayerMetadata.Link() |
| 108 | + l3.name = 'roads' |
| 109 | + l3.type = 'WWW:LINK' |
| 110 | + l3.description = 'full dataset download' |
| 111 | + l3.url = 'http://example.org/roads.tgz' |
| 112 | + l3.format = 'ESRI Shapefile' |
| 113 | + l3.mimeType = 'application/gzip' |
| 114 | + l3.size = '283676' |
| 115 | + |
| 116 | + m.setLinks([l, l2, l3]) |
| 117 | + |
| 118 | + # set widget metadata |
| 119 | + w.setMetadata(m) |
| 120 | + self.assertEqual(w.mode(), QgsMetadataWidget.LayerMetadata) |
| 121 | + |
| 122 | + m = w.metadata() |
| 123 | + self.assertIsInstance(m, QgsLayerMetadata) |
| 124 | + |
| 125 | + self.assertEqual(m.identifier(), '1234') |
| 126 | + self.assertEqual(m.parentIdentifier(), 'xyz') |
| 127 | + self.assertEqual(m.language(), 'en-CA') |
| 128 | + self.assertEqual(m.type(), 'dataset') |
| 129 | + self.assertEqual(m.title(), 'roads') |
| 130 | + self.assertEqual(m.abstract(), 'my roads') |
| 131 | + self.assertEqual(m.fees(), 'None') |
| 132 | + self.assertEqual(m.constraints()[0].constraint, 'None') |
| 133 | + self.assertEqual(m.constraints()[0].type, 'access') |
| 134 | + self.assertEqual(m.rights(), ['Copyright foo 2017']) |
| 135 | + self.assertEqual(m.licenses(), ['WTFPL']) |
| 136 | + self.assertEqual(m.history(), ['history a', 'history b']) |
| 137 | + #self.assertEqual(m.encoding(), 'utf-8') |
| 138 | + self.assertEqual( |
| 139 | + m.keywords(), |
| 140 | + {'GEMET': ['kw1', 'kw2'], 'gmd:topicCategory': ['natural']}) |
| 141 | + self.assertEqual(m.crs().authid(), 'EPSG:4326') |
| 142 | + |
| 143 | + extent = m.extent().spatialExtents()[0] |
| 144 | + self.assertEqual(extent.extentCrs.authid(), 'EPSG:4326') |
| 145 | + self.assertEqual(extent.bounds.xMinimum(), -180.0) |
| 146 | + self.assertEqual(extent.bounds.yMinimum(), -90.0) |
| 147 | + self.assertEqual(extent.bounds.xMaximum(), 180.0) |
| 148 | + self.assertEqual(extent.bounds.yMaximum(), 90.0) |
| 149 | + self.assertEqual(m.extent().temporalExtents()[0].begin(), QDateTime(QDate(2001, 12, 17), QTime(9, 30, 47))) |
| 150 | + self.assertTrue(m.extent().temporalExtents()[0].isInstant()) |
| 151 | + |
| 152 | + self.assertEqual(m.contacts()[0].name, 'John Smith') |
| 153 | + self.assertEqual(m.contacts()[0].organization, 'ACME') |
| 154 | + self.assertEqual(m.contacts()[0].position, 'staff') |
| 155 | + self.assertEqual(m.contacts()[0].voice, '1500 515 555') |
| 156 | + self.assertEqual(m.contacts()[0].fax, 'xx.xxx.xxx.xxxx') |
| 157 | + self.assertEqual(m.contacts()[0].email, 'foo@example.org') |
| 158 | + self.assertEqual(m.contacts()[0].role, 'pointOfContact') |
| 159 | + self.assertEqual(m.contacts()[0].addresses[0].type, 'postal') |
| 160 | + self.assertEqual(m.contacts()[0].addresses[0].address, '123 Main Street') |
| 161 | + self.assertEqual(m.contacts()[0].addresses[0].city, 'anycity') |
| 162 | + self.assertEqual(m.contacts()[0].addresses[0].administrativeArea, 'anyprovince') |
| 163 | + self.assertEqual(m.contacts()[0].addresses[0].postalCode, '90210') |
| 164 | + self.assertEqual(m.contacts()[0].addresses[0].country, 'Canada') |
| 165 | + self.assertEqual(m.links()[0].name, 'geonode:roads') |
| 166 | + self.assertEqual(m.links()[0].type, 'OGC:WMS') |
| 167 | + self.assertEqual(m.links()[0].description, 'my GeoNode road layer') |
| 168 | + self.assertEqual(m.links()[0].url, 'http://example.org/wms') |
| 169 | + self.assertEqual(m.links()[1].name, 'geonode:roads') |
| 170 | + self.assertEqual(m.links()[1].type, 'OGC:WFS') |
| 171 | + self.assertEqual(m.links()[1].description, 'my GeoNode road layer') |
| 172 | + self.assertEqual(m.links()[1].url, 'http://example.org/wfs') |
| 173 | + self.assertEqual(m.links()[2].name, 'roads') |
| 174 | + self.assertEqual(m.links()[2].type, 'WWW:LINK') |
| 175 | + self.assertEqual(m.links()[2].description, 'full dataset download') |
| 176 | + self.assertEqual(m.links()[2].url, 'http://example.org/roads.tgz') |
| 177 | + self.assertEqual(m.links()[2].format, 'ESRI Shapefile') |
| 178 | + self.assertEqual(m.links()[2].mimeType, 'application/gzip') |
| 179 | + self.assertEqual(m.links()[2].size, '283676') |
| 180 | + |
| 181 | + def testProjectMode(self): |
| 182 | + """ |
| 183 | + Create a fully populated QgsProjectMetadata object, then set it to the widget and re-read back |
| 184 | + the generated metadata to ensure that no content is lost. |
| 185 | + """ |
| 186 | + w = QgsMetadataWidget() |
| 187 | + |
| 188 | + m = QgsProjectMetadata() |
| 189 | + m.setIdentifier('1234') |
| 190 | + m.setParentIdentifier('xyz') |
| 191 | + m.setLanguage('en-CA') |
| 192 | + m.setType('project') |
| 193 | + m.setTitle('roads') |
| 194 | + m.setAbstract('my roads') |
| 195 | + m.setHistory(['history a', 'history b']) |
| 196 | + m.setKeywords({ |
| 197 | + 'GEMET': ['kw1', 'kw2'], |
| 198 | + 'gmd:topicCategory': ['natural'], |
| 199 | + }) |
| 200 | + |
| 201 | + c = QgsAbstractMetadataBase.Contact() |
| 202 | + c.name = 'John Smith' |
| 203 | + c.organization = 'ACME' |
| 204 | + c.position = 'staff' |
| 205 | + c.voice = '1500 515 555' |
| 206 | + c.fax = 'xx.xxx.xxx.xxxx' |
| 207 | + c.email = 'foo@example.org' |
| 208 | + c.role = 'pointOfContact' |
| 209 | + address = QgsAbstractMetadataBase.Address() |
| 210 | + address.type = 'postal' |
| 211 | + address.address = '123 Main Street' |
| 212 | + address.city = 'anycity' |
| 213 | + address.administrativeArea = 'anyprovince' |
| 214 | + address.postalCode = '90210' |
| 215 | + address.country = 'Canada' |
| 216 | + c.addresses = [address] |
| 217 | + m.setContacts([c]) |
| 218 | + |
| 219 | + l = QgsAbstractMetadataBase.Link() |
| 220 | + l.name = 'geonode:roads' |
| 221 | + l.type = 'OGC:WMS' |
| 222 | + l.description = 'my GeoNode road layer' |
| 223 | + l.url = 'http://example.org/wms' |
| 224 | + |
| 225 | + l2 = QgsAbstractMetadataBase.Link() |
| 226 | + l2.name = 'geonode:roads' |
| 227 | + l2.type = 'OGC:WFS' |
| 228 | + l2.description = 'my GeoNode road layer' |
| 229 | + l2.url = 'http://example.org/wfs' |
| 230 | + |
| 231 | + l3 = QgsAbstractMetadataBase.Link() |
| 232 | + l3.name = 'roads' |
| 233 | + l3.type = 'WWW:LINK' |
| 234 | + l3.description = 'full dataset download' |
| 235 | + l3.url = 'http://example.org/roads.tgz' |
| 236 | + l3.format = 'ESRI Shapefile' |
| 237 | + l3.mimeType = 'application/gzip' |
| 238 | + l3.size = '283676' |
| 239 | + |
| 240 | + m.setLinks([l, l2, l3]) |
| 241 | + |
| 242 | + m.setAuthor('my author') |
| 243 | + m.setCreationDateTime(QDateTime(QDate(2001, 12, 17), QTime(9, 30, 47))) |
| 244 | + |
| 245 | + # set widget metadata |
| 246 | + w.setMetadata(m) |
| 247 | + self.assertEqual(w.mode(), QgsMetadataWidget.ProjectMetadata) |
| 248 | + |
| 249 | + m = w.metadata() |
| 250 | + self.assertIsInstance(m, QgsProjectMetadata) |
| 251 | + |
| 252 | + self.assertEqual(m.identifier(), '1234') |
| 253 | + self.assertEqual(m.parentIdentifier(), 'xyz') |
| 254 | + self.assertEqual(m.language(), 'en-CA') |
| 255 | + self.assertEqual(m.type(), 'project') |
| 256 | + self.assertEqual(m.title(), 'roads') |
| 257 | + self.assertEqual(m.abstract(), 'my roads') |
| 258 | + self.assertEqual(m.history(), ['history a', 'history b']) |
| 259 | + self.assertEqual( |
| 260 | + m.keywords(), |
| 261 | + {'GEMET': ['kw1', 'kw2'], 'gmd:topicCategory': ['natural']}) |
| 262 | + |
| 263 | + self.assertEqual(m.contacts()[0].name, 'John Smith') |
| 264 | + self.assertEqual(m.contacts()[0].organization, 'ACME') |
| 265 | + self.assertEqual(m.contacts()[0].position, 'staff') |
| 266 | + self.assertEqual(m.contacts()[0].voice, '1500 515 555') |
| 267 | + self.assertEqual(m.contacts()[0].fax, 'xx.xxx.xxx.xxxx') |
| 268 | + self.assertEqual(m.contacts()[0].email, 'foo@example.org') |
| 269 | + self.assertEqual(m.contacts()[0].role, 'pointOfContact') |
| 270 | + self.assertEqual(m.contacts()[0].addresses[0].type, 'postal') |
| 271 | + self.assertEqual(m.contacts()[0].addresses[0].address, '123 Main Street') |
| 272 | + self.assertEqual(m.contacts()[0].addresses[0].city, 'anycity') |
| 273 | + self.assertEqual(m.contacts()[0].addresses[0].administrativeArea, 'anyprovince') |
| 274 | + self.assertEqual(m.contacts()[0].addresses[0].postalCode, '90210') |
| 275 | + self.assertEqual(m.contacts()[0].addresses[0].country, 'Canada') |
| 276 | + self.assertEqual(m.links()[0].name, 'geonode:roads') |
| 277 | + self.assertEqual(m.links()[0].type, 'OGC:WMS') |
| 278 | + self.assertEqual(m.links()[0].description, 'my GeoNode road layer') |
| 279 | + self.assertEqual(m.links()[0].url, 'http://example.org/wms') |
| 280 | + self.assertEqual(m.links()[1].name, 'geonode:roads') |
| 281 | + self.assertEqual(m.links()[1].type, 'OGC:WFS') |
| 282 | + self.assertEqual(m.links()[1].description, 'my GeoNode road layer') |
| 283 | + self.assertEqual(m.links()[1].url, 'http://example.org/wfs') |
| 284 | + self.assertEqual(m.links()[2].name, 'roads') |
| 285 | + self.assertEqual(m.links()[2].type, 'WWW:LINK') |
| 286 | + self.assertEqual(m.links()[2].description, 'full dataset download') |
| 287 | + self.assertEqual(m.links()[2].url, 'http://example.org/roads.tgz') |
| 288 | + self.assertEqual(m.links()[2].format, 'ESRI Shapefile') |
| 289 | + self.assertEqual(m.links()[2].mimeType, 'application/gzip') |
| 290 | + self.assertEqual(m.links()[2].size, '283676') |
| 291 | + |
| 292 | + self.assertEqual(m.author(), 'my author') |
| 293 | + self.assertEqual(m.creationDateTime(), QDateTime(QDate(2001, 12, 17), QTime(9, 30, 47))) |
| 294 | + |
| 295 | + |
| 296 | +if __name__ == '__main__': |
| 297 | + unittest.main() |
0 commit comments