Skip to content

Commit

Permalink
add tests for raster style saving in postgresql db
Browse files Browse the repository at this point in the history
  • Loading branch information
JanCaha committed Jan 22, 2025
1 parent a694faa commit f8c9a40
Showing 1 changed file with 88 additions and 0 deletions.
88 changes: 88 additions & 0 deletions tests/src/python/test_provider_postgresraster.py
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,94 @@ def testBlockSize(self):
self.assertEqual(dp.xBlockSize(), 2)
self.assertEqual(dp.yBlockSize(), 2)

def testStyle(self):
rl = QgsRasterLayer(
self.dbconn
+ ' sslmode=disable srid=3035 table="public"."raster_tiled_3035" sql=',
"test",
"postgresraster",
)

self.assertTrue(rl.isValid())

self.assertEqual(
int(rl.dataProvider().styleStorageCapabilities())
& Qgis.ProviderStyleStorageCapability.LoadFromDatabase,
Qgis.ProviderStyleStorageCapability.LoadFromDatabase,
)
self.assertEqual(
int(rl.dataProvider().styleStorageCapabilities())
& Qgis.ProviderStyleStorageCapability.SaveToDatabase,
Qgis.ProviderStyleStorageCapability.SaveToDatabase,
)
self.assertEqual(
int(rl.dataProvider().styleStorageCapabilities())
& Qgis.ProviderStyleStorageCapability.DeleteFromDatabase,
Qgis.ProviderStyleStorageCapability.DeleteFromDatabase,
)

# not style yet for layer
res, err = QgsProviderRegistry.instance().styleExists(
"postgresraster", rl.source(), ""
)
self.assertFalse(res)
self.assertFalse(err)

related_count, idlist, namelist, desclist, errmsg = rl.listStylesInDatabase()
self.assertEqual(related_count, -1)
self.assertEqual(idlist, [])
self.assertEqual(namelist, [])
self.assertEqual(desclist, [])
self.assertFalse(errmsg)

# Save style twice, one as as default
errmsg = rl.saveStyleToDatabase("related raster style", "test style", False, "")
self.assertEqual(errmsg, "")

related_count, idlist, namelist, desclist, errmsg = rl.listStylesInDatabase()
self.assertEqual(related_count, 1)
self.assertEqual(idlist, ["1"])
self.assertEqual(namelist, ["related raster style"])
self.assertEqual(desclist, ["test style"])
self.assertFalse(errmsg)

errmsg = rl.saveStyleToDatabase(
"related raster style default", "default test style", True, ""
)
self.assertEqual(errmsg, "")

# check style exist
res, err = QgsProviderRegistry.instance().styleExists(
"postgresraster", rl.source(), "related raster style default"
)
self.assertTrue(res)
self.assertFalse(err)

qml, errmsg = rl.getStyleFromDatabase("2")
self.assertTrue(qml)
self.assertEqual(errmsg, "")

related_count, idlist, namelist, desclist, errmsg = rl.listStylesInDatabase()
self.assertEqual(related_count, 2)
self.assertEqual(idlist, ["2", "1"])
self.assertEqual(
namelist, ["related raster style default", "related raster style"]
)
self.assertEqual(desclist, ["default test style", "test style"])
self.assertFalse(errmsg)

# Remove these style
res, errmsg = rl.deleteStyleFromDatabase("1")
self.assertTrue(res)
self.assertFalse(errmsg)

related_count, idlist, namelist, desclist, errmsg = rl.listStylesInDatabase()
self.assertEqual(related_count, 1)
self.assertEqual(idlist, ["2"])
self.assertEqual(namelist, ["related raster style default"])
self.assertEqual(desclist, ["default test style"])
self.assertFalse(errmsg)


if __name__ == "__main__":
unittest.main()

0 comments on commit f8c9a40

Please sign in to comment.