Herunterladen als
root/tests/em/tst_em.cpp @ 64dc4a24
cca98131 | Christian Ehringfeld | #include "tst_em.h"
|
|
e7cfc35a | Christian Ehringfeld | ||
cca98131 | Christian Ehringfeld | void EmTest::initTestCase() {
|
|
e7cfc35a | Christian Ehringfeld | CuteEntityManager::EntityInstanceFactory::registerClass<Group>();
|
|
CuteEntityManager::EntityInstanceFactory::registerClass<Person>();
|
|||
CuteEntityManager::EntityInstanceFactory::registerClass<Article>();
|
|||
f8b8663b | Christian Ehringfeld | CuteEntityManager::EntityInstanceFactory::registerClass<Employee>();
|
|
CuteEntityManager::EntityInstanceFactory::registerClass<WorkerGroup>();
|
|||
95a2ba7e | Christian Ehringfeld | // this->e = new
|
|
// CuteEntityManager::EntityManager("QSQLITE",
|
|||
// QDir::currentPath() + "/db.sqlite", "", "", "", 0, true);
|
|||
e7cfc35a | Christian Ehringfeld | this->e = new CuteEntityManager::EntityManager("QSQLITE",
|
|
01fe6db3 | Christian Ehringfeld | ":memory:", "", "", "", "", true, "foreign_keys = ON", false);
|
|
e7cfc35a | Christian Ehringfeld | }
|
|
cca98131 | Christian Ehringfeld | void EmTest::cleanupTestCase() {
|
|
e7cfc35a | Christian Ehringfeld | if (this->e) {
|
|
delete this->e;
|
|||
this->e = nullptr;
|
|||
}
|
|||
}
|
|||
cca98131 | Christian Ehringfeld | void EmTest::testCheckDuplicates() {
|
|
e7cfc35a | Christian Ehringfeld | QSharedPointer<Article> article = QSharedPointer<Article>(new Article(10,
|
|
QString("TestItem")));
|
|||
QSharedPointer<Entity> entity = article.objectCast<Entity>();
|
|||
QVERIFY(this->e->create(entity));
|
|||
QSharedPointer<Entity> copy = QSharedPointer<Entity>(article->copy());
|
|||
QVERIFY(!this->e->create(copy, true, true));
|
|||
}
|
|||
cca98131 | Christian Ehringfeld | void EmTest::testBasics() {
|
|
e7cfc35a | Christian Ehringfeld | QSharedPointer<Article> article = QSharedPointer<Article>(new Article(10,
|
|
QString("TestItem")));
|
|||
QSharedPointer<Entity> entity = article.objectCast<Entity>();
|
|||
QVERIFY(this->e->create(entity));
|
|||
article->setPrice(20);
|
|||
article->setName("NewTestItem");
|
|||
QVERIFY(this->e->save(entity));
|
|||
QHash<QString, QVariant> attrs = QHash<QString, QVariant>();
|
|||
attrs.insert("price", 20);
|
|||
attrs.insert("name", "NewTestItem");
|
|||
20e1ffec | Christian Ehringfeld | QSharedPointer<Article> article2 = this->e->findEntityByAttributes<Article>(attrs);
|
|
e7cfc35a | Christian Ehringfeld | QVERIFY(article2);
|
|
entity = article2.objectCast<Entity>();
|
|||
QVERIFY(this->e->remove(entity));
|
|||
QCOMPARE(this->e->count("article"), (quint8)0);
|
|||
}
|
|||
cca98131 | Christian Ehringfeld | void EmTest::init() {
|
|
e7cfc35a | Christian Ehringfeld | QStringList inits = QStringList() << "Person" << "Group" << "Article";
|
|
f8b8663b | Christian Ehringfeld | QVERIFY2(this->e->startup("emTestA", inits), "Failure");
|
|
d8f2b16b | Christian Ehringfeld | auto migrations = this->e->findAll<CuteEntityManager::DatabaseMigration>();
|
|
QCOMPARE(migrations.size(), 1);
|
|||
QCOMPARE(migrations.at(0)->getVersion(), QString("emTestA"));
|
|||
}
|
|||
cca98131 | Christian Ehringfeld | void EmTest::testDatabaseMigrationTable() {
|
|
d8f2b16b | Christian Ehringfeld | auto tables = this->e->getSchema()->getTables();
|
|
QString tblName = "cuteentitymanager::databasemigration";
|
|||
bool containsMigration = tables.contains(tblName);
|
|||
QVERIFY(containsMigration);
|
|||
if(containsMigration) {
|
|||
auto schema = tables.value(tblName);
|
|||
auto columns = schema->getColumns();
|
|||
QCOMPARE(columns.size(), 3);
|
|||
this->containsColumn(columns, "id", QVariant::Int, tblName, true);
|
|||
this->containsColumn(columns, "version", QVariant::String);
|
|||
22b97d74 | Christian Ehringfeld | this->containsColumn(columns, "applyTime", QVariant::String);
|
|
d8f2b16b | Christian Ehringfeld | }
|
|
}
|
|||
cca98131 | Christian Ehringfeld | void EmTest::testTableCreation() {
|
|
b3c9a8bf | Christian Ehringfeld | auto tables = this->e->getSchema()->getTables();
|
|
bool containsArticle = tables.contains("article");
|
|||
QVERIFY(containsArticle);
|
|||
if(containsArticle) {
|
|||
auto schema = tables.value("article");
|
|||
auto columns = schema->getColumns();
|
|||
QCOMPARE(columns.size(), 3);
|
|||
this->containsColumn(columns, "id", QVariant::Int, "article", true);
|
|||
this->containsColumn(columns, "name", QVariant::String);
|
|||
this->containsColumn(columns, "price", QVariant::Double);
|
|||
}
|
|||
bool containsPerson = tables.contains("person");
|
|||
QVERIFY(containsPerson);
|
|||
if(containsPerson) {
|
|||
auto schema = tables.value("person");
|
|||
auto columns = schema->getColumns();
|
|||
QCOMPARE(columns.size(), 8);
|
|||
this->containsColumn(columns, "id", QVariant::Int, "person", true);
|
|||
this->containsColumn(columns, "namePrefix", QVariant::String);
|
|||
this->containsColumn(columns, "firstName", QVariant::String);
|
|||
this->containsColumn(columns, "familyName", QVariant::String);
|
|||
this->containsColumn(columns, "customPictureFileName", QVariant::String);
|
|||
22b97d74 | Christian Ehringfeld | this->containsColumn(columns, "birthday", QVariant::String);
|
|
b3c9a8bf | Christian Ehringfeld | this->containsColumn(columns, "nickName", QVariant::String);
|
|
this->containsColumn(columns, "gender", QVariant::Int);
|
|||
}
|
|||
bool containsGroup = tables.contains("group");
|
|||
QVERIFY(containsGroup);
|
|||
if(containsGroup) {
|
|||
auto schema = tables.value("group");
|
|||
auto columns = schema->getColumns();
|
|||
QCOMPARE(columns.size(), 3);
|
|||
this->containsColumn(columns, "id", QVariant::Int, "group", true);
|
|||
this->containsColumn(columns, "name", QVariant::String);
|
|||
22b97d74 | Christian Ehringfeld | this->containsColumn(columns, "leader_id", QVariant::Int);
|
|
b3c9a8bf | Christian Ehringfeld | }
|
|
d8f2b16b | Christian Ehringfeld | bool containsPersonGroups = tables.contains("person_groups");
|
|
QVERIFY(containsPersonGroups);
|
|||
if(containsArticle) {
|
|||
auto schema = tables.value("person_groups");
|
|||
auto columns = schema->getColumns();
|
|||
QCOMPARE(columns.size(), 3);
|
|||
this->containsColumn(columns, "id", QVariant::Int, "person_groups", true);
|
|||
22b97d74 | Christian Ehringfeld | this->containsColumn(columns, "person_id", QVariant::Int);
|
|
this->containsColumn(columns, "group_id", QVariant::Int);
|
|||
d8f2b16b | Christian Ehringfeld | }
|
|
b3c9a8bf | Christian Ehringfeld | QVERIFY(!tables.contains("group_persons"));
|
|
e7cfc35a | Christian Ehringfeld | }
|
|
4af3f83d | Christian Ehringfeld | ||
cca98131 | Christian Ehringfeld | void EmTest::containsColumn(QHash<QString, QSharedPointer<QSqlField>> &columns,
|
|
QString name,
|
|||
QVariant::Type type, QString tableName, bool pk) {
|
|||
b3c9a8bf | Christian Ehringfeld | bool containsColumn = columns.contains(name);
|
|
QVERIFY(containsColumn);
|
|||
if(containsColumn) {
|
|||
auto column = columns.value(name);
|
|||
22b97d74 | Christian Ehringfeld | qDebug() << name << column->type() << type;
|
|
b3c9a8bf | Christian Ehringfeld | if(type != QVariant::UserType) {
|
|
QCOMPARE(column->type(), type);
|
|||
}
|
|||
d8f2b16b | Christian Ehringfeld | if(!name.isEmpty()) {
|
|
QString pkIndex = this->e->getDb()->getDatabase().primaryIndex(tableName).fieldName(0);
|
|||
bool same = (pkIndex == name);
|
|||
QVERIFY((pk && same) || (!pk && !same));
|
|||
b3c9a8bf | Christian Ehringfeld | }
|
|
}
|
|||
}
|
|||
d8f2b16b | Christian Ehringfeld | ||
cca98131 | Christian Ehringfeld | void EmTest::cleanup() {
|
|
e7cfc35a | Christian Ehringfeld | auto qb = this->e->getQueryBuilder();
|
|
f8b8663b | Christian Ehringfeld | QVERIFY(this->e->executeQuery(qb->dropTable("person_groups")));
|
|
e7cfc35a | Christian Ehringfeld | QVERIFY(this->e->executeQuery(qb->dropTable("group")));
|
|
QVERIFY(this->e->executeQuery(qb->dropTable("person")));
|
|||
QVERIFY(this->e->executeQuery(qb->dropTable("article")));
|
|||
95a2ba7e | Christian Ehringfeld | // QVERIFY(this->e->executeQuery(qb->dropTable("workergroup_workers")));
|
|
// QVERIFY(this->e->executeQuery(qb->dropTable("workergroup")));
|
|||
// QVERIFY(this->e->executeQuery(qb->dropTable("employee")));
|
|||
e7cfc35a | Christian Ehringfeld | auto tableNames = this->e->getSchema()->getTableNames();
|
|
QVERIFY(!tableNames.contains("person"));
|
|||
QVERIFY(!tableNames.contains("group"));
|
|||
QVERIFY(!tableNames.contains("article"));
|
|||
f8b8663b | Christian Ehringfeld | QVERIFY(!tableNames.contains("person_groups"));
|
|
e7cfc35a | Christian Ehringfeld | QVERIFY(this->e->removeAll("cuteentitymanager::databasemigration"));
|
|
}
|
|||
cca98131 | Christian Ehringfeld | void EmTest::testRelationTableCreation() {
|
|
4062c152 | Christian Ehringfeld | this->createRelationTables();
|
|
20e1ffec | Christian Ehringfeld | auto tables = this->e->getSchema()->getTables();
|
|
QVERIFY(tables.contains("workergroup"));
|
|||
22b97d74 | Christian Ehringfeld | bool containsEmployee = tables.contains("employee");
|
|
QVERIFY(containsEmployee);
|
|||
if(containsEmployee) {
|
|||
auto schema = tables.value("employee");
|
|||
auto columns = schema->getColumns();
|
|||
QCOMPARE(columns.size(), 4);
|
|||
this->containsColumn(columns, "id", QVariant::Int, "employee", true);
|
|||
this->containsColumn(columns, "persNumber", QVariant::String);
|
|||
this->containsColumn(columns, "manager", QVariant::Int);
|
|||
this->containsColumn(columns, "department", QVariant::String);
|
|||
}
|
|||
20e1ffec | Christian Ehringfeld | QVERIFY(tables.contains("workergroup_workers"));
|
|
QVERIFY(!tables.contains("group_employee"));
|
|||
QVERIFY(!tables.contains("employee_workergroups"));
|
|||
QVERIFY(!tables.contains("employee_groups"));
|
|||
QVERIFY(tables.contains("cuteentitymanager::databasemigration"));
|
|||
f8b8663b | Christian Ehringfeld | auto migrations = this->e->findAll<CuteEntityManager::DatabaseMigration>();
|
|
QCOMPARE(migrations.size(), 2);
|
|||
QCOMPARE(migrations.at(1)->getVersion(), QString("emTestB"));
|
|||
4062c152 | Christian Ehringfeld | this->deleteRelationTables();
|
|
}
|
|||
cca98131 | Christian Ehringfeld | void EmTest::testInheritedRelations() {
|
|
d8f2b16b | Christian Ehringfeld | QSharedPointer<Employee> e1 = QSharedPointer<Employee>(new Employee(42, "Fenja", "S.",
|
|
Person::Gender::FEMALE, "fenja.jpeg", "", "Lotta", QDate(1990, 10, 10), "Psychology"));
|
|||
20e1ffec | Christian Ehringfeld | QSharedPointer<Employee> e2 = QSharedPointer<Employee>(new Employee(11, "Janine",
|
|
"Musterfrau",
|
|||
Person::Gender::FEMALE, "janine.jpeg", "", "", QDate(2000, 1, 1), "Health", true));
|
|||
QSharedPointer<WorkerGroup> wg = QSharedPointer<WorkerGroup>(new
|
|||
WorkerGroup("Taskforce P&H", 42));
|
|||
wg->addWorker(e1);
|
|||
wg->addWorker(e2);
|
|||
auto entityWorkerGroup = wg.objectCast<Entity>();
|
|||
b3c9a8bf | Christian Ehringfeld | try {
|
|
QVERIFY(this->e->create(entityWorkerGroup));
|
|||
QSharedPointer<Group> g = QSharedPointer<Group>(new Group("EmployeeGroup"));
|
|||
g->setPersons({e1, e2});
|
|||
auto entityGroup = g.objectCast<Entity>();
|
|||
QVERIFY(this->e->create(entityGroup));
|
|||
22b97d74 | Christian Ehringfeld | g->setName("Taskforce 0008");
|
|
QVERIFY(this->e->merge(entityGroup));
|
|||
QVERIFY(this->e->remove(entityGroup));
|
|||
b3c9a8bf | Christian Ehringfeld | } catch(QString e) {
|
|
QFAIL(e.toUtf8().constData());
|
|||
}
|
|||
20e1ffec | Christian Ehringfeld | }
|
|
cca98131 | Christian Ehringfeld | void EmTest::createRelationTables() {
|
|
4062c152 | Christian Ehringfeld | QStringList relationTables = QStringList() << "Employee" << "WorkerGroup";
|
|
QVERIFY2(this->e->startup("emTestB", relationTables), "Failure");
|
|||
}
|
|||
cca98131 | Christian Ehringfeld | void EmTest::deleteRelationTables() {
|
|
f8b8663b | Christian Ehringfeld | 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")));
|
|||
d8f2b16b | Christian Ehringfeld | auto tableNames = this->e->getSchema()->getTableNames();
|
|
QVERIFY(!tableNames.contains("workergroup_workers"));
|
|||
QVERIFY(!tableNames.contains("employee"));
|
|||
QVERIFY(!tableNames.contains("workergroup"));
|
|||
QHash<QString, QVariant> attributes;
|
|||
attributes["version"] = QString("emTestB");
|
|||
QSharedPointer<DatabaseMigration> dbm = e->findEntityByAttributes<DatabaseMigration>
|
|||
(attributes, true);
|
|||
auto ent = dbm.objectCast<Entity>();
|
|||
QVERIFY(this->e->remove(ent));
|
|||
auto migrations = this->e->findAll<CuteEntityManager::DatabaseMigration>();
|
|||
QCOMPARE(migrations.size(), 1);
|
|||
f8b8663b | Christian Ehringfeld | }
|
|
cca98131 | Christian Ehringfeld | void EmTest::testFindById() {
|
|
f8b8663b | Christian Ehringfeld | QSharedPointer<Person> p = QSharedPointer<Person>(new Person("Patrick", "De",
|
|
Person::Gender::MALE, "patrick.jpeg", "", "Pat", QDate(2000, 1, 1)));
|
|||
e7cfc35a | Christian Ehringfeld | auto ent = p.objectCast<Entity>();
|
|
QVERIFY(this->e->create(ent));
|
|||
auto id = p->getId();
|
|||
QVERIFY(id > -1);
|
|||
QVERIFY(this->e->findById(id, p->getClassname()));
|
|||
}
|
|||
cca98131 | Christian Ehringfeld | void EmTest::testFindId() {
|
|
f8b8663b | Christian Ehringfeld | QSharedPointer<Person> p = QSharedPointer<Person>(new Person("Essi", "Sa",
|
|
Person::Gender::MALE, "essi.jpeg", "", "Essi", QDate(2000, 1, 1)));
|
|||
e7cfc35a | Christian Ehringfeld | auto ent = p.objectCast<Entity>();
|
|
QVERIFY(this->e->create(ent));
|
|||
auto entity = QSharedPointer<Entity>(p->copy());
|
|||
auto foundId = this->e->findId(entity);
|
|||
QVERIFY(foundId > -1 && foundId == ent->getId());
|
|||
}
|
|||
cca98131 | Christian Ehringfeld | void EmTest::testHasChanged() {
|
|
f8b8663b | Christian Ehringfeld | QSharedPointer<Person> p = QSharedPointer<Person>(new Person("Jelena", "Fl",
|
|
e7cfc35a | Christian Ehringfeld | Person::Gender::MALE, "max.jpeg", "", "Maxi", QDate(2000, 1, 1)));
|
|
auto ent = p.objectCast<Entity>();
|
|||
QVERIFY(this->e->create(ent));
|
|||
f8b8663b | Christian Ehringfeld | p->setFirstName("Laura");
|
|
e7cfc35a | Christian Ehringfeld | p->setFamilyName("Musterfrau");
|
|
p->setBirthday(QDate(200, 1, 2));
|
|||
QVERIFY(this->e->hasChanged(ent));
|
|||
}
|
|||
cca98131 | Christian Ehringfeld | void EmTest::testValidate() {
|
|
f8b8663b | Christian Ehringfeld | QSharedPointer<Person> p = QSharedPointer<Person>(new Person("Patrick", "Pe",
|
|
Person::Gender::MALE, "patrick2.jpeg", "", "Maxi", QDate(2000, 1, 1)));
|
|||
e7cfc35a | Christian Ehringfeld | auto ent = p.objectCast<Entity>();
|
|
QVERIFY(this->e->validate(ent));
|
|||
p->setFirstName("M");
|
|||
QVERIFY(!this->e->validate(ent) && ent->getErrors().size() == 1);
|
|||
p->setBirthday(QDate(2030, 10, 10));
|
|||
QVERIFY(!this->e->validate(ent) && ent->getErrors().size() == 2);
|
|||
}
|
|||
cca98131 | Christian Ehringfeld | void EmTest::testRelations() {
|
|
f8b8663b | Christian Ehringfeld | QSharedPointer<Person> p1 = QSharedPointer<Person>(new Person("Lucien", "We",
|
|
Person::Gender::MALE, "lucien.jpeg", "", "Luc", QDate(2000, 1, 1)));
|
|||
QSharedPointer<Person> p2 = QSharedPointer<Person>(new Person("Janine", "Musterfrau",
|
|||
Person::Gender::FEMALE, "janine.jpeg", "", "", QDate(2000, 1, 1)));
|
|||
QSharedPointer<Person> p3 = QSharedPointer<Person>(new Person("Fenja", "Sey.",
|
|||
d8f2b16b | Christian Ehringfeld | Person::Gender::FEMALE, "fenja.jpeg", "", "Lotta", QDate(1990, 11, 11)));
|
|
20e1ffec | Christian Ehringfeld | QSharedPointer<Group> g = QSharedPointer<Group>(new Group("TestGroup"));
|
|
31ca3a70 | Christian Ehringfeld | QSharedPointer<Group> g2 = QSharedPointer<Group>(new Group("TestGroup2"));
|
|
e7cfc35a | Christian Ehringfeld | g->setLeader(p1);
|
|
31ca3a70 | Christian Ehringfeld | g2->setLeader(p2);
|
|
e7cfc35a | Christian Ehringfeld | g->setPersons({p1});
|
|
auto gEnt = g.objectCast<Entity>();
|
|||
auto pEnt = p3.objectCast<Entity>();
|
|||
31ca3a70 | Christian Ehringfeld | auto pEnt1 = p1.objectCast<Entity>();
|
|
e7cfc35a | Christian Ehringfeld | QVERIFY(this->e->save(gEnt));
|
|
31ca3a70 | Christian Ehringfeld | auto maintainedGroups = p1->getMaintainedGroups();
|
|
maintainedGroups.append(g2);
|
|||
p1->setMaintainedGroups(maintainedGroups);
|
|||
this->e->merge(pEnt1, true, false, true);
|
|||
e7cfc35a | Christian Ehringfeld | QVERIFY(p1->getId() > -1);
|
|
31ca3a70 | Christian Ehringfeld | QCOMPARE(p1->getMaintainedGroups().size(), 2);
|
|
e7cfc35a | Christian Ehringfeld | QVERIFY(p1->getGroups().size() > 0);
|
|
g->addPerson(p2);
|
|||
g->setName("TestGroupExtended");
|
|||
QVERIFY(this->e->save(gEnt));
|
|||
p3->setGroups({g});
|
|||
QVERIFY(this->e->save(pEnt, true, true));
|
|||
QVERIFY(g->getPersons().size() == 3);
|
|||
QList<QSharedPointer<Group>> groups;
|
|||
p3->setGroups(groups);
|
|||
QVERIFY(this->e->save(pEnt, true, true));
|
|||
this->e->refresh(gEnt);
|
|||
95a2ba7e | Christian Ehringfeld | qDebug() << g->getPersons().size();
|
|
e7cfc35a | Christian Ehringfeld | QVERIFY(g->getPersons().size() == 2);
|
|
auto firstPerson = g->getPersons().first();
|
|||
g->removePerson(firstPerson);
|
|||
QVERIFY(this->e->save(gEnt, true, true));
|
|||
auto entityFp = firstPerson.objectCast<Entity>();
|
|||
this->e->refresh(entityFp);
|
|||
QVERIFY(firstPerson->getGroups().size() == 0 && g->getPersons().size() == 1);
|
|||
}
|