Skip to content

Commit

Permalink
[postgres] Unit tests for citext field type
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jul 1, 2019
1 parent 07032d9 commit 1164d20
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
58 changes: 58 additions & 0 deletions tests/src/python/test_provider_postgres.py
Expand Up @@ -225,6 +225,64 @@ def testByteaType(self):
}
self.assertEqual(values, expected)

def testCitextType(self):
vl = QgsVectorLayer('{} table="qgis_test"."citext_table" sql='.format(self.dbconn), "testbytea", "postgres")
self.assertTrue(vl.isValid())

fields = vl.dataProvider().fields()
self.assertEqual(fields.at(fields.indexFromName('fld1')).type(), QVariant.String)

values = {feat['id']: feat['fld1'] for feat in vl.getFeatures()}
expected = {
1: 'test val',
2: NULL
}
self.assertEqual(values, expected)

# editing citext values
self.execSQLCommand('DROP TABLE IF EXISTS qgis_test."citext_table_edit" CASCADE')
self.execSQLCommand(
'CREATE TABLE qgis_test."citext_table_edit" ( pk SERIAL NOT NULL PRIMARY KEY, txt citext)')
self.execSQLCommand("INSERT INTO qgis_test.\"citext_table_edit\" (pk, txt) VALUES "
"(1, 'text')")
vl = QgsVectorLayer(
self.dbconn + ' sslmode=disable table="qgis_test"."citext_table_edit" sql=',
'test', 'postgres')
self.assertTrue(vl.isValid())
values = {feat['pk']: feat['txt'] for feat in vl.getFeatures()}
expected = {
1: 'text'
}
self.assertEqual(values, expected)

# change attribute value
self.assertTrue(vl.dataProvider().changeAttributeValues({1: {1: 'teeeext'}}))
values = {feat['pk']: feat['txt'] for feat in vl.getFeatures()}
expected = {
1: 'teeeext'
}
self.assertEqual(values, expected)

# add feature
f = QgsFeature()
f.setAttributes([2, 'teeeeeeeeeext'])
self.assertTrue(vl.dataProvider().addFeature(f))
values = {feat['pk']: feat['txt'] for feat in vl.getFeatures()}
expected = {
1: 'teeeext',
2: 'teeeeeeeeeext'
}
self.assertEqual(values, expected)

# change feature
self.assertTrue(vl.dataProvider().changeFeatures({2: {1: 'teeeeeeeeeeeeeeeeeeeeeeext'}}, {}))
values = {feat['pk']: feat['txt'] for feat in vl.getFeatures()}
expected = {
1: 'teeeext',
2: 'teeeeeeeeeeeeeeeeeeeeeeext'
}
self.assertEqual(values, expected)

def testQueryLayers(self):
def test_query(dbconn, query, key):
ql = QgsVectorLayer('%s srid=4326 table="%s" (geom) key=\'%s\' sql=' % (dbconn, query.replace('"', '\\"'), key), "testgeom", "postgres")
Expand Down
17 changes: 17 additions & 0 deletions tests/testdata/provider/testdata_pg.sql
Expand Up @@ -20,6 +20,8 @@ SET client_min_messages = warning;
--

CREATE EXTENSION IF NOT EXISTS postgis;
CREATE EXTENSION IF NOT EXISTS citext;


--- Create qgis_test schema
DROP SCHEMA IF EXISTS qgis_test CASCADE;
Expand Down Expand Up @@ -516,6 +518,21 @@ INSERT INTO qgis_test.boolean_table VALUES
(3, NULL);


--------------------------------------
-- Table for citext
--

CREATE TABLE qgis_test.citext_table
(
id int PRIMARY KEY,
fld1 citext
);

INSERT INTO qgis_test.citext_table VALUES
(1, 'test val'),
(2, NULL);


--------------------------------------
-- Table for bytea
--
Expand Down

0 comments on commit 1164d20

Please sign in to comment.