From 767923937fe0a046fc166e5fa56f49dd1f735e08 Mon Sep 17 00:00:00 2001 From: Damiano Date: Mon, 5 Apr 2021 00:14:14 +0200 Subject: [PATCH] Added python tests for settings entries classes --- .../settings/qgssettingsentry.sip.in | 98 ++++--- src/core/settings/qgssettingsentry.cpp | 113 ++++++-- src/core/settings/qgssettingsentry.h | 146 ++++++++-- tests/src/python/CMakeLists.txt | 1 + tests/src/python/test_qgssettingsentry.py | 252 ++++++++++++++++++ 5 files changed, 523 insertions(+), 87 deletions(-) create mode 100644 tests/src/python/test_qgssettingsentry.py diff --git a/python/core/auto_generated/settings/qgssettingsentry.sip.in b/python/core/auto_generated/settings/qgssettingsentry.sip.in index aa7714e9a116..14787c3ec76d 100644 --- a/python/core/auto_generated/settings/qgssettingsentry.sip.in +++ b/python/core/auto_generated/settings/qgssettingsentry.sip.in @@ -53,15 +53,17 @@ to validate set values and provide more accurate settings description for the gu Flag }; - QgsSettingsEntryBase( QString key, - QgsSettings::Section section, - QVariant defaultValue = QVariant(), - QString description = QString() ); + + QgsSettingsEntryBase( const QString &key, + const QString &pluginName, + const QVariant &defaultValue = QVariant(), + const QString &description = QString() ); %Docstring Constructor for :py:class:`QgsSettingsEntry`. +This constructor is intended to be used from plugins. The ``key`` argument specifies the key of the settings. -The ``section`` argument specifies the section of the settings. +The ``pluginName`` argument is inserted in the key after the section. The ``default`` value argument specifies the default value for the settings entry. The ``description`` argument specifies a description for the settings entry. %End @@ -144,20 +146,22 @@ A variant settings entry. %End public: + QgsSettingsEntryVariant( const QString &key, - QgsSettings::Section section, + const QString &pluginName, const QVariant &defaultValue = QVariant(), const QString &description = QString() ); %Docstring Constructor for QgsSettingsEntryVariant. +This constructor is intended to be used from plugins. -The ``key`` argument specifies the final part of the settings key. -The ``parentGroup`` argument specifies a parent group which is used to rebuild -the entiere settings key and to determine the settings section. +The ``key`` argument specifies the key of the settings. +The ``pluginName`` argument is inserted in the key after the section. The ``default`` value argument specifies the default value for the settings entry. The ``description`` argument specifies a description for the settings entry. %End + virtual bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const; %Docstring @@ -196,23 +200,19 @@ A string settings entry. %End public: + QgsSettingsEntryString( const QString &key, - QgsSettings::Section section, - const QString &defaultValue = QString(), - const QString &description = QString(), - int minLength = 0, - int maxLength = -1 ); + const QString &pluginName, + const QVariant &defaultValue = QVariant(), + const QString &description = QString() ); %Docstring Constructor for QgsSettingsEntryString. +This constructor is intended to be used from plugins. -The ``key`` argument specifies the final part of the settings key. -The ``parentGroup`` argument specifies a parent group which is used to rebuild -the entiere settings key and to determine the settings section. +The ``key`` argument specifies the key of the settings. +The ``pluginName`` argument is inserted in the key after the section. The ``default`` value argument specifies the default value for the settings entry. The ``description`` argument specifies a description for the settings entry. -The ``minLength`` argument specifies the minimal length of the string value. -The ``maxLength`` argument specifies the maximal length of the string value. -By -1 the there is no limit %End virtual bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const; @@ -236,11 +236,25 @@ Get settings default value. virtual SettingsType settingsType() const; %Docstring \copydoc :py:class:`QgsSettingsEntry`.settingsType +%End + + void setMinLength( int minLength ); +%Docstring +Set the string minimum length. + +minLength The string minimum length. %End int minLength(); %Docstring Returns the string minimum length. +%End + + void setMaxLength( int maxLength ); +%Docstring +Set the string maximum length. + +maxLength The string maximum length. %End int maxLength(); @@ -264,16 +278,17 @@ A string list settings entry. %End public: + QgsSettingsEntryStringList( const QString &key, - QgsSettings::Section section, - const QStringList &defaultValue = QStringList(), + const QString &pluginName, + const QVariant &defaultValue = QVariant(), const QString &description = QString() ); %Docstring Constructor for QgsSettingsEntryStringList. +This constructor is intended to be used from plugins. -The ``key`` argument specifies the final part of the settings key. -The ``parentGroup`` argument specifies a parent group which is used to rebuild -the entiere settings key and to determine the settings section. +The ``key`` argument specifies the key of the settings. +The ``pluginName`` argument is inserted in the key after the section. The ``default`` value argument specifies the default value for the settings entry. The ``description`` argument specifies a description for the settings entry. %End @@ -317,16 +332,17 @@ A boolean settings entry. %End public: + QgsSettingsEntryBool( const QString &key, - QgsSettings::Section section, - bool defaultValue = false, + const QString &pluginName, + const QVariant &defaultValue = QVariant(), const QString &description = QString() ); %Docstring Constructor for QgsSettingsEntryBool. +This constructor is intended to be used from plugins. -The ``key`` argument specifies the final part of the settings key. -The ``parentGroup`` argument specifies a parent group which is used to rebuild -the entiere settings key and to determine the settings section. +The ``key`` argument specifies the key of the settings. +The ``pluginName`` argument is inserted in the key after the section. The ``default`` value argument specifies the default value for the settings entry. The ``description`` argument specifies a description for the settings entry. %End @@ -353,7 +369,6 @@ Get settings default value. %Docstring \copydoc :py:class:`QgsSettingsEntry`.settingsType %End - }; @@ -373,15 +388,15 @@ An integer settings entry. QgsSettingsEntryInteger( const QString &key, - QgsSettings::Section section, - qlonglong defaultValue = 0, + const QString &pluginName, + const QVariant &defaultValue = QVariant(), const QString &description = QString() ); %Docstring Constructor for QgsSettingsEntryInteger. +This constructor is intended to be used from plugins. -The ``key`` argument specifies the final part of the settings key. -The ``parentGroup`` argument specifies a parent group which is used to rebuild -the entiere settings key and to determine the settings section. +The ``key`` argument specifies the key of the settings. +The ``pluginName`` argument is inserted in the key after the section. The ``default`` value argument specifies the default value for the settings entry. The ``description`` argument specifies a description for the settings entry. %End @@ -452,20 +467,19 @@ A double settings entry. QgsSettingsEntryDouble( const QString &key, - QgsSettings::Section section, - double defaultValue = 0.0, + const QString &pluginName, + const QVariant &defaultValue = QVariant(), const QString &description = QString() ); %Docstring Constructor for QgsSettingsEntryDouble. +This constructor is intended to be used from plugins. -The ``key`` argument specifies the final part of the settings key. -The ``parentGroup`` argument specifies a parent group which is used to rebuild -the entiere settings key and to determine the settings section. +The ``key`` argument specifies the key of the settings. +The ``pluginName`` argument is inserted in the key after the section. The ``default`` value argument specifies the default value for the settings entry. The ``description`` argument specifies a description for the settings entry. %End - virtual bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const; %Docstring diff --git a/src/core/settings/qgssettingsentry.cpp b/src/core/settings/qgssettingsentry.cpp index 12014bccb034..2146cf33577d 100644 --- a/src/core/settings/qgssettingsentry.cpp +++ b/src/core/settings/qgssettingsentry.cpp @@ -17,14 +17,21 @@ #include "qgslogger.h" -QgsSettingsEntryBase::QgsSettingsEntryBase( QString key, - QgsSettings::Section section, - QVariant defaultValue, - QString description ) +QgsSettingsEntryBase::QgsSettingsEntryBase( const QString &key, QgsSettings::Section section, const QVariant &defaultValue, const QString &description ) : mKey( key ) , mDefaultValue( defaultValue ) , mSection( section ) , mDescription( description ) + , mPluginName() +{ +} + +QgsSettingsEntryBase::QgsSettingsEntryBase( const QString &key, const QString &pluginName, const QVariant &defaultValue, const QString &description ) + : mKey( key ) + , mDefaultValue( defaultValue ) + , mSection( QgsSettings::Plugins ) + , mDescription( description ) + , mPluginName( pluginName ) { } @@ -34,23 +41,26 @@ QgsSettingsEntryBase::~QgsSettingsEntryBase() QString QgsSettingsEntryBase::key( const QString &dynamicKeyPart ) const { - if ( dynamicKeyPart.isEmpty() == false ) + QString completeKey = mKey; + if ( !mPluginName.isEmpty() ) + completeKey.prepend( mPluginName + "/" ); + + if ( dynamicKeyPart.isEmpty() ) { - if ( hasDynamicKey() == false ) - { - QgsLogger::warning( QStringLiteral( "Settings '%1' don't have a dynamic key, the provided dynamic key part will be ignored" ).arg( mKey ) ); - return mKey; - } + if ( hasDynamicKey() ) + QgsLogger::warning( QStringLiteral( "Settings '%1' have a dynamic key but the dynamic key part was not provided" ).arg( completeKey ) ); - QString completeKey = mKey; - return completeKey.replace( '%', dynamicKeyPart ); + return completeKey; } else { - if ( hasDynamicKey() == true ) - QgsLogger::warning( QStringLiteral( "Settings '%1' have a dynamic key but the dynamic key part was not provided" ).arg( mKey ) ); + if ( !hasDynamicKey() ) + { + QgsLogger::warning( QStringLiteral( "Settings '%1' don't have a dynamic key, the provided dynamic key part will be ignored" ).arg( completeKey ) ); + return completeKey; + } - return mKey; + return completeKey.replace( '%', dynamicKeyPart ); } } @@ -108,6 +118,14 @@ QgsSettingsEntryVariant::QgsSettingsEntryVariant( const QString &key, QgsSetting { } +QgsSettingsEntryVariant::QgsSettingsEntryVariant( const QString &key, const QString &pluginName, const QVariant &defaultValue, const QString &description ) + : QgsSettingsEntryBase( key, + pluginName, + defaultValue, + description ) +{ +} + bool QgsSettingsEntryVariant::setValue( const QVariant &value, const QString &dynamicKeyPart ) const { return QgsSettingsEntryBase::setValue( value, dynamicKeyPart ); @@ -138,6 +156,16 @@ QgsSettingsEntryString::QgsSettingsEntryString( const QString &key, QgsSettings: { } +QgsSettingsEntryString::QgsSettingsEntryString( const QString &key, const QString &pluginName, const QVariant &defaultValue, const QString &description ) + : QgsSettingsEntryBase( key, + pluginName, + defaultValue, + description ) + , mMinLength( 0 ) + , mMaxLength( -1 ) +{ +} + bool QgsSettingsEntryString::setValue( const QVariant &value, const QString &dynamicKeyPart ) const { if ( value.canConvert() == false ) @@ -186,20 +214,27 @@ QgsSettingsEntryBase::SettingsType QgsSettingsEntryString::settingsType() const return QgsSettingsEntryBase::String; } +void QgsSettingsEntryString::setMinLength( int minLength ) +{ + mMinLength = minLength; +} + int QgsSettingsEntryString::minLength() { return mMinLength; } +void QgsSettingsEntryString::setMaxLength( int maxLength ) +{ + mMaxLength = maxLength; +} + int QgsSettingsEntryString::maxLength() { return mMaxLength; } -QgsSettingsEntryStringList::QgsSettingsEntryStringList( const QString &key, - QgsSettings::Section section, - const QStringList &defaultValue, - const QString &description ) +QgsSettingsEntryStringList::QgsSettingsEntryStringList( const QString &key, QgsSettings::Section section, const QStringList &defaultValue, const QString &description ) : QgsSettingsEntryBase( key, section, defaultValue, @@ -207,6 +242,14 @@ QgsSettingsEntryStringList::QgsSettingsEntryStringList( const QString &key, { } +QgsSettingsEntryStringList::QgsSettingsEntryStringList( const QString &key, const QString &pluginName, const QVariant &defaultValue, const QString &description ) + : QgsSettingsEntryBase( key, + pluginName, + defaultValue, + description ) +{ +} + bool QgsSettingsEntryStringList::setValue( const QVariant &value, const QString &dynamicKeyPart ) const { if ( value.canConvert() == false ) @@ -235,10 +278,7 @@ QgsSettingsEntryBase::SettingsType QgsSettingsEntryStringList::settingsType() co return QgsSettingsEntryBase::StringList; } -QgsSettingsEntryBool::QgsSettingsEntryBool( const QString &key, - QgsSettings::Section section, - bool defaultValue, - const QString &description ) +QgsSettingsEntryBool::QgsSettingsEntryBool( const QString &key, QgsSettings::Section section, bool defaultValue, const QString &description ) : QgsSettingsEntryBase( key, section, defaultValue, @@ -246,6 +286,14 @@ QgsSettingsEntryBool::QgsSettingsEntryBool( const QString &key, { } +QgsSettingsEntryBool::QgsSettingsEntryBool( const QString &key, const QString &pluginName, const QVariant &defaultValue, const QString &description ) + : QgsSettingsEntryBase( key, + pluginName, + defaultValue, + description ) +{ +} + bool QgsSettingsEntryBool::setValue( const QVariant &value, const QString &dynamicKeyPart ) const { if ( value.canConvert() == false ) @@ -282,7 +330,16 @@ QgsSettingsEntryInteger::QgsSettingsEntryInteger( const QString &key, QgsSetting , mMinValue( minValue ) , mMaxValue( maxValue ) { +} +QgsSettingsEntryInteger::QgsSettingsEntryInteger( const QString &key, const QString &pluginName, const QVariant &defaultValue, const QString &description ) + : QgsSettingsEntryBase( key, + pluginName, + defaultValue, + description ) + , mMinValue( std::numeric_limits::min() ) + , mMaxValue( std::numeric_limits::max() ) +{ } bool QgsSettingsEntryInteger::setValue( const QVariant &value, const QString &dynamicKeyPart ) const @@ -361,7 +418,17 @@ QgsSettingsEntryDouble::QgsSettingsEntryDouble( const QString &key, QgsSettings: , mMaxValue( maxValue ) , mDisplayHintDecimals( displayDecimals ) { +} +QgsSettingsEntryDouble::QgsSettingsEntryDouble( const QString &key, const QString &pluginName, const QVariant &defaultValue, const QString &description ) + : QgsSettingsEntryBase( key, + pluginName, + defaultValue, + description ) + , mMinValue( std::numeric_limits::min() ) + , mMaxValue( std::numeric_limits::max() ) + , mDisplayHintDecimals( 1 ) +{ } bool QgsSettingsEntryDouble::setValue( const QVariant &value, const QString &dynamicKeyPart ) const diff --git a/src/core/settings/qgssettingsentry.h b/src/core/settings/qgssettingsentry.h index 96ca0be35473..810cd3281cc4 100644 --- a/src/core/settings/qgssettingsentry.h +++ b/src/core/settings/qgssettingsentry.h @@ -69,6 +69,8 @@ class CORE_EXPORT QgsSettingsEntryBase Flag }; +#ifndef SIP_RUN + /** * Constructor for QgsSettingsEntry. * @@ -77,10 +79,26 @@ class CORE_EXPORT QgsSettingsEntryBase * The \a default value argument specifies the default value for the settings entry. * The \a description argument specifies a description for the settings entry. */ - QgsSettingsEntryBase( QString key, + QgsSettingsEntryBase( const QString &key, QgsSettings::Section section, - QVariant defaultValue = QVariant(), - QString description = QString() ); + const QVariant &defaultValue = QVariant(), + const QString &description = QString() ); + +#endif + + /** + * Constructor for QgsSettingsEntry. + * This constructor is intended to be used from plugins. + * + * The \a key argument specifies the key of the settings. + * The \a pluginName argument is inserted in the key after the section. + * The \a default value argument specifies the default value for the settings entry. + * The \a description argument specifies a description for the settings entry. + */ + QgsSettingsEntryBase( const QString &key, + const QString &pluginName, + const QVariant &defaultValue = QVariant(), + const QString &description = QString() ); /** * Destructor for QgsSettingsEntry. @@ -153,7 +171,7 @@ class CORE_EXPORT QgsSettingsEntryBase QVariant mDefaultValue; QgsSettings::Section mSection; QString mDescription; - + QString mPluginName; }; @@ -167,6 +185,8 @@ class CORE_EXPORT QgsSettingsEntryVariant : public QgsSettingsEntryBase { public: +#ifndef SIP_RUN + /** * Constructor for QgsSettingsEntryVariant. * @@ -181,6 +201,23 @@ class CORE_EXPORT QgsSettingsEntryVariant : public QgsSettingsEntryBase const QVariant &defaultValue = QVariant(), const QString &description = QString() ); +#endif + + /** + * Constructor for QgsSettingsEntryVariant. + * This constructor is intended to be used from plugins. + * + * The \a key argument specifies the key of the settings. + * The \a pluginName argument is inserted in the key after the section. + * The \a default value argument specifies the default value for the settings entry. + * The \a description argument specifies a description for the settings entry. + */ + QgsSettingsEntryVariant( const QString &key, + const QString &pluginName, + const QVariant &defaultValue = QVariant(), + const QString &description = QString() ); + + //! \copydoc QgsSettingsEntry::setValue bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const override; @@ -211,6 +248,8 @@ class CORE_EXPORT QgsSettingsEntryString : public QgsSettingsEntryBase { public: +#ifndef SIP_RUN + /** * Constructor for QgsSettingsEntryString. * @@ -230,6 +269,22 @@ class CORE_EXPORT QgsSettingsEntryString : public QgsSettingsEntryBase int minLength = 0, int maxLength = -1 ); +#endif + + /** + * Constructor for QgsSettingsEntryString. + * This constructor is intended to be used from plugins. + * + * The \a key argument specifies the key of the settings. + * The \a pluginName argument is inserted in the key after the section. + * The \a default value argument specifies the default value for the settings entry. + * The \a description argument specifies a description for the settings entry. + */ + QgsSettingsEntryString( const QString &key, + const QString &pluginName, + const QVariant &defaultValue = QVariant(), + const QString &description = QString() ); + //! \copydoc QgsSettingsEntry::setValue bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const override; @@ -248,11 +303,25 @@ class CORE_EXPORT QgsSettingsEntryString : public QgsSettingsEntryBase //! \copydoc QgsSettingsEntry::settingsType virtual SettingsType settingsType() const override; + /** + * Set the string minimum length. + * + * minLength The string minimum length. + */ + void setMinLength( int minLength ); + /** * Returns the string minimum length. */ int minLength(); + /** + * Set the string maximum length. + * + * maxLength The string maximum length. + */ + void setMaxLength( int maxLength ); + /** * Returns the string maximum length. By -1 there is no limitation. */ @@ -276,6 +345,8 @@ class CORE_EXPORT QgsSettingsEntryStringList : public QgsSettingsEntryBase { public: +#ifndef SIP_RUN + /** * Constructor for QgsSettingsEntryStringList. * @@ -290,6 +361,22 @@ class CORE_EXPORT QgsSettingsEntryStringList : public QgsSettingsEntryBase const QStringList &defaultValue = QStringList(), const QString &description = QString() ); +#endif + + /** + * Constructor for QgsSettingsEntryStringList. + * This constructor is intended to be used from plugins. + * + * The \a key argument specifies the key of the settings. + * The \a pluginName argument is inserted in the key after the section. + * The \a default value argument specifies the default value for the settings entry. + * The \a description argument specifies a description for the settings entry. + */ + QgsSettingsEntryStringList( const QString &key, + const QString &pluginName, + const QVariant &defaultValue = QVariant(), + const QString &description = QString() ); + //! \copydoc QgsSettingsEntry::setValue bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const override; @@ -321,6 +408,8 @@ class CORE_EXPORT QgsSettingsEntryBool : public QgsSettingsEntryBase { public: +#ifndef SIP_RUN + /** * Constructor for QgsSettingsEntryBool. * @@ -335,6 +424,22 @@ class CORE_EXPORT QgsSettingsEntryBool : public QgsSettingsEntryBase bool defaultValue = false, const QString &description = QString() ); +#endif + + /** + * Constructor for QgsSettingsEntryBool. + * This constructor is intended to be used from plugins. + * + * The \a key argument specifies the key of the settings. + * The \a pluginName argument is inserted in the key after the section. + * The \a default value argument specifies the default value for the settings entry. + * The \a description argument specifies a description for the settings entry. + */ + QgsSettingsEntryBool( const QString &key, + const QString &pluginName, + const QVariant &defaultValue = QVariant(), + const QString &description = QString() ); + //! \copydoc QgsSettingsEntry::setValue bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const override; @@ -352,7 +457,6 @@ class CORE_EXPORT QgsSettingsEntryBool : public QgsSettingsEntryBase //! \copydoc QgsSettingsEntry::settingsType virtual SettingsType settingsType() const override; - }; @@ -386,23 +490,23 @@ class CORE_EXPORT QgsSettingsEntryInteger : public QgsSettingsEntryBase const QString &description = QString(), qlonglong minValue = std::numeric_limits::min(), qlonglong maxValue = std::numeric_limits::max() ); -#else + +#endif /** * Constructor for QgsSettingsEntryInteger. + * This constructor is intended to be used from plugins. * - * The \a key argument specifies the final part of the settings key. - * The \a parentGroup argument specifies a parent group which is used to rebuild - * the entiere settings key and to determine the settings section. + * The \a key argument specifies the key of the settings. + * The \a pluginName argument is inserted in the key after the section. * The \a default value argument specifies the default value for the settings entry. * The \a description argument specifies a description for the settings entry. */ QgsSettingsEntryInteger( const QString &key, - QgsSettings::Section section, - qlonglong defaultValue = 0, + const QString &pluginName, + const QVariant &defaultValue = QVariant(), const QString &description = QString() ); -#endif //! \copydoc QgsSettingsEntry::setValue bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const override; @@ -487,24 +591,22 @@ class CORE_EXPORT QgsSettingsEntryDouble : public QgsSettingsEntryBase double maxValue = std::numeric_limits::max(), int displayDecimals = 1 ); -#else +#endif /** * Constructor for QgsSettingsEntryDouble. + * This constructor is intended to be used from plugins. * - * The \a key argument specifies the final part of the settings key. - * The \a parentGroup argument specifies a parent group which is used to rebuild - * the entiere settings key and to determine the settings section. + * The \a key argument specifies the key of the settings. + * The \a pluginName argument is inserted in the key after the section. * The \a default value argument specifies the default value for the settings entry. * The \a description argument specifies a description for the settings entry. */ QgsSettingsEntryDouble( const QString &key, - QgsSettings::Section section, - double defaultValue = 0.0, + const QString &pluginName, + const QVariant &defaultValue = QVariant(), const QString &description = QString() ); -#endif - //! \copydoc QgsSettingsEntry::setValue bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const override; @@ -616,7 +718,7 @@ class CORE_EXPORT QgsSettingsEntryEnum : public QgsSettingsEntryBase T value( const QString &dynamicKeyPart = QString() ) const { return QgsSettings().enumValue( key( dynamicKeyPart ), - defaultValueAsVariant().template value(), + defaultValueAsVariant().value(), section() ); } @@ -724,7 +826,7 @@ class CORE_EXPORT QgsSettingsEntryFlag : public QgsSettingsEntryBase T value( const QString &dynamicKeyPart = QString() ) const { return QgsSettings().flagValue( key( dynamicKeyPart ), - defaultValueAsVariant().template value(), + defaultValueAsVariant().value(), section() ); } diff --git a/tests/src/python/CMakeLists.txt b/tests/src/python/CMakeLists.txt index b4110721387a..4ef757ed589e 100644 --- a/tests/src/python/CMakeLists.txt +++ b/tests/src/python/CMakeLists.txt @@ -340,6 +340,7 @@ ADD_PYTHON_TEST(PyQgsDBManagerGpkg test_db_manager_gpkg.py) ADD_PYTHON_TEST(PyQgsDBManagerSpatialite test_db_manager_spatialite.py) ADD_PYTHON_TEST(PyQgsFileDownloader test_qgsfiledownloader.py) ADD_PYTHON_TEST(PyQgsSettings test_qgssettings.py) +ADD_PYTHON_TEST(PyQgsSettingsEntry test_qgssettingsentry.py) ADD_PYTHON_TEST(PyQgsZipUtils test_qgsziputils.py) ADD_PYTHON_TEST(PyQgsSourceSelectProvider test_qgssourceselectprovider.py) ADD_PYTHON_TEST(PyQgsAuthManagerProxy test_authmanager_proxy.py) diff --git a/tests/src/python/test_qgssettingsentry.py b/tests/src/python/test_qgssettingsentry.py new file mode 100644 index 000000000000..195bc8269020 --- /dev/null +++ b/tests/src/python/test_qgssettingsentry.py @@ -0,0 +1,252 @@ +# -*- coding: utf-8 -*- +""" +Test the QgsSettingsEntry classes + +Run with: ctest -V -R PyQgsSettingsEntry + +.. note:: This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. +""" + +import os +import tempfile +from qgis.core import QgsSettings, QgsSettingsEntryBase, QgsSettingsEntryVariant, QgsSettingsEntryString, QgsSettingsEntryStringList, QgsSettingsEntryBool, QgsSettingsEntryInteger, QgsSettingsEntryDouble, QgsTolerance, QgsMapLayerProxyModel +from qgis.testing import start_app, unittest +from qgis.PyQt.QtCore import QSettings, QVariant +from pathlib import Path + +__author__ = 'Damiano Lombardi' +__date__ = '02/04/2021' +__copyright__ = 'Copyright 2021, The QGIS Project' + + +start_app() + + +class TestQgsSettingsEntry(unittest.TestCase): + + cnt = 0 + + def setUp(self): + self.pluginName = "UnitTest" + + def tearDown(self): + pass + + def test_settings_entry_base(self): + settingsKey = "settingsEntryBase/variantValue" + settingsKeyComplete = self.pluginName + "/" + settingsKey + + # Make sure settings does not exists + QgsSettings().remove(settingsKeyComplete, QgsSettings.Plugins) + + defaultValue = 42 + description = "Variant value for basic functionality test" + settingsEntryVariant = QgsSettingsEntryVariant(settingsKey, self.pluginName, defaultValue, description) + + # Check key + self.assertEqual(settingsEntryVariant.key(), settingsKeyComplete) + + # Passing dynamicKeyPart to a non dynamic settings has no effect + self.assertEqual(settingsEntryVariant.key("gugus"), settingsKeyComplete) + + self.assertEqual(settingsEntryVariant.hasDynamicKey(), False) + + # At this point settings should still not exists in underlyng QSettings as it was still not written (setValue) + self.assertEqual(settingsEntryVariant.exists(), False) + settingsEntryVariant.setValue(43) + self.assertEqual(settingsEntryVariant.exists(), True) + settingsEntryVariant.remove() + self.assertEqual(settingsEntryVariant.exists(), False) + + # Section + self.assertEqual(settingsEntryVariant.section(), QgsSettings.Plugins) + + # DefaultValue + self.assertEqual(settingsEntryVariant.defaultValueAsVariant(), defaultValue) + + # Set/Get value + # as settings still does not exists return default value + self.assertEqual(settingsEntryVariant.valueAsVariant(), defaultValue) + settingsEntryVariant.setValue(43) + # Verify setValue using QgsSettings + self.assertEqual(QgsSettings().value(settingsKeyComplete, defaultValue, section=QgsSettings.Plugins), 43) + self.assertEqual(settingsEntryVariant.valueAsVariant(), 43) + + # Settings type + self.assertEqual(settingsEntryVariant.settingsType(), QgsSettingsEntryBase.Variant) + + # Description + self.assertEqual(settingsEntryVariant.description(), description) + + def test_settings_entry_base_dynamic_key(self): + settingsKeyDynamic = "settingsEntryBase/%/variantValue" + dynamicKeyPart1 = "first" + dynamicKeyPart2 = "second" + settingsKeyComplete1 = self.pluginName + "/" + settingsKeyDynamic.replace("%", dynamicKeyPart1) + settingsKeyComplete2 = self.pluginName + "/" + settingsKeyDynamic.replace("%", dynamicKeyPart2) + + # Make sure settings does not exists + QgsSettings().remove(settingsKeyComplete1, QgsSettings.Plugins) + QgsSettings().remove(settingsKeyComplete2, QgsSettings.Plugins) + + defaultValue = 42 + settingsEntryVariantDynamic = QgsSettingsEntryVariant(settingsKeyDynamic, self.pluginName, defaultValue, "Variant value for dynamic key functionality test") + + # Check key + self.assertEqual(settingsEntryVariantDynamic.key(dynamicKeyPart1), settingsKeyComplete1) + self.assertEqual(settingsEntryVariantDynamic.key(dynamicKeyPart2), settingsKeyComplete2) + + # Get set values + settingsEntryVariantDynamic.setValue(43, dynamicKeyPart1) + settingsEntryVariantDynamic.setValue(44, dynamicKeyPart2) + self.assertEqual(QgsSettings().value(settingsKeyComplete1, defaultValue, section=QgsSettings.Plugins), 43) + self.assertEqual(QgsSettings().value(settingsKeyComplete2, defaultValue, section=QgsSettings.Plugins), 44) + self.assertEqual(settingsEntryVariantDynamic.value(dynamicKeyPart1), 43) + self.assertEqual(settingsEntryVariantDynamic.value(dynamicKeyPart2), 44) + + def test_settings_entry_variant(self): + settingsKey = "settingsEntryVariant/variantValue" + settingsKeyComplete = self.pluginName + "/" + settingsKey + + # Make sure settings does not exists + QgsSettings().remove(settingsKeyComplete, QgsSettings.Plugins) + + defaultValue = 42 + description = "Variant value functionality test" + settingsEntryVariant = QgsSettingsEntryVariant(settingsKey, self.pluginName, defaultValue, description) + + # Set/Get value + # as settings still does not exists return default value + self.assertEqual(settingsEntryVariant.valueAsVariant(), defaultValue) + settingsEntryVariant.setValue("abc") + # Verify setValue using QgsSettings + self.assertEqual(QgsSettings().value(settingsKeyComplete, defaultValue, section=QgsSettings.Plugins), "abc") + self.assertEqual(settingsEntryVariant.valueAsVariant(), "abc") + + # Settings type + self.assertEqual(settingsEntryVariant.settingsType(), QgsSettingsEntryBase.Variant) + + def test_settings_entry_string(self): + settingsKey = "settingsEntryString/stringValue" + settingsKeyComplete = self.pluginName + "/" + settingsKey + + # Make sure settings does not exists + QgsSettings().remove(settingsKeyComplete, QgsSettings.Plugins) + + defaultValue = "abc" + description = "String value functionality test" + settingsEntryString = QgsSettingsEntryString(settingsKey, self.pluginName, defaultValue, description) + + # Set/Get value + # as settings still does not exists return default value + self.assertEqual(settingsEntryString.valueAsVariant(), defaultValue) + settingsEntryString.setValue("xyz") + # Verify setValue using QgsSettings + self.assertEqual(QgsSettings().value(settingsKeyComplete, defaultValue, section=QgsSettings.Plugins), "xyz") + self.assertEqual(settingsEntryString.valueAsVariant(), "xyz") + + # Settings type + self.assertEqual(settingsEntryString.settingsType(), QgsSettingsEntryBase.String) + + def test_settings_entry_stringlist(self): + settingsKey = "settingsEntryStringList/stringListValue" + settingsKeyComplete = self.pluginName + "/" + settingsKey + + # Make sure settings does not exists + QgsSettings().remove(settingsKeyComplete, QgsSettings.Plugins) + + defaultValue = ["abc", "def"] + description = "String list value functionality test" + settingsEntryStringList = QgsSettingsEntryStringList(settingsKey, self.pluginName, defaultValue, description) + + # Set/Get value + # as settings still does not exists return default value + self.assertEqual(settingsEntryStringList.valueAsVariant(), defaultValue) + settingsEntryStringList.setValue(["uvw", "xyz"]) + # Verify setValue using QgsSettings + self.assertEqual(QgsSettings().value(settingsKeyComplete, defaultValue, section=QgsSettings.Plugins), ["uvw", "xyz"]) + self.assertEqual(settingsEntryStringList.valueAsVariant(), ["uvw", "xyz"]) + + # Settings type + self.assertEqual(settingsEntryStringList.settingsType(), QgsSettingsEntryBase.StringList) + + def test_settings_entry_bool(self): + settingsKey = "settingsEntryBool/boolValue" + settingsKeyComplete = self.pluginName + "/" + settingsKey + + # Make sure settings does not exists + QgsSettings().remove(settingsKeyComplete, QgsSettings.Plugins) + + defaultValue = False + description = "Bool value functionality test" + settingsEntryBool = QgsSettingsEntryBool(settingsKey, self.pluginName, defaultValue, description) + + # Set/Get value + # as settings still does not exists return default value + self.assertEqual(settingsEntryBool.valueAsVariant(), defaultValue) + settingsEntryBool.setValue(True) + # Verify setValue using QgsSettings + self.assertEqual(QgsSettings().value(settingsKeyComplete, defaultValue, section=QgsSettings.Plugins), True) + self.assertEqual(settingsEntryBool.valueAsVariant(), True) + + # Settings type + self.assertEqual(settingsEntryBool.settingsType(), QgsSettingsEntryBase.Bool) + + def test_settings_entry_integer(self): + settingsKey = "settingsEntryInteger/integerValue" + settingsKeyComplete = self.pluginName + "/" + settingsKey + + # Make sure settings does not exists + QgsSettings().remove(settingsKeyComplete, QgsSettings.Plugins) + + defaultValue = 42 + description = "Integer value functionality test" + settingsEntryInteger = QgsSettingsEntryInteger(settingsKey, self.pluginName, defaultValue, description) + + # Set/Get value + # as settings still does not exists return default value + self.assertEqual(settingsEntryInteger.valueAsVariant(), defaultValue) + settingsEntryInteger.setValue(43) + # Verify setValue using QgsSettings + self.assertEqual(QgsSettings().value(settingsKeyComplete, defaultValue, section=QgsSettings.Plugins), 43) + self.assertEqual(settingsEntryInteger.valueAsVariant(), 43) + + # Settings type + self.assertEqual(settingsEntryInteger.settingsType(), QgsSettingsEntryBase.Integer) + + def test_settings_entry_double(self): + settingsKey = "settingsEntryDouble/doubleValue" + settingsKeyComplete = self.pluginName + "/" + settingsKey + + # Make sure settings does not exists + QgsSettings().remove(settingsKeyComplete, QgsSettings.Plugins) + + defaultValue = 3.14 + description = "Double value functionality test" + settingsEntryDouble = QgsSettingsEntryDouble(settingsKey, self.pluginName, defaultValue, description) + + # Set/Get value + # as settings still does not exists return default value + self.assertEqual(settingsEntryDouble.valueAsVariant(), defaultValue) + settingsEntryDouble.setValue(1.618) + # Verify setValue using QgsSettings + self.assertEqual(QgsSettings().value(settingsKeyComplete, defaultValue, section=QgsSettings.Plugins), 1.618) + self.assertEqual(settingsEntryDouble.valueAsVariant(), 1.618) + + # Settings type + self.assertEqual(settingsEntryDouble.settingsType(), QgsSettingsEntryBase.Double) + + def test_settings_entry_enum(self): + # Todo implement QgsSettingsEntryEnum for python + pass + + def test_settings_entry_flag(self): + # Todo implement QgsSettingsEntryFlag for python + pass + + +if __name__ == '__main__': + unittest.main()