Herunterladen als
root/tests/em/tst_querybuilder.cpp @ 1994157c
cca98131 | Christian Ehringfeld | #include "tst_querybuilder.h"
|
|
e7cfc35a | Christian Ehringfeld | ||
4af3f83d | Christian Ehringfeld | void QuerybuilderTest::initTestCase() {
|
|
CuteEntityManager::EntityInstanceFactory::registerClass<Group>();
|
|||
CuteEntityManager::EntityInstanceFactory::registerClass<Person>();
|
|||
CuteEntityManager::EntityInstanceFactory::registerClass<Article>();
|
|||
CuteEntityManager::EntityInstanceFactory::registerClass<Employee>();
|
|||
CuteEntityManager::EntityInstanceFactory::registerClass<WorkerGroup>();
|
|||
this->e = new CuteEntityManager::EntityManager("QSQLITE",
|
|||
5c7f1541 | Christian Ehringfeld | ":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",
|
|||
Person::Gender::MALE, "lucien.jpeg", "", "Luc", QDate(1995, 2, 4)));
|
|||
QSharedPointer<Employee> p2 = QSharedPointer<Employee>(new Employee(11, "Janine",
|
|||
"Musterfrau",
|
|||
Person::Gender::FEMALE, "janine.jpeg", "", "", QDate(1992, 8, 5), "B"));
|
|||
QSharedPointer<Employee> p3 = QSharedPointer<Employee>(new Employee(42, "Fenja", "Sey.",
|
|||
Person::Gender::FEMALE, "fenja.jpeg", "", "Lotta", QDate(1990, 11, 11), "A"));
|
|||
QSharedPointer<Person> p4 = QSharedPointer<Person>(new Person("Fenja", "Neu",
|
|||
Person::Gender::FEMALE, "fenja2.jpeg", "", "Fenni", QDate(1980, 5, 30)));
|
|||
01fe6db3 | Christian Ehringfeld | QSharedPointer<Employee> p5 = QSharedPointer<Employee>(new Employee(90, "Milan", "Mes.",
|
|
Person::Gender::MALE, "milan.jpeg", "", "", QDate(1994, 3, 27), "D"));
|
|||
QSharedPointer<Person> p6 = QSharedPointer<Person>(new Person("Kristina", "Zero",
|
|||
Person::Gender::FEMALE, "kristina.jpeg", "", "", 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"));
|
|||
g->setLeader(p1);
|
|||
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);
|
|||
auto gEnt = g.objectCast<Entity>();
|
|||
auto g2Ent = g2.objectCast<Entity>();
|
|||
auto wgEnt = wg.objectCast<Entity>();
|
|||
01fe6db3 | Christian Ehringfeld | auto p5Ent = p5.objectCast<Entity>();
|
|
auto p6Ent = p6.objectCast<Entity>();
|
|||
cca98131 | Christian Ehringfeld | try {
|
|
01fe6db3 | Christian Ehringfeld | QVERIFY(this->e->save(p5Ent));
|
|
cca98131 | Christian Ehringfeld | QVERIFY(this->e->save(gEnt));
|
|
QVERIFY(this->e->save(wgEnt));
|
|||
QVERIFY(this->e->save(g2Ent));
|
|||
01fe6db3 | Christian Ehringfeld | QVERIFY(this->e->save(p6Ent));
|
|
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"));
|
|||
e7cfc35a | Christian Ehringfeld | }
|
|
4af3f83d | Christian Ehringfeld | void QuerybuilderTest::testFindByAttributesManyToOneRelation() {
|
|
QHash<QString, QVariant> attributes;
|
|||
39ea0d06 | Christian Ehringfeld | attributes["firstName"] = QString("Lucien");
|
|
attributes["familyName"] = QString("We");
|
|||
QSharedPointer<Person> p = e->findEntityByAttributes<Person>(attributes, true);
|
|||
cca98131 | Christian Ehringfeld | QVERIFY(p);
|
|
39ea0d06 | Christian Ehringfeld | attributes.clear();
|
|
cca98131 | Christian Ehringfeld | attributes["leader"] = QVariant(p);
|
|
4af3f83d | Christian Ehringfeld | QSharedPointer<Group> group = e->findEntityByAttributes<Group>
|
|
(attributes, true);
|
|||
QVERIFY(group);
|
|||
cca98131 | Christian Ehringfeld | QCOMPARE(group->getName(), QString("Group Psy"));
|
|
e7cfc35a | Christian Ehringfeld | }
|
|
4af3f83d | Christian Ehringfeld | void QuerybuilderTest::testFindByAttributesManyToOneRelationAttribute() {
|
|
QHash<QString, QVariant> attributes;
|
|||
attributes["leader.firstName"] = QString("Fenja");
|
|||
QSharedPointer<Group> group = e->findEntityByAttributes<Group>
|
|||
(attributes, true);
|
|||
QVERIFY(group);
|
|||
}
|
|||
void QuerybuilderTest::testFindByAttributesManyToManyRelation() {
|
|||
QHash<QString, QVariant> attributes;
|
|||
46d2de48 | Christian Ehringfeld | attributes["firstName"] = "Kristina";
|
|
cca98131 | Christian Ehringfeld | QSharedPointer<Person> p = e->findEntityByAttributes<Person>(attributes, true);
|
|
QVERIFY(p);
|
|||
46d2de48 | Christian Ehringfeld | QCOMPARE(p->getFamilyName(), QString("Zero"));
|
|
cca98131 | Christian Ehringfeld | attributes.clear();
|
|
attributes["persons"] = QVariant(p);
|
|||
QSharedPointer<Group> group = e->findEntityByAttributes<Group>
|
|||
(attributes, true);
|
|||
QVERIFY(group);
|
|||
QCOMPARE(group->getName(), QString("Group Health"));
|
|||
4af3f83d | Christian Ehringfeld | }
|
|
cca98131 | Christian Ehringfeld | void QuerybuilderTest::testFindByAttributesManyToManyRelationWithList() {
|
|
4af3f83d | Christian Ehringfeld | QHash<QString, QVariant> attributes;
|
|
cca98131 | Christian Ehringfeld | QList<QSharedPointer<Person>> persons = QList<QSharedPointer<Person>>();
|
|
attributes["firstName"] = QString("Lucien");
|
|||
QSharedPointer<Person> p1 = e->findEntityByAttributes<Person>(attributes, true);
|
|||
QVERIFY(p1);
|
|||
QCOMPARE(p1->getFamilyName(), QString("We"));
|
|||
persons.append(p1);
|
|||
attributes["firstName"] = QString("Janine");
|
|||
QSharedPointer<Person> p2 = e->findEntityByAttributes<Person>(attributes, true);
|
|||
QVERIFY(p2);
|
|||
QCOMPARE(p2->getFamilyName(), QString("Musterfrau"));
|
|||
persons.append(p2);
|
|||
attributes.clear();
|
|||
QVariant var;
|
|||
var.setValue<QList<QSharedPointer<Person>>>(persons);
|
|||
attributes["persons"] = var;
|
|||
4af3f83d | Christian Ehringfeld | QSharedPointer<Group> group = e->findEntityByAttributes<Group>
|
|
(attributes, true);
|
|||
QVERIFY(group);
|
|||
cca98131 | Christian Ehringfeld | QCOMPARE(group->getName(), QString("Group Psy"));
|
|
4af3f83d | Christian Ehringfeld | }
|
|
cca98131 | Christian Ehringfeld | void QuerybuilderTest::testFindByAttributesManyToManyRelationAttribute() {
|
|
QHash<QString, QVariant> attributes;
|
|||
attributes["persons.firstName"] = QString("Janine");
|
|||
QSharedPointer<Group> group = e->findEntityByAttributes<Group>
|
|||
(attributes, true);
|
|||
QVERIFY(group);
|
|||
QCOMPARE(group->getName(), QString("Group Psy"));
|
|||
4af3f83d | Christian Ehringfeld | }
|
|
void QuerybuilderTest::testQueryBuilder() {
|
|||
01fe6db3 | Christian Ehringfeld | auto qb = e->getQueryBuilder();
|
|
Query q = Query();
|
|||
q.appendWhere(q.like(qb, QString("firstName"), QString("Fenj"),
|
|||
JokerPosition::BEHIND));
|
|||
q.setLimit(10);
|
|||
QList<QSharedPointer<Person>> list = e->find<Person>(q);
|
|||
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 {
|
|||
QList<QSharedPointer<Employee>> list = e->find<Employee>(q);
|
|||
QCOMPARE(list.size(), 3);
|
|||
} catch(QString e) {
|
|||
qWarning() << e;
|
|||
QFAIL("Exception");
|
|||
}
|
|||
}
|
|||
void QuerybuilderTest::testQueryBuilderEntityInheritanceWithoutJoin() {
|
|||
Query q = Query();
|
|||
try {
|
|||
QList<QSharedPointer<Employee>> list = e->find<Employee>(q, true);
|
|||
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);
|
|||
QList<QSharedPointer<Person>> list = e->find<Person>(q, true);
|
|||
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"));
|
|
QList<QSharedPointer<Employee>> list = e->find<Employee>(q, false);
|
|||
QCOMPARE(list.size(), 1);
|
|||
46d2de48 | Christian Ehringfeld | QCOMPARE(list.at(0)->getFirstName(), QString("Milan"));
|
|
QCOMPARE(list.at(0)->getFamilyName(), QString("Mes."));
|
|||
01fe6db3 | Christian Ehringfeld | }
|
|
void QuerybuilderTest::testQueryBuilderManyToOneRelation() {
|
|||
auto qb = e->getQueryBuilder();
|
|||
Query q = Query();
|
|||
q.appendWhere(q.equal(qb, "persNumber", 42));
|
|||
QList<QSharedPointer<Employee>> list = e->find<Employee>(q, true);
|
|||
QCOMPARE(list.size(), 1);
|
|||
QCOMPARE(list.at(0)->getNickName(), QString("Lotta"));
|
|||
q = Query();
|
|||
q.appendWhere(q.equal(qb, "leader", QVariant(list.at(0))));
|
|||
QList<QSharedPointer<Group>> groupList = e->find<Group>(q, false);
|
|||
QCOMPARE(groupList.size(), 1);
|
|||
QCOMPARE(groupList.at(0)->getName(), QString("Group Health"));
|
|||
}
|
|||
void QuerybuilderTest::testQueryBuilderManyToOneRelationAttribute() {
|
|||
auto qb = e->getQueryBuilder();
|
|||
Query q = Query();
|
|||
q.appendWhere(q.equal(qb, "leader.firstName", QString("Fenja")));
|
|||
QList<QSharedPointer<Group>> groupList = e->find<Group>(q, false, false);
|
|||
QCOMPARE(groupList.size(), 1);
|
|||
QCOMPARE(groupList.at(0)->getName(), QString("Group Health"));
|
|||
QCOMPARE(groupList.at(0)->getPersons().size(), 0);
|
|||
}
|
|||
void QuerybuilderTest::testQueryBuilderManyToManyRelation() {
|
|||
auto qb = e->getQueryBuilder();
|
|||
Query q = Query();
|
|||
q.appendWhere(q.equal(qb, "persNumber", 42));
|
|||
QList<QSharedPointer<Employee>> list = e->find<Employee>(q, true);
|
|||
QCOMPARE(list.size(), 1);
|
|||
QCOMPARE(list.at(0)->getNickName(), QString("Lotta"));
|
|||
q = Query();
|
|||
q.appendWhere(q.equal(qb, "persons", QVariant(list.at(0))));
|
|||
QList<QSharedPointer<Group>> groupList = e->find<Group>(q, false);
|
|||
QCOMPARE(groupList.size(), 1);
|
|||
QCOMPARE(groupList.at(0)->getName(), QString("Group Health"));
|
|||
}
|
|||
void QuerybuilderTest::testQueryBuilderManyToManyRelationAttribute() {
|
|||
auto qb = e->getQueryBuilder();
|
|||
Query q = Query();
|
|||
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 | }
|