From 6c3f418988fa9d60d36cf1a511db9b3ecf2f46b9 Mon Sep 17 00:00:00 2001 From: Tim Sutton Date: Mon, 1 Apr 2013 12:58:53 +0200 Subject: [PATCH] Use ~.qgis2 and ~/.config/QuantumGIS/QGIS2.conf for settings for QGIS 2. Added unit test for memory provider. --- python/utils.py | 2 +- src/app/main.cpp | 2 +- src/app/qgisapp.cpp | 3 +- src/core/qgsapplication.cpp | 2 +- tests/src/python/test_qgsmemoryprovider.py | 35 ++++- tests/src/python/test_qgsvectorlayer.py | 171 ++++++++++----------- 6 files changed, 119 insertions(+), 96 deletions(-) diff --git a/python/utils.py b/python/utils.py index c6bc753e02e..768ff61dc0e 100755 --- a/python/utils.py +++ b/python/utils.py @@ -140,7 +140,7 @@ def findPlugins(path): def updateAvailablePlugins(): - """ go thrgouh the plugin_paths list and find out what plugins are available """ + """ Go through the plugin_paths list and find out what plugins are available. """ # merge the lists plugins = [] metadata_parser = {} diff --git a/src/app/main.cpp b/src/app/main.cpp index daab5bebdfe..35e3d9c8c3e 100644 --- a/src/app/main.cpp +++ b/src/app/main.cpp @@ -530,7 +530,7 @@ int main( int argc, char *argv[] ) // Set up the QSettings environment must be done after qapp is created QCoreApplication::setOrganizationName( "QuantumGIS" ); QCoreApplication::setOrganizationDomain( "qgis.org" ); - QCoreApplication::setApplicationName( "QGIS" ); + QCoreApplication::setApplicationName( "QGIS2" ); QCoreApplication::setAttribute( Qt::AA_DontShowIconsInMenus, false ); QSettings* customizationsettings; diff --git a/src/app/qgisapp.cpp b/src/app/qgisapp.cpp index 52093858041..d2dcf5daef9 100644 --- a/src/app/qgisapp.cpp +++ b/src/app/qgisapp.cpp @@ -865,7 +865,8 @@ void QgisApp::readSettings() { QSettings settings; // get the users theme preference from the settings - setTheme( settings.value( "/Themes", "default" ).toString() ); + // defaulting to 'gis' theme + setTheme( settings.value( "/Themes", "gis" ).toString() ); // Add the recently accessed project file paths to the File menu mRecentProjectPaths = settings.value( "/UI/recentProjectsList" ).toStringList(); diff --git a/src/core/qgsapplication.cpp b/src/core/qgsapplication.cpp index 5341114dd60..a04e78cba0e 100644 --- a/src/core/qgsapplication.cpp +++ b/src/core/qgsapplication.cpp @@ -84,7 +84,7 @@ void QgsApplication::init( QString customConfigPath ) { if ( customConfigPath.isEmpty() ) { - customConfigPath = QDir::homePath() + QString( "/.qgis/" ); + customConfigPath = QDir::homePath() + QString( "/.qgis2/" ); } qRegisterMetaType( "QgsGeometry::Error" ); diff --git a/tests/src/python/test_qgsmemoryprovider.py b/tests/src/python/test_qgsmemoryprovider.py index 4e567b72413..409e64e4bd2 100644 --- a/tests/src/python/test_qgsmemoryprovider.py +++ b/tests/src/python/test_qgsmemoryprovider.py @@ -29,6 +29,7 @@ from utilities import (getQgisTestApp, ) QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() + class TestQgsMemoryProvider(TestCase): def testPointCtor(self): @@ -56,7 +57,7 @@ class TestQgsMemoryProvider(TestCase): assert res, "Failed to add attributes" myMessage = ('Expected: %s\nGot: %s\n' % - (3, len(provider.fields()))) + (3, len(provider.fields()))) assert len(provider.fields()) == 3, myMessage @@ -76,17 +77,17 @@ class TestQgsMemoryProvider(TestCase): for f in provider.getFeatures(QgsFeatureRequest()): attrs = f.attributes() myMessage = ('Expected: %s\nGot: %s\n' % - ("Johny", str(attrs[0].toString()))) + ("Johny", str(attrs[0].toString()))) assert str(attrs[0].toString()) == "Johny", myMessage myMessage = ('Expected: %s\nGot: %s\n' % - (20, attrs[1].toInt()[0])) + (20, attrs[1].toInt()[0])) assert attrs[1].toInt()[0] == 20, myMessage myMessage = ('Expected: %s\nGot: %s\n' % - (0.3, attrs[2].toFloat()[0])) + (0.3, attrs[2].toFloat()[0])) assert (attrs[0].toFloat()[0] - 0.3) < 0.0000001, myMessage @@ -97,6 +98,32 @@ class TestQgsMemoryProvider(TestCase): assert str(geom.exportToWkt()) == "POINT(10.0 10.0)", myMessage + def testGetFields(self): + layer = QgsVectorLayer("Point", "test", "memory") + provider = layer.dataProvider() + + provider.addAttributes([QgsField("name", QVariant.String,), + QgsField("age", QVariant.Int), + QgsField("size", QVariant.Double)]) + myMessage = ('Expected: %s\nGot: %s\n' % + (3, len(provider.fields()))) + + assert len(provider.fields()) == 3, myMessage + + ft = QgsFeature() + ft.setGeometry(QgsGeometry.fromPoint(QgsPoint(10,10))) + ft.setAttributes([QVariant("Johny"), + QVariant(20), + QVariant(0.3)]) + provider.addFeatures([ft]) + + for f in provider.getFeatures(QgsFeatureRequest()): + myMessage = ('Expected: %s\nGot: %s\n' % + ("Johny", str(f['name'].toString()))) + + self.assertEqual(str(f["name"].toString()), "Johny", myMessage) + + def testFromUri(self): """Test we can construct the mem provider from a uri""" myMemoryLayer = QgsVectorLayer( diff --git a/tests/src/python/test_qgsvectorlayer.py b/tests/src/python/test_qgsvectorlayer.py index 39d1e2f5ccf..5e4fde7347f 100644 --- a/tests/src/python/test_qgsvectorlayer.py +++ b/tests/src/python/test_qgsvectorlayer.py @@ -15,13 +15,21 @@ __revision__ = '$Format:%H$' import os from PyQt4.QtCore import QVariant -from qgis.core import QgsVectorLayer, QgsFeature, QgsFeatureRequest, QgsGeometry, QgsPoint, QgsField, QgsFields, QgsMapLayerRegistry, QgsVectorJoinInfo +from qgis.core import (QgsVectorLayer, + QgsFeature, + QgsFeatureRequest, + QgsGeometry, + QgsPoint, + QgsField, + QgsFields, + QgsMapLayerRegistry, + QgsVectorJoinInfo) from utilities import (unitTestDataPath, getQgisTestApp, TestCase, unittest, #expectedFailure - ) + # ) QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() def createEmptyLayer(): @@ -30,7 +38,8 @@ def createEmptyLayer(): return layer def createLayerWithOnePoint(): - layer = QgsVectorLayer("Point?field=fldtxt:string&field=fldint:integer", "addfeat", "memory") + layer = QgsVectorLayer("Point?field=fldtxt:string&field=fldint:integer", + "addfeat", "memory") pr = layer.dataProvider() f = QgsFeature() f.setAttributes([QVariant("test"), QVariant(123)]) @@ -40,7 +49,9 @@ def createLayerWithOnePoint(): return layer def createJoinLayer(): - joinLayer = QgsVectorLayer("Point?field=x:string&field=y:integer&field=z:integer", "joinlayer", "memory") + joinLayer = QgsVectorLayer( + "Point?field=x:string&field=y:integer&field=z:integer", + "joinlayer", "memory") pr = joinLayer.dataProvider() f1 = QgsFeature() f1.setAttributes([QVariant("foo"), QVariant(123), QVariant(321)]) @@ -63,18 +74,24 @@ def dumpFeature(f): print "no geometry" print "attrs: %s" % str(f.attributes()) + def formatAttributes(attrs): return repr([ unicode(a.toString()) for a in attrs ]) + def dumpEditBuffer(layer): editBuffer = layer.editBuffer() if not editBuffer: - print "NO EDITING!" - return + print "NO EDITING!" + return print "ADDED:" - for fid,f in editBuffer.addedFeatures().iteritems(): print "%d: %s | %s" % (f.id(), formatAttributes(f.attributes()), f.geometry().exportToWkt()) + for fid,f in editBuffer.addedFeatures().iteritems(): + print "%d: %s | %s" % ( + f.id(), formatAttributes(f.attributes()), + f.geometry().exportToWkt()) print "CHANGED GEOM:" - for fid,geom in editBuffer.changedGeometries().iteritems(): print "%d | %s" % (f.id(), f.geometry().exportToWkt()) + for fid, geom in editBuffer.changedGeometries().iteritems(): + print "%d | %s" % (f.id(), f.geometry().exportToWkt()) class TestQgsVectorLayer(TestCase): @@ -87,9 +104,7 @@ class TestQgsVectorLayer(TestCase): myMessage = '\nExpected: %s\nGot: %s' % (myCount, myExpectedCount) assert myCount == myExpectedCount, myMessage - """ - ADD FEATURE - """ + # ADD FEATURE def test_AddFeature(self): layer = createEmptyLayer() @@ -113,7 +128,6 @@ class TestQgsVectorLayer(TestCase): # check select+nextFeature self.assertRaises(StopIteration, layer.getFeatures().next) - checkBefore() # try to add feature without editing mode @@ -137,9 +151,7 @@ class TestQgsVectorLayer(TestCase): checkAfter() assert layer.dataProvider().featureCount() == 1 - """ - DELETE FEATURE - """ + #DELETE FEATURE def test_DeleteFeature(self): layer = createLayerWithOnePoint() @@ -152,7 +164,8 @@ class TestQgsVectorLayer(TestCase): self.assertRaises(StopIteration, layer.getFeatures().next) # check feature at id - self.assertRaises(StopIteration, layer.getFeatures(QgsFeatureRequest(fid)).next) + self.assertRaises(StopIteration, + layer.getFeatures(QgsFeatureRequest(fid)).next) def checkBefore(): assert layer.pendingFeatureCount() == 1 @@ -166,7 +179,6 @@ class TestQgsVectorLayer(TestCase): # check feature at id f2 = layer.getFeatures(QgsFeatureRequest(fid)).next() - checkBefore() # try to delete feature without editing mode @@ -194,7 +206,6 @@ class TestQgsVectorLayer(TestCase): checkAfter() assert layer.dataProvider().featureCount() == 0 - def test_DeleteFeatureAfterAddFeature(self): layer = createEmptyLayer() @@ -213,7 +224,6 @@ class TestQgsVectorLayer(TestCase): def checkAfter2(): checkBefore() # should be the same state: no features - checkBefore() # add feature @@ -239,9 +249,7 @@ class TestQgsVectorLayer(TestCase): assert layer.dataProvider().featureCount() == 0 - """ - CHANGE ATTRIBUTE - """ + #CHANGE ATTRIBUTE def test_ChangeAttribute(self): layer = createLayerWithOnePoint() @@ -335,9 +343,7 @@ class TestQgsVectorLayer(TestCase): #print "COMMIT ERRORS:" #for item in list(layer.commitErrors()): print item - """ - CHANGE GEOMETRY - """ + # CHANGE GEOMETRY def test_ChangeGeometry(self): layer = createLayerWithOnePoint() @@ -378,8 +384,6 @@ class TestQgsVectorLayer(TestCase): assert layer.commitChanges() checkAfter() - - def test_ChangeGeometryAfterChangeAttribute(self): layer = createLayerWithOnePoint() fid = 1 @@ -464,9 +468,7 @@ class TestQgsVectorLayer(TestCase): #print "COMMIT ERRORS:" #for item in list(layer.commitErrors()): print item - """ - ADD ATTRIBUTE - """ + # ADD ATTRIBUTE def test_AddAttribute(self): layer = createLayerWithOnePoint() @@ -509,7 +511,9 @@ class TestQgsVectorLayer(TestCase): assert f2[1].toInt()[0] == 123 assert not f2[2].isValid() - #for nt in layer.dataProvider().nativeTypes(): print nt.mTypeDesc, nt.mTypeName, nt.mType, nt.mMinLen, nt.mMaxLen, nt.mMinPrec, nt.mMaxPrec + #for nt in layer.dataProvider().nativeTypes(): + # print (nt.mTypeDesc, nt.mTypeName, nt.mType, nt.mMinLen, + # nt.mMaxLen, nt.mMinPrec, nt.mMaxPrec) assert layer.dataProvider().supportedType(fld1) # without editing mode @@ -562,7 +566,6 @@ class TestQgsVectorLayer(TestCase): assert f2[1].toInt()[0] == 42 assert not f2[2].isValid() - layer.startEditing() checkBefore() @@ -587,35 +590,36 @@ class TestQgsVectorLayer(TestCase): #for item in list(layer.commitErrors()): print item def test_AddAttributeAfterChangeValue(self): - pass # not interesting to test...? + pass # not interesting to test...? + def test_AddAttributeAfterDeleteAttribute(self): pass # maybe it would be good to test - """ - DELETE ATTRIBUTE - """ + # DELETE ATTRIBUTE def test_DeleteAttribute(self): layer = createLayerWithOnePoint() - layer.dataProvider().addAttributes( [QgsField("flddouble", QVariant.Double, "double")] ) - layer.dataProvider().changeAttributeValues( { 1: { 2 : QVariant(5.5) } } ) + layer.dataProvider().addAttributes( + [QgsField("flddouble", QVariant.Double, "double")] ) + layer.dataProvider().changeAttributeValues( + {1:{2: QVariant(5.5)}}) # without editing mode assert not layer.deleteAttribute(0) def checkBefore(): - flds = layer.pendingFields() - assert len(flds) == 3 - assert flds[0].name() == "fldtxt" - assert flds[1].name() == "fldint" - assert flds[2].name() == "flddouble" + flds = layer.pendingFields() + assert len(flds) == 3 + assert flds[0].name() == "fldtxt" + assert flds[1].name() == "fldint" + assert flds[2].name() == "flddouble" - f = layer.getFeatures().next() - attrs = f.attributes() - assert len(attrs) == 3 - assert attrs[0].toString() == "test" - assert attrs[1].toInt()[0] == 123 - assert attrs[2].toDouble()[0] == 5.5 + f = layer.getFeatures().next() + attrs = f.attributes() + assert len(attrs) == 3 + assert attrs[0].toString() == "test" + assert attrs[1].toInt()[0] == 123 + assert attrs[2].toDouble()[0] == 5.5 layer.startEditing() @@ -624,39 +628,39 @@ class TestQgsVectorLayer(TestCase): assert layer.deleteAttribute(0) def checkAfterOneDelete(): - flds = layer.pendingFields() - #for fld in flds: print "FLD", fld.name() - assert len(flds) == 2 - assert flds[0].name() == "fldint" - assert flds[1].name() == "flddouble" - assert layer.pendingAllAttributesList() == [0,1] + flds = layer.pendingFields() + #for fld in flds: print "FLD", fld.name() + assert len(flds) == 2 + assert flds[0].name() == "fldint" + assert flds[1].name() == "flddouble" + assert layer.pendingAllAttributesList() == [0,1] - f = layer.getFeatures().next() - attrs = f.attributes() - assert len(attrs) == 2 - assert attrs[0].toInt()[0] == 123 - assert attrs[1].toDouble()[0] == 5.5 + f = layer.getFeatures().next() + attrs = f.attributes() + assert len(attrs) == 2 + assert attrs[0].toInt()[0] == 123 + assert attrs[1].toDouble()[0] == 5.5 - checkAfterOneDelete() + checkAfterOneDelete() # delete last attribute assert layer.deleteAttribute(0) def checkAfterTwoDeletes(): - assert layer.pendingAllAttributesList() == [0] - flds = layer.pendingFields() - #for fld in flds: print "FLD", fld.name() - assert len(flds) == 1 - assert flds[0].name() == "flddouble" + assert layer.pendingAllAttributesList() == [0] + flds = layer.pendingFields() + #for fld in flds: print "FLD", fld.name() + assert len(flds) == 1 + assert flds[0].name() == "flddouble" - f = layer.getFeatures().next() - attrs = f.attributes() - assert len(attrs) == 1 - assert attrs[0].toDouble()[0] == 5.5 - # check feature at id - f2 = layer.getFeatures(QgsFeatureRequest( f.id() )).next() - assert len(f2.attributes()) == 1 - assert f2[0].toDouble()[0] == 5.5 + f = layer.getFeatures().next() + attrs = f.attributes() + assert len(attrs) == 1 + assert attrs[0].toDouble()[0] == 5.5 + # check feature at id + f2 = layer.getFeatures(QgsFeatureRequest( f.id() )).next() + assert len(f2.attributes()) == 1 + assert f2[0].toDouble()[0] == 5.5 checkAfterTwoDeletes() layer.undoStack().undo() @@ -671,7 +675,6 @@ class TestQgsVectorLayer(TestCase): assert layer.commitChanges() # COMMIT! checkAfterTwoDeletes() - def test_DeleteAttributeAfterAddAttribute(self): layer = createLayerWithOnePoint() fld1 = QgsField("fld1", QVariant.Int, "integer") @@ -714,7 +717,6 @@ class TestQgsVectorLayer(TestCase): layer.commitChanges() checkAfter() - def test_DeleteAttributeAfterAddFeature(self): layer = createLayerWithOnePoint() layer.dataProvider().deleteFeatures([1]) # no need for this feature @@ -767,8 +769,6 @@ class TestQgsVectorLayer(TestCase): layer.commitChanges() checkAfter2() - - def test_DeleteAttributeAfterChangeValue(self): layer = createLayerWithOnePoint() @@ -817,8 +817,6 @@ class TestQgsVectorLayer(TestCase): layer.commitChanges() checkAfter2() - - def test_fields(self): layer = createLayerWithOnePoint() @@ -826,7 +824,6 @@ class TestQgsVectorLayer(TestCase): assert flds.indexFromName("fldint") == 1 assert flds.indexFromName("fldXXX") == -1 - def test_getFeatures(self): layer = createLayerWithOnePoint() @@ -884,7 +881,6 @@ class TestQgsVectorLayer(TestCase): assert f2[2].toString() == "foo" assert f2[3].toInt()[0] == 321 - def test_InvalidOperations(self): layer = createLayerWithOnePoint() @@ -898,12 +894,14 @@ class TestQgsVectorLayer(TestCase): # DELETE FEATURE assert not layer.deleteFeature(-333) - # we do not check for existence of the feature id if it's not newly added feature + # we do not check for existence of the feature id if it's + # not newly added feature #assert not layer.deleteFeature(333) # CHANGE GEOMETRY - assert not layer.changeGeometry(-333, QgsGeometry.fromPoint(QgsPoint(1,1))) + assert not layer.changeGeometry( + -333, QgsGeometry.fromPoint(QgsPoint(1, 1))) # CHANGE VALUE @@ -912,19 +910,16 @@ class TestQgsVectorLayer(TestCase): # ADD ATTRIBUTE - assert not layer.addAttribute( QgsField() ) + assert not layer.addAttribute(QgsField()) # DELETE ATTRIBUTE assert not layer.deleteAttribute(-1) - # TODO: # - fetch rect: feat with changed geometry: 1. in rect, 2. out of rect # - more join tests # - import - if __name__ == '__main__': unittest.main() -