mirror of
https://github.com/nextcloud/desktop.git
synced 2024-11-23 05:25:50 +03:00
Add SyncJournalDB autotests.
There also was a bug in syncjournaldb: in some error conditions the sql query wasn't reset properly. We now always reset a query before calling bindValue.
This commit is contained in:
parent
81584c6d51
commit
e5269a4151
6 changed files with 171 additions and 12 deletions
|
@ -493,6 +493,7 @@ bool SyncJournalDb::setFileRecord( const SyncJournalFileRecord& _record )
|
|||
if( fileId.isEmpty() ) fileId = "";
|
||||
QString remotePerm (record._remotePerm);
|
||||
if (remotePerm.isEmpty()) remotePerm = QString(); // have NULL in DB (vs empty)
|
||||
_setFileRecordQuery->reset();
|
||||
_setFileRecordQuery->bindValue(1, QString::number(phash));
|
||||
_setFileRecordQuery->bindValue(2, plen);
|
||||
_setFileRecordQuery->bindValue(3, record._path );
|
||||
|
@ -516,7 +517,6 @@ bool SyncJournalDb::setFileRecord( const SyncJournalFileRecord& _record )
|
|||
<< record._mode
|
||||
<< QString::number(Utility::qDateTimeToTime_t(record._modtime)) << QString::number(record._type)
|
||||
<< record._etag << record._fileId << record._remotePerm;
|
||||
_setFileRecordQuery->reset();
|
||||
|
||||
return true;
|
||||
} else {
|
||||
|
@ -534,6 +534,7 @@ bool SyncJournalDb::deleteFileRecord(const QString& filename, bool recursively)
|
|||
// always delete the actual file.
|
||||
|
||||
qlonglong phash = getPHash(filename);
|
||||
_deleteFileRecordPhash->reset();
|
||||
_deleteFileRecordPhash->bindValue( 1, QString::number(phash) );
|
||||
|
||||
if( !_deleteFileRecordPhash->exec() ) {
|
||||
|
@ -543,8 +544,8 @@ bool SyncJournalDb::deleteFileRecord(const QString& filename, bool recursively)
|
|||
return false;
|
||||
}
|
||||
qDebug() << _deleteFileRecordPhash->lastQuery() << phash << filename;
|
||||
_deleteFileRecordPhash->reset();
|
||||
if( recursively) {
|
||||
_deleteFileRecordRecursively->reset();
|
||||
_deleteFileRecordRecursively->bindValue(1, filename);
|
||||
if( !_deleteFileRecordRecursively->exec() ) {
|
||||
qWarning() << "Exec error of SQL statement: "
|
||||
|
@ -553,7 +554,6 @@ bool SyncJournalDb::deleteFileRecord(const QString& filename, bool recursively)
|
|||
return false;
|
||||
}
|
||||
qDebug() << _deleteFileRecordRecursively->lastQuery() << filename;
|
||||
_deleteFileRecordRecursively->reset();
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
|
@ -571,6 +571,7 @@ SyncJournalFileRecord SyncJournalDb::getFileRecord( const QString& filename )
|
|||
SyncJournalFileRecord rec;
|
||||
|
||||
if( checkConnect() ) {
|
||||
_getFileRecordQuery->reset();
|
||||
_getFileRecordQuery->bindValue(1, QString::number(phash));
|
||||
|
||||
if (!_getFileRecordQuery->exec()) {
|
||||
|
@ -590,8 +591,6 @@ SyncJournalFileRecord SyncJournalDb::getFileRecord( const QString& filename )
|
|||
rec._etag = _getFileRecordQuery->baValue(7);
|
||||
rec._fileId = _getFileRecordQuery->baValue(8);
|
||||
rec._remotePerm = _getFileRecordQuery->baValue(9);
|
||||
|
||||
_getFileRecordQuery->reset();
|
||||
} else {
|
||||
QString err = _getFileRecordQuery->error();
|
||||
qDebug() << "No journal entry found for " << filename;
|
||||
|
@ -712,6 +711,7 @@ SyncJournalDb::DownloadInfo SyncJournalDb::getDownloadInfo(const QString& file)
|
|||
DownloadInfo res;
|
||||
|
||||
if( checkConnect() ) {
|
||||
_getDownloadInfoQuery->reset();
|
||||
_getDownloadInfoQuery->bindValue(1, file);
|
||||
|
||||
if (!_getDownloadInfoQuery->exec()) {
|
||||
|
@ -725,7 +725,6 @@ SyncJournalDb::DownloadInfo SyncJournalDb::getDownloadInfo(const QString& file)
|
|||
} else {
|
||||
res._valid = false;
|
||||
}
|
||||
_getDownloadInfoQuery->reset();
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
@ -739,6 +738,7 @@ void SyncJournalDb::setDownloadInfo(const QString& file, const SyncJournalDb::Do
|
|||
}
|
||||
|
||||
if (i._valid) {
|
||||
_setDownloadInfoQuery->reset();
|
||||
_setDownloadInfoQuery->bindValue(1, file);
|
||||
_setDownloadInfoQuery->bindValue(2, i._tmpfile);
|
||||
_setDownloadInfoQuery->bindValue(3, i._etag );
|
||||
|
@ -750,9 +750,9 @@ void SyncJournalDb::setDownloadInfo(const QString& file, const SyncJournalDb::Do
|
|||
}
|
||||
|
||||
qDebug() << _setDownloadInfoQuery->lastQuery() << file << i._tmpfile << i._etag << i._errorCount;
|
||||
_setDownloadInfoQuery->reset();
|
||||
|
||||
} else {
|
||||
_deleteDownloadInfoQuery->reset();
|
||||
_deleteDownloadInfoQuery->bindValue( 1, file );
|
||||
|
||||
if( !_deleteDownloadInfoQuery->exec() ) {
|
||||
|
@ -760,7 +760,6 @@ void SyncJournalDb::setDownloadInfo(const QString& file, const SyncJournalDb::Do
|
|||
return;
|
||||
}
|
||||
qDebug() << _deleteDownloadInfoQuery->lastQuery() << file;
|
||||
_deleteDownloadInfoQuery->reset();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -810,6 +809,7 @@ SyncJournalDb::UploadInfo SyncJournalDb::getUploadInfo(const QString& file)
|
|||
|
||||
if( checkConnect() ) {
|
||||
|
||||
_getUploadInfoQuery->reset();
|
||||
_getUploadInfoQuery->bindValue(1, file);
|
||||
|
||||
if (!_getUploadInfoQuery->exec()) {
|
||||
|
@ -827,7 +827,6 @@ SyncJournalDb::UploadInfo SyncJournalDb::getUploadInfo(const QString& file)
|
|||
res._modtime = Utility::qDateTimeFromTime_t(_getUploadInfoQuery->int64Value(4));
|
||||
res._valid = ok;
|
||||
}
|
||||
_getUploadInfoQuery->reset();
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
@ -841,6 +840,7 @@ void SyncJournalDb::setUploadInfo(const QString& file, const SyncJournalDb::Uplo
|
|||
}
|
||||
|
||||
if (i._valid) {
|
||||
_setUploadInfoQuery->reset();
|
||||
_setUploadInfoQuery->bindValue(1, file);
|
||||
_setUploadInfoQuery->bindValue(2, i._chunk);
|
||||
_setUploadInfoQuery->bindValue(3, i._transferid );
|
||||
|
@ -854,8 +854,8 @@ void SyncJournalDb::setUploadInfo(const QString& file, const SyncJournalDb::Uplo
|
|||
}
|
||||
|
||||
qDebug() << _setUploadInfoQuery->lastQuery() << file << i._chunk << i._transferid << i._errorCount;
|
||||
_setUploadInfoQuery->reset();
|
||||
} else {
|
||||
_deleteUploadInfoQuery->reset();
|
||||
_deleteUploadInfoQuery->bindValue(1, file);
|
||||
|
||||
if( !_deleteUploadInfoQuery->exec() ) {
|
||||
|
@ -863,7 +863,6 @@ void SyncJournalDb::setUploadInfo(const QString& file, const SyncJournalDb::Uplo
|
|||
return;
|
||||
}
|
||||
qDebug() << _deleteUploadInfoQuery->lastQuery() << file;
|
||||
_deleteUploadInfoQuery->reset();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -906,6 +905,7 @@ SyncJournalBlacklistRecord SyncJournalDb::blacklistEntry( const QString& file )
|
|||
// SELECT lastTryEtag, lastTryModtime, retrycount, errorstring
|
||||
|
||||
if( checkConnect() ) {
|
||||
_blacklistQuery->reset();
|
||||
_blacklistQuery->bindValue( 1, file );
|
||||
if( _blacklistQuery->exec() ){
|
||||
if( _blacklistQuery->next() ) {
|
||||
|
@ -919,7 +919,6 @@ SyncJournalBlacklistRecord SyncJournalDb::blacklistEntry( const QString& file )
|
|||
qWarning() << "Exec error blacklist: " << _blacklistQuery->lastQuery() << " : "
|
||||
<< _blacklistQuery->error();
|
||||
}
|
||||
_blacklistQuery->reset();
|
||||
}
|
||||
|
||||
return entry;
|
||||
|
@ -1153,6 +1152,25 @@ bool SyncJournalDb::isUpdateFrom_1_5()
|
|||
return _possibleUpgradeFromMirall_1_5;
|
||||
}
|
||||
|
||||
bool operator==(const SyncJournalDb::DownloadInfo & lhs,
|
||||
const SyncJournalDb::DownloadInfo & rhs)
|
||||
{
|
||||
return lhs._errorCount == rhs._errorCount
|
||||
&& lhs._etag == rhs._etag
|
||||
&& lhs._tmpfile == rhs._tmpfile
|
||||
&& lhs._valid == rhs._valid;
|
||||
|
||||
}
|
||||
|
||||
bool operator==(const SyncJournalDb::UploadInfo & lhs,
|
||||
const SyncJournalDb::UploadInfo & rhs)
|
||||
{
|
||||
return lhs._errorCount == rhs._errorCount
|
||||
&& lhs._chunk == rhs._chunk
|
||||
&& lhs._modtime == rhs._modtime
|
||||
&& lhs._valid == rhs._valid
|
||||
&& lhs._size == rhs._size
|
||||
&& lhs._transferid == rhs._transferid;
|
||||
}
|
||||
|
||||
} // namespace Mirall
|
||||
|
|
|
@ -142,5 +142,10 @@ private:
|
|||
QList<QString> _avoidReadFromDbOnNextSyncFilter;
|
||||
};
|
||||
|
||||
bool operator==(const SyncJournalDb::DownloadInfo & lhs,
|
||||
const SyncJournalDb::DownloadInfo & rhs);
|
||||
bool operator==(const SyncJournalDb::UploadInfo & lhs,
|
||||
const SyncJournalDb::UploadInfo & rhs);
|
||||
|
||||
} // namespace Mirall
|
||||
#endif // SYNCJOURNALDB_H
|
||||
|
|
|
@ -88,4 +88,18 @@ SyncJournalBlacklistRecord::SyncJournalBlacklistRecord(const SyncFileItem& item,
|
|||
|
||||
}
|
||||
|
||||
|
||||
bool operator==(const SyncJournalFileRecord & lhs,
|
||||
const SyncJournalFileRecord & rhs)
|
||||
{
|
||||
return lhs._path == rhs._path
|
||||
&& lhs._inode == rhs._inode
|
||||
&& lhs._modtime == rhs._modtime
|
||||
&& lhs._type == rhs._type
|
||||
&& lhs._etag == rhs._etag
|
||||
&& lhs._fileId == rhs._fileId
|
||||
&& lhs._remotePerm == rhs._remotePerm
|
||||
&& lhs._mode == rhs._mode;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -43,6 +43,9 @@ public:
|
|||
int _mode;
|
||||
};
|
||||
|
||||
bool operator==(const SyncJournalFileRecord & lhs,
|
||||
const SyncJournalFileRecord & rhs);
|
||||
|
||||
class SyncJournalBlacklistRecord
|
||||
{
|
||||
public:
|
||||
|
|
|
@ -28,6 +28,7 @@ endif(UNIX AND NOT APPLE)
|
|||
owncloud_add_test(CSyncSqlite "")
|
||||
owncloud_add_test(NetrcParser ../src/owncloudcmd/netrcparser.cpp)
|
||||
owncloud_add_test(OwnSql ../src/mirall/ownsql.cpp)
|
||||
owncloud_add_test(SyncJournalDB ../src/mirall/syncjournaldb.cpp)
|
||||
|
||||
|
||||
|
||||
|
|
118
test/testsyncjournaldb.h
Normal file
118
test/testsyncjournaldb.h
Normal file
|
@ -0,0 +1,118 @@
|
|||
/*
|
||||
* This software is in the public domain, furnished "as is", without technical
|
||||
* support, and with no warranty, express or implied, as to its usefulness for
|
||||
* any purpose.
|
||||
* */
|
||||
|
||||
#ifndef MIRALL_TESTSYNCJOURNALDB_H
|
||||
#define MIRALL_TESTSYNCJOURNALDB_H
|
||||
|
||||
#include <QtTest>
|
||||
|
||||
#include <sqlite3.h>
|
||||
|
||||
#include "mirall/syncjournaldb.h"
|
||||
#include "mirall/syncjournalfilerecord.h"
|
||||
|
||||
using namespace Mirall;
|
||||
|
||||
namespace {
|
||||
|
||||
const char testdbC[] = "/tmp";
|
||||
}
|
||||
|
||||
class TestSyncJournalDB : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
TestSyncJournalDB()
|
||||
: _db(testdbC)
|
||||
{
|
||||
}
|
||||
|
||||
QDateTime dropMsecs(QDateTime time)
|
||||
{
|
||||
return Utility::qDateTimeFromTime_t(Utility::qDateTimeToTime_t(time));
|
||||
}
|
||||
|
||||
private slots:
|
||||
|
||||
void initTestCase()
|
||||
{
|
||||
}
|
||||
|
||||
void cleanupTestCase()
|
||||
{
|
||||
}
|
||||
|
||||
void testFileRecord()
|
||||
{
|
||||
SyncJournalFileRecord record = _db.getFileRecord("nonexistant");
|
||||
QVERIFY(!record.isValid());
|
||||
|
||||
record._path = "foo";
|
||||
record._inode = 1234;
|
||||
record._modtime = dropMsecs(QDateTime::currentDateTime());
|
||||
record._type = 5;
|
||||
record._etag = "789789";
|
||||
record._fileId = "abcd";
|
||||
record._remotePerm = "744";
|
||||
record._mode = -17;
|
||||
QVERIFY(_db.setFileRecord(record));
|
||||
|
||||
SyncJournalFileRecord storedRecord = _db.getFileRecord("foo");
|
||||
QVERIFY(storedRecord == record);
|
||||
|
||||
QVERIFY(_db.deleteFileRecord("foo"));
|
||||
record = _db.getFileRecord("foo");
|
||||
QVERIFY(!record.isValid());
|
||||
}
|
||||
|
||||
void testDownloadInfo()
|
||||
{
|
||||
typedef SyncJournalDb::DownloadInfo Info;
|
||||
Info record = _db.getDownloadInfo("nonexistant");
|
||||
QVERIFY(!record._valid);
|
||||
|
||||
record._errorCount = 5;
|
||||
record._etag = "ABCDEF";
|
||||
record._valid = true;
|
||||
record._tmpfile = "/tmp/foo";
|
||||
_db.setDownloadInfo("foo", record);
|
||||
|
||||
Info storedRecord = _db.getDownloadInfo("foo");
|
||||
QVERIFY(storedRecord == record);
|
||||
|
||||
_db.setDownloadInfo("foo", Info());
|
||||
Info wipedRecord = _db.getDownloadInfo("foo");
|
||||
QVERIFY(!wipedRecord._valid);
|
||||
}
|
||||
|
||||
void testUploadInfo()
|
||||
{
|
||||
typedef SyncJournalDb::UploadInfo Info;
|
||||
Info record = _db.getUploadInfo("nonexistant");
|
||||
QVERIFY(!record._valid);
|
||||
|
||||
record._errorCount = 5;
|
||||
record._chunk = 12;
|
||||
record._transferid = 812974891;
|
||||
record._size = 12894789147;
|
||||
record._modtime = dropMsecs(QDateTime::currentDateTime());
|
||||
record._valid = true;
|
||||
_db.setUploadInfo("foo", record);
|
||||
|
||||
Info storedRecord = _db.getUploadInfo("foo");
|
||||
QVERIFY(storedRecord == record);
|
||||
|
||||
_db.setUploadInfo("foo", Info());
|
||||
Info wipedRecord = _db.getUploadInfo("foo");
|
||||
QVERIFY(!wipedRecord._valid);
|
||||
}
|
||||
|
||||
private:
|
||||
SyncJournalDb _db;
|
||||
};
|
||||
|
||||
#endif
|
Loading…
Reference in a new issue