Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterPetrik committed Oct 3, 2023
1 parent 7c88794 commit 4e3e2e6
Show file tree
Hide file tree
Showing 11 changed files with 225 additions and 12 deletions.
2 changes: 2 additions & 0 deletions app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ if (ENABLE_TESTS)
test/testutilsfunctions.cpp
test/testvariablesmanager.cpp
test/testactiveproject.cpp
test/testprojectchecksumcache.cpp
)

set(MM_HDRS
Expand All @@ -220,6 +221,7 @@ if (ENABLE_TESTS)
test/testutilsfunctions.h
test/testvariablesmanager.h
test/testactiveproject.h
test/testprojectchecksumcache.h
)
endif ()

Expand Down
6 changes: 6 additions & 0 deletions app/test/inputtests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "test/testmaptools.h"
#include "test/testlayertree.h"
#include "test/testactiveproject.h"
#include "test/testprojectchecksumcache.h"

#if not defined APPLE_PURCHASING
#include "test/testpurchasing.h"
Expand Down Expand Up @@ -181,6 +182,11 @@ int InputTests::runTest() const
TestActiveProject activeProjectTest( mApi );
nFailed = QTest::qExec( &activeProjectTest, mTestArgs );
}
else if ( mTestRequested == "--testProjectChecksumCache" )
{
TestProjectChecksumCache projectChecksumTest;
nFailed = QTest::qExec( &projectChecksumTest, mTestArgs );
}
#if not defined APPLE_PURCHASING
else if ( mTestRequested == "--testPurchasing" )
{
Expand Down
26 changes: 21 additions & 5 deletions app/test/testmerginapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ void TestMerginApi::testMultiChunkUploadDownload()
bigFile.write( QByteArray( 1024 * 1024, static_cast<char>( 'A' + i ) ) ); // AAAA.....BBBB.....CCCC.....
bigFile.close();

QByteArray checksum = CoreUtils::calculate( bigFilePath );
QByteArray checksum = CoreUtils::calculateChecksum( bigFilePath );
QVERIFY( !checksum.isEmpty() );

// upload
Expand All @@ -546,7 +546,7 @@ void TestMerginApi::testMultiChunkUploadDownload()
downloadRemoteProject( mApi, mUsername, projectName );

// verify it's there and with correct content
QByteArray checksum2 = CoreUtils::calculate( bigFilePath );
QByteArray checksum2 = CoreUtils::calculateChecksum( bigFilePath );
QVERIFY( QFileInfo::exists( bigFilePath ) );
QCOMPARE( checksum, checksum2 );
}
Expand All @@ -567,7 +567,7 @@ void TestMerginApi::testEmptyFileUploadDownload()
QFile::copy( mTestDataPath + "/" + TEST_EMPTY_FILE_NAME, emptyFileDestinationPath );
QVERIFY( QFileInfo::exists( emptyFileDestinationPath ) );

QByteArray checksum = CoreUtils::calculate( emptyFileDestinationPath );
QByteArray checksum = CoreUtils::calculateChecksum( emptyFileDestinationPath );
QVERIFY( !checksum.isEmpty() );

//upload
Expand All @@ -579,7 +579,7 @@ void TestMerginApi::testEmptyFileUploadDownload()
downloadRemoteProject( mApi, mUsername, projectName );

// verify it's there and with correct content
QByteArray checksum2 = CoreUtils::calculate( emptyFileDestinationPath );
QByteArray checksum2 = CoreUtils::calculateChecksum( emptyFileDestinationPath );
QVERIFY( QFileInfo::exists( emptyFileDestinationPath ) );
QCOMPARE( checksum, checksum2 );
}
Expand Down Expand Up @@ -785,6 +785,7 @@ void TestMerginApi::testPushNoChanges()
QCOMPARE( project2.mergin.status, ProjectStatus::UpToDate );

QCOMPARE( MerginApi::localProjectChanges( projectDir ), ProjectDiff() );
QVERIFY( !MerginApi::hasLocalProjectChanges( projectDir ) );
}

void TestMerginApi::testUpdateAddedFile()
Expand Down Expand Up @@ -1123,6 +1124,7 @@ void TestMerginApi::testDiffUpload()
QVERIFY( QFileInfo::exists( projectDir + "/.mergin/base.gpkg" ) );

QCOMPARE( MerginApi::localProjectChanges( projectDir ), ProjectDiff() ); // no local changes expected
QVERIFY( !MerginApi::hasLocalProjectChanges( projectDir ) );

// replace gpkg with a new version with a modified geometry
QFile::remove( projectDir + "/base.gpkg" );
Expand All @@ -1132,6 +1134,7 @@ void TestMerginApi::testDiffUpload()
ProjectDiff expectedDiff;
expectedDiff.localUpdated = QSet<QString>() << "base.gpkg";
QCOMPARE( diff, expectedDiff );
QVERIFY( MerginApi::hasLocalProjectChanges( projectDir ) );

