Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision f8b8663b

Von Christian Ehringfeld vor etwa 9 Jahren hinzugefügt

  • ID f8b8663b25c9040c33d41ba1b0cd2c33a9a25722
  • Vorgänger b9d50b0d
  • Nachfolger 4062c152

fixxed possible crash in detection of nonInheritedRelations
test case update

Unterschiede anzeigen:

src/entityhelper.cpp
auto superObject = EntityInstanceFactory::newSuperClassInstance(entity);
if (superObject) {
auto superRelations = superObject->getRelations();
auto iterator = superRelations.constBegin();
while (iterator != relations.constEnd()) {
if (relations.contains(iterator.key())) {
relations.remove(iterator.key());
for (auto i = superRelations.constBegin(); i != superRelations.constEnd(); ++i) {
if (relations.contains(i.key())) {
relations.remove(i.key());
}
++iterator;
}
delete superObject;
superObject = nullptr;
......
&entity, QSharedPointer<Entity> add, const QMetaProperty &property) {
QVariant var = property.read(entity.data());
QList<QSharedPointer<Entity>> list = (!var.isNull() && var.data() &&
var.canConvert<QList<QVariant>>() ? EntityInstanceFactory::castQVariantList( var) : QList<QSharedPointer<Entity>>());
var.canConvert<QList<QVariant>>() ? EntityInstanceFactory::castQVariantList(
var) : QList<QSharedPointer<Entity>>());
if (!list.contains(add)) {
list.append(add);
EntityHelper::setListProperty(entity, list, property);
tests/em/em.pro
SOURCES += tst_em.cpp \
tst_querybuildertest.cpp \
../models.cpp
DEFINES += SRCDIR=\\\"$$PWD/\\\"
tests/em/tst_em.cpp
void testHasChanged();
void testValidate();
void testRelations();
void testRelationTableCreation();
private:
CuteEntityManager::EntityManager *e;
......
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",
":memory:", "", "", "", "", true, "foreign_keys = ON");
}
......
void Em::init() {
QStringList inits = QStringList() << "Person" << "Group" << "Article";
QVERIFY2(this->e->startup("test0.1", inits), "Failure");
QVERIFY2(this->e->startup("emTestA", inits), "Failure");
auto tableNames = this->e->getSchema()->getTableNames();
QVERIFY(tableNames.contains("article"));
QVERIFY(tableNames.contains("person"));
QVERIFY(tableNames.contains("group"));
QVERIFY(tableNames.contains("group_persons"));
QVERIFY(tableNames.contains("person_groups"));
QVERIFY(!tableNames.contains("group_persons"));
QVERIFY(tableNames.contains("cuteentitymanager::databasemigration"));
auto migrations = this->e->findAll<CuteEntityManager::DatabaseMigration>();
QCOMPARE(migrations.size(), 1);
QCOMPARE(migrations.at(0)->getVersion(), QString("test0.1"));
QCOMPARE(migrations.at(0)->getVersion(), QString("emTestA"));
}
void Em::cleanup() {
auto qb = this->e->getQueryBuilder();
QVERIFY(this->e->executeQuery(qb->dropTable("group_persons")));
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->executeQuery(qb->dropTable("article")));
......
QVERIFY(!tableNames.contains("person"));
QVERIFY(!tableNames.contains("group"));
QVERIFY(!tableNames.contains("article"));
QVERIFY(!tableNames.contains("group_persons"));
QVERIFY(!tableNames.contains("person_groups"));
QVERIFY(this->e->removeAll("cuteentitymanager::databasemigration"));
}
void Em::testRelationTableCreation() {
QStringList relationTables = QStringList() << "Employee" << "WorkerGroup";
QVERIFY2(this->e->startup("emTestB", relationTables), "Failure");
auto tableNames = this->e->getSchema()->getTableNames();
QVERIFY(tableNames.contains("workergroup"));
QVERIFY(tableNames.contains("employee"));
QVERIFY(tableNames.contains("workergroup_workers"));
QVERIFY(!tableNames.contains("group_employee"));
QVERIFY(!tableNames.contains("employee_workergroups"));
QVERIFY(!tableNames.contains("employee_groups"));
QVERIFY(tableNames.contains("cuteentitymanager::databasemigration"));
auto migrations = this->e->findAll<CuteEntityManager::DatabaseMigration>();
QCOMPARE(migrations.size(), 2);
QCOMPARE(migrations.at(1)->getVersion(), QString("emTestB"));
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")));
}
void Em::testFindById() {
QSharedPointer<Person> p = QSharedPointer<Person>(new Person("Max", "Mustermann",
Person::Gender::MALE, "max.jpeg", "", "Maxi", QDate(2000, 1, 1)));
QSharedPointer<Person> p = QSharedPointer<Person>(new Person("Patrick", "De",
Person::Gender::MALE, "patrick.jpeg", "", "Pat", QDate(2000, 1, 1)));
auto ent = p.objectCast<Entity>();
QVERIFY(this->e->create(ent));
auto id = p->getId();
......
}
void Em::testFindId() {
QSharedPointer<Person> p = QSharedPointer<Person>(new Person("Max", "Mustermann",
Person::Gender::MALE, "max.jpeg", "", "Maxi", QDate(2000, 1, 1)));
QSharedPointer<Person> p = QSharedPointer<Person>(new Person("Essi", "Sa",
Person::Gender::MALE, "essi.jpeg", "", "Essi", QDate(2000, 1, 1)));
auto ent = p.objectCast<Entity>();
QVERIFY(this->e->create(ent));
auto entity = QSharedPointer<Entity>(p->copy());
......
}
void Em::testHasChanged() {
QSharedPointer<Person> p = QSharedPointer<Person>(new Person("Max", "Mustermann",
QSharedPointer<Person> p = QSharedPointer<Person>(new Person("Jelena", "Fl",
Person::Gender::MALE, "max.jpeg", "", "Maxi", QDate(2000, 1, 1)));
auto ent = p.objectCast<Entity>();
QVERIFY(this->e->create(ent));
p->setFirstName("Charlotte");
p->setFirstName("Laura");
p->setFamilyName("Musterfrau");
p->setBirthday(QDate(200, 1, 2));
QVERIFY(this->e->hasChanged(ent));
}
void Em::testValidate() {
QSharedPointer<Person> p = QSharedPointer<Person>(new Person("Max", "Mustermann",
Person::Gender::MALE, "max.jpeg", "", "Maxi", QDate(2000, 1, 1)));
QSharedPointer<Person> p = QSharedPointer<Person>(new Person("Patrick", "Pe",
Person::Gender::MALE, "patrick2.jpeg", "", "Maxi", QDate(2000, 1, 1)));
auto ent = p.objectCast<Entity>();
QVERIFY(this->e->validate(ent));
p->setFirstName("M");
......
}
void Em::testRelations() {
QSharedPointer<Person> p1 = QSharedPointer<Person>(new Person("Max", "Mustermann",
Person::Gender::MALE, "max.jpeg", "", "Maxi", QDate(2000, 1, 1)));
QSharedPointer<Person> p2 = QSharedPointer<Person>(new Person("Marie", "Musterfrau",
Person::Gender::FEMALE, "max.jpeg", "", "", QDate(2000, 1, 1)));
QSharedPointer<Person> p3 = QSharedPointer<Person>(new Person("Fenja", "Musterfrau",
Person::Gender::FEMALE, "max.jpeg", "", "Fenni", QDate(2000, 1, 1)));
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.",
Person::Gender::FEMALE, "fenja.jpeg", "", "Lotta", QDate(1990, 1, 1)));
QSharedPointer<Group> g = QSharedPointer<Group>(new Group());
g->setName("TestGroup");
g->setLeader(p1);
tests/em/tst_querybuildertest.cpp
QVERIFY2(true, "Failure");
}
QTEST_APPLESS_MAIN(QuerybuilderTest)
//QTEST_APPLESS_MAIN(QuerybuilderTest)
#include "tst_querybuildertest.moc"
tests/models.cpp
const QHash<QString, CuteEntityManager::Relation> Person::getRelations() const {
auto hash = QHash<QString, CuteEntityManager::Relation>();
hash.insert("groups", CuteEntityManager::Relation("groups",
RelationType::MANY_TO_MANY,
QString("persons")));
RelationType::MANY_TO_MANY));
hash.insert("maintainedGroups", CuteEntityManager::Relation("maintainedGroups",
RelationType::ONE_TO_MANY,
QString("leader")));
......
const QHash<QString, CuteEntityManager::Relation> Group::getRelations() const {
auto hash = QHash<QString, CuteEntityManager::Relation>();
hash.insert("persons", CuteEntityManager::Relation("persons",
RelationType::MANY_TO_MANY));
RelationType::MANY_TO_MANY, QString("groups")));
hash.insert("leader", CuteEntityManager::Relation("leader",
RelationType::MANY_TO_ONE));
return hash;
......
rules.append(ValidationRule("date", "birthday", params));
return rules;
}
const QHash<QString, CuteEntityManager::Relation> Employee::getRelations() const {
auto relations = Person::getRelations();
return relations;
}
QString Employee::getDepartment() const {
return department;
}
void Employee::setDepartment(const QString &value) {
department = value;
}
bool Employee::isManager() const {
return manager;
}
void Employee::setManager(bool value) {
manager = value;
}
quint64 Employee::getPersNumber() const {
return persNumber;
}
void Employee::setPersNumber(const quint64 &value) {
persNumber = value;
}
const QHash<QString, CuteEntityManager::Relation> WorkerGroup::getRelations() const {
auto hash = QHash<QString, CuteEntityManager::Relation>();
hash.insert("workers", CuteEntityManager::Relation("workers",
RelationType::MANY_TO_MANY));
return hash;
}
QString WorkerGroup::getName() const {
return name;
}
void WorkerGroup::setName(const QString &value) {
name = value;
}
quint32 WorkerGroup::getEfficiency() const {
return efficiency;
}
void WorkerGroup::setEfficiency(const quint32 &value) {
efficiency = value;
}
bool WorkerGroup::isActive() const {
return active;
}
void WorkerGroup::setActive(bool value) {
active = value;
}
QList<QSharedPointer<Employee>> WorkerGroup::getWorkers() const {
return workers;
}
void WorkerGroup::setWorkers(const QList<QSharedPointer<Employee>> &value) {
workers = value;
}
tests/models.h
using namespace CuteEntityManager;
class Group;
class Relation;
class Person: public Entity {
Q_OBJECT
......
QList <QSharedPointer<Group>> maintainedGroups;
};
class Relation;
class Employee : public Person {
Q_OBJECT
Q_PROPERTY(QString department READ getDepartment WRITE setDepartment)
Q_PROPERTY(quint64 persNumber READ getPersNumber WRITE setPersNumber)
Q_PROPERTY(bool manager READ isManager WRITE setManager)
public:
Employee() : Person() { }
virtual const QHash<QString, CuteEntityManager::Relation> getRelations() const
override;
QString getDepartment() const;
void setDepartment(const QString &value);
bool isManager() const;
void setManager(bool value);
quint64 getPersNumber() const;
void setPersNumber(const quint64 &value);
private:
QString department;
quint64 persNumber;
bool manager = false;
};
class WorkerGroup : public Entity {
Q_OBJECT
Q_PROPERTY(QString name READ getName WRITE setName)
Q_PROPERTY(quint32 efficiency READ getEfficiency WRITE setEfficiency)
Q_PROPERTY(bool active READ isActive WRITE setActive)
Q_PROPERTY(QList<QSharedPointer<Employee>> workers READ getWorkers WRITE
setWorkers)
public:
WorkerGroup() : Entity() { }
virtual const QHash<QString, CuteEntityManager::Relation> getRelations() const
override;
QString getName() const;
void setName(const QString &value);
quint32 getEfficiency() const;
void setEfficiency(const quint32 &value);
bool isActive() const;
void setActive(bool value);
QList<QSharedPointer<Employee>> getWorkers() const;
void setWorkers(const QList<QSharedPointer<Employee>> &value);
private:
QString name;
quint32 efficiency;
bool active = true;
QList<QSharedPointer<Employee>> workers;
};
class Group: public CuteEntityManager::Entity {
Q_OBJECT
Q_PROPERTY(QList<QSharedPointer<Person>> persons READ getPersons WRITE
......
QString getName() const;
void setName(const QString &value);
QSharedPointer<Person> getLeader() const;
void setLeader(const QSharedPointer<Person> &value);
QList<QSharedPointer<Person>> getPersons() const;
void addPerson(const QSharedPointer<Person> &value);
void setPersons(const QList<QSharedPointer<Person>> &value);
tests/tests.pro
TEMPLATE = subdirs
SUBDIRS += \
em \
querybuilder \
validators

Auch abrufbar als: Unified diff