Skip to content
Permalink
Browse files

basic tests for json writing

  • Loading branch information
signedav committed Dec 17, 2018
1 parent 8f5a732 commit 7dbd7a11000c9446dcb5895806474477bd8ebcc4
@@ -44,5 +44,5 @@ QgsEditorConfigWidget *QgsListWidgetFactory::configWidget( QgsVectorLayer *vl, i
unsigned int QgsListWidgetFactory::fieldScore( const QgsVectorLayer *vl, int fieldIdx ) const
{
const QgsField field = vl->fields().field( fieldIdx );
return ( field.type() == QVariant::List || field.type() == QVariant::StringList || field.typeName() == QLatin1String( "json" ) || field.typeName() == QLatin1String( "jsonb" ) ) && field.subType() != QVariant::Invalid ? 20 : 0;
return ( field.type() == QVariant::List || field.type() == QVariant::StringList || field.typeName() == QLatin1String( "json" ) || field.typeName() == QLatin1String( "jsonb" ) || field.typeName() == QLatin1String( "JSON" ) ) && field.subType() != QVariant::Invalid ? 20 : 0;
}
@@ -1016,7 +1016,7 @@ void QgsOgrProvider::loadFields()
OGRFieldSubType ogrSubType = OFSTNone;

QVariant::Type varType;
QVariant::Type varSubType;
QVariant::Type varSubType = QVariant::Invalid;
switch ( ogrType )
{
case OFTInteger:
@@ -1529,17 +1529,29 @@ bool QgsOgrProvider::addFeaturePrivate( QgsFeature &f, Flags flags )
break;

case OFTString:
if ( OGR_Fld_GetSubType( fldDef ) == OFSTJSON )
{
QString stringValue;
/*
* if ( OGR_Fld_GetSubType( fldDef ) == OFSTJSON )
{
QgsDebugMsgLevel( QStringLiteral( "Does it need to be decoded or something DAVE? JSON Stuff" ), 3 );
stringValue = QString::fromUtf8( QJsonDocument::fromVariant( attrVal.toMap() ).toJson().data() );
if( stringValue == QStringLiteral( "{\n}\n" ) )
stringValue = QString::fromUtf8( QJsonDocument::fromVariant( attrVal.toList() ).toJson().data() );
if( stringValue == QStringLiteral( "[\n]\n" ) )
stringValue = QString::fromUtf8( QJsonDocument::fromVariant( attrVal.toString() ).toJson().data() );
}
else
{
*/ stringValue = attrVal.toString();
//}

QgsDebugMsgLevel( QStringLiteral( "Writing string attribute %1 with %2, encoding %3" )
.arg( qgisAttId )
.arg( attrVal.toString(),
textEncoding()->name().data() ), 3 );
OGR_F_SetFieldString( feature.get(), ogrAttId, textEncoding()->fromUnicode( attrVal.toString() ).constData() );
OGR_F_SetFieldString( feature.get(), ogrAttId, textEncoding()->fromUnicode( stringValue ).constData() );
break;

}
case OFTBinary:
{
const QByteArray ba = attrVal.toByteArray();
@@ -2110,14 +2122,20 @@ bool QgsOgrProvider::changeAttributeValues( const QgsChangedAttributesMap &attr_
case OFTString:
{
QString stringValue;
if ( OGR_Fld_GetSubType( fd ) == OFSTJSON )
/*
* if ( OGR_Fld_GetSubType( fd ) == OFSTJSON )
{
stringValue = QString::fromUtf8( QJsonDocument::fromVariant( it2->toMap() ).toJson().data() );
if( stringValue == QStringLiteral( "{\n}\n" ) )
stringValue = QString::fromUtf8( QJsonDocument::fromVariant( it2->toList() ).toJson().data() );
if( stringValue == QStringLiteral( "[\n]\n" ) )
stringValue = QString::fromUtf8( QJsonDocument::fromVariant( it2->toString() ).toJson().data() );
}
else
{
stringValue = it2->toString();
}
{*/
stringValue = it2->toString();
//}

OGR_F_SetFieldString( of.get(), f, textEncoding()->fromUnicode( stringValue ).constData() );
break;
}
@@ -1244,14 +1244,25 @@ def testJson(self):
fi = vl.getFeatures(QgsFeatureRequest())
f = QgsFeature()

#test dict value
#test reading dict value from attribute
while fi.nextFeature(f):
fid = vl.fields().lookupField('fid')
if fid == 1:
value = vl.fields().lookupField('json_content')
self.assertIsInstance(f.attributes()[value], dict)
self.assertEqual(f.attributes()[value_idx], {'foo': 'bar'})
break
if f['fid'] == 1:
self.assertIsInstance(f['json_content'], dict)
self.assertEqual(f['json_content'], {'foo': 'bar'})
#test changing dict value in attribute
f['json_content'] = {'foo': 'baz'}
self.assertEqual(f['json_content'], {'foo': 'baz'})
#test changint dict to list
f['json_content'] = ['eins', 'zwei', 'drei']
self.assertEqual(f['json_content'], ['eins', 'zwei', 'drei'])
#test changing list value in attribute
f['json_content'] = ['eins', 'zwei', 'drei', 4]
self.assertEqual(f['json_content'], ['eins', 'zwei', 'drei', 4])

#test adding attribute with list value
#test changing to invalid value in attribute
#test changing list ot string
#test reading string


if __name__ == '__main__':

0 comments on commit 7dbd7a1

Please sign in to comment.
You can’t perform that action at this time.