GeodiffUtils::ChangesetSummary expectedSummary;
expectedSummary["simple"] = GeodiffUtils::TableSummary( 0, 1, 0 );
Expand All @@ -1142,6 +1145,7 @@ void TestMerginApi::testDiffUpload()
uploadRemoteProject( mApi, mUsername, projectName );

QCOMPARE( MerginApi::localProjectChanges( projectDir ), ProjectDiff() ); // no local changes expected
QVERIFY( !MerginApi::hasLocalProjectChanges( projectDir ) );
}

void TestMerginApi::testDiffSubdirsUpload()
Expand All @@ -1157,7 +1161,7 @@ void TestMerginApi::testDiffSubdirsUpload()
QVERIFY( QFileInfo::exists( projectDir + "/.mergin/" + base ) );

QCOMPARE( MerginApi::localProjectChanges( projectDir ), ProjectDiff() ); // no local changes expected

QVERIFY( !MerginApi::hasLocalProjectChanges( projectDir ) );

// replace gpkg with a new version with a modified geometry
QFile::remove( projectDir + "/" + base );
Expand All @@ -1167,6 +1171,7 @@ void TestMerginApi::testDiffSubdirsUpload()
ProjectDiff expectedDiff;
expectedDiff.localUpdated = QSet<QString>() << base ;
QCOMPARE( diff, expectedDiff );
QVERIFY( MerginApi::hasLocalProjectChanges( projectDir ) );

GeodiffUtils::ChangesetSummary expectedSummary;
expectedSummary["simple"] = GeodiffUtils::TableSummary( 0, 1, 0 );
Expand All @@ -1177,6 +1182,7 @@ void TestMerginApi::testDiffSubdirsUpload()
uploadRemoteProject( mApi, mUsername, projectName );

QCOMPARE( MerginApi::localProjectChanges( projectDir ), ProjectDiff() ); // no local changes expected
QVERIFY( !MerginApi::hasLocalProjectChanges( projectDir ) );
}

void TestMerginApi::testDiffUpdateBasic()
Expand All @@ -1194,6 +1200,7 @@ void TestMerginApi::testDiffUpdateBasic()

QVERIFY( QFileInfo::exists( projectDir + "/.mergin/base.gpkg" ) );
QCOMPARE( MerginApi::localProjectChanges( projectDir ), ProjectDiff() ); // no local changes expected
QVERIFY( !MerginApi::hasLocalProjectChanges( projectDir ) );

QgsVectorLayer *vl0 = new QgsVectorLayer( projectDir + "/base.gpkg|layername=simple", "base", "ogr" );
QVERIFY( vl0->isValid() );
Expand Down Expand Up @@ -1223,6 +1230,7 @@ void TestMerginApi::testDiffUpdateBasic()
delete vl;

QCOMPARE( MerginApi::localProjectChanges( projectDir ), ProjectDiff() ); // no local changes expected
QVERIFY( !MerginApi::hasLocalProjectChanges( projectDir ) );

QVERIFY( !GeodiffUtils::hasPendingChanges( projectDir, "base.gpkg" ) );
}
Expand All @@ -1242,6 +1250,7 @@ void TestMerginApi::testDiffUpdateWithRebase()

QVERIFY( QFileInfo::exists( projectDir + "/.mergin/base.gpkg" ) );
QCOMPARE( MerginApi::localProjectChanges( projectDir ), ProjectDiff() ); // no local changes expected
QVERIFY( !MerginApi::hasLocalProjectChanges( projectDir ) );

//
// download with mApiExtra + modify + upload
Expand Down Expand Up @@ -1275,6 +1284,7 @@ void TestMerginApi::testDiffUpdateWithRebase()
ProjectDiff expectedDiff;
expectedDiff.localUpdated = QSet<QString>() << "base.gpkg";
QCOMPARE( diff, expectedDiff );
QVERIFY( MerginApi::hasLocalProjectChanges( projectDir ) );

// check that geodiff knows there was one added feature
GeodiffUtils::ChangesetSummary expectedSummary;
Expand All @@ -1298,6 +1308,7 @@ void TestMerginApi::testDiffUpdateWithRebase()
// like before the update - there should be locally modified base.gpkg with the changes we did
QCOMPARE( MerginApi::localProjectChanges( projectDir ), expectedDiff );
QCOMPARE( GeodiffUtils::parseChangesetSummary( changes ), expectedSummary );
QVERIFY( MerginApi::hasLocalProjectChanges( projectDir ) );
}

void TestMerginApi::testDiffUpdateWithRebaseFailed()
Expand All @@ -1318,6 +1329,7 @@ void TestMerginApi::testDiffUpdateWithRebaseFailed()

