test_attributeformmodel.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /***************************************************************************
  2. test_attributeformmodel.h
  3. --------------------
  4. begin : Jul 2021
  5. copyright : (C) 2021 by Mathieu Pellerin
  6. email : mathieu@opengis.ch
  7. ***************************************************************************/
  8. /***************************************************************************
  9. * *
  10. * This program is free software; you can redistribute it and/or modify *
  11. * it under the terms of the GNU General Public License as published by *
  12. * the Free Software Foundation; either version 2 of the License, or *
  13. * (at your option) any later version. *
  14. * *
  15. ***************************************************************************/
  16. #define QFIELDTEST_MAIN
  17. #include "attributeformmodel.h"
  18. #include "featuremodel.h"
  19. #include "catch2.h"
  20. TEST_CASE( "Attribute form model" )
  21. {
  22. std::unique_ptr<QgsVectorLayer> layer = std::make_unique<QgsVectorLayer>( QStringLiteral( "Point?crs=EPSG:3857&field=fid:integer&field=str:string&field=str2:string" ), QStringLiteral( "Input Layer" ), QStringLiteral( "memory" ) );
  23. REQUIRE( layer->isValid() );
  24. QgsFeature f1;
  25. f1.setAttributes( QgsAttributes() << 1 << QStringLiteral( "string_a1" ) << QStringLiteral( "string_b1" ) );
  26. qDebug() << f1.attributes();
  27. QgsFeature f2;
  28. f2.setAttributes( QgsAttributes() << 2 << QStringLiteral( "string_a2" ) << QStringLiteral( "string_b2" ) );
  29. QgsFeature f3;
  30. f3.setAttributes( QgsAttributes() << 1 << QStringLiteral( "string_a3" ) << QStringLiteral( "string_b3" ) );
  31. layer->startEditing();
  32. layer->addFeature( f1 );
  33. layer->addFeature( f2 );
  34. layer->addFeature( f3 );
  35. layer->commitChanges();
  36. REQUIRE( layer->featureCount() == 3 );
  37. layer->setDefaultValueDefinition( 2, QgsDefaultValue( QStringLiteral( " coalesce(\"str\",'') || '__'" ), true ) );
  38. std::unique_ptr<AttributeFormModel> attributeFormModel = std::make_unique< AttributeFormModel >();
  39. std::unique_ptr<FeatureModel> featureModel = std::make_unique< FeatureModel >();
  40. attributeFormModel->setFeatureModel( featureModel.get() );
  41. featureModel->setCurrentLayer( layer.get() );
  42. SECTION( "Attributes" )
  43. {
  44. featureModel->setFeature( layer->getFeature( 1 ) );
  45. REQUIRE( attributeFormModel->attribute( QStringLiteral( "str" ) ) == QStringLiteral( "string_a1" ) );
  46. REQUIRE( attributeFormModel->data( attributeFormModel->index( 1, 0 ), AttributeFormModel::AttributeValue ) == QStringLiteral( "string_a1" ) );
  47. }
  48. SECTION( "FeatureDefaultValue" )
  49. {
  50. featureModel->resetFeature();
  51. featureModel->resetAttributes();
  52. attributeFormModel->setData( attributeFormModel->index( 1, 0 ), QString( "new_feature" ), AttributeFormModel::AttributeValue );
  53. // test default value changed on update with new feature
  54. REQUIRE( attributeFormModel->attribute( QStringLiteral( "str2" ) ) == QStringLiteral( "new_feature__" ) );
  55. // create a feature through the attribute form model
  56. REQUIRE( attributeFormModel->create() );
  57. QgsFeatureId fid = featureModel->feature().id();
  58. REQUIRE( fid > 0 );
  59. attributeFormModel->setData( attributeFormModel->index( 1, 0 ), QString( "edit_feature" ), AttributeFormModel::AttributeValue );
  60. // test default value changed on update with existing feature being edited
  61. REQUIRE( attributeFormModel->attribute( QStringLiteral( "str2" ) ) == QStringLiteral( "edit_feature__" ) );
  62. REQUIRE( attributeFormModel->save() );
  63. QgsFeature feature = layer->getFeature( fid );
  64. REQUIRE( feature.attributes().at( 2 ) == QStringLiteral( "edit_feature__" ) );
  65. }
  66. }