createtablesqlstatementbuilder-test.cpp 5.73 KB
Newer Older
1 2
/****************************************************************************
**
3 4
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
5 6 7 8 9 10 11
**
** 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
12 13 14
** 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.
15
**
16 17 18 19 20 21 22
** 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.
23 24 25
**
****************************************************************************/

Marco Bubke's avatar
Marco Bubke committed
26
#include "googletest.h"
27 28 29 30 31 32 33 34

#include <QString>

#include <createtablesqlstatementbuilder.h>
#include <sqlstatementbuilderexception.h>

#include <utf8stringvector.h>

Marco Bubke's avatar
Marco Bubke committed
35 36 37 38 39
namespace {

using Sqlite::ColumnDefinition;
using Sqlite::SqlStatementBuilderException;

40 41 42 43
class CreateTableSqlStatementBuilder : public ::testing::Test
{
protected:
    void bindValues();
Marco Bubke's avatar
Marco Bubke committed
44 45 46 47 48 49
    static const QVector<ColumnDefinition> createColumnDefintions();
    static const ColumnDefinition createColumnDefintion(const Utf8String &name,
                                                        ColumnType type,
                                                        bool isPrimaryKey = false);
protected:
    Sqlite::CreateTableSqlStatementBuilder builder;
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
};

TEST_F(CreateTableSqlStatementBuilder, IsNotValidAfterCreation)
{
    ASSERT_FALSE(builder.isValid());
}

TEST_F(CreateTableSqlStatementBuilder, IsValidAfterBinding)
{
    bindValues();

    ASSERT_TRUE(builder.isValid());
}

TEST_F(CreateTableSqlStatementBuilder, InvalidAfterClear)
{
    bindValues();

    builder.clear();

    ASSERT_TRUE(!builder.isValid());
}

TEST_F(CreateTableSqlStatementBuilder, NoSqlStatementAfterClear)
{
    bindValues();
    builder.sqlStatement();

    builder.clear();

    ASSERT_THROW(builder.sqlStatement(), SqlStatementBuilderException);
}

TEST_F(CreateTableSqlStatementBuilder, SqlStatement)
{
    bindValues();

    ASSERT_THAT(builder.sqlStatement(),
                Utf8StringLiteral("CREATE TABLE IF NOT EXISTS test(id INTEGER PRIMARY KEY, name TEXT, number NUMERIC)"));}

TEST_F(CreateTableSqlStatementBuilder, AddColumnToExistingColumns)
{
    bindValues();

    builder.addColumnDefinition(Utf8StringLiteral("number2"), ColumnType::Real);

    ASSERT_THAT(builder.sqlStatement(),
                Utf8StringLiteral("CREATE TABLE IF NOT EXISTS test(id INTEGER PRIMARY KEY, name TEXT, number NUMERIC, number2 REAL)"));}

TEST_F(CreateTableSqlStatementBuilder, ChangeTable)
{
    bindValues();

    builder.setTable(Utf8StringLiteral("test2"));

    ASSERT_THAT(builder.sqlStatement(),
                Utf8StringLiteral("CREATE TABLE IF NOT EXISTS test2(id INTEGER PRIMARY KEY, name TEXT, number NUMERIC)"));
}

TEST_F(CreateTableSqlStatementBuilder, IsInvalidAfterClearColumsOnly)
{
    bindValues();
    builder.sqlStatement();

    builder.clearColumns();

    ASSERT_THROW(builder.sqlStatement(), SqlStatementBuilderException);
}

TEST_F(CreateTableSqlStatementBuilder, ClearColumnsAndAddColumnNewColumns)
{
    bindValues();
    builder.clearColumns();

    builder.addColumnDefinition(Utf8StringLiteral("name3"), ColumnType::Text);
    builder.addColumnDefinition(Utf8StringLiteral("number3"), ColumnType::Real);

    ASSERT_THAT(builder.sqlStatement(),
                Utf8StringLiteral("CREATE TABLE IF NOT EXISTS test(name3 TEXT, number3 REAL)"));
}

TEST_F(CreateTableSqlStatementBuilder, SetWitoutRowId)
{
    bindValues();

    builder.setUseWithoutRowId(true);

    ASSERT_THAT(builder.sqlStatement(),
                Utf8StringLiteral("CREATE TABLE IF NOT EXISTS test(id INTEGER PRIMARY KEY, name TEXT, number NUMERIC) WITHOUT ROWID"));
}

TEST_F(CreateTableSqlStatementBuilder, SetColumnDefinitions)
{
    builder.clear();
    builder.setTable(Utf8StringLiteral("test"));

    builder.setColumnDefinitions(createColumnDefintions());

    ASSERT_THAT(builder.sqlStatement(),
                Utf8StringLiteral("CREATE TABLE IF NOT EXISTS test(id INTEGER PRIMARY KEY, name TEXT, number NUMERIC)"));
}

void CreateTableSqlStatementBuilder::bindValues()
{
    builder.clear();
    builder.setTable(Utf8StringLiteral("test"));
    builder.addColumnDefinition(Utf8StringLiteral("id"), ColumnType::Integer, true);
    builder.addColumnDefinition(Utf8StringLiteral("name"), ColumnType::Text);
    builder.addColumnDefinition(Utf8StringLiteral("number"),ColumnType:: Numeric);
}

Marco Bubke's avatar
Marco Bubke committed
161
const QVector<ColumnDefinition> CreateTableSqlStatementBuilder::createColumnDefintions()
162
{
Marco Bubke's avatar
Marco Bubke committed
163
    QVector<ColumnDefinition>  columnDefinitions;
164 165 166 167 168 169 170
    columnDefinitions.append(createColumnDefintion(Utf8StringLiteral("id"), ColumnType::Integer, true));
    columnDefinitions.append(createColumnDefintion(Utf8StringLiteral("name"), ColumnType::Text));
    columnDefinitions.append(createColumnDefintion(Utf8StringLiteral("number"), ColumnType::Numeric));

    return columnDefinitions;
}

Marco Bubke's avatar
Marco Bubke committed
171
const ColumnDefinition CreateTableSqlStatementBuilder::createColumnDefintion(const Utf8String &name, ColumnType type, bool isPrimaryKey)
172
{
Marco Bubke's avatar
Marco Bubke committed
173
    ColumnDefinition columnDefinition;
174 175 176 177 178 179 180

    columnDefinition.setName(name);
    columnDefinition.setType(type);
    columnDefinition.setIsPrimaryKey(isPrimaryKey);

    return columnDefinition;
}
Marco Bubke's avatar
Marco Bubke committed
181 182

}