Skip to content

Commit

Permalink
Add base class QgsTest method to make it easy to retrieve an isolated…
Browse files Browse the repository at this point in the history
… copy of a test data file
  • Loading branch information
nyalldawson committed Oct 6, 2023
1 parent ad5059f commit c75cd53
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 36 deletions.
28 changes: 28 additions & 0 deletions src/test/qgstest.h
Expand Up @@ -161,11 +161,39 @@ class TEST_EXPORT QgsTest : public QObject
return mTestDataDir.filePath( filePath.startsWith( '/' ) ? filePath.mid( 1 ) : filePath );
}

/**
* Copies the test data with the given file path to a
* temporary directory and returns the full path to the copy.
*/
QString copyTestData( const QString &filePath )
{
const QString srcPath = testDataPath( filePath );
const QFileInfo srcFileInfo( srcPath );

// lazy create temporary dir
if ( !mTemporaryDir )
mTemporaryDir = std::make_unique< QTemporaryDir >();

// we put all copies into a subdirectory of the temporary dir, so that we isolate clean copies
// of the same source file used by different test functions
mTemporaryCopyCount++;
const QString temporarySubdirectory = QStringLiteral( "test_%1" ).arg( mTemporaryCopyCount );
QDir().mkdir( mTemporaryDir->filePath( temporarySubdirectory ) );

const QString copiedDataPath = mTemporaryDir->filePath( temporarySubdirectory + '/' + srcFileInfo.fileName() );

QFile::copy( srcPath, copiedDataPath );
return copiedDataPath;
}

protected:

QString mName;
QString mReport;
QString mControlPathPrefix;
std::unique_ptr< QTemporaryDir > mTemporaryDir;
int mTemporaryCopyCount = 0;

const QDir mTestDataDir;

bool renderMapSettingsCheck( const QString &name, const QString &referenceImage, const QgsMapSettings &mapSettings, int allowedMismatch = 0, int colorTolerance = 0 )
Expand Down
53 changes: 17 additions & 36 deletions tests/src/providers/testqgscopcprovider.cpp
Expand Up @@ -85,12 +85,7 @@ class TestQgsCopcProvider : public QgsTest
void testStatsCalculator();
void testSaveLoadStats();
void testPointCloudRequest();

void testQgsRangeRequestCache();

private:
QString mTestDataDir;
QTemporaryDir mTempDir;
};

//runs before all tests
Expand All @@ -100,7 +95,7 @@ void TestQgsCopcProvider::initTestCase()
QgsApplication::init();
QgsApplication::initQgis();

mTestDataDir = QStringLiteral( TEST_DATA_DIR ) + '/'; //defined in CmakeLists.txt

}

//runs after all tests
Expand Down Expand Up @@ -203,12 +198,11 @@ void TestQgsCopcProvider::querySublayers()
QVERIFY( res.empty() );

// valid copc layer
const QString dataPath = mTempDir.filePath( QStringLiteral( "sunshine-coast-layers.copc.laz" ) );
QFile::copy( mTestDataDir + QStringLiteral( "/point_clouds/copc/sunshine-coast.copc.laz" ), dataPath );
const QString dataPath = copyTestData( QStringLiteral( "/point_clouds/copc/sunshine-coast.copc.laz" ) );

res = copcMetadata->querySublayers( dataPath );
QCOMPARE( res.count(), 1 );
QCOMPARE( res.at( 0 ).name(), QStringLiteral( "sunshine-coast-layers.copc" ) );
QCOMPARE( res.at( 0 ).name(), QStringLiteral( "sunshine-coast.copc" ) );
QCOMPARE( res.at( 0 ).uri(), dataPath );
QCOMPARE( res.at( 0 ).providerKey(), QStringLiteral( "copc" ) );
QCOMPARE( res.at( 0 ).type(), Qgis::LayerType::PointCloud );
Expand All @@ -228,8 +222,7 @@ void TestQgsCopcProvider::brokenPath()

void TestQgsCopcProvider::testLazInfo()
{
const QString dataPath = mTempDir.filePath( QStringLiteral( "long-star1.copc.laz" ) );
QFile::copy( mTestDataDir + QStringLiteral( "point_clouds/copc/lone-star.copc.laz" ), dataPath );
const QString dataPath = copyTestData( QStringLiteral( "point_clouds/copc/lone-star.copc.laz" ) );

std::ifstream file( dataPath.toStdString(), std::ios::binary );
QgsLazInfo lazInfo = QgsLazInfo::fromFile( file );
Expand Down Expand Up @@ -257,8 +250,7 @@ void TestQgsCopcProvider::testLazInfo()

void TestQgsCopcProvider::validLayer()
{
const QString dataPath = mTempDir.filePath( QStringLiteral( "sunshine-coastv.copc.laz" ) );
QFile::copy( mTestDataDir + QStringLiteral( "point_clouds/copc/sunshine-coast.copc.laz" ), dataPath );
const QString dataPath = copyTestData( QStringLiteral( "point_clouds/copc/sunshine-coast.copc.laz" ) );

std::unique_ptr< QgsPointCloudLayer > layer = std::make_unique< QgsPointCloudLayer >( dataPath, QStringLiteral( "layer" ), QStringLiteral( "copc" ) );
QVERIFY( layer->isValid() );
Expand All @@ -280,8 +272,7 @@ void TestQgsCopcProvider::validLayer()

void TestQgsCopcProvider::validLayerWithCopcHierarchy()
{
const QString dataPath = mTempDir.filePath( QStringLiteral( "long-star-hierarychy.copc.laz" ) );
QFile::copy( mTestDataDir + QStringLiteral( "point_clouds/copc/lone-star.copc.laz" ), dataPath );
const QString dataPath = copyTestData( QStringLiteral( "point_clouds/copc/lone-star.copc.laz" ) );

std::unique_ptr< QgsPointCloudLayer > layer = std::make_unique< QgsPointCloudLayer >( dataPath, QStringLiteral( "layer" ), QStringLiteral( "copc" ) );
QVERIFY( layer->isValid() );
Expand All @@ -299,8 +290,7 @@ void TestQgsCopcProvider::validLayerWithCopcHierarchy()

void TestQgsCopcProvider::attributes()
{
const QString dataPath = mTempDir.filePath( QStringLiteral( "sunshine-coast-attributes.copc.laz" ) );
QFile::copy( mTestDataDir + QStringLiteral( "/point_clouds/copc/sunshine-coast.copc.laz" ), dataPath );
const QString dataPath = copyTestData( QStringLiteral( "/point_clouds/copc/sunshine-coast.copc.laz" ) );

std::unique_ptr< QgsPointCloudLayer > layer = std::make_unique< QgsPointCloudLayer >( dataPath, QStringLiteral( "layer" ), QStringLiteral( "copc" ) );
QVERIFY( layer->isValid() );
Expand Down Expand Up @@ -347,8 +337,7 @@ void TestQgsCopcProvider::attributes()

void TestQgsCopcProvider::calculateZRange()
{
const QString dataPath = mTempDir.filePath( QStringLiteral( "sunshine-coast-calculate-z-range.copc.laz" ) );
QFile::copy( mTestDataDir + QStringLiteral( "/point_clouds/copc/sunshine-coast.copc.laz" ), dataPath );
const QString dataPath = copyTestData( QStringLiteral( "/point_clouds/copc/sunshine-coast.copc.laz" ) );

std::unique_ptr< QgsPointCloudLayer > layer = std::make_unique< QgsPointCloudLayer >( dataPath, QStringLiteral( "layer" ), QStringLiteral( "copc" ) );
QVERIFY( layer->isValid() );
Expand All @@ -369,15 +358,14 @@ void TestQgsCopcProvider::testIdentify_data()
{
QTest::addColumn<QString>( "srcDatasetPath" );

QTest::newRow( "copc" ) << mTestDataDir + QStringLiteral( "point_clouds/copc/sunshine-coast.copc.laz" );
QTest::newRow( "copc" ) << QStringLiteral( "point_clouds/copc/sunshine-coast.copc.laz" );
}

void TestQgsCopcProvider::testIdentify()
{
QFETCH( QString, srcDatasetPath );

const QString datasetPath = mTempDir.filePath( QStringLiteral( "pointcloud-identify.copc.laz" ) );
QFile::copy( srcDatasetPath, datasetPath );
const QString datasetPath = copyTestData( srcDatasetPath );

std::unique_ptr< QgsPointCloudLayer > layer = std::make_unique< QgsPointCloudLayer >( datasetPath, QStringLiteral( "layer" ), QStringLiteral( "copc" ) );

Expand Down Expand Up @@ -535,8 +523,7 @@ void TestQgsCopcProvider::testIdentify()
void TestQgsCopcProvider::testExtraBytesAttributesExtraction()
{
{
const QString dataPath = mTempDir.filePath( QStringLiteral( "extra-bytes-attributes-extraction.copc.laz" ) );
QFile::copy( mTestDataDir + QStringLiteral( "/point_clouds/copc/extrabytes-dataset.copc.laz" ), dataPath );
const QString dataPath = copyTestData( QStringLiteral( "/point_clouds/copc/extrabytes-dataset.copc.laz" ) );

std::ifstream file( dataPath.toStdString(), std::ios::binary );
QgsLazInfo lazInfo = QgsLazInfo::fromFile( file );
Expand All @@ -561,8 +548,7 @@ void TestQgsCopcProvider::testExtraBytesAttributesExtraction()
}

{
const QString dataPath = mTempDir.filePath( QStringLiteral( "no-extrabytes-dataset.copc.laz" ) );
QFile::copy( mTestDataDir + QStringLiteral( "/point_clouds/copc/no-extrabytes-dataset.copc.laz" ), dataPath );
const QString dataPath = copyTestData( QStringLiteral( "/point_clouds/copc/no-extrabytes-dataset.copc.laz" ) );

std::ifstream file( dataPath.toStdString(), std::ios::binary );
QgsLazInfo lazInfo = QgsLazInfo::fromFile( file );
Expand All @@ -573,8 +559,7 @@ void TestQgsCopcProvider::testExtraBytesAttributesExtraction()

void TestQgsCopcProvider::testExtraBytesAttributesValues()
{
const QString dataPath = mTempDir.filePath( QStringLiteral( "extrabytes-attributes-values.copc.laz" ) );
QFile::copy( mTestDataDir + QStringLiteral( "/point_clouds/copc/extrabytes-dataset.copc.laz" ), dataPath );
const QString dataPath = copyTestData( QStringLiteral( "/point_clouds/copc/extrabytes-dataset.copc.laz" ) );

std::unique_ptr< QgsPointCloudLayer > layer = std::make_unique< QgsPointCloudLayer >( dataPath, QStringLiteral( "layer" ), QStringLiteral( "copc" ) );
QVERIFY( layer->isValid() );
Expand Down Expand Up @@ -658,8 +643,7 @@ void TestQgsCopcProvider::testExtraBytesAttributesValues()

void TestQgsCopcProvider::testPointCloudIndex()
{
const QString dataPath = mTempDir.filePath( QStringLiteral( "lone-star-point-cloud-index.copc.laz" ) );
QFile::copy( mTestDataDir + QStringLiteral( "/point_clouds/copc/lone-star.copc.laz" ), dataPath );
const QString dataPath = copyTestData( QStringLiteral( "/point_clouds/copc/lone-star.copc.laz" ) );

std::unique_ptr< QgsPointCloudLayer > layer = std::make_unique< QgsPointCloudLayer >( dataPath, QStringLiteral( "layer" ), QStringLiteral( "copc" ) );
QVERIFY( layer->isValid() );
Expand Down Expand Up @@ -716,8 +700,7 @@ void TestQgsCopcProvider::testPointCloudIndex()

void TestQgsCopcProvider::testStatsCalculator()
{
const QString dataPath = mTempDir.filePath( QStringLiteral( "extrabytes-dataset-stats-calculator.copc.laz" ) );
QFile::copy( mTestDataDir + QStringLiteral( "/point_clouds/copc/extrabytes-dataset.copc.laz" ), dataPath );
const QString dataPath = copyTestData( QStringLiteral( "/point_clouds/copc/extrabytes-dataset.copc.laz" ) );

std::unique_ptr< QgsPointCloudLayer > layer = std::make_unique< QgsPointCloudLayer >( dataPath, QStringLiteral( "layer" ), QStringLiteral( "copc" ) );
QgsPointCloudIndex *index = layer->dataProvider()->index();
Expand Down Expand Up @@ -937,8 +920,7 @@ void TestQgsCopcProvider::testSaveLoadStats()
{
QgsPointCloudStatistics calculatedStats;
QgsPointCloudStatistics readStats;
const QString dataPath = mTempDir.filePath( QStringLiteral( "save-load-stats.copc.laz" ) );
QFile::copy( mTestDataDir + QStringLiteral( "/point_clouds/copc/lone-star.copc.laz" ), dataPath );
const QString dataPath = copyTestData( QStringLiteral( "/point_clouds/copc/lone-star.copc.laz" ) );

{
std::unique_ptr< QgsPointCloudLayer > layer = std::make_unique< QgsPointCloudLayer >( dataPath, QStringLiteral( "layer" ), QStringLiteral( "copc" ) );
Expand Down Expand Up @@ -967,8 +949,7 @@ void TestQgsCopcProvider::testSaveLoadStats()

void TestQgsCopcProvider::testPointCloudRequest()
{
const QString dataPath = mTempDir.filePath( QStringLiteral( "point-cloud-request.copc.laz" ) );
QFile::copy( mTestDataDir + QStringLiteral( "/point_clouds/copc/lone-star.copc.laz" ), dataPath );
const QString dataPath = copyTestData( QStringLiteral( "/point_clouds/copc/lone-star.copc.laz" ) );

std::unique_ptr< QgsPointCloudLayer > layer = std::make_unique< QgsPointCloudLayer >( dataPath, QStringLiteral( "layer" ), QStringLiteral( "copc" ) );
QVERIFY( layer->isValid() );
Expand Down

0 comments on commit c75cd53

Please sign in to comment.