test_digitizinglogger.cpp 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /***************************************************************************
  2. test_layerobserver.h
  3. --------------------
  4. begin : Apr 2020
  5. copyright : (C) 2020 by Ivan Ivanov
  6. email : ivan@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 "digitizinglogger.h"
  18. #include <qgscoordinatereferencesystem.h>
  19. #include "catch2.h"
  20. TEST_CASE( "Digitizing logger" )
  21. {
  22. std::unique_ptr<QgsVectorLayer> logsLayer = std::make_unique<QgsVectorLayer>( QStringLiteral( "Point?crs=EPSG:3857&field=fid:integer&field=digitizing_action:string&field=digitizing_layer_name:string&field=digitizing_layer_id:string&field=digitizing_datetime:datetime" ), QStringLiteral( "Logs Layer" ), QStringLiteral( "memory" ) );
  23. REQUIRE( logsLayer->isValid() );
  24. logsLayer->setDefaultValueDefinition( 1, QgsDefaultValue( QStringLiteral( "@digitizing_type" ), false ) );
  25. logsLayer->setDefaultValueDefinition( 2, QgsDefaultValue( QStringLiteral( "@digitizing_layer_name" ), false ) );
  26. logsLayer->setDefaultValueDefinition( 3, QgsDefaultValue( QStringLiteral( "@digitizing_layer_id" ), false ) );
  27. logsLayer->setDefaultValueDefinition( 4, QgsDefaultValue( QStringLiteral( "@digitizing_datetime" ), false ) );
  28. QgsProject::instance()->addMapLayer( logsLayer.get() );
  29. QgsProject::instance()->setCrs( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:3857" ) ) );
  30. QgsProject::instance()->writeEntry( QStringLiteral( "qfieldsync" ), QStringLiteral( "digitizingLogsLayer" ), logsLayer->id() );
  31. qDebug() << logsLayer->id();
  32. std::unique_ptr<QgsVectorLayer> layer = std::make_unique<QgsVectorLayer>( QStringLiteral( "Point?crs=EPSG:3857&field=fid:integer&field=str:string" ), QStringLiteral( "Input Layer" ), QStringLiteral( "memory" ) );
  33. REQUIRE( layer->isValid() );
  34. std::unique_ptr<DigitizingLogger> digitizingLogger = std::make_unique<DigitizingLogger>();
  35. digitizingLogger->setType( QStringLiteral( "rock" ) );
  36. digitizingLogger->setProject( QgsProject::instance() );
  37. digitizingLogger->setDigitizingLayer( layer.get() );
  38. SECTION( "AddAndWritePoints" )
  39. {
  40. digitizingLogger->clearCoordinates();
  41. digitizingLogger->addCoordinate( QgsPoint( 1, 1 ) );
  42. digitizingLogger->addCoordinate( QgsPoint( 2, 2 ) );
  43. digitizingLogger->writeCoordinates();
  44. REQUIRE( logsLayer->featureCount() == 2 );
  45. digitizingLogger->addCoordinate( QgsPoint( 1, 1 ) );
  46. digitizingLogger->addCoordinate( QgsPoint( 2, 2 ) );
  47. digitizingLogger->removeLastCoordinate();
  48. digitizingLogger->writeCoordinates();
  49. REQUIRE( logsLayer->featureCount() == 3 );
  50. QgsFeature feature = logsLayer->getFeature( 1 );
  51. REQUIRE( feature.attributes().at( 1 ) == digitizingLogger->type() );
  52. REQUIRE( feature.attributes().at( 2 ) == layer->name() );
  53. REQUIRE( feature.attributes().at( 3 ) == layer->id() );
  54. REQUIRE( feature.attributes().at( 4 ).toDateTime().isValid() == true );
  55. }
  56. }