Skip to content

Commit

Permalink
Unit tests for binary editor widget
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Nov 12, 2018
1 parent 429320d commit 92f34a9
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/gui/editorwidgets/qgsbinarywidgetwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ QgsBinaryWidgetWrapper::QgsBinaryWidgetWrapper( QgsVectorLayer *vl, int fieldIdx

QVariant QgsBinaryWidgetWrapper::value() const
{
return mValue;
return mValue.isEmpty() || mValue.isNull() ? QVariant( QVariant::Invalid ) : mValue;
}

void QgsBinaryWidgetWrapper::showIndeterminateState()
Expand Down Expand Up @@ -109,7 +109,10 @@ bool QgsBinaryWidgetWrapper::valid() const

void QgsBinaryWidgetWrapper::setValue( const QVariant &value )
{
mValue = value.toByteArray();
mValue = value.isValid() && !value.isNull() && value.canConvert< QByteArray >() ? value.toByteArray() : QByteArray();
if ( mValue.length() == 0 )
mValue = QByteArray();

if ( mLabel )
{
if ( !mValue.isEmpty() )
Expand Down
5 changes: 5 additions & 0 deletions tests/src/gui/testqgseditorwidgetregistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ class TestQgsEditorWidgetRegistry: public QObject
checkSimple( QStringLiteral( "string[]" ), QStringLiteral( "List" ) );
}

void binaryType()
{
checkSimple( QStringLiteral( "binary" ), QStringLiteral( "Binary" ) );
}

void configuredType()
{
QgsVectorLayer vl( QStringLiteral( "LineString?crs=epsg:3111&field=pk:int&field=col1:string" ), QStringLiteral( "vl" ), QStringLiteral( "memory" ) );
Expand Down
1 change: 1 addition & 0 deletions tests/src/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ ADD_PYTHON_TEST(PyQgsAttributeTableConfig test_qgsattributetableconfig.py)
ADD_PYTHON_TEST(PyQgsAttributeTableModel test_qgsattributetablemodel.py)
ADD_PYTHON_TEST(PyQgsAuthenticationSystem test_qgsauthsystem.py)
ADD_PYTHON_TEST(PyQgsBearingUtils test_qgsbearingutils.py)
ADD_PYTHON_TEST(PyQgsBinaryWidget test_qgsbinarywidget.py)
ADD_PYTHON_TEST(PyQgsBlendModes test_qgsblendmodes.py)
ADD_PYTHON_TEST(PyQgsBox3d test_qgsbox3d.py)
ADD_PYTHON_TEST(PyQgsCategorizedSymbolRenderer test_qgscategorizedsymbolrenderer.py)
Expand Down
71 changes: 71 additions & 0 deletions tests/src/python/test_qgsbinarywidget.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for binary editor widgets.
.. 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.
"""
__author__ = 'Nyall Dawson'
__date__ = '11/11/2018'
__copyright__ = 'Copyright 2018, The QGIS Project'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

import qgis # NOQA

from qgis.PyQt.QtCore import QByteArray
from qgis.core import QgsFeature, QgsGeometry, QgsPointXY, QgsVectorLayer, NULL
from qgis.gui import QgsGui
from qgis.testing import start_app, unittest

start_app()


class TestQgsBinaryWidget(unittest.TestCase):

@classmethod
def setUpClass(cls):
QgsGui.editorWidgetRegistry().initEditors()

def setUp(self):
"""
create a layer with one feature
"""
self.layer = QgsVectorLayer("Point?crs=EPSG:21781&field=fldint:integer&field=fldbin:binary",
"addfeat", "memory")
self.assertTrue(self.layer.isValid())
f = QgsFeature()
bin_1 = b'xxx'
bin_val1 = QByteArray(bin_1)
f.setAttributes([123, bin_val1])
f.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(600000, 200000)))

def __createBinaryWidget(self):
"""
create a binary widget
"""
reg = QgsGui.editorWidgetRegistry()
configWdg = reg.createConfigWidget('Binary', self.layer, 1, None)
config = configWdg.config()
binary_widget = reg.create('Binary', self.layer, 1, config, None, None)
return binary_widget

def testValue(self):
widget = self.__createBinaryWidget()
self.assertTrue(widget.widget())

self.assertFalse(widget.value())

bin_2 = b'yyy'
bin_val2 = QByteArray(bin_2)

widget.setValue(bin_val2)
self.assertEqual(widget.value(), bin_val2)

widget.setValue(NULL)
self.assertEqual(widget.value(), NULL)


if __name__ == '__main__':
unittest.main()

0 comments on commit 92f34a9

Please sign in to comment.