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:
Christian Kamm 2014-10-23 15:15:47 +02:00
parent 81584c6d51
commit e5269a4151
6 changed files with 171 additions and 12 deletions

View file

@ -493,6 +493,7 @@ bool SyncJournalDb::setFileRecord( const SyncJournalFileRecord& _record )
if( fileId.isEmpty() ) fileId = ""; if( fileId.isEmpty() ) fileId = "";
QString remotePerm (record._remotePerm); QString remotePerm (record._remotePerm);
if (remotePerm.isEmpty()) remotePerm = QString(); // have NULL in DB (vs empty) if (remotePerm.isEmpty()) remotePerm = QString(); // have NULL in DB (vs empty)
_setFileRecordQuery->reset();
_setFileRecordQuery->bindValue(1, QString::number(phash)); _setFileRecordQuery->bindValue(1, QString::number(phash));
_setFileRecordQuery->bindValue(2, plen); _setFileRecordQuery->bindValue(2, plen);
_setFileRecordQuery->bindValue(3, record._path ); _setFileRecordQuery->bindValue(3, record._path );
@ -516,7 +517,6 @@ bool SyncJournalDb::setFileRecord( const SyncJournalFileRecord& _record )
<< record._mode << record._mode
<< QString::number(Utility::qDateTimeToTime_t(record._modtime)) << QString::number(record._type) << QString::number(Utility::qDateTimeToTime_t(record._modtime)) << QString::number(record._type)
<< record._etag << record._fileId << record._remotePerm; << record._etag << record._fileId << record._remotePerm;
_setFileRecordQuery->reset();
return true; return true;
} else { } else {
@ -534,6 +534,7 @@ bool SyncJournalDb::deleteFileRecord(const QString& filename, bool recursively)
// always delete the actual file. // always delete the actual file.
qlonglong phash = getPHash(filename); qlonglong phash = getPHash(filename);
_deleteFileRecordPhash->reset();
_deleteFileRecordPhash->bindValue( 1, QString::number(phash) ); _deleteFileRecordPhash->bindValue( 1, QString::number(phash) );
if( !_deleteFileRecordPhash->exec() ) { if( !_deleteFileRecordPhash->exec() ) {
@ -543,8 +544,8 @@ bool SyncJournalDb::deleteFileRecord(const QString& filename, bool recursively)
return false; return false;
} }
qDebug() << _deleteFileRecordPhash->lastQuery() << phash << filename; qDebug() << _deleteFileRecordPhash->lastQuery() << phash << filename;
_deleteFileRecordPhash->reset();
if( recursively) { if( recursively) {
_deleteFileRecordRecursively->reset();
_deleteFileRecordRecursively->bindValue(1, filename); _deleteFileRecordRecursively->bindValue(1, filename);
if( !_deleteFileRecordRecursively->exec() ) { if( !_deleteFileRecordRecursively->exec() ) {
qWarning() << "Exec error of SQL statement: " qWarning() << "Exec error of SQL statement: "
@ -553,7 +554,6 @@ bool SyncJournalDb::deleteFileRecord(const QString& filename, bool recursively)
return false; return false;
} }
qDebug() << _deleteFileRecordRecursively->lastQuery() << filename; qDebug() << _deleteFileRecordRecursively->lastQuery() << filename;
_deleteFileRecordRecursively->reset();
} }
return true; return true;
} else { } else {
@ -571,6 +571,7 @@ SyncJournalFileRecord SyncJournalDb::getFileRecord( const QString& filename )
SyncJournalFileRecord rec; SyncJournalFileRecord rec;
if( checkConnect() ) { if( checkConnect() ) {
_getFileRecordQuery->reset();
_getFileRecordQuery->bindValue(1, QString::number(phash)); _getFileRecordQuery->bindValue(1, QString::number(phash));
if (!_getFileRecordQuery->exec()) { if (!_getFileRecordQuery->exec()) {
@ -590,8 +591,6 @@ SyncJournalFileRecord SyncJournalDb::getFileRecord( const QString& filename )
rec._etag = _getFileRecordQuery->baValue(7); rec._etag = _getFileRecordQuery->baValue(7);
rec._fileId = _getFileRecordQuery->baValue(8); rec._fileId = _getFileRecordQuery->baValue(8);
rec._remotePerm = _getFileRecordQuery->baValue(9); rec._remotePerm = _getFileRecordQuery->baValue(9);
_getFileRecordQuery->reset();
} else { } else {
QString err = _getFileRecordQuery->error(); QString err = _getFileRecordQuery->error();
qDebug() << "No journal entry found for " << filename; qDebug() << "No journal entry found for " << filename;
@ -712,6 +711,7 @@ SyncJournalDb::DownloadInfo SyncJournalDb::getDownloadInfo(const QString& file)
DownloadInfo res; DownloadInfo res;
if( checkConnect() ) { if( checkConnect() ) {
_getDownloadInfoQuery->reset();
_getDownloadInfoQuery->bindValue(1, file); _getDownloadInfoQuery->bindValue(1, file);
if (!_getDownloadInfoQuery->exec()) { if (!_getDownloadInfoQuery->exec()) {
@ -725,7 +725,6 @@ SyncJournalDb::DownloadInfo SyncJournalDb::getDownloadInfo(const QString& file)
} else { } else {
res._valid = false; res._valid = false;
} }
_getDownloadInfoQuery->reset();
} }
return res; return res;
} }
@ -739,6 +738,7 @@ void SyncJournalDb::setDownloadInfo(const QString& file, const SyncJournalDb::Do
} }
if (i._valid) { if (i._valid) {
_setDownloadInfoQuery->reset();
_setDownloadInfoQuery->bindValue(1, file); _setDownloadInfoQuery->bindValue(1, file);
_setDownloadInfoQuery->bindValue(2, i._tmpfile); _setDownloadInfoQuery->bindValue(2, i._tmpfile);
_setDownloadInfoQuery->bindValue(3, i._etag ); _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; qDebug() << _setDownloadInfoQuery->lastQuery() << file << i._tmpfile << i._etag << i._errorCount;
_setDownloadInfoQuery->reset();
} else { } else {
_deleteDownloadInfoQuery->reset();
_deleteDownloadInfoQuery->bindValue( 1, file ); _deleteDownloadInfoQuery->bindValue( 1, file );
if( !_deleteDownloadInfoQuery->exec() ) { if( !_deleteDownloadInfoQuery->exec() ) {
@ -760,7 +760,6 @@ void SyncJournalDb::setDownloadInfo(const QString& file, const SyncJournalDb::Do
return; return;
} }
qDebug() << _deleteDownloadInfoQuery->lastQuery() << file; qDebug() << _deleteDownloadInfoQuery->lastQuery() << file;
_deleteDownloadInfoQuery->reset();
} }
} }
@ -810,6 +809,7 @@ SyncJournalDb::UploadInfo SyncJournalDb::getUploadInfo(const QString& file)
if( checkConnect() ) { if( checkConnect() ) {
_getUploadInfoQuery->reset();
_getUploadInfoQuery->bindValue(1, file); _getUploadInfoQuery->bindValue(1, file);
if (!_getUploadInfoQuery->exec()) { if (!_getUploadInfoQuery->exec()) {
@ -827,7 +827,6 @@ SyncJournalDb::UploadInfo SyncJournalDb::getUploadInfo(const QString& file)
res._modtime = Utility::qDateTimeFromTime_t(_getUploadInfoQuery->int64Value(4)); res._modtime = Utility::qDateTimeFromTime_t(_getUploadInfoQuery->int64Value(4));
res._valid = ok; res._valid = ok;
} }
_getUploadInfoQuery->reset();
} }
return res; return res;
} }
@ -841,6 +840,7 @@ void SyncJournalDb::setUploadInfo(const QString& file, const SyncJournalDb::Uplo
} }
if (i._valid) { if (i._valid) {
_setUploadInfoQuery->reset();
_setUploadInfoQuery->bindValue(1, file); _setUploadInfoQuery->bindValue(1, file);
_setUploadInfoQuery->bindValue(2, i._chunk); _setUploadInfoQuery->bindValue(2, i._chunk);
_setUploadInfoQuery->bindValue(3, i._transferid ); _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; qDebug() << _setUploadInfoQuery->lastQuery() << file << i._chunk << i._transferid << i._errorCount;
_setUploadInfoQuery->reset();
} else { } else {
_deleteUploadInfoQuery->reset();
_deleteUploadInfoQuery->bindValue(1, file); _deleteUploadInfoQuery->bindValue(1, file);
if( !_deleteUploadInfoQuery->exec() ) { if( !_deleteUploadInfoQuery->exec() ) {
@ -863,7 +863,6 @@ void SyncJournalDb::setUploadInfo(const QString& file, const SyncJournalDb::Uplo
return; return;
} }
qDebug() << _deleteUploadInfoQuery->lastQuery() << file; qDebug() << _deleteUploadInfoQuery->lastQuery() << file;
_deleteUploadInfoQuery->reset();
} }
} }
@ -906,6 +905,7 @@ SyncJournalBlacklistRecord SyncJournalDb::blacklistEntry( const QString& file )
// SELECT lastTryEtag, lastTryModtime, retrycount, errorstring // SELECT lastTryEtag, lastTryModtime, retrycount, errorstring
if( checkConnect() ) { if( checkConnect() ) {
_blacklistQuery->reset();
_blacklistQuery->bindValue( 1, file ); _blacklistQuery->bindValue( 1, file );
if( _blacklistQuery->exec() ){ if( _blacklistQuery->exec() ){
if( _blacklistQuery->next() ) { if( _blacklistQuery->next() ) {
@ -919,7 +919,6 @@ SyncJournalBlacklistRecord SyncJournalDb::blacklistEntry( const QString& file )
qWarning() << "Exec error blacklist: " << _blacklistQuery->lastQuery() << " : " qWarning() << "Exec error blacklist: " << _blacklistQuery->lastQuery() << " : "
<< _blacklistQuery->error(); << _blacklistQuery->error();
} }
_blacklistQuery->reset();
} }
return entry; return entry;
@ -1153,6 +1152,25 @@ bool SyncJournalDb::isUpdateFrom_1_5()
return _possibleUpgradeFromMirall_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 } // namespace Mirall

View file

@ -142,5 +142,10 @@ private:
QList<QString> _avoidReadFromDbOnNextSyncFilter; 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 } // namespace Mirall
#endif // SYNCJOURNALDB_H #endif // SYNCJOURNALDB_H

View file

@ -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;
}
} }

View file

@ -43,6 +43,9 @@ public:
int _mode; int _mode;
}; };
bool operator==(const SyncJournalFileRecord & lhs,
const SyncJournalFileRecord & rhs);
class SyncJournalBlacklistRecord class SyncJournalBlacklistRecord
{ {
public: public:

View file

@ -28,6 +28,7 @@ endif(UNIX AND NOT APPLE)
owncloud_add_test(CSyncSqlite "") owncloud_add_test(CSyncSqlite "")
owncloud_add_test(NetrcParser ../src/owncloudcmd/netrcparser.cpp) owncloud_add_test(NetrcParser ../src/owncloudcmd/netrcparser.cpp)
owncloud_add_test(OwnSql ../src/mirall/ownsql.cpp) owncloud_add_test(OwnSql ../src/mirall/ownsql.cpp)
owncloud_add_test(SyncJournalDB ../src/mirall/syncjournaldb.cpp)

118
test/testsyncjournaldb.h Normal file
View 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