Skip to content
Permalink
Browse files

Create unit test for QgsMetadataBase

  • Loading branch information
nyalldawson committed Mar 22, 2018
1 parent 5cf2656 commit a96d59a1a623700de44f7c361e9fcd72bd01e045
Showing with 516 additions and 218 deletions.
  1. +1 −0 tests/src/python/CMakeLists.txt
  2. +0 −218 tests/src/python/test_qgslayermetadata.py
  3. +515 −0 tests/src/python/test_qgsmetadatabase.py
@@ -109,6 +109,7 @@ ADD_PYTHON_TEST(PyQgsMapRendererCache test_qgsmaprenderercache.py)
ADD_PYTHON_TEST(PyQgsMapThemeCollection test_qgsmapthemecollection.py)
ADD_PYTHON_TEST(PyQgsMapUnitScale test_qgsmapunitscale.py)
ADD_PYTHON_TEST(PyQgsMargins test_qgsmargins.py)
ADD_PYTHON_TEST(PyQgsMetadataBase test_qgsmetadatabase.py)
ADD_PYTHON_TEST(PyQgsMemoryProvider test_provider_memory.py)
ADD_PYTHON_TEST(PyQgsMultiEditToolButton test_qgsmultiedittoolbutton.py)
ADD_PYTHON_TEST(PyQgsNetworkContentFetcher test_qgsnetworkcontentfetcher.py)
@@ -142,105 +142,6 @@ def testEquality(self):
self.assertNotEqual(constraint, QgsLayerMetadata.Constraint('c2', 'type1'))
self.assertNotEqual(constraint, QgsLayerMetadata.Constraint('c', 'type2'))

a = QgsLayerMetadata.Address()
a.type = 'postal'
a.address = '13 north rd'
a.city = 'huxleys haven'
a.administrativeArea = 'land of the queens'
a.postalCode = '4123'
a.country = 'straya!'
a2 = QgsLayerMetadata.Address(a)
self.assertEqual(a, a2)
a2.type = 'postal2'
self.assertNotEqual(a, a2)
a2 = QgsLayerMetadata.Address(a)
a2.address = 'address2'
self.assertNotEqual(a, a2)
a2 = QgsLayerMetadata.Address(a)
a2.city = 'city'
self.assertNotEqual(a, a2)
a2 = QgsLayerMetadata.Address(a)
a2.administrativeArea = 'area2'
self.assertNotEqual(a, a2)
a2 = QgsLayerMetadata.Address(a)
a2.postalCode = 'postal2'
self.assertNotEqual(a, a2)
a2 = QgsLayerMetadata.Address(a)
a2.country = 'country2'
self.assertNotEqual(a, a2)

c = QgsLayerMetadata.Contact()
c.name = 'name'
c.organization = 'org'
c.position = 'pos'
c.voice = '1500 515 555'
c.fax = 'fax'
c.email = 'email'
c.role = 'role'
a = QgsLayerMetadata.Address()
a.type = 'postal'
a2 = QgsLayerMetadata.Address()
a2.type = 'street'
c.addresses = [a, a2]
c2 = QgsLayerMetadata.Contact(c)
self.assertEqual(c, c2)
c2.name = 'name2'
self.assertNotEqual(c, c2)
c2 = QgsLayerMetadata.Contact(c)
c2.organization = 'org2'
self.assertNotEqual(c, c2)
c2 = QgsLayerMetadata.Contact(c)
c2.position = 'pos2'
self.assertNotEqual(c, c2)
c2 = QgsLayerMetadata.Contact(c)
c2.voice = 'voice2'
self.assertNotEqual(c, c2)
c2 = QgsLayerMetadata.Contact(c)
c2.fax = 'fax2'
self.assertNotEqual(c, c2)
c2 = QgsLayerMetadata.Contact(c)
c2.email = 'email2'
self.assertNotEqual(c, c2)
c2 = QgsLayerMetadata.Contact(c)
c2.role = 'role2'
self.assertNotEqual(c, c2)
c2 = QgsLayerMetadata.Contact(c)
c2.addresses = [a2]
self.assertNotEqual(c, c2)

# link
l = QgsLayerMetadata.Link()
l.name = 'name'
l.type = 'type'
l.description = 'desc'
l.url = 'url'
l.format = 'format'
l.mimeType = 'mime'
l.size = '112'
l2 = QgsLayerMetadata.Link(l)
self.assertEqual(l, l2)
l2 = QgsLayerMetadata.Link(l)
l2.name = 'name2'
self.assertNotEqual(l, l2)
l2 = QgsLayerMetadata.Link(l)
l2.type = 'type2'
self.assertNotEqual(l, l2)
l2 = QgsLayerMetadata.Link(l)
l2.description = 'desc2'
self.assertNotEqual(l, l2)
l2 = QgsLayerMetadata.Link(l)
l2.url = 'url2'
self.assertNotEqual(l, l2)
l2 = QgsLayerMetadata.Link(l)
l2.format = 'format2'
self.assertNotEqual(l, l2)
l2 = QgsLayerMetadata.Link(l)
l2.mimeType = 'mime2'
self.assertNotEqual(l, l2)
l2 = QgsLayerMetadata.Link(l)
l2.size = '113'
self.assertNotEqual(l, l2)

def testExtent(self):
e = QgsLayerMetadata.Extent()
se = QgsLayerMetadata.SpatialExtent()
@@ -263,123 +164,6 @@ def testExtent(self):
self.assertEqual(m.extent().temporalExtents()[0].begin(), QDateTime(QDate(2017, 1, 3), QTime(11, 34, 56)))
self.assertEqual(m.extent().temporalExtents()[0].end(), QDateTime(QDate(2018, 1, 3), QTime(12, 35, 57)))

def testKeywords(self):
m = QgsLayerMetadata()

m.setKeywords({'gmd:topicCategory': ['natural']})
self.assertEqual(m.keywords(), {'gmd:topicCategory': ['natural']})
self.assertEqual(m.categories(), ['natural'])
self.assertTrue(m.removeKeywords('gmd:topicCategory'))

m.setKeywords({'vocab a': ['keyword a', 'other a'],
'vocab b': ['keyword b', 'other b']})
self.assertEqual(m.keywords(), {'vocab a': ['keyword a', 'other a'],
'vocab b': ['keyword b', 'other b']})
self.assertEqual(m.keywordVocabularies(), ['vocab a', 'vocab b'])
self.assertEqual(m.keywords('vocab a'), ['keyword a', 'other a'])
self.assertEqual(m.keywords('vocab b'), ['keyword b', 'other b'])
self.assertEqual(m.keywords('not valid'), [])

m.addKeywords('vocab c', ['keyword c'])
self.assertEqual(m.keywords(), {'vocab a': ['keyword a', 'other a'],
'vocab b': ['keyword b', 'other b'],
'vocab c': ['keyword c']})
# replace existing using addKeywords
m.addKeywords('vocab c', ['c'])
self.assertEqual(m.keywords(), {'vocab a': ['keyword a', 'other a'],
'vocab b': ['keyword b', 'other b'],
'vocab c': ['c']})
# replace existing using setKeywords
m.setKeywords({'x': ['x'], 'y': ['y']})
self.assertEqual(m.keywords(), {'x': ['x'],
'y': ['y']})

def testAddress(self):
a = QgsLayerMetadata.Address()
a.type = 'postal'
a.address = '13 north rd'
a.city = 'huxleys haven'
a.administrativeArea = 'land of the queens'
a.postalCode = '4123'
a.country = 'straya!'
self.assertEqual(a.type, 'postal')
self.assertEqual(a.address, '13 north rd')
self.assertEqual(a.city, 'huxleys haven')
self.assertEqual(a.administrativeArea, 'land of the queens')
self.assertEqual(a.postalCode, '4123')
self.assertEqual(a.country, 'straya!')

def testContact(self):
c = QgsLayerMetadata.Contact()
c.name = 'Prince Gristle'
c.organization = 'Bergen co'
c.position = 'prince'
c.voice = '1500 515 555'
c.fax = 'who the f*** still uses fax?'
c.email = 'limpbiskitrulez69@hotmail.com'
c.role = 'person to blame when all goes wrong'
a = QgsLayerMetadata.Address()
a.type = 'postal'
a2 = QgsLayerMetadata.Address()
a2.type = 'street'
c.addresses = [a, a2]
self.assertEqual(c.name, 'Prince Gristle')
self.assertEqual(c.organization, 'Bergen co')
self.assertEqual(c.position, 'prince')
self.assertEqual(c.voice, '1500 515 555')
self.assertEqual(c.fax, 'who the f*** still uses fax?')
self.assertEqual(c.email, 'limpbiskitrulez69@hotmail.com')
self.assertEqual(c.role, 'person to blame when all goes wrong')
self.assertEqual(c.addresses[0].type, 'postal')
self.assertEqual(c.addresses[1].type, 'street')

m = QgsLayerMetadata()
c2 = QgsLayerMetadata.Contact(c)
c2.name = 'Bridgette'

m.setContacts([c, c2])
self.assertEqual(m.contacts()[0].name, 'Prince Gristle')
self.assertEqual(m.contacts()[1].name, 'Bridgette')

# add contact
c3 = QgsLayerMetadata.Contact(c)
c3.name = 'Princess Poppy'
m.addContact(c3)
self.assertEqual(len(m.contacts()), 3)
self.assertEqual(m.contacts()[2].name, 'Princess Poppy')

def testLinks(self):
l = QgsLayerMetadata.Link()
l.name = 'Trashbat'
l.type = 'fashion'
l.description = 'registered in the cook islands!'
l.url = 'http://trashbat.co.uk'
l.format = 'whois'
l.mimeType = 'text/string'
l.size = '112'
self.assertEqual(l.name, 'Trashbat')
self.assertEqual(l.type, 'fashion')
self.assertEqual(l.description, 'registered in the cook islands!')
self.assertEqual(l.url, 'http://trashbat.co.uk')
self.assertEqual(l.format, 'whois')
self.assertEqual(l.mimeType, 'text/string')
self.assertEqual(l.size, '112')

m = QgsLayerMetadata()
l2 = QgsLayerMetadata.Link(l)
l2.name = 'Trashbat2'

m.setLinks([l, l2])
self.assertEqual(m.links()[0].name, 'Trashbat')
self.assertEqual(m.links()[1].name, 'Trashbat2')

# add link
l3 = QgsLayerMetadata.Link(l)
l3.name = 'Trashbat3'
m.addLink(l3)
self.assertEqual(len(m.links()), 3)
self.assertEqual(m.links()[2].name, 'Trashbat3')

def createTestMetadata(self):
"""
Returns a standard metadata which can be tested with checkExpectedMetadata
@@ -548,8 +332,6 @@ def testSaveReadFromXml(self):
"""
Test saving and reading metadata from a XML.
"""
vl = QgsVectorLayer('Point', 'test', 'memory')
self.assertTrue(vl.isValid())

# save metadata to XML
m = self.createTestMetadata()

0 comments on commit a96d59a

Please sign in to comment.
You can’t perform that action at this time.