Skip to content
Permalink
Browse files

Tests to check correctness of WHERE clauses for PostgreSQL

  • Loading branch information
espinafre authored and nyalldawson committed Jun 10, 2020
1 parent bdba59a commit 952542abf9dbdfb09d84d60b48c6e9005deb0d5d
Showing with 111 additions and 0 deletions.
  1. +111 −0 tests/src/providers/testqgspostgresprovider.cpp
@@ -16,6 +16,9 @@
#include <QObject>

#include <qgspostgresprovider.h>
#include <qgspostgresconn.h>
#include <qgsfields.h>


class TestQgsPostgresProvider: public QObject
{
@@ -34,6 +37,7 @@ class TestQgsPostgresProvider: public QObject
void decodeJsonMap();
void decodeJsonbMap();
void testDecodeDateTimes();
void testQuotedValueBigInt();
};


@@ -190,5 +194,112 @@ void TestQgsPostgresProvider::testDecodeDateTimes()

}

void TestQgsPostgresProvider::testQuotedValueBigInt()
{
QgsFields fields;
QList<int> pkAttrs;
QVariantList vlst;
QgsPostgresSharedData *sdata;

QgsField f0, f1, f2, f3;

// 4 byte integer
f0.setName( "fld_integer" );
f0.setType( QVariant::Int );
f0.setTypeName( "int" );

fields.clear();
pkAttrs.clear();
vlst.clear();

fields.append( f0 );
pkAttrs.append( 0 );
vlst.append( 42 );

sdata = new QgsPostgresSharedData;
// for positive integers, fid == the value, there is no map.
sdata->insertFid( 42, vlst );

QCOMPARE( QgsPostgresUtils::whereClause( 42, fields, NULL, QgsPostgresPrimaryKeyType::PktInt, pkAttrs, std::shared_ptr<QgsPostgresSharedData>( sdata ) ), QString( "\"fld_integer\"=42" ) );

// 8 byte integer
f1.setName( "fld_bigint" );
f1.setType( QVariant::LongLong );
f1.setTypeName( "int8" );

fields.clear();
pkAttrs.clear();
vlst.clear();

fields.append( f1 );
pkAttrs.append( 0 );
vlst.append( -9223372036854775800LL ); // way outside int4 range

sdata = new QgsPostgresSharedData;
sdata->insertFid( 1LL, vlst );

QCOMPARE( QgsPostgresUtils::whereClause( 1LL, fields, NULL, QgsPostgresPrimaryKeyType::PktInt64, pkAttrs, std::shared_ptr<QgsPostgresSharedData>( sdata ) ), QString( "\"fld_bigint\"=-9223372036854775800" ) );

// double
f2.setName( "fld_double" );
f2.setType( QVariant::Double );
f2.setTypeName( "float8" );

fields.clear();
pkAttrs.clear();
vlst.clear();

fields.append( f2 );
pkAttrs.append( 0 );
vlst.append( 3.141592741 );

sdata = new QgsPostgresSharedData;
sdata->insertFid( 1LL, vlst );

QCOMPARE( QgsPostgresUtils::whereClause( 1LL, fields, NULL, QgsPostgresPrimaryKeyType::PktFidMap, pkAttrs, std::shared_ptr<QgsPostgresSharedData>( sdata ) ), QString( "\"fld_double\"::text='3.141592741'" ) );

// text
f3.setName( "fld_text" );
f3.setType( QVariant::String );
f3.setTypeName( "text" );

fields.clear();
pkAttrs.clear();
vlst.clear();

fields.append( f3 );
pkAttrs.append( 0 );
vlst.append( QString( "QGIS 'Rocks'!" ) );

sdata = new QgsPostgresSharedData;
sdata->insertFid( 1LL, vlst );

QCOMPARE( QgsPostgresUtils::whereClause( 1LL, fields, NULL, QgsPostgresPrimaryKeyType::PktFidMap, pkAttrs, std::shared_ptr<QgsPostgresSharedData>( sdata ) ), QString( "\"fld_text\"::text='QGIS ''Rocks''!'" ) );

// Composite bigint + text + int
pkAttrs.clear();

pkAttrs.append( 0 );
pkAttrs.append( 1 );
pkAttrs.append( 2 );

vlst.clear();
vlst.append( -9223372036854775800LL );
vlst.append( QString( "QGIS 'Rocks'!" ) );
vlst.append( 42 );

fields.clear();
fields.append( f1 );
fields.append( f3 );
fields.append( f0 );

sdata = new QgsPostgresSharedData;
sdata->insertFid( 1LL, vlst );

// TODO: FIXME: Please don't cast integer primary key types to text if they are part of a FidMap!
// It hurts the database performance badly.
QCOMPARE( QgsPostgresUtils::whereClause( 1LL, fields, NULL, QgsPostgresPrimaryKeyType::PktFidMap, pkAttrs, std::shared_ptr<QgsPostgresSharedData>( sdata ) ), QString( "\"fld_bigint\"='-9223372036854775800' AND \"fld_text\"::text='QGIS ''Rocks''!' AND \"fld_integer\"::text='42'" ) );
}

QGSTEST_MAIN( TestQgsPostgresProvider )
#include "testqgspostgresprovider.moc"

0 comments on commit 952542a

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