1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
|
/*
* Copyright (C) 2010-2015 Jeremy Lainé
* Contact: https://github.com/jlaine/qdjango
*
* This file is part of the QDjango Library.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
#include "QDjango.h"
#include "QDjangoModel.h"
#include "QDjangoQuerySet.h"
#include "QDjangoWhere.h"
#include "util.h"
class TestModel : public QDjangoModel
{
public:
TestModel(QObject *parent = 0) : QDjangoModel(parent) {}
// expose foreign key methods so they can be tested
QObject *foreignKey(const char *name) const
{ return QDjangoModel::foreignKey(name); }
void setForeignKey(const char *name, QObject *value)
{ QDjangoModel::setForeignKey(name, value); }
};
class Author : public TestModel
{
Q_OBJECT
Q_PROPERTY(QString name READ name WRITE setName)
public:
Author(QObject *parent = 0) : TestModel(parent) {}
QString name() const { return m_name; }
void setName(const QString &name) { m_name = name; }
private:
QString m_name;
};
class Book : public TestModel
{
Q_OBJECT
Q_PROPERTY(Author* author READ author WRITE setAuthor)
Q_PROPERTY(QString title READ title WRITE setTitle)
Q_CLASSINFO("author", "on_delete=cascade")
public:
Book(QObject *parent = 0)
: TestModel(parent)
{
setForeignKey("author", new Author(this));
}
Author *author() const { return qobject_cast<Author*>(foreignKey("author")); }
void setAuthor(Author *author) { setForeignKey("author", author); }
QString title() const { return m_title; }
void setTitle(const QString &title) { m_title = title; }
private:
QString m_title;
};
class BookWithNullAuthor : public TestModel
{
Q_OBJECT
Q_PROPERTY(Author* author READ author WRITE setAuthor)
Q_PROPERTY(QString title READ title WRITE setTitle)
Q_CLASSINFO("author", "null=true on_delete=cascade")
public:
BookWithNullAuthor(QObject *parent = 0) : TestModel(parent) {}
Author *author() const { return qobject_cast<Author*>(foreignKey("author")); }
void setAuthor(Author *author) { setForeignKey("author", author); }
QString title() const { return m_title; }
void setTitle(const QString &title) { m_title = title; }
private:
QString m_title;
};
/** Test QDjangoModel class.
*/
class tst_QDjangoModel : public QObject
{
Q_OBJECT
private slots:
void initTestCase();
void init();
void deleteCascade();
void foreignKey();
void foreignKey_null();
void setForeignKey();
void filterRelated();
void filterRelatedReverse();
void filterRelatedReverse_null();
void primaryKey();
void selectRelated();
void selectRelated_null();
void toString();
void cleanup();
void cleanupTestCase();
};
/** Create database tables before running tests.
*/
void tst_QDjangoModel::initTestCase()
{
QVERIFY(initialiseDatabase());
QDjango::registerModel<Author>();
QDjango::registerModel<Book>();
QDjango::registerModel<BookWithNullAuthor>();
}
/** Load fixtures.
*/
void tst_QDjangoModel::init()
{
QVERIFY(QDjango::createTables());
Author author1;
author1.setName("First author");
QCOMPARE(author1.save(), true);
Author author2;
author2.setName("Second author");
QCOMPARE(author2.save(), true);
Book book;
book.setAuthor(&author1);
book.setTitle("Some book");
QCOMPARE(book.save(), true);
Book book2;
book2.setAuthor(&author2);
book2.setTitle("Other book");
QCOMPARE(book2.save(), true);
BookWithNullAuthor book3;
book3.setTitle("Book with null author");
QCOMPARE(book3.save(), true);
BookWithNullAuthor book4;
book4.setAuthor(&author1);
book4.setTitle("Some book");
QCOMPARE(book4.save(), true);
}
void tst_QDjangoModel::deleteCascade()
{
const QDjangoQuerySet<Author> authors;
const QDjangoQuerySet<Book> books;
QCOMPARE(authors.count(), 2);
QCOMPARE(books.count(), 2);
QVERIFY(authors.filter(QDjangoWhere("name", QDjangoWhere::Equals, "First author")).remove());
QCOMPARE(authors.count(), 1);
QCOMPARE(books.count(), 1);
}
void tst_QDjangoModel::foreignKey()
{
QTest::ignoreMessage(QtWarningMsg, "QDjangoMetaModel cannot get foreign model for invalid key 'bad'");
Book book;
QVERIFY(!book.foreignKey("bad"));
QVERIFY(book.foreignKey("author"));
}
void tst_QDjangoModel::foreignKey_null()
{
QTest::ignoreMessage(QtWarningMsg, "QDjangoMetaModel cannot get foreign model for invalid key 'bad'");
BookWithNullAuthor book;
QVERIFY(!book.foreignKey("bad"));
QVERIFY(!book.foreignKey("author"));
}
void tst_QDjangoModel::setForeignKey()
{
QTest::ignoreMessage(QtWarningMsg, "QDjangoMetaModel cannot set foreign model for invalid key 'bad'");
Book book;
book.setForeignKey("bad", 0);
book.setForeignKey("author", 0);
}
/** Perform filtering on foreign keys.
*/
void tst_QDjangoModel::filterRelated()
{
QDjangoQuerySet<Book> books;
QDjangoQuerySet<Book> qs = books.filter(
QDjangoWhere("author__name", QDjangoWhere::Equals, "First author"));
CHECKWHERE(qs.where(), QLatin1String("T0.\"name\" = ?"), QVariantList() << "First author");
QCOMPARE(qs.count(), 1);
QCOMPARE(qs.size(), 1);
Book *book = qs.at(0);
QVERIFY(book != 0);
QCOMPARE(book->title(), QLatin1String("Some book"));
delete book;
}
void tst_QDjangoModel::filterRelatedReverse()
{
QDjangoQuerySet<Author> authors;
QDjangoQuerySet<Author> qs = authors.filter(
QDjangoWhere("book__title", QDjangoWhere::Equals, "Some book"));
QVERIFY(!qs.values().isEmpty());
QCOMPARE(qs.count(), 1);
QCOMPARE(qs.size(), 1);
Author *author = qs.at(0);
QVERIFY(author != 0);
QCOMPARE(author->name(), QLatin1String("First author"));
delete author;
}
void tst_QDjangoModel::filterRelatedReverse_null()
{
QDjangoQuerySet<Author> authors;
QDjangoQuerySet<Author> qs = authors.filter(
QDjangoWhere("bookwithnullauthor__title", QDjangoWhere::Equals, "Some book"));
QVERIFY(!qs.values().isEmpty());
QCOMPARE(qs.count(), 1);
QCOMPARE(qs.size(), 1);
Author *author = qs.at(0);
QVERIFY(author != 0);
QCOMPARE(author->name(), QLatin1String("First author"));
delete author;
}
void tst_QDjangoModel::primaryKey()
{
Author author;
QCOMPARE(author.pk(), QVariant());
author.setPk(1);
QCOMPARE(author.pk(), QVariant(1));
}
/** Test eager loading of foreign keys.
*/
void tst_QDjangoModel::selectRelated()
{
// without eager loading
QDjangoQuerySet<Book> qs;
Book *book = qs.get(QDjangoWhere("title", QDjangoWhere::Equals, "Some book"));
QVERIFY(book != 0);
QCOMPARE(book->title(), QLatin1String("Some book"));
QVERIFY(book->author() != 0);
QCOMPARE(book->author()->name(), QLatin1String("First author"));
delete book;
// with eager loading
book = qs.selectRelated().get(QDjangoWhere("title", QDjangoWhere::Equals, "Some book"));
QVERIFY(book != 0);
QCOMPARE(book->title(), QLatin1String("Some book"));
QVERIFY(book->author() != 0);
QCOMPARE(book->author()->name(), QLatin1String("First author"));
delete book;
}
void tst_QDjangoModel::selectRelated_null()
{
// without eager loading
QDjangoQuerySet<BookWithNullAuthor> qs;
BookWithNullAuthor *book = qs.get(QDjangoWhere("title", QDjangoWhere::Equals, "Book with null author"));
QVERIFY(book != 0);
QCOMPARE(book->title(), QLatin1String("Book with null author"));
QVERIFY(!book->author());
delete book;
// with eager loading
book = qs.selectRelated().get(QDjangoWhere("title", QDjangoWhere::Equals, "Book with null author"));
QVERIFY(book != 0);
QCOMPARE(book->title(), QLatin1String("Book with null author"));
QVERIFY(!book->author());
delete book;
}
void tst_QDjangoModel::toString()
{
QDjangoQuerySet<Book> qs;
Book *book = qs.get(QDjangoWhere("title", QDjangoWhere::Equals, "Some book"));
QVERIFY(book != 0);
QCOMPARE(book->toString(), QLatin1String("Book(id=1)"));
delete book;
}
/** Clear database tables after each test.
*/
void tst_QDjangoModel::cleanup()
{
QVERIFY(QDjango::dropTables());
}
/** Drop database tables after running tests.
*/
void tst_QDjangoModel::cleanupTestCase()
{
}
QTEST_MAIN(tst_QDjangoModel)
#include "tst_qdjangomodel.moc"
|