2015-06-01 18:51:55 +02:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
2016-01-15 14:55:33 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2015-06-01 18:51:55 +02:00
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator.
|
|
|
|
|
**
|
|
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2016-01-15 14:55:33 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2015-06-01 18:51:55 +02:00
|
|
|
**
|
2016-01-15 14:55:33 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2015-06-01 18:51:55 +02:00
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2016-09-15 17:41:41 +02:00
|
|
|
#include "googletest.h"
|
|
|
|
|
|
2017-07-26 18:43:07 +02:00
|
|
|
#include <sqlitedatabase.h>
|
2015-06-16 12:38:04 +02:00
|
|
|
#include <sqlitereadstatement.h>
|
|
|
|
|
#include <sqlitereadwritestatement.h>
|
|
|
|
|
#include <sqlitewritestatement.h>
|
2015-06-01 18:51:55 +02:00
|
|
|
|
2015-06-16 12:38:04 +02:00
|
|
|
#include <QDir>
|
2017-07-27 15:59:54 +02:00
|
|
|
|
|
|
|
|
#include <vector>
|
2015-06-01 18:51:55 +02:00
|
|
|
|
|
|
|
|
namespace {
|
2017-07-26 16:02:24 +02:00
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
using testing::ElementsAre;
|
|
|
|
|
|
2017-07-26 16:02:24 +02:00
|
|
|
using Sqlite::SqliteException;
|
2017-07-26 18:43:07 +02:00
|
|
|
using Sqlite::SqliteDatabase;
|
2017-07-26 16:02:24 +02:00
|
|
|
using Sqlite::SqliteReadStatement;
|
|
|
|
|
using Sqlite::SqliteReadWriteStatement;
|
|
|
|
|
using Sqlite::SqliteWriteStatement;
|
|
|
|
|
|
2015-06-01 18:51:55 +02:00
|
|
|
class SqliteStatement : public ::testing::Test
|
|
|
|
|
{
|
|
|
|
|
protected:
|
|
|
|
|
void SetUp() override;
|
|
|
|
|
void TearDown() override;
|
|
|
|
|
|
2017-07-26 18:43:07 +02:00
|
|
|
SqliteDatabase database;
|
2015-06-01 18:51:55 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
TEST_F(SqliteStatement, PrepareFailure)
|
|
|
|
|
{
|
2017-07-27 15:59:54 +02:00
|
|
|
ASSERT_THROW(SqliteReadStatement("blah blah blah", database), SqliteException);
|
|
|
|
|
ASSERT_THROW(SqliteWriteStatement("blah blah blah", database), SqliteException);
|
|
|
|
|
ASSERT_THROW(SqliteReadStatement("INSERT INTO test(name, number) VALUES (?, ?)", database), SqliteException);
|
|
|
|
|
ASSERT_THROW(SqliteWriteStatement("SELECT name, number FROM test '", database), SqliteException);
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(SqliteStatement, CountRows)
|
|
|
|
|
{
|
2017-07-27 15:59:54 +02:00
|
|
|
SqliteReadStatement statement("SELECT * FROM test", database);
|
2015-06-01 18:51:55 +02:00
|
|
|
int nextCount = 0;
|
|
|
|
|
while (statement.next())
|
|
|
|
|
++nextCount;
|
|
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
int sqlCount = SqliteReadStatement::toValue<int>("SELECT count(*) FROM test", database);
|
2015-06-01 18:51:55 +02:00
|
|
|
|
|
|
|
|
ASSERT_THAT(nextCount, sqlCount);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(SqliteStatement, Value)
|
|
|
|
|
{
|
2017-07-27 15:59:54 +02:00
|
|
|
SqliteReadStatement statement("SELECT name, number FROM test ORDER BY name", database);
|
2015-06-01 18:51:55 +02:00
|
|
|
statement.next();
|
|
|
|
|
|
|
|
|
|
statement.next();
|
|
|
|
|
|
|
|
|
|
ASSERT_THAT(statement.value<int>(0), 0);
|
|
|
|
|
ASSERT_THAT(statement.value<qint64>(0), 0);
|
|
|
|
|
ASSERT_THAT(statement.value<double>(0), 0.0);
|
2017-07-27 15:59:54 +02:00
|
|
|
ASSERT_THAT(statement.text(0), "foo");
|
2015-06-01 18:51:55 +02:00
|
|
|
ASSERT_THAT(statement.value<int>(1), 23);
|
|
|
|
|
ASSERT_THAT(statement.value<qint64>(1), 23);
|
|
|
|
|
ASSERT_THAT(statement.value<double>(1), 23.3);
|
2017-07-27 15:59:54 +02:00
|
|
|
ASSERT_THAT(statement.text(1), "23.3");
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(SqliteStatement, ValueFailure)
|
|
|
|
|
{
|
2017-07-27 15:59:54 +02:00
|
|
|
SqliteReadStatement statement("SELECT name, number FROM test", database);
|
2015-06-01 18:51:55 +02:00
|
|
|
ASSERT_THROW(statement.value<int>(0), SqliteException);
|
|
|
|
|
|
|
|
|
|
statement.reset();
|
|
|
|
|
|
|
|
|
|
while (statement.next()) {}
|
|
|
|
|
ASSERT_THROW(statement.value<int>(0), SqliteException);
|
|
|
|
|
|
|
|
|
|
statement.reset();
|
|
|
|
|
|
|
|
|
|
statement.next();
|
|
|
|
|
ASSERT_THROW(statement.value<int>(-1), SqliteException);
|
|
|
|
|
ASSERT_THROW(statement.value<int>(2), SqliteException);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(SqliteStatement, ToIntergerValue)
|
|
|
|
|
{
|
2017-07-27 15:59:54 +02:00
|
|
|
ASSERT_THAT(SqliteReadStatement::toValue<int>("SELECT number FROM test WHERE name='foo'", database), 23);
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(SqliteStatement, ToLongIntergerValue)
|
|
|
|
|
{
|
2017-07-27 15:59:54 +02:00
|
|
|
ASSERT_THAT(SqliteReadStatement::toValue<qint64>("SELECT number FROM test WHERE name='foo'", database), 23LL);
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(SqliteStatement, ToDoubleValue)
|
|
|
|
|
{
|
2017-07-27 15:59:54 +02:00
|
|
|
ASSERT_THAT(SqliteReadStatement::toValue<double>("SELECT number FROM test WHERE name='foo'", database), 23.3);
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
TEST_F(SqliteStatement, ToStringValue)
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
2017-07-27 15:59:54 +02:00
|
|
|
ASSERT_THAT(SqliteReadStatement::toValue<Utils::SmallString>("SELECT name FROM test WHERE name='foo'", database), "foo");
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(SqliteStatement, Utf8Values)
|
|
|
|
|
{
|
2017-07-27 15:59:54 +02:00
|
|
|
SqliteReadStatement statement("SELECT name, number FROM test ORDER by name", database);
|
2015-06-01 18:51:55 +02:00
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
auto values = statement.values<Utils::SmallStringVector>();
|
2015-06-01 18:51:55 +02:00
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
ASSERT_THAT(values, ElementsAre("bar", "foo", "poo"));
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
2017-07-27 15:59:54 +02:00
|
|
|
TEST_F(SqliteStatement, DoubleValues)
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
2017-07-27 15:59:54 +02:00
|
|
|
SqliteReadStatement statement("SELECT name, number FROM test ORDER by name", database);
|
2015-06-01 18:51:55 +02:00
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
auto values = statement.values<std::vector<double>>(1);
|
2015-06-01 18:51:55 +02:00
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
ASSERT_THAT(values, ElementsAre(0.0, 23.3, 40.0));
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(SqliteStatement, ValuesFailure)
|
|
|
|
|
{
|
2017-07-27 15:59:54 +02:00
|
|
|
SqliteReadStatement statement("SELECT name, number FROM test", database);
|
2015-06-01 18:51:55 +02:00
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
ASSERT_THROW(statement.values<Utils::SmallStringVector>({1, 2}), SqliteException);
|
|
|
|
|
ASSERT_THROW(statement.values<Utils::SmallStringVector>({-1, 1}), SqliteException);
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(SqliteStatement, ColumnNames)
|
|
|
|
|
{
|
2017-07-27 15:59:54 +02:00
|
|
|
SqliteReadStatement statement("SELECT name, number FROM test", database);
|
2015-06-01 18:51:55 +02:00
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
auto columnNames = statement.columnNames();
|
2015-06-01 18:51:55 +02:00
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
ASSERT_THAT(columnNames, ElementsAre("name", "number"));
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
TEST_F(SqliteStatement, BindString)
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
|
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
SqliteReadStatement statement("SELECT name, number FROM test WHERE name=?", database);
|
2015-06-01 18:51:55 +02:00
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
statement.bind(1, "foo");
|
2015-06-01 18:51:55 +02:00
|
|
|
|
|
|
|
|
statement.next();
|
|
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
ASSERT_THAT(statement.text(0), "foo");
|
2015-06-01 18:51:55 +02:00
|
|
|
ASSERT_THAT(statement.value<double>(1), 23.3);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(SqliteStatement, BindInteger)
|
|
|
|
|
{
|
2017-07-27 15:59:54 +02:00
|
|
|
SqliteReadStatement statement("SELECT name, number FROM test WHERE number=?", database);
|
2015-06-01 18:51:55 +02:00
|
|
|
|
|
|
|
|
statement.bind(1, 40);
|
|
|
|
|
statement.next();
|
|
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
ASSERT_THAT(statement.text(0),"poo");
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(SqliteStatement, BindLongInteger)
|
|
|
|
|
{
|
2017-07-27 15:59:54 +02:00
|
|
|
SqliteReadStatement statement("SELECT name, number FROM test WHERE number=?", database);
|
2015-06-01 18:51:55 +02:00
|
|
|
|
|
|
|
|
statement.bind(1, qint64(40));
|
|
|
|
|
statement.next();
|
|
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
ASSERT_THAT(statement.text(0), "poo");
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(SqliteStatement, BindDouble)
|
|
|
|
|
{
|
2017-07-27 15:59:54 +02:00
|
|
|
SqliteReadStatement statement("SELECT name, number FROM test WHERE number=?", database);
|
2015-06-01 18:51:55 +02:00
|
|
|
|
|
|
|
|
statement.bind(1, 23.3);
|
|
|
|
|
statement.next();
|
|
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
ASSERT_THAT(statement.text(0), "foo");
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(SqliteStatement, BindIntegerByParameter)
|
|
|
|
|
{
|
2017-07-27 15:59:54 +02:00
|
|
|
SqliteReadStatement statement("SELECT name, number FROM test WHERE number=@number", database);
|
2015-06-01 18:51:55 +02:00
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
statement.bind("@number", 40);
|
2015-06-01 18:51:55 +02:00
|
|
|
statement.next();
|
|
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
ASSERT_THAT(statement.text(0), "poo");
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(SqliteStatement, BindLongIntegerByParameter)
|
|
|
|
|
{
|
2017-07-27 15:59:54 +02:00
|
|
|
SqliteReadStatement statement("SELECT name, number FROM test WHERE number=@number", database);
|
2015-06-01 18:51:55 +02:00
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
statement.bind("@number", qint64(40));
|
2015-06-01 18:51:55 +02:00
|
|
|
statement.next();
|
|
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
ASSERT_THAT(statement.text(0), "poo");
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(SqliteStatement, BindDoubleByIndex)
|
|
|
|
|
{
|
2017-07-27 15:59:54 +02:00
|
|
|
SqliteReadStatement statement("SELECT name, number FROM test WHERE number=@number", database);
|
2015-06-01 18:51:55 +02:00
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
statement.bind(statement.bindingIndexForName("@number"), 23.3);
|
2015-06-01 18:51:55 +02:00
|
|
|
statement.next();
|
|
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
ASSERT_THAT(statement.text(0), "foo");
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(SqliteStatement, BindFailure)
|
|
|
|
|
{
|
2017-07-27 15:59:54 +02:00
|
|
|
SqliteReadStatement statement("SELECT name, number FROM test WHERE number=@number", database);
|
2015-06-01 18:51:55 +02:00
|
|
|
|
|
|
|
|
ASSERT_THROW(statement.bind(0, 40), SqliteException);
|
|
|
|
|
ASSERT_THROW(statement.bind(2, 40), SqliteException);
|
2017-07-27 15:59:54 +02:00
|
|
|
ASSERT_THROW(statement.bind("@name", 40), SqliteException);
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(SqliteStatement, RequestBindingNamesFromStatement)
|
|
|
|
|
{
|
2017-07-27 15:59:54 +02:00
|
|
|
SqliteWriteStatement statement("UPDATE test SET name=@name, number=@number WHERE rowid=@id", database);
|
2015-06-01 18:51:55 +02:00
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
ASSERT_THAT(statement.bindingColumnNames(), ElementsAre("name", "number", "id"));
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(SqliteStatement, ClosedDatabase)
|
|
|
|
|
{
|
2017-07-26 18:43:07 +02:00
|
|
|
database.close();
|
2017-07-27 15:59:54 +02:00
|
|
|
ASSERT_THROW(SqliteWriteStatement("INSERT INTO test(name, number) VALUES (?, ?)", database), SqliteException);
|
|
|
|
|
ASSERT_THROW(SqliteReadStatement("SELECT * FROM test", database), SqliteException);
|
|
|
|
|
ASSERT_THROW(SqliteReadWriteStatement("INSERT INTO test(name, number) VALUES (?, ?)", database), SqliteException);
|
2017-07-26 18:43:07 +02:00
|
|
|
database.open(QDir::tempPath() + QStringLiteral("/SqliteStatementTest.db"));
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SqliteStatement::SetUp()
|
|
|
|
|
{
|
2017-07-26 18:43:07 +02:00
|
|
|
database.setJournalMode(JournalMode::Memory);
|
2017-07-27 15:59:54 +02:00
|
|
|
database.open(":memory:");
|
2017-07-31 17:44:45 +02:00
|
|
|
database.execute("CREATE TABLE test(name TEXT UNIQUE, number NUMERIC)");
|
|
|
|
|
database.execute("INSERT INTO test VALUES ('bar', 'blah')");
|
|
|
|
|
database.execute("INSERT INTO test VALUES ('foo', 23.3)");
|
|
|
|
|
database.execute("INSERT INTO test VALUES ('poo', 40)");
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SqliteStatement::TearDown()
|
|
|
|
|
{
|
2017-07-26 18:43:07 +02:00
|
|
|
database.close();
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|