Skip to content

Commit

Permalink
Add some extra tests. Use it when on Add button click
Browse files Browse the repository at this point in the history
- the 'Unique Id' checkbox could be checked without given an id
- UNchecking that checkbox but leaving the id would still use the id
- you could use the Add button with an invalid query, now you run
the same tests when adding the layer as you would with the Test button
- check the fix for Unique Id Field (was never hit)
  • Loading branch information
rduivenvoorde authored and nyalldawson committed Sep 6, 2021
1 parent f0f5203 commit 5c35539
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
28 changes: 24 additions & 4 deletions src/providers/virtual/qgsvirtuallayersourceselect.cpp
Expand Up @@ -178,7 +178,7 @@ QgsVirtualLayerDefinition QgsVirtualLayerSourceSelect::getVirtualLayerDef()
{
def.setQuery( mQueryEdit->text() );
}
if ( ! mUIDField->text().isEmpty() )
if ( mUIDColumnNameChck->isChecked() && ! mUIDField->text().isEmpty() )
{
def.setUid( mUIDField->text() );
}
Expand Down Expand Up @@ -207,7 +207,7 @@ QgsVirtualLayerDefinition QgsVirtualLayerSourceSelect::getVirtualLayerDef()
return def;
}

void QgsVirtualLayerSourceSelect::testQuery()
bool QgsVirtualLayerSourceSelect::preFlight()
{
const QgsVirtualLayerDefinition def = getVirtualLayerDef();
// If the definition is empty just do nothing.
Expand All @@ -220,7 +220,11 @@ void QgsVirtualLayerSourceSelect::testQuery()
if ( vl->isValid() )
{
const QStringList fieldNames = vl->fields().names();
if ( !mUIDField->text().isEmpty() && !vl->fields().names().contains( mUIDField->text() ) )
if ( mUIDColumnNameChck->isChecked() && mUIDField->text().isEmpty() )
{
QMessageBox::warning( nullptr, tr( "Test Virtual Layer " ), tr( "Checkbox 'Unique identifier column' is checked, but no field given" ) );
}
else if ( mUIDColumnNameChck->isChecked() && !mUIDField->text().isEmpty() && !vl->fields().names().contains( mUIDField->text() ) )
{
QStringList bulletedFieldNames;
for ( const QString &fieldName : fieldNames )
Expand All @@ -230,13 +234,24 @@ void QgsVirtualLayerSourceSelect::testQuery()
QMessageBox::warning( nullptr, tr( "Test Virtual Layer " ), tr( "The unique identifier field <b>%1</b> was not found in list of fields:<ul>%2</ul>" ).arg( mUIDField->text(), bulletedFieldNames.join( ' ' ) ) );
}
else
QMessageBox::information( nullptr, tr( "Test Virtual Layer" ), tr( "No error" ) );
{
return true;
}
}
else
{
QMessageBox::critical( nullptr, tr( "Test Virtual Layer" ), vl->dataProvider()->error().summary() );
}
}
return false;
}

void QgsVirtualLayerSourceSelect::testQuery()
{
if ( preFlight() )
{
QMessageBox::information( nullptr, tr( "Test Virtual Layer" ), tr( "No error" ) );
}
}

void QgsVirtualLayerSourceSelect::addLayer()
Expand Down Expand Up @@ -378,6 +393,11 @@ void QgsVirtualLayerSourceSelect::importLayer()

void QgsVirtualLayerSourceSelect::addButtonClicked()
{
if ( ! preFlight() )
{
return;
}

QString layerName = QStringLiteral( "virtual_layer" );
QString id;
bool replace = false;
Expand Down
1 change: 1 addition & 0 deletions src/providers/virtual/qgsvirtuallayersourceselect.h
Expand Up @@ -64,6 +64,7 @@ class QgsVirtualLayerSourceSelect : public QgsAbstractDataSourceWidget, private
QgsEmbeddedLayerSelectDialog *mEmbeddedSelectionDialog = nullptr;
void addEmbeddedLayer( const QString &name, const QString &provider, const QString &encoding, const QString &source );
QgsLayerTreeView *mTreeView = nullptr;
bool preFlight();
};

#endif

0 comments on commit 5c35539

Please sign in to comment.