Skip to content

Commit

Permalink
Passed unit tests!
Browse files Browse the repository at this point in the history
  • Loading branch information
Doug Beney committed Jan 24, 2019
1 parent f5a3a30 commit 472fab4
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 64 deletions.
4 changes: 2 additions & 2 deletions VibratoNotes-Desktop.pro
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ SOURCES += \
$$PWD/src/ui-managers/notelist-views/genericview.cpp \
$$PWD/ui/notebook_editparent.cpp \
$$PWD/src/sql/sqlmanager.cpp \
src/models/delegates/noteitemdelegate.cpp
$$PWD/src/models/delegates/noteitemdelegate.cpp

HEADERS += \
$$PWD/src/meta/info/appconfig.h \
Expand Down Expand Up @@ -96,7 +96,7 @@ HEADERS += \
$$PWD/src/ui-managers/notelist-views/genericview.h \
$$PWD/ui/notebook_editparent.h \
$$PWD/src/sql/sqlmanager.h \
src/models/delegates/noteitemdelegate.h
$$PWD/src/models/delegates/noteitemdelegate.h

INCLUDEPATH += $$PWD/include
INCLUDEPATH += $$PWD/src/models/views # Location of customlistview
Expand Down
41 changes: 21 additions & 20 deletions src/sql/sqlmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ QVector<Note*> SQLManager::notes() {

// Parse a tags array
QVector<QUuid> tags;
QString tagVariantsQuery = QString("select tag from notes_tags where note = %1").arg(sync_hash);
QString tagVariantsQuery = QString("select tag from notes_tags where note = \"%1\"").arg(sync_hash);
for ( QVariant tagVariant : column(tagVariantsQuery) )
tags.append( tagVariant.toString() );

Expand Down Expand Up @@ -231,7 +231,7 @@ QVector<Notebook*> SQLManager::m_getNotebooks(Notebook *parent) {
QString queryString =
QString("select %1 from notebooks where parent = :sync_hash").arg( notebookColumns().join(", ") );
q.prepare(queryString);
q.bindValue(":sync_hash", parentSyncHash);
q.bindValue(":sync_hash", parentSyncHash.toString(QUuid::WithoutBraces));
q.exec();
MapVector notebookResults = rows(q, notebookColumns());

Expand Down Expand Up @@ -282,7 +282,7 @@ bool SQLManager::addNote(Note *note)
QSqlQuery q;
q.prepare(queryString);

q.bindValue(":sync_hash" , note->syncHash());
q.bindValue(":sync_hash" , note->syncHash().toString(QUuid::WithoutBraces));
q.bindValue(":title" , note->title());
q.bindValue(":text" , note->text());
q.bindValue(":date_created" , note->dateCreated());
Expand All @@ -299,7 +299,7 @@ bool SQLManager::addNote(Note *note)
for ( QUuid tagSyncHash : note->tags() ) {
tagQ.prepare("insert into notes_tags (note, tag) values "
"(:noteSyncHash, :tagSyncHash)");
tagQ.bindValue(":noteSyncHash", note->syncHash());
tagQ.bindValue(":noteSyncHash", note->syncHash().toString(QUuid::WithoutBraces));
tagQ.bindValue(":tagSyncHash", tagSyncHash);
tagQ.exec();
logSqlError(tagQ.lastError());
Expand All @@ -313,9 +313,10 @@ bool SQLManager::updateNoteToDB(Note* note) {
///
// Update the note
///

QSqlTableModel model;
model.setTable("notes");
model.setFilter( QString("sync_hash = %1").arg(note->syncHash().toString(QUuid::WithoutBraces)) );
model.setFilter( QString("sync_hash = \"%1\"").arg(note->syncHash().toString(QUuid::WithoutBraces)) );
model.select();

if ( model.rowCount() > 1 )
Expand All @@ -337,7 +338,7 @@ bool SQLManager::updateNoteToDB(Note* note) {
// Update the note's tags
///
QString noteTagsQuery
= QString("select note, tag from notes_tags where note = %1").arg(note->syncHash().toString(QUuid::WithoutBraces));
= QString("select note, tag from notes_tags where note = \"%1\"").arg(note->syncHash().toString(QUuid::WithoutBraces));
VariantList curTagObjects = column(noteTagsQuery, 1);
QVector<QUuid> curTagSyncIDs;
for (QVariant obj : curTagObjects)
Expand All @@ -362,7 +363,7 @@ bool SQLManager::updateNoteFromDB(Note* note) {
QString queryString
= QString("select %1 from notes where sync_hash = :sync_hash").arg(noteColumns().join(", "));
query.prepare( queryString );
query.bindValue(":sync_hash", note->syncHash());
query.bindValue(":sync_hash", note->syncHash().toString(QUuid::WithoutBraces));
query.exec();

if (!logSqlError(query.lastError()) )
Expand All @@ -382,7 +383,7 @@ bool SQLManager::updateNoteFromDB(Note* note) {

QSqlTableModel tags;
tags.setTable("notes_tags");
tags.setFilter( QString("note = %1").arg(note->syncHash().toString(QUuid::WithoutBraces)) );
tags.setFilter( QString("note = \"%1\"").arg(note->syncHash().toString(QUuid::WithoutBraces)) );
tags.select();

QVector<QUuid> tagList;
Expand All @@ -397,7 +398,7 @@ bool SQLManager::updateNoteFromDB(Note* note) {
bool SQLManager::deleteNote(Note* note) {
QSqlQuery q;
q.prepare("DELETE FROM notes WHERE sync_hash = :sync_hash");
q.bindValue(":sync_hash", note->syncHash());
q.bindValue(":sync_hash", note->syncHash().toString(QUuid::WithoutBraces));
q.exec();
return logSqlError(q.lastError());
}
Expand All @@ -420,7 +421,7 @@ bool SQLManager::addNotebook(Notebook* notebook) {
if ( notebook->parent() != nullptr )
parentSyncHash = notebook->parent()->syncHash();

q.bindValue(":sync_hash" , notebook->syncHash());
q.bindValue(":sync_hash" , notebook->syncHash().toString(QUuid::WithoutBraces));
q.bindValue(":title" , notebook->title());
q.bindValue(":date_modified" , notebook->dateModified());
q.bindValue(":parent" , parentSyncHash);
Expand All @@ -436,7 +437,7 @@ bool SQLManager::addNotebook(Notebook* notebook) {
bool SQLManager::updateNotebookToDB(Notebook* notebook) {
QSqlTableModel model;
model.setTable("notebooks");
model.setFilter( QString("sync_hash = %1").arg(notebook->syncHash().toString(QUuid::WithoutBraces)) );
model.setFilter( QString("sync_hash = \"%1\"").arg(notebook->syncHash().toString(QUuid::WithoutBraces)) );
model.select();

if ( model.rowCount() > 1 )
Expand Down Expand Up @@ -464,7 +465,7 @@ bool SQLManager::updateNotebookFromDB(Notebook* notebook) {
QString queryString
= QString("select %1 from notebooks where sync_hash = :sync_hash").arg(notebookColumns().join(", "));
query.prepare( queryString );
query.bindValue(":sync_hash", notebook->syncHash());
query.bindValue(":sync_hash", notebook->syncHash().toString(QUuid::WithoutBraces));
query.exec();
Map notebookRow = row(query, notebookColumns());

Expand All @@ -484,13 +485,13 @@ bool SQLManager::deleteNotebook(Notebook* notebook, bool delete_children) {
// Delete notebook
QSqlQuery q;
q.prepare("DELETE FROM notebooks WHERE sync_hash = :sync_hash");
q.bindValue(":sync_hash", notebook->syncHash());
q.bindValue(":sync_hash", notebook->syncHash().toString(QUuid::WithoutBraces));
q.exec();

// Change notes under this notebook to use default notebook
logSqlError(q.lastError());
q.prepare("UPDATE notes SET notebook = NULL WHERE notebook = :sync_hash");
q.bindValue(":sync_hash", notebook->syncHash());
q.bindValue(":sync_hash", notebook->syncHash().toString(QUuid::WithoutBraces));
q.exec();

// Delete children
Expand All @@ -515,7 +516,7 @@ bool SQLManager::addTag(Tag *tag) {
QSqlQuery q;
q.prepare(queryString);

q.bindValue(":sync_hash", tag->syncHash());
q.bindValue(":sync_hash", tag->syncHash().toString(QUuid::WithoutBraces));
q.bindValue(":title", tag->title());
q.bindValue(":date_modified", tag->title());
q.bindValue(":row", tag->row());
Expand All @@ -530,7 +531,7 @@ bool SQLManager::addTag(Tag *tag) {
bool SQLManager::updateTagToDB(Tag* tag) {
QSqlTableModel model;
model.setTable("tags");
model.setFilter( QString("sync_hash = %1").arg(tag->syncHash().toString(QUuid::WithoutBraces)) );
model.setFilter( QString("sync_hash = \"%1\"").arg(tag->syncHash().toString(QUuid::WithoutBraces)) );
model.select();

if ( model.rowCount() > 1 )
Expand All @@ -548,7 +549,7 @@ bool SQLManager::updateTagFromDB(Tag* tag) {
QString queryString
= QString("select %1 from tags where sync_hash = :sync_hash").arg(tagColumns().join(", "));
query.prepare( queryString );
query.bindValue(":sync_hash", tag->syncHash());
query.bindValue(":sync_hash", tag->syncHash().toString(QUuid::WithoutBraces));
query.exec();
Map tagRow = row(query, tagColumns());

Expand All @@ -566,19 +567,19 @@ bool SQLManager::updateTagFromDB(Tag* tag) {
bool SQLManager::deleteTag(Tag* tag) {
QSqlQuery q;
q.prepare("DELETE FROM tags WHERE sync_hash = :sync_hash");
q.bindValue(":sync_hash", tag->syncHash());
q.bindValue(":sync_hash", tag->syncHash().toString(QUuid::WithoutBraces));
q.exec();
logSqlError(q.lastError());
q.prepare("DELETE FROM notes_tags WHERE tag = :sync_hash");
q.bindValue(":sync_hash", tag->syncHash());
q.bindValue(":sync_hash", tag->syncHash().toString(QUuid::WithoutBraces));
q.exec();
return logSqlError(q.lastError());
}

bool SQLManager::tagExists(QUuid noteSyncHash, QUuid tagSyncHash) {
QSqlTableModel model;
model.setTable("notes_tags");
model.setFilter( QString("note = %1 and tag = %2").arg(noteSyncHash.toString(QUuid::WithoutBraces), tagSyncHash.toString(QUuid::WithoutBraces)) );
model.setFilter( QString("note = \"%1\" and tag = \"%2\"").arg(noteSyncHash.toString(QUuid::WithoutBraces), tagSyncHash.toString(QUuid::WithoutBraces)) );
model.select();
return model.rowCount() > 0;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/VibratoNotes-Desktop-Tests.pro
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ QT += testlib

include(../VibratoNotes-Desktop.pro)

TARGET = UnitTests
TARGET = vibrato-unittests
TEMPLATE = app
DEFINES += UNIT_TEST

Expand Down
79 changes: 38 additions & 41 deletions tests/unit-tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
#include <helper-io.hpp>
#define private private



#include <QtTest/qtest.h>
#include <QCoreApplication>
#include <QDebug>
Expand Down Expand Up @@ -49,36 +47,35 @@ void GenericTest::numberToStringWord()

void GenericTest::dateFormatting()
{
Note *note = new Note(-1, 1, "Test Note", "",
isoDate( "2000-01-01T09:38:59Z"), // Date Created
QDateTime::currentDateTime(), // Date Modified
false, -1, {}, false);
Note *note = new Note();
note->setDateCreated(isoDate("2000-01-01T09:38:59Z"));
note->setDateModified(QDateTime::currentDateTime());

// For unit tests, the current date will always be:
// 2000-12-25T09:38:59Z
QCOMPARE(note->date_created_str(), "January 1, 2000");
QCOMPARE(note->date_created_str_informative(), "January 1, 2000 at 9:38AM UTC");
QCOMPARE(note->dateCreatedStr(), "January 1, 2000");
QCOMPARE(note->dateCreatedStrInformative(), "January 1, 2000 at 9:38AM UTC");

QCOMPARE(note->date_modified_str(), "Just now");
QCOMPARE(note->dateModifiedStr(), "Just now");

note->setDate_modified( isoDate("2000-12-25T09:36:59Z") ); // two minutes ago
QCOMPARE(note->date_modified_str(), "Two minutes ago");
note->setDateModified( isoDate("2000-12-25T09:36:59Z") ); // two minutes ago
QCOMPARE(note->dateModifiedStr(), "Two minutes ago");

note->setDate_modified( isoDate("2000-12-25T09:26:59Z") ); // twelve minutes ago
QCOMPARE(note->date_modified_str(), "12 minutes ago");
note->setDateModified( isoDate("2000-12-25T09:26:59Z") ); // twelve minutes ago
QCOMPARE(note->dateModifiedStr(), "12 minutes ago");

note->setDate_modified( isoDate("2000-11-01T09:38:59Z") ); // one month ago
QCOMPARE(note->date_modified_str(), "One month ago");
note->setDateModified( isoDate("2000-11-01T09:38:59Z") ); // one month ago
QCOMPARE(note->dateModifiedStr(), "One month ago");

note->setDate_modified( isoDate("1999-12-25T09:38:59Z") ); // one year ago
QCOMPARE(note->date_modified_str(), "One year ago");
note->setDateModified( isoDate("1999-12-25T09:38:59Z") ); // one year ago
QCOMPARE(note->dateModifiedStr(), "One year ago");

note->setDate_modified( isoDate("1989-12-25T09:38:59Z") ); // eleven years ago
QCOMPARE(note->date_modified_str(), "11 years ago");
note->setDateModified( isoDate("1989-12-25T09:38:59Z") ); // eleven years ago
QCOMPARE(note->dateModifiedStr(), "11 years ago");

// Invalid date scenerio where modified date is later than current
note->setDate_modified( isoDate("2001-01-01T09:38:59Z") );
QCOMPARE(note->date_modified_str(), "Just now"); // "Just now" is possibly the safest response we could expect.

note->setDateModified( isoDate("2001-01-01T09:38:59Z") );
QCOMPARE(note->dateModifiedStr(), "Just now"); // "Just now" is possibly the safest response we could expect.
}

void addNoteToSQLite()
Expand All @@ -89,6 +86,7 @@ void addNoteToSQLite()
void GenericTest::sqlmanager()
{
SQLManager manager;
qDebug() << "SQLite3 database location:" << manager.location();

//
// Test: SQL Errors
Expand Down Expand Up @@ -133,10 +131,11 @@ void GenericTest::sqlmanager()
//
// Test: Adding a note
//
Note note(-1, 1, "Test Note", "Hello world.",
isoDate( "2000-01-01T09:38:59Z"), // Date Created
QDateTime::currentDateTime(), // Date Modified
false, -1, {}, false);
Note note;
note.setTitle("Test Note");
note.setText("Hello world");
note.setDateCreated(isoDate( "2000-01-01T09:38:59Z"));
note.setDateModified(QDateTime::currentDateTime());

QVERIFY( manager.addNote(&note) );

Expand All @@ -151,20 +150,19 @@ void GenericTest::sqlmanager()
//
// Test: Updating note from database
//
manager.realBasicQuery( QString("update notes set title = 'from database' where id = %1").arg(note.id()) );
manager.realBasicQuery( QString("update notes set title = 'from database' where sync_hash = %1").arg(note.syncHash().toString(QUuid::WithoutBraces)) );
manager.updateNoteFromDB(&note);
mynote = manager.row("select title from notes", {"title"});
QCOMPARE(note.title(), mynote["title"].toString());

//
// Test: Loading a notebook
//
QVERIFY (manager.realBasicQuery("insert into notebooks (sync_id, id, title, parent) values "
"(1, 2, 'Recipes', -1)"));
Notebook notebook("11111111-1111-1111-1111-111111111111");
notebook.setTitle("Recipes");
manager.addNotebook(&notebook);
QVector<Notebook*> notebooks = manager.notebooks();
for (Notebook *n : notebooks) {
QCOMPARE( n->syncId(), 1 );
QCOMPARE( n->id(), 2 );
QCOMPARE( n->title(), "Recipes" );
QCOMPARE( n->parent(), nullptr );
}
Expand All @@ -175,17 +173,13 @@ void GenericTest::sqlmanager()
//
// Test: Loading nested notebooks (hierarchial)
//
QVERIFY (manager.realBasicQuery("insert into notebooks (sync_id, id, title, parent) values "
"(2, 3, 'Cool', 1)"));
QVERIFY (manager.realBasicQuery("insert into notebooks (sync_hash, title, parent) values "
"('33333333-3333-3333-3333-333333333333', 'Cool', '11111111-1111-1111-1111-111111111111')"));
notebooks = manager.notebooks();
for (Notebook *n : notebooks) {
QCOMPARE( n->syncId(), 1 );
QCOMPARE( n->id(), 2 );
QCOMPARE( n->title(), "Recipes" );
QCOMPARE( n->parent(), nullptr );
for (Notebook *n2 : n->children()) {
QCOMPARE( n2->syncId(), 2 );
QCOMPARE( n2->id(), 3 );
QCOMPARE( n2->title(), "Cool" );
QCOMPARE( n2->parent(), n );
}
Expand All @@ -199,16 +193,19 @@ void GenericTest::sqlmanager()
//
QStringList tags = {"fun", "2018", "2019", "pictures"};
QVERIFY( manager.realBasicQuery("insert into tags (title) values ('fun'), ('2018'), ('2019'), ('pictures')"));
VariantList tagNames = manager.column("select * from tags", 2);
for (int i=0; i<tags.length(); i++)
VariantList tagNames = manager.column("select title from tags", 0);
qDebug() << "The Tags" << tagNames;
for (int i=0; i<tags.length(); i++) {
QCOMPARE( tagNames[i].toString(), tags[i] );
}

//
// Test: Add new Tag
//
Tag *newTag = new Tag(1, 5, "cats&dogs");
Tag *newTag = new Tag();
newTag->setTitle("cats&dogs");
manager.addTag(newTag);
tagNames = manager.column("select * from tags", 2);
tagNames = manager.column("select title from tags", 0);
QStringList tlist;
tags.append("cats&dogs");
for (QVariant t : tagNames)
Expand Down

0 comments on commit 472fab4

Please sign in to comment.