Herunterladen als
root/tests/em/tst_querybuilder.cpp @ 9c269b1f
cca98131 | Christian Ehringfeld | #include "tst_querybuilder.h"
|
|
f13a6e62 | Christian Ehringfeld | #include <QDebug>
|
|
e7cfc35a | Christian Ehringfeld | ||
4af3f83d | Christian Ehringfeld | void QuerybuilderTest::initTestCase() {
|
|
CuteEntityManager::EntityInstanceFactory::registerClass<Group>();
|
|||
CuteEntityManager::EntityInstanceFactory::registerClass<Person>();
|
|||
CuteEntityManager::EntityInstanceFactory::registerClass<Employee>();
|
|||
CuteEntityManager::EntityInstanceFactory::registerClass<WorkerGroup>();
|
|||
e3fc2748 | Christian Ehringfeld | this->e = new CuteEntityManager::EntityManager("QSQLITE", ":memory:", "", "", "", "",
|
|
true, "foreign_keys = ON", false);
|
|||
4af3f83d | Christian Ehringfeld | QStringList inits = QStringList() << "Person" << "Group" << "Employee" << "WorkerGroup";
|
|
QVERIFY2(this->e->startup("queryBuilderTest", inits), "Failure");
|
|||
QSharedPointer<Person> p1 = QSharedPointer<Person>(new Person("Lucien", "We",
|
|||
21826301 | Christian Ehringfeld | Person::Gender::MALE, "Luc", QDate(1995, 2, 4)));
|
|
4af3f83d | Christian Ehringfeld | QSharedPointer<Employee> p2 = QSharedPointer<Employee>(new Employee(11, "Janine",
|
|
"Musterfrau",
|
|||
21826301 | Christian Ehringfeld | Person::Gender::FEMALE, "", QDate(1992, 8, 5), "B"));
|
|
4af3f83d | Christian Ehringfeld | QSharedPointer<Employee> p3 = QSharedPointer<Employee>(new Employee(42, "Fenja", "Sey.",
|
|
21826301 | Christian Ehringfeld | Person::Gender::FEMALE, "Lotta", QDate(1990, 11, 11), "A"));
|
|
4af3f83d | Christian Ehringfeld | QSharedPointer<Person> p4 = QSharedPointer<Person>(new Person("Fenja", "Neu",
|
|
21826301 | Christian Ehringfeld | Person::Gender::FEMALE, "Fenni", QDate(1980, 5, 30)));
|
|
01fe6db3 | Christian Ehringfeld | QSharedPointer<Employee> p5 = QSharedPointer<Employee>(new Employee(90, "Milan", "Mes.",
|
|
21826301 | Christian Ehringfeld | Person::Gender::MALE, "", QDate(1994, 3, 27), "D"));
|
|
01fe6db3 | Christian Ehringfeld | QSharedPointer<Person> p6 = QSharedPointer<Person>(new Person("Kristina", "Zero",
|
|
21826301 | Christian Ehringfeld | Person::Gender::FEMALE, "", QDate(1996, 5, 17)));
|
|
4af3f83d | Christian Ehringfeld | QSharedPointer<Group> g = QSharedPointer<Group>(new Group("Group Psy"));
|
|
QSharedPointer<Group> g2 = QSharedPointer<Group>(new Group("Group Health"));
|
|||
31ca3a70 | Christian Ehringfeld | QSharedPointer<Group> g3 = QSharedPointer<Group>(new Group("Group ABC"));
|
|
4af3f83d | Christian Ehringfeld | g->setLeader(p1);
|
|
31ca3a70 | Christian Ehringfeld | g3->setLeader(p3);
|
|
g3->setPersons({p3});
|
|||
4af3f83d | Christian Ehringfeld | g->setPersons({p1, p2, p4});
|
|
cca98131 | Christian Ehringfeld | g2->setLeader(p3);
|
|
g2->setPersons({p3});
|
|||
4af3f83d | Christian Ehringfeld | QSharedPointer<WorkerGroup> wg = QSharedPointer<WorkerGroup>(new
|
|
WorkerGroup("Taskforce P&H", 50));
|
|||
wg->addWorker(p2);
|
|||
wg->addWorker(p3);
|
|||
cca98131 | Christian Ehringfeld | try {
|
|
e3fc2748 | Christian Ehringfeld | QVERIFY(this->e->save(p5));
|
|
QVERIFY(this->e->save(g));
|
|||
QVERIFY(this->e->save(wg));
|
|||
QVERIFY(this->e->save(g2));
|
|||
QVERIFY(this->e->save(p6));
|
|||
QVERIFY(this->e->save(g3));
|
|||
cca98131 | Christian Ehringfeld | } catch(QString s) {
|
|
5c7f1541 | Christian Ehringfeld | qWarning() << s;
|
|
cca98131 | Christian Ehringfeld | }
|
|
e7cfc35a | Christian Ehringfeld | }
|
|
4af3f83d | Christian Ehringfeld | void QuerybuilderTest::cleanupTestCase() {
|
|
auto qb = this->e->getQueryBuilder();
|
|||
QVERIFY(this->e->executeQuery(qb->dropTable("workergroup_workers")));
|
|||
QVERIFY(this->e->executeQuery(qb->dropTable("employee")));
|
|||
QVERIFY(this->e->executeQuery(qb->dropTable("workergroup")));
|
|||
QVERIFY(this->e->executeQuery(qb->dropTable("person_groups")));
|
|||
QVERIFY(this->e->executeQuery(qb->dropTable("group")));
|
|||
QVERIFY(this->e->executeQuery(qb->dropTable("person")));
|
|||
QVERIFY(this->e->removeAll("cuteentitymanager::databasemigration"));
|
|||
if (this->e) {
|
|||
delete this->e;
|
|||
this->e = nullptr;
|
|||
}
|
|||
e7cfc35a | Christian Ehringfeld | }
|
|
4af3f83d | Christian Ehringfeld | void QuerybuilderTest::testFindByAttributes() {
|
|
QHash<QString, QVariant> attributes;
|
|||
attributes["persNumber"] = 42;
|
|||
QSharedPointer<Employee> p = e->findEntityByAttributes<Employee>(attributes, true);
|
|||
QVERIFY(p);
|
|||
QCOMPARE(p->getNickName(), QString("Lotta"));
|
|||
64dc4a24 | Christian Ehringfeld | }
|
|
void QuerybuilderTest::testFindByAttributesManyToManyResolve() {
|
|||
QHash<QString, QVariant> attributes;
|
|||
attributes["persNumber"] = 42;
|
|||
QSharedPointer<Employee> p = e->findEntityByAttributes<Employee>(attributes, true);
|
|||
QVERIFY(p);
|
|||
QCOMPARE(p->getNickName(), QString("Lotta"));
|
|||
31ca3a70 | Christian Ehringfeld | QCOMPARE(p->getGroups().size(), 2);
|
|
51f88a34 | Christian Ehringfeld | QVERIFY(p->getGroups().first());
|
|
4cde82a2 | Christian Ehringfeld | QCOMPARE(p->getGroups().first()->getPersons().size(),1);
|
|
f13a6e62 | Christian Ehringfeld | QCOMPARE(p->getGroups().first()->getPersons().last()->getFamilyName(), QString("Sey."));
|
|
64dc4a24 | Christian Ehringfeld | }
|
|
void QuerybuilderTest::testFindByAttributesOneToManyResolve() {
|
|||
QHash<QString, QVariant> attributes;
|
|||
attributes["persNumber"] = 42;
|
|||
QSharedPointer<Employee> p = e->findEntityByAttributes<Employee>(attributes, true);
|
|||
QVERIFY(p);
|
|||
31ca3a70 | Christian Ehringfeld | QCOMPARE(p->getMaintainedGroups().size(), 2);
|
|
64dc4a24 | Christian Ehringfeld | }
|
|
void QuerybuilderTest::testFindByAttributesOneToOneResolve() {
|
|||
}
|
|||
void QuerybuilderTest::testFindByAttributesManyToOneResolve() {
|
|||
QHash<QString, QVariant> attributes;
|
|||
attributes["name"] = QString("Group Health");
|
|||
QSharedPointer<Group> g = this->e->findEntityByAttributes<Group>(attributes, true);
|
|||
31ca3a70 | Christian Ehringfeld | QVERIFY(g->getLeader());
|
|
QCOMPARE(g->getLeader()->getFirstName(), QString("Fenja"));
|
|||
QCOMPARE(g->getLeader()->getFamilyName(), QString("Sey."));
|
|||
e7cfc35a | Christian Ehringfeld | }
|
|
95d9cf46 | Christian Ehringfeld | void QuerybuilderTest::testFindByAttributesSuperClassAttribute() {
|
|
auto qb = e->getQueryBuilder();
|
|||
Query q = Query();
|
|||
q.appendWhere(q.equal(qb, "nickName", QString("Lotta")));
|
|||
QList<QSharedPointer<Employee>> list = e->find<Employee>(q, true);
|
|||
QCOMPARE(list.size(), 1);
|
|||
QCOMPARE(list.at(0)->getPersNumber(), (unsigned long long)42);
|
|||
}
|
|||
void QuerybuilderTest::testQueryBuilderCount() {
|
|||
auto qb = e->getQueryBuilder();
|
|||
Query q = Query();
|
|||
77000f90 | Christian Ehringfeld | q.appendWhere(q.equal(qb, "gender", (int) Person::Gender::FEMALE));
|
|
95d9cf46 | Christian Ehringfeld | q.appendFrom("person");
|
|
QCOMPARE(this->e->count(q), (quint32)4);
|
|||
}
|
|||
4af3f83d | Christian Ehringfeld | void QuerybuilderTest::testQueryBuilder() {
|
|
64dc4a24 | Christian Ehringfeld | auto qb = this->e->getQueryBuilder();
|
|
01fe6db3 | Christian Ehringfeld | Query q = Query();
|
|
q.appendWhere(q.like(qb, QString("firstName"), QString("Fenj"),
|
|||
JokerPosition::BEHIND));
|
|||
q.setLimit(10);
|
|||
64dc4a24 | Christian Ehringfeld | QList<QSharedPointer<Person>> list = this->e->find<Person>(q);
|
|
01fe6db3 | Christian Ehringfeld | QCOMPARE(list.size(), 2);
|
|
}
|
|||
void QuerybuilderTest::testQueryBuilderEntityInheritance() {
|
|||
auto qb = e->getQueryBuilder();
|
|||
QSharedPointer<Employee> emp = QSharedPointer<Employee>(new Employee());
|
|||
Query q = Query();
|
|||
q.appendJoins(q.joinBaseClasses(qb, emp));
|
|||
try {
|
|||
64dc4a24 | Christian Ehringfeld | QList<QSharedPointer<Employee>> list = this->e->find<Employee>(q);
|
|
01fe6db3 | Christian Ehringfeld | QCOMPARE(list.size(), 3);
|
|
} catch(QString e) {
|
|||
qWarning() << e;
|
|||
QFAIL("Exception");
|
|||
}
|
|||
}
|
|||
void QuerybuilderTest::testQueryBuilderEntityInheritanceWithoutJoin() {
|
|||
Query q = Query();
|
|||
try {
|
|||
64dc4a24 | Christian Ehringfeld | QList<QSharedPointer<Employee>> list = this->e->find<Employee>(q, true);
|
|
01fe6db3 | Christian Ehringfeld | QCOMPARE(list.size(), 3);
|
|
} catch(QString e) {
|
|||
qWarning() << e;
|
|||
QFAIL("Exception");
|
|||
}
|
|||
}
|
|||
void QuerybuilderTest::testQueryBuilderArbitraryOperator() {
|
|||
auto qb = e->getQueryBuilder();
|
|||
Query q = Query();
|
|||
q.appendWhere(q.arbitraryOperator(qb, "<", "birthday",
|
|||
QDate(1991, 10, 10)));
|
|||
q.setDistinct(true);
|
|||
q.appendOrderBy(OrderBy(QString("birthday"), Direction::SORT_DESC));
|
|||
q.setLimit(10);
|
|||
64dc4a24 | Christian Ehringfeld | QList<QSharedPointer<Person>> list = this->e->find<Person>(q, true);
|
|
01fe6db3 | Christian Ehringfeld | QCOMPARE(list.size(), 2);
|
|
46d2de48 | Christian Ehringfeld | QCOMPARE(list.at(0)->getFirstName(), QString("Fenja"));
|
|
QCOMPARE(list.at(0)->getFamilyName(), QString("Sey."));
|
|||
QCOMPARE(list.at(1)->getFirstName(), QString("Fenja"));
|
|||
QCOMPARE(list.at(1)->getFamilyName(), QString("Neu"));
|
|||
01fe6db3 | Christian Ehringfeld | }
|
|
void QuerybuilderTest::testQueryBuilderJoins() {
|
|||
auto qb = e->getQueryBuilder();
|
|||
Query q = Query();
|
|||
46d2de48 | Christian Ehringfeld | q.appendWhere(q.equal(qb, "firstName", "Milan"));
|
|
01fe6db3 | Christian Ehringfeld | q.appendJoin(Join("person", "person.id = employee.id"));
|
|
64dc4a24 | Christian Ehringfeld | QList<QSharedPointer<Employee>> list = this->e->find<Employee>(q, false);
|
|
01fe6db3 | Christian Ehringfeld | QCOMPARE(list.size(), 1);
|
|
46d2de48 | Christian Ehringfeld | QCOMPARE(list.at(0)->getFirstName(), QString("Milan"));
|
|
QCOMPARE(list.at(0)->getFamilyName(), QString("Mes."));
|
|||
01fe6db3 | Christian Ehringfeld | }
|
|
3b82c8c0 | Christian Ehringfeld | void QuerybuilderTest::testQueryBuilderSingleAttributeOr() {
|
|
auto qb = e->getQueryBuilder();
|
|||
Query q = Query();
|
|||
q.appendWhere(q.equal(qb, "nickName", QString("Lotta")));
|
|||
q.appendWhere(q.orOperator(qb));
|
|||
q.appendWhere(q.equal(qb, "nickName", QString("Fenni")));
|
|||
QList<QSharedPointer<Person>> list = e->find<Person>(q, true);
|
|||
QCOMPARE(list.size(), 2);
|
|||
01fe6db3 | Christian Ehringfeld | }
|
|
void QuerybuilderTest::testQueryBuilderManyToOneRelationAttribute() {
|
|||
auto qb = e->getQueryBuilder();
|
|||
Query q = Query();
|
|||
e3fc2748 | Christian Ehringfeld | q.appendJoinWith("leader");
|
|
q.appendOrderBy("name", Direction::SORT_DESC);
|
|||
01fe6db3 | Christian Ehringfeld | q.appendWhere(q.equal(qb, "leader.firstName", QString("Fenja")));
|
|
QList<QSharedPointer<Group>> groupList = e->find<Group>(q, false, false);
|
|||
e3fc2748 | Christian Ehringfeld | QCOMPARE(groupList.size(), 2);
|
|
01fe6db3 | Christian Ehringfeld | QCOMPARE(groupList.at(0)->getName(), QString("Group Health"));
|
|
4cde82a2 | Christian Ehringfeld | //should be with cache 1 and without cache 0
|
|
e3fc2748 | Christian Ehringfeld | QCOMPARE(groupList.at(0)->getPersons().size(), 1);
|
|
01fe6db3 | Christian Ehringfeld | }
|
|
void QuerybuilderTest::testQueryBuilderManyToManyRelationAttribute() {
|
|||
auto qb = e->getQueryBuilder();
|
|||
Query q = Query();
|
|||
e3fc2748 | Christian Ehringfeld | q.appendJoinWith("persons");
|
|
01fe6db3 | Christian Ehringfeld | q.appendWhere(q.equal(qb, "persons.firstName", QString("Janine")));
|
|
QList<QSharedPointer<Group>> groupList = e->find<Group>(q, false);
|
|||
QCOMPARE(groupList.size(), 1);
|
|||
QCOMPARE(groupList.at(0)->getName(), QString("Group Psy"));
|
|||
QCOMPARE(groupList.at(0)->getPersons().size(), 3);
|
|||
4af3f83d | Christian Ehringfeld | }
|
|
373a84e2 | Christian Ehringfeld | ||
f8748139 | Christian Ehringfeld | void QuerybuilderTest::testEnum()
|
|
{
|
|||
auto qb = e->getQueryBuilder();
|
|||
Query q = Query();
|
|||
QVariant var;
|
|||
var.setValue<Person::Gender>(Person::Gender::FEMALE);
|
|||
q.appendWhere(q.equal(qb, "gender", var.toInt()));
|
|||
QList<QSharedPointer<Person>> females = e->find<Person>(q, false);
|
|||
QCOMPARE(females.size(), 4);
|
|||
QCOMPARE(females.first()->getGender(), Person::Gender::FEMALE);
|
|||
}
|
|||
373a84e2 | Christian Ehringfeld | void QuerybuilderTest::testRefresh() {
|
|
a873a3ba | Christian Ehringfeld | e->clearCache();
|
|
373a84e2 | Christian Ehringfeld | auto persons = e->findAll<Person>(false);
|
|
QCOMPARE(persons.first()->getGroups().size(), 0);
|
|||
QHash<QString, QVariant> attributes;
|
|||
attributes["name"] = QString("Group Health");
|
|||
a873a3ba | Christian Ehringfeld | QSharedPointer<Group> g = this->e->findEntityByAttributes<Group>(attributes, false,
|
|
false);
|
|||
373a84e2 | Christian Ehringfeld | QCOMPARE(g->getPersons().count(), 0);
|
|
e3fc2748 | Christian Ehringfeld | e->refresh(g, true);
|
|
373a84e2 | Christian Ehringfeld | QCOMPARE(g->getPersons().count(), 1);
|
|
}
|