mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-15 00:04:00 -04:00
Python exceptions for invalid keys in QgsFields methods (fix #13221)
This commit is contained in:
parent
e800419c9d
commit
42d5b9b2aa
@ -215,6 +215,18 @@ class QgsFields
|
||||
bool appendExpressionField( const QgsField& field, int originIndex );
|
||||
//! Remove a field with the given index
|
||||
void remove( int fieldIdx );
|
||||
%MethodCode
|
||||
if ( a0 < 0 || a0 >= sipCpp->count() )
|
||||
{
|
||||
PyErr_SetString(PyExc_KeyError, QByteArray::number(a0));
|
||||
sipIsErr = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
sipCpp->remove( a0 );
|
||||
}
|
||||
%End
|
||||
|
||||
//! Extend with fields from another QgsFields container
|
||||
void extend( const QgsFields& other );
|
||||
|
||||
@ -244,19 +256,78 @@ class QgsFields
|
||||
sipIsErr = 1;
|
||||
else
|
||||
sipRes = new QgsField(sipCpp->operator[](idx));
|
||||
|
||||
%End
|
||||
|
||||
//! Get field at particular index (must be in range 0..N-1)
|
||||
const QgsField& at( int i ) const;
|
||||
const QgsField& at( int i ) const /Factory/;
|
||||
%MethodCode
|
||||
if ( a0 < 0 || a0 >= sipCpp->count() )
|
||||
{
|
||||
PyErr_SetString(PyExc_KeyError, QByteArray::number(a0));
|
||||
sipIsErr = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
sipRes = new QgsField( sipCpp->at( a0 ) );
|
||||
}
|
||||
%End
|
||||
|
||||
//! Get field at particular index (must be in range 0..N-1)
|
||||
const QgsField& field( int fieldIdx ) const;
|
||||
const QgsField& field( int fieldIdx ) const /Factory/;
|
||||
%MethodCode
|
||||
if ( a0 < 0 || a0 >= sipCpp->count() )
|
||||
{
|
||||
PyErr_SetString(PyExc_KeyError, QByteArray::number(a0));
|
||||
sipIsErr = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
sipRes = new QgsField( sipCpp->field( a0 ) );
|
||||
}
|
||||
%End
|
||||
|
||||
//! Get field at particular index (must be in range 0..N-1)
|
||||
const QgsField& field( const QString& name ) const;
|
||||
const QgsField& field( const QString& name ) const /Factory/;
|
||||
%MethodCode
|
||||
int fieldIdx = sipCpp->indexFromName(*a0);
|
||||
if (fieldIdx == -1)
|
||||
{
|
||||
PyErr_SetString(PyExc_KeyError, a0->toAscii());
|
||||
sipIsErr = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
sipRes = new QgsField( sipCpp->field( *a0 ) );
|
||||
}
|
||||
%End
|
||||
|
||||
//! Get field's origin (value from an enumeration)
|
||||
FieldOrigin fieldOrigin( int fieldIdx ) const;
|
||||
%MethodCode
|
||||
if ( a0 < 0 || a0 >= sipCpp->count() )
|
||||
{
|
||||
PyErr_SetString(PyExc_KeyError, QByteArray::number(a0));
|
||||
sipIsErr = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
sipRes = sipCpp->fieldOrigin( a0 );
|
||||
}
|
||||
%End
|
||||
|
||||
//! Get field's origin index (its meaning is specific to each type of origin)
|
||||
int fieldOriginIndex( int fieldIdx ) const;
|
||||
%MethodCode
|
||||
if ( a0 < 0 || a0 >= sipCpp->count() )
|
||||
{
|
||||
PyErr_SetString(PyExc_KeyError, QByteArray::number(a0));
|
||||
sipIsErr = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
sipRes = sipCpp->fieldOriginIndex( a0 );
|
||||
}
|
||||
%End
|
||||
|
||||
//! Look up field's index from name. Returns -1 on error
|
||||
int indexFromName( const QString& name ) const;
|
||||
|
@ -53,6 +53,7 @@ ADD_PYTHON_TEST(PyQgsShapefileProvider test_provider_shapefile.py)
|
||||
ADD_PYTHON_TEST(PyQgsMemoryProvider test_provider_memory.py)
|
||||
ADD_PYTHON_TEST(PyQgsVectorColorRamp test_qgsvectorcolorramp.py)
|
||||
ADD_PYTHON_TEST(PyQgsSyntacticSugar test_syntactic_sugar.py)
|
||||
ADD_PYTHON_TEST(PyQgsField test_qgsfield.py)
|
||||
|
||||
IF (NOT WIN32)
|
||||
ADD_PYTHON_TEST(PyQgsLogger test_qgslogger.py)
|
||||
|
84
tests/src/python/test_qgsfield.py
Normal file
84
tests/src/python/test_qgsfield.py
Normal file
@ -0,0 +1,84 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""QGIS Unit tests for QgsField.
|
||||
|
||||
.. 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__ = '16/08/2015'
|
||||
__copyright__ = 'Copyright 2015, The QGIS Project'
|
||||
# This will get replaced with a git SHA1 when you do a git archive
|
||||
__revision__ = '$Format:%H$'
|
||||
|
||||
import qgis
|
||||
import os
|
||||
|
||||
from qgis.core import QgsField, QgsVectorLayer, NULL
|
||||
from utilities import (unitTestDataPath,
|
||||
getQgisTestApp,
|
||||
TestCase,
|
||||
unittest
|
||||
)
|
||||
from unittest import expectedFailure
|
||||
|
||||
QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp()
|
||||
|
||||
class TestQgsFields(TestCase):
|
||||
|
||||
def test_expections(self):
|
||||
ml=QgsVectorLayer("Point?crs=epsg:4236&field=id:integer&field=value:double",
|
||||
"test_data", "memory")
|
||||
assert ml.isValid()
|
||||
fields=ml.fields()
|
||||
|
||||
#check no error
|
||||
fields.remove(1)
|
||||
#check exceptions raised
|
||||
with self.assertRaises(KeyError):
|
||||
fields.remove(-1)
|
||||
with self.assertRaises(KeyError):
|
||||
fields.remove(111)
|
||||
|
||||
fields=ml.fields()
|
||||
#check no error
|
||||
fields.at(1)
|
||||
#check exceptions raised
|
||||
with self.assertRaises(KeyError):
|
||||
fields.at(-1)
|
||||
with self.assertRaises(KeyError):
|
||||
fields.at(111)
|
||||
|
||||
#check no error
|
||||
fields.field(1)
|
||||
#check exceptions raised
|
||||
with self.assertRaises(KeyError):
|
||||
fields.field(-1)
|
||||
with self.assertRaises(KeyError):
|
||||
fields.field(111)
|
||||
|
||||
#check no error
|
||||
fields.field('value')
|
||||
#check exceptions raised
|
||||
with self.assertRaises(KeyError):
|
||||
fields.field('bad')
|
||||
|
||||
#check no error
|
||||
fields.fieldOrigin(1)
|
||||
#check exceptions raised
|
||||
with self.assertRaises(KeyError):
|
||||
fields.fieldOrigin(-1)
|
||||
with self.assertRaises(KeyError):
|
||||
fields.fieldOrigin(111)
|
||||
|
||||
#check no error
|
||||
fields.fieldOriginIndex(1)
|
||||
#check exceptions raised
|
||||
with self.assertRaises(KeyError):
|
||||
fields.fieldOriginIndex(-1)
|
||||
with self.assertRaises(KeyError):
|
||||
fields.fieldOriginIndex(111)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
Loading…
x
Reference in New Issue
Block a user