QVERIFY( QFileInfo::exists( projectDir + "/.mergin/base.gpkg" ) );
QCOMPARE( MerginApi::localProjectChanges( projectDir ), ProjectDiff() ); // no local changes expected
QVERIFY( !MerginApi::hasLocalProjectChanges( projectDir ) );

//
// download with mApiExtra + modify + upload
Expand All @@ -1343,6 +1355,7 @@ void TestMerginApi::testDiffUpdateWithRebaseFailed()
expectedDiff.localUpdated = QSet<QString>() << "base.gpkg";
qDebug() << diff.dump();
QCOMPARE( diff, expectedDiff );
QVERIFY( MerginApi::hasLocalProjectChanges( projectDir ) );

// check that geodiff knows there was one added feature
QString changes = GeodiffUtils::diffableFilePendingChanges( projectDir, "base.gpkg", true );
Expand All @@ -1368,6 +1381,7 @@ void TestMerginApi::testDiffUpdateWithRebaseFailed()
ProjectDiff expectedDiffFinal;
expectedDiffFinal.localAdded = QSet<QString>() << conflictFilename;
QCOMPARE( MerginApi::localProjectChanges( projectDir ), expectedDiffFinal );
QVERIFY( MerginApi::hasLocalProjectChanges( projectDir ) );
}

void TestMerginApi::testUpdateWithDiffs()
Expand All @@ -1385,6 +1399,7 @@ void TestMerginApi::testUpdateWithDiffs()

QVERIFY( QFileInfo::exists( projectDir + "/.mergin/base.gpkg" ) );
QCOMPARE( MerginApi::localProjectChanges( projectDir ), ProjectDiff() ); // no local changes expected
QVERIFY( !MerginApi::hasLocalProjectChanges( projectDir ) );

//
// download with mApiExtra + modify + upload
Expand Down Expand Up @@ -1415,6 +1430,7 @@ void TestMerginApi::testUpdateWithDiffs()
delete vl;

QCOMPARE( MerginApi::localProjectChanges( projectDir ), ProjectDiff() );
QVERIFY( !MerginApi::hasLocalProjectChanges( projectDir ) );
QVERIFY( !GeodiffUtils::hasPendingChanges( projectDir, "base.gpkg" ) );
}

Expand Down
138 changes: 138 additions & 0 deletions app/test/testprojectchecksumcache.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "testprojectchecksumcache.h"
#include "projectchecksumcache.h"
#include "coreutils.h"
#include "testutils.h"
#include "inpututils.h"

#include <QElapsedTimer>
#include <QtTest/QtTest>

TestProjectChecksumCache::TestProjectChecksumCache() = default;

TestProjectChecksumCache::~TestProjectChecksumCache() = default;

void TestProjectChecksumCache::init()
{
}

void TestProjectChecksumCache::cleanup()
{
}

void TestProjectChecksumCache::testFilesCheckum()
{
QString projectName = QStringLiteral( "testFilesCheckum" );
QString projectDir = QDir::tempPath() + "/" + projectName;

InputUtils::cpDir( TestUtils::testDataDir() + "/planes", projectDir );
InputUtils::copyFile( TestUtils::testDataDir() + "/photo.jpg", projectDir + "/bigfile.jpg" );

QString cacheFilePath = projectDir + "/.mergin/checksum.cache" ;
QString checksumDirectTxt1 = CoreUtils::calculateChecksum( projectDir + "/lines.qml" );
QVERIFY( !checksumDirectTxt1.isEmpty() );

QElapsedTimer timer;
timer.start();
QString checksumDirectBigFile = CoreUtils::calculateChecksum( projectDir + "/bigfile.jpg" );
qint64 elapsedForChecksumDirectBigFile = timer.elapsed();
QVERIFY( !checksumDirectBigFile.isEmpty() );

{
// Cold start - delete cache file
InputUtils::removeFile( cacheFilePath );
ProjectChecksumCache cache( projectDir );
QCOMPARE( cache.cacheFilePath(), cacheFilePath );

// Test gpkg
QString checksumDirectGpkg = CoreUtils::calculateChecksum( projectDir + "/constraint-layers.gpkg" );
QCOMPARE( checksumDirectGpkg, "c81be103072ecea025ff92a813916db8e42b7bbb" );
QString checksumFromCacheGpkg = cache.get( "constraint-layers.gpkg" );
QCOMPARE( checksumDirectGpkg, checksumFromCacheGpkg );

// Test non-existent file
InputUtils::removeFile( projectDir + "/photo.jpg" );
QVERIFY( cache.get( "photo.jpg" ).isEmpty() );

// Test text file
QString checksumFromCacheTxt = cache.get( "lines.qml" );
QCOMPARE( checksumDirectTxt1, checksumFromCacheTxt );

// Test photo - big file
QString checksumFromCacheBigFile = cache.get( "bigfile.jpg" );
QCOMPARE( checksumDirectBigFile, checksumFromCacheBigFile );
}

// Test that cache is saved
QVERIFY( QFileInfo( cacheFilePath ).exists() );
QDateTime cacheModifiedTime = QFileInfo( cacheFilePath ).lastModified();

// Modify txt file, remove constraint-layers.gpkg file and add photo file, do not touch bigfile.jpg
InputUtils::removeFile( projectDir + "/constraint-layers.gpkg" );
InputUtils::copyFile( TestUtils::testDataDir() + "/photo.jpg", projectDir + "/photo.jpg" );
QString checksumDirectPhoto = CoreUtils::calculateChecksum( projectDir + "/photo.jpg" );
QVERIFY( !checksumDirectPhoto.isEmpty() );

QFile f( projectDir + "/lines.qml" );
if ( f.open( QIODevice::WriteOnly ) )
{
QTextStream stream( &f );
stream << "something really really cool";
f.close();
}
QString checksumDirectTxt2 = CoreUtils::calculateChecksum( projectDir + "/lines.qml" );
QVERIFY( !checksumDirectTxt2.isEmpty() );
QVERIFY( checksumDirectTxt1 != checksumDirectTxt2 );

{
// Start with existent cache file
ProjectChecksumCache cache( projectDir );

// Test non-existent gpkg - NOT taken from previous cache
QVERIFY( cache.get( "constraint-layers.gpkg" ).isEmpty() );

// Test new file - NOT taken from previous cache
QString checksumDirectPhoto = CoreUtils::calculateChecksum( projectDir + "/photo.jpg" );
QString checksumFromCachePhoto = cache.get( "photo.jpg" );
QCOMPARE( checksumDirectPhoto, checksumFromCachePhoto );

// Test modified file - NOT taken from previous cache
QString checksumFromCacheTxt = cache.get( "lines.qml" );
QCOMPARE( checksumFromCacheTxt, checksumDirectTxt2 );

// Test bigfile - checksum taken from previous cache!
// time should be faster than when calculated directly (let say at least 2 times)
QElapsedTimer timer2;
timer2.start();
QString checksumFromCacheBigFile = cache.get( "bigfile.jpg" );
qint64 elapsedTimeFromCache = timer2.elapsed();
QVERIFY( elapsedTimeFromCache * 2 < elapsedForChecksumDirectBigFile );
QCOMPARE( checksumDirectBigFile, checksumFromCacheBigFile );
}

// Test that cache is re-saved
QVERIFY( QFileInfo( cacheFilePath ).exists() );
QDateTime cacheModifiedTime2 = QFileInfo( cacheFilePath ).lastModified();
QVERIFY( cacheModifiedTime != cacheModifiedTime2 );

{
// Start with existent cache file
ProjectChecksumCache cache( projectDir );

// Test geo gpkg
QString checksumFromCacheGeoGpkg = cache.get( "bigfile.jpg" );
QCOMPARE( checksumDirectBigFile, checksumFromCacheGeoGpkg );
}
// Test that cache is NOT re-saved
QVERIFY( QFileInfo( cacheFilePath ).exists() );
QDateTime cacheModifiedTime3 = QFileInfo( cacheFilePath ).lastModified();
QCOMPARE( cacheModifiedTime2, cacheModifiedTime3 );
}
29 changes: 29 additions & 0 deletions app/test/testprojectchecksumcache.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef TESTPROJECTCHECKSUMCACHE_H
#define TESTPROJECTCHECKSUMCACHE_H

#include <QObject>

class TestProjectChecksumCache : public QObject
{
Q_OBJECT
public:
explicit TestProjectChecksumCache( );
~TestProjectChecksumCache();

private slots:
void init();
void cleanup();

void testFilesCheckum();
};

#endif // TESTPROJECTCHECKSUMCACHE_H
5 changes: 3 additions & 2 deletions core/coreutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
#include <QFile>
#include <QDirIterator>
#include <QTextStream>
#include <QCryptographicHash>
#include <QRegularExpression>

#include "qcoreapplication.h"
#include "merginapi.h"

const QString CoreUtils::LOG_TO_DEVNULL = QStringLiteral();
const QString CoreUtils::LOG_TO_STDOUT = QStringLiteral( "TO_STDOUT" );
Expand Down Expand Up @@ -163,7 +164,7 @@ QString CoreUtils::findUniquePath( const QString &path )
return uniquePath;
}

QByteArray CoreUtils::calculate( const QString &filePath )
QByteArray CoreUtils::calculateChecksum( const QString &filePath )
{
QFile f( filePath );
if ( f.open( QFile::ReadOnly ) )
Expand Down
Loading

1 comment on commit 4e3e2e6

@inputapp-bot
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

iOS - version 23.10.462311 just submitted!

Please sign in to comment.