|
72 | 72 | #include "qgsproviderregistry.h"
|
73 | 73 | #include "qgsprovidermetadata.h"
|
74 | 74 | #include "qgsproviderconnectioncombobox.h"
|
| 75 | +#include "qgsdatabaseschemacombobox.h" |
75 | 76 |
|
76 | 77 | class TestParamType : public QgsProcessingParameterDefinition
|
77 | 78 | {
|
@@ -205,6 +206,7 @@ class TestProcessingGui : public QObject
|
205 | 206 | void testMapThemeWrapper();
|
206 | 207 | void testDateTimeWrapper();
|
207 | 208 | void testProviderConnectionWrapper();
|
| 209 | + void testDatabaseSchemaWrapper(); |
208 | 210 |
|
209 | 211 | private:
|
210 | 212 |
|
@@ -4743,6 +4745,205 @@ void TestProcessingGui::testProviderConnectionWrapper()
|
4743 | 4745 | QVERIFY( !static_cast< QgsProcessingParameterProviderConnection * >( def.get() )->defaultValue().isValid() );
|
4744 | 4746 | }
|
4745 | 4747 |
|
| 4748 | +void TestProcessingGui::testDatabaseSchemaWrapper() |
| 4749 | +{ |
| 4750 | +#ifdef ENABLE_PGTEST |
| 4751 | + // register some connections |
| 4752 | + QgsProviderMetadata *md = QgsProviderRegistry::instance()->providerMetadata( QStringLiteral( "postgres" ) ); |
| 4753 | + |
| 4754 | + QString dbConn = getenv( "QGIS_PGTEST_DB" ); |
| 4755 | + if ( dbConn.isEmpty() ) |
| 4756 | + { |
| 4757 | + dbConn = "service=\"qgis_test\""; |
| 4758 | + } |
| 4759 | + QgsAbstractProviderConnection *conn = md->createConnection( QStringLiteral( "%1 sslmode=disable" ).arg( dbConn ), QVariantMap() ); |
| 4760 | + md->saveConnection( conn, QStringLiteral( "aa" ) ); |
| 4761 | + |
| 4762 | + const QStringList schemas = dynamic_cast<QgsAbstractDatabaseProviderConnection *>( conn )->schemas(); |
| 4763 | + QVERIFY( !schemas.isEmpty() ); |
| 4764 | + |
| 4765 | + auto testWrapper = [&schemas]( QgsProcessingGui::WidgetType type ) |
| 4766 | + { |
| 4767 | + QgsProcessingParameterProviderConnection connParam( QStringLiteral( "conn" ), QStringLiteral( "connection" ), QStringLiteral( "postgres" ), true ); |
| 4768 | + TestLayerWrapper connWrapper( &connParam ); |
| 4769 | + |
| 4770 | + QgsProcessingParameterDatabaseSchema param( QStringLiteral( "schema" ), QStringLiteral( "schema" ), QStringLiteral( "conn" ), QVariant(), false ); |
| 4771 | + |
| 4772 | + QgsProcessingDatabaseSchemaWidgetWrapper wrapper( ¶m, type ); |
| 4773 | + |
| 4774 | + QgsProcessingContext context; |
| 4775 | + QWidget *w = wrapper.createWrappedWidget( context ); |
| 4776 | + // no connection associated yet |
| 4777 | + QCOMPARE( static_cast< QgsDatabaseSchemaComboBox * >( wrapper.wrappedWidget() )->comboBox()->count(), 0 ); |
| 4778 | + |
| 4779 | + // Set the parent widget connection value |
| 4780 | + connWrapper.setWidgetValue( QStringLiteral( "aa" ), context ); |
| 4781 | + wrapper.setParentConnectionWrapperValue( &connWrapper ); |
| 4782 | + |
| 4783 | + // now we should have schemas available |
| 4784 | + QCOMPARE( static_cast< QgsDatabaseSchemaComboBox * >( wrapper.wrappedWidget() )->comboBox()->count(), schemas.count() ); |
| 4785 | + |
| 4786 | + QSignalSpy spy( &wrapper, &QgsProcessingDatabaseSchemaWidgetWrapper::widgetValueHasChanged ); |
| 4787 | + wrapper.setWidgetValue( QStringLiteral( "qgis_test" ), context ); |
| 4788 | + QCOMPARE( spy.count(), 1 ); |
| 4789 | + QCOMPARE( wrapper.widgetValue().toString(), QStringLiteral( "qgis_test" ) ); |
| 4790 | + QCOMPARE( static_cast< QgsDatabaseSchemaComboBox * >( wrapper.wrappedWidget() )->currentSchema(), QStringLiteral( "qgis_test" ) ); |
| 4791 | + wrapper.setWidgetValue( QStringLiteral( "public" ), context ); |
| 4792 | + QCOMPARE( wrapper.widgetValue().toString(), QStringLiteral( "public" ) ); |
| 4793 | + QCOMPARE( static_cast< QgsDatabaseSchemaComboBox * >( wrapper.wrappedWidget() )->currentSchema(), QStringLiteral( "public" ) ); |
| 4794 | + QCOMPARE( spy.count(), 2 ); |
| 4795 | + wrapper.setWidgetValue( QStringLiteral( "public" ), context ); |
| 4796 | + QCOMPARE( spy.count(), 2 ); |
| 4797 | + |
| 4798 | + switch ( type ) |
| 4799 | + { |
| 4800 | + case QgsProcessingGui::Standard: |
| 4801 | + case QgsProcessingGui::Batch: |
| 4802 | + { |
| 4803 | + // batch or standard mode, only valid schemas can be set! |
| 4804 | + // not valid |
| 4805 | + wrapper.setWidgetValue( QStringLiteral( "cc" ), context ); |
| 4806 | + QCOMPARE( spy.count(), 3 ); |
| 4807 | + QVERIFY( !wrapper.widgetValue().isValid() ); |
| 4808 | + QCOMPARE( static_cast< QgsDatabaseSchemaComboBox * >( wrapper.wrappedWidget() )->comboBox()->currentIndex(), -1 ); |
| 4809 | + break; |
| 4810 | + |
| 4811 | + } |
| 4812 | + case QgsProcessingGui::Modeler: |
| 4813 | + // invalid schemas permitted |
| 4814 | + wrapper.setWidgetValue( QStringLiteral( "cc" ), context ); |
| 4815 | + QCOMPARE( spy.count(), 3 ); |
| 4816 | + QCOMPARE( static_cast< QgsDatabaseSchemaComboBox * >( wrapper.wrappedWidget() )->comboBox()->currentText(), QStringLiteral( "cc" ) ); |
| 4817 | + QCOMPARE( wrapper.widgetValue().toString(), QStringLiteral( "cc" ) ); |
| 4818 | + wrapper.setWidgetValue( QStringLiteral( "aa" ), context ); |
| 4819 | + QCOMPARE( spy.count(), 4 ); |
| 4820 | + QCOMPARE( static_cast< QgsDatabaseSchemaComboBox * >( wrapper.wrappedWidget() )->comboBox()->currentText(), QStringLiteral( "aa" ) ); |
| 4821 | + QCOMPARE( wrapper.widgetValue().toString(), QStringLiteral( "aa" ) ); |
| 4822 | + break; |
| 4823 | + } |
| 4824 | + |
| 4825 | + // make sure things are ok if connection is changed back to nothing |
| 4826 | + connWrapper.setWidgetValue( QVariant(), context ); |
| 4827 | + wrapper.setParentConnectionWrapperValue( &connWrapper ); |
| 4828 | + |
| 4829 | + switch ( type ) |
| 4830 | + { |
| 4831 | + case QgsProcessingGui::Standard: |
| 4832 | + case QgsProcessingGui::Batch: |
| 4833 | + { |
| 4834 | + QCOMPARE( spy.count(), 3 ); |
| 4835 | + break; |
| 4836 | + } |
| 4837 | + |
| 4838 | + case QgsProcessingGui::Modeler: |
| 4839 | + QCOMPARE( spy.count(), 5 ); |
| 4840 | + break; |
| 4841 | + } |
| 4842 | + QVERIFY( !wrapper.widgetValue().isValid() ); |
| 4843 | + |
| 4844 | + wrapper.setWidgetValue( QStringLiteral( "qgis_test" ), context ); |
| 4845 | + switch ( type ) |
| 4846 | + { |
| 4847 | + case QgsProcessingGui::Standard: |
| 4848 | + case QgsProcessingGui::Batch: |
| 4849 | + { |
| 4850 | + QVERIFY( !wrapper.widgetValue().isValid() ); |
| 4851 | + break; |
| 4852 | + } |
| 4853 | + |
| 4854 | + case QgsProcessingGui::Modeler: |
| 4855 | + // invalid schemas permitted |
| 4856 | + QCOMPARE( spy.count(), 6 ); |
| 4857 | + QCOMPARE( static_cast< QgsDatabaseSchemaComboBox * >( wrapper.wrappedWidget() )->comboBox()->currentText(), QStringLiteral( "qgis_test" ) ); |
| 4858 | + QCOMPARE( wrapper.widgetValue().toString(), QStringLiteral( "qgis_test" ) ); |
| 4859 | + |
| 4860 | + break; |
| 4861 | + } |
| 4862 | + delete w; |
| 4863 | + |
| 4864 | + connWrapper.setWidgetValue( QStringLiteral( "aa" ), context ); |
| 4865 | + |
| 4866 | + // optional |
| 4867 | + QgsProcessingParameterDatabaseSchema param2( QStringLiteral( "schema" ), QStringLiteral( "schema" ), QStringLiteral( "conn" ), QVariant(), true ); |
| 4868 | + QgsProcessingDatabaseSchemaWidgetWrapper wrapper3( ¶m2, type ); |
| 4869 | + w = wrapper3.createWrappedWidget( context ); |
| 4870 | + |
| 4871 | + wrapper3.setParentConnectionWrapperValue( &connWrapper ); |
| 4872 | + |
| 4873 | + QSignalSpy spy3( &wrapper3, &QgsProcessingDatabaseSchemaWidgetWrapper::widgetValueHasChanged ); |
| 4874 | + wrapper3.setWidgetValue( QStringLiteral( "qgis_test" ), context ); |
| 4875 | + QCOMPARE( spy3.count(), 1 ); |
| 4876 | + QCOMPARE( wrapper3.widgetValue().toString(), QStringLiteral( "qgis_test" ) ); |
| 4877 | + QCOMPARE( static_cast< QgsDatabaseSchemaComboBox * >( wrapper3.wrappedWidget() )->comboBox()->currentText(), QStringLiteral( "qgis_test" ) ); |
| 4878 | + wrapper3.setWidgetValue( QStringLiteral( "public" ), context ); |
| 4879 | + QCOMPARE( spy3.count(), 2 ); |
| 4880 | + QCOMPARE( wrapper3.widgetValue().toString(), QStringLiteral( "public" ) ); |
| 4881 | + QCOMPARE( static_cast< QgsDatabaseSchemaComboBox * >( wrapper3.wrappedWidget() )->comboBox()->currentText(), QStringLiteral( "public" ) ); |
| 4882 | + wrapper3.setWidgetValue( QVariant(), context ); |
| 4883 | + QCOMPARE( spy3.count(), 3 ); |
| 4884 | + QVERIFY( !wrapper3.widgetValue().isValid() ); |
| 4885 | + |
| 4886 | + delete w; |
| 4887 | + QLabel *l = wrapper.createWrappedLabel(); |
| 4888 | + if ( wrapper.type() != QgsProcessingGui::Batch ) |
| 4889 | + { |
| 4890 | + QVERIFY( l ); |
| 4891 | + QCOMPARE( l->text(), QStringLiteral( "schema" ) ); |
| 4892 | + QCOMPARE( l->toolTip(), param.toolTip() ); |
| 4893 | + delete l; |
| 4894 | + } |
| 4895 | + else |
| 4896 | + { |
| 4897 | + QVERIFY( !l ); |
| 4898 | + } |
| 4899 | + |
| 4900 | + }; |
| 4901 | + |
| 4902 | + // standard wrapper |
| 4903 | + testWrapper( QgsProcessingGui::Standard ); |
| 4904 | + |
| 4905 | + // batch wrapper |
| 4906 | + testWrapper( QgsProcessingGui::Batch ); |
| 4907 | + |
| 4908 | + // modeler wrapper |
| 4909 | + testWrapper( QgsProcessingGui::Modeler ); |
| 4910 | + |
| 4911 | + // config widget |
| 4912 | + QgsProcessingParameterWidgetContext widgetContext; |
| 4913 | + QgsProcessingContext context; |
| 4914 | + std::unique_ptr< QgsProcessingParameterDefinitionWidget > widget = qgis::make_unique< QgsProcessingParameterDefinitionWidget >( QStringLiteral( "databaseschema" ), context, widgetContext ); |
| 4915 | + std::unique_ptr< QgsProcessingParameterDefinition > def( widget->createParameter( QStringLiteral( "param_name" ) ) ); |
| 4916 | + QCOMPARE( def->name(), QStringLiteral( "param_name" ) ); |
| 4917 | + QVERIFY( !( def->flags() & QgsProcessingParameterDefinition::FlagOptional ) ); // should default to mandatory |
| 4918 | + QVERIFY( !( def->flags() & QgsProcessingParameterDefinition::FlagAdvanced ) ); |
| 4919 | + QVERIFY( !static_cast< QgsProcessingParameterDatabaseSchema * >( def.get() )->defaultValue().isValid() ); |
| 4920 | + |
| 4921 | + // using a parameter definition as initial values |
| 4922 | + QgsProcessingParameterDatabaseSchema schemaParam( QStringLiteral( "n" ), QStringLiteral( "test desc" ), QStringLiteral( "connparam" ), QStringLiteral( "aaa" ), false ); |
| 4923 | + widget = qgis::make_unique< QgsProcessingParameterDefinitionWidget >( QStringLiteral( "databaseschema" ), context, widgetContext, &schemaParam ); |
| 4924 | + def.reset( widget->createParameter( QStringLiteral( "param_name" ) ) ); |
| 4925 | + QCOMPARE( def->name(), QStringLiteral( "param_name" ) ); |
| 4926 | + QCOMPARE( def->description(), QStringLiteral( "test desc" ) ); |
| 4927 | + QVERIFY( !( def->flags() & QgsProcessingParameterDefinition::FlagOptional ) ); |
| 4928 | + QVERIFY( !( def->flags() & QgsProcessingParameterDefinition::FlagAdvanced ) ); |
| 4929 | + QCOMPARE( static_cast< QgsProcessingParameterDatabaseSchema * >( def.get() )->defaultValue().toString(), QStringLiteral( "aaa" ) ); |
| 4930 | + QCOMPARE( static_cast< QgsProcessingParameterDatabaseSchema * >( def.get() )->parentConnectionParameterName(), QStringLiteral( "connparam" ) ); |
| 4931 | + schemaParam.setFlags( QgsProcessingParameterDefinition::FlagAdvanced | QgsProcessingParameterDefinition::FlagOptional ); |
| 4932 | + schemaParam.setDefaultValue( QStringLiteral( "xxx" ) ); |
| 4933 | + widget = qgis::make_unique< QgsProcessingParameterDefinitionWidget >( QStringLiteral( "databaseschema" ), context, widgetContext, &schemaParam ); |
| 4934 | + def.reset( widget->createParameter( QStringLiteral( "param_name" ) ) ); |
| 4935 | + QCOMPARE( def->name(), QStringLiteral( "param_name" ) ); |
| 4936 | + QCOMPARE( def->description(), QStringLiteral( "test desc" ) ); |
| 4937 | + QVERIFY( def->flags() & QgsProcessingParameterDefinition::FlagOptional ); |
| 4938 | + QVERIFY( def->flags() & QgsProcessingParameterDefinition::FlagAdvanced ); |
| 4939 | + QCOMPARE( static_cast< QgsProcessingParameterDatabaseSchema * >( def.get() )->defaultValue().toString(), QStringLiteral( "xxx" ) ); |
| 4940 | + schemaParam.setDefaultValue( QVariant() ); |
| 4941 | + widget = qgis::make_unique< QgsProcessingParameterDefinitionWidget >( QStringLiteral( "databaseschema" ), context, widgetContext, &schemaParam ); |
| 4942 | + def.reset( widget->createParameter( QStringLiteral( "param_name" ) ) ); |
| 4943 | + QVERIFY( !static_cast< QgsProcessingParameterDatabaseSchema * >( def.get() )->defaultValue().isValid() ); |
| 4944 | +#endif |
| 4945 | +} |
| 4946 | + |
4746 | 4947 | void TestProcessingGui::cleanupTempDir()
|
4747 | 4948 | {
|
4748 | 4949 | QDir tmpDir = QDir( mTempDir );
|
|
0 commit comments