Skip to content

Commit

Permalink
[GRASS] edit test fixed for GRASS 6
Browse files Browse the repository at this point in the history
  • Loading branch information
blazek committed Oct 13, 2015
1 parent 4bd6cf1 commit 9556a76
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 44 deletions.
42 changes: 9 additions & 33 deletions src/providers/grass/qgsgrassvectormaplayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -983,49 +983,25 @@ void QgsGrassVectorMapLayer::deleteAttribute( int cat, QString &error )
bool QgsGrassVectorMapLayer::recordExists( int cat, QString &error )
{
QgsDebugMsg( QString( "mField = %1 cat = %2" ).arg( mField ).arg( cat ) );
bool exists = false;
if ( !mDriver )
{
error = tr( "Driver is not open" );
QgsDebugMsg( error );
return false;
}

QgsDebugMsg( "Database opened -> select record" );
QgsDebugMsg( "Database open -> select record" );

dbString dbstr;
db_init_string( &dbstr );

QString query = QString( "SELECT count(*) FROM %1 WHERE %2 = %3" ).arg( mFieldInfo->table ).arg( mFieldInfo->key ).arg( cat );
db_set_string( &dbstr, query.toLatin1().data() );

QgsDebugMsg( QString( "SQL: %1" ).arg( db_get_string( &dbstr ) ) );

dbCursor cursor;
if ( db_open_select_cursor( mDriver, &dbstr, &cursor, DB_SCROLL ) != DB_OK )
{
error = tr( "Cannot query database: %1" ).arg( query );
}
else
// DBF driver in GRASS does not support count(*)
dbValue value;
int nValues = db_select_value(mDriver, mFieldInfo->table, mFieldInfo->key, cat, mFieldInfo->key, &value);
if ( nValues == -1)
{
int more;
if ( db_fetch( &cursor, DB_NEXT, &more ) != DB_OK )
{
error = tr( "Cannot fetch DB record" );
}
else
{
dbTable *table = db_get_cursor_table( &cursor );
dbColumn *column = db_get_table_column( table, 0 );
dbValue *value = db_get_column_value( column );
int count = db_get_value_int( value );
QgsDebugMsg( QString( "count = %1" ).arg( count ) );
exists = count > 0;
}
db_close_cursor( &cursor );
error = tr( "Cannot select record from table" );
return false;
}
db_free_string( &dbstr );
return exists;

return nValues > 0;
}

bool QgsGrassVectorMapLayer::isOrphan( int cat, QString &error )
Expand Down
28 changes: 17 additions & 11 deletions tests/src/providers/grass/testqgsgrassprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1205,7 +1205,7 @@ bool TestQgsGrassProvider::equal( QgsFeature feature, QgsFeature expectedFeature
indexes.remove( index );
if ( feature.attribute( index ) != expectedFeature.attribute( i ) )
{
reportRow( QString( "Attribute name %1, value: '%2'' does not match expected value: '%2'" )
reportRow( QString( "Attribute name %1, value: '%2' does not match expected value: '%2'" )
.arg( name ).arg( feature.attribute( index ).toString() ).arg( expectedFeature.attribute( i ).toString() ) );
return false;
}
Expand Down Expand Up @@ -1257,6 +1257,7 @@ bool TestQgsGrassProvider::compare( QList<QgsFeature> features, QList<QgsFeature

bool TestQgsGrassProvider::compare( QString uri, QgsGrassObject mapObject, QgsVectorLayer *expectedLayer, bool& ok )
{
Q_UNUSED(mapObject)
QList<QgsFeature> expectedFeatures = getFeatures( expectedLayer );

// read the map using another layer/provider
Expand All @@ -1281,6 +1282,10 @@ bool TestQgsGrassProvider::compare( QString uri, QgsGrassObject mapObject, QgsVe
reportRow( "comparison with shared layer failed" );
}

// We cannot test attribute table changes with independent layer in GRASS 6, which is using
// DBF files, which are written when driver is closed (map layer keeps driver open during editing)
bool independentOk = true;
#if GRASS_VERSION_MAJOR >= 7
// Open an independent layer which does not share data with edited one
// build topology
G_TRY
Expand All @@ -1291,19 +1296,19 @@ bool TestQgsGrassProvider::compare( QString uri, QgsGrassObject mapObject, QgsVe
int result = Vect_open_old( map, mapObject.name().toUtf8().data(), mapObject.mapset().toUtf8().data() );

if ( result == -1 )
{
QgsGrass::vectDestroyMapStruct( map );
throw QgsGrass::Exception( "Cannot open map " + mapObject.name() );
}
{
QgsGrass::vectDestroyMapStruct( map );
throw QgsGrass::Exception( "Cannot open map " + mapObject.name() );
}

#if ( GRASS_VERSION_MAJOR == 6 && GRASS_VERSION_MINOR >= 4 ) || GRASS_VERSION_MAJOR > 6
Vect_build( map );
Vect_build( map );
#else
Vect_build( map, stderr );
Vect_build( map, stderr );
#endif
//Vect_set_release_support( map );
Vect_close( map );
QgsGrass::vectDestroyMapStruct( map );
//Vect_set_release_support( map );
Vect_close( map );
QgsGrass::vectDestroyMapStruct( map );
}
G_CATCH( QgsGrass::Exception &e )
{
Expand All @@ -1327,7 +1332,7 @@ bool TestQgsGrassProvider::compare( QString uri, QgsGrassObject mapObject, QgsVe
QgsGrassVectorMapStore::setStore( 0 );
delete mapStore;

bool independentOk = compare( features, expectedFeatures, ok );
independentOk = compare( features, expectedFeatures, ok );
if ( independentOk )
{
//reportRow( "comparison with independent layer ok" );
Expand All @@ -1336,6 +1341,7 @@ bool TestQgsGrassProvider::compare( QString uri, QgsGrassObject mapObject, QgsVe
{
reportRow( "comparison with independent layer failed" );
}
#endif // GRASS_VERSION_MAJOR >= 7

return sharedOk && independentOk;
}
Expand Down

0 comments on commit 9556a76

Please sign in to comment.