/** Test eager loading of foreign keys. */ void tst_QDjangoModel::selectRelated() { // load fixtures { Item *item1 = new Item; item1->setName("first"); QCOMPARE(item1->save(), true); Item *item2 = new Item; item2->setName("second"); QCOMPARE(item2->save(), true); Owner owner; owner.setName("owner"); owner.setItem1(item1); owner.setItem2(item2); QCOMPARE(owner.save(), true); } // without eager loading QDjangoQuerySet<Owner> qs; Owner *owner = qs.get(QDjangoWhere("name", QDjangoWhere::Equals, "owner")); QVERIFY(owner != 0); QCOMPARE(owner->item1()->name(), QLatin1String("first")); QCOMPARE(owner->item2()->name(), QLatin1String("second")); delete owner; // with eager loading owner = qs.selectRelated().get(QDjangoWhere("name", QDjangoWhere::Equals, "owner")); QVERIFY(owner != 0); QCOMPARE(owner->item1()->name(), QLatin1String("first")); QCOMPARE(owner->item2()->name(), QLatin1String("second")); delete owner; }
void TestModels::test_ModelAPIs_schedule() { const QDjangoQuerySet<Schedule> schedule_set; Schedule* schedule = schedule_set.get(QDjangoWhere("id", QDjangoWhere::Equals, 2)); Schedule* schedule2 = ModelAPIs::Instance()->schedule(2); QCOMPARE(schedule->start(), schedule2->start()); }
void TestModels::test_ModelAPIs_unit() { // // get an existing user // other = users.get(QDjangoWhere("username", QDjangoWhere::Equals, "foouser")); // QVERIFY(other != 0); // QCOMPARE(other->username(), QLatin1String("foouser")); // QCOMPARE(other->password(), QLatin1String("foopass")); const QDjangoQuerySet<Unit> units_set; Unit* unit = units_set.get(QDjangoWhere("name", QDjangoWhere::Equals, "second")); //qDebug() << unit->name(); Unit* unit2; unit2 = ModelAPIs::unit("second"); QCOMPARE(unit->name(), unit2->name()); // There is no element. Unit* unit3; unit3 = ModelAPIs::unit("no_exist"); QVERIFY(unit3 == 0); }
bool XmppServerPrivate::handleStanza(const QDomElement &element) { const QString to = element.attribute("to"); if (QXmppUtils::jidToDomain(to) != server()->domain()) return false; if (element.tagName() == "iq" && PrivateStorageIq::isPrivateStorageIq(element)) { PrivateStorageIq request; request.parse(element); if (request.type() == QXmppIq::Result) return true; // check the namespace is valid const QString xmlns = request.payload().attribute("xmlns"); if (xmlns.isEmpty()) { QXmppIq response; response.setId(request.id()); response.setTo(request.from()); response.setType(QXmppIq::Error); server()->sendPacket(response); return true; } const QString bareFrom = QXmppUtils::jidToBareJid(request.from()); QDjangoQuerySet<PrivateStorage> qs; qs = qs.filter(QDjangoWhere("jid", QDjangoWhere::Equals, bareFrom)); if (request.type() == QXmppIq::Get) { PrivateStorageIq response; response.setId(request.id()); response.setTo(request.from()); response.setType(QXmppIq::Result); PrivateStorage storage; if (qs.get(QDjangoWhere("xmlns", QDjangoWhere::Equals, xmlns), &storage)) { QDomDocument doc; doc.setContent(storage.data()); response.setPayload(doc.documentElement()); } else { response.setPayload(request.payload()); } server()->sendPacket(response); } else if (request.type() == QXmppIq::Set) { PrivateStorage storage; if (!qs.get(QDjangoWhere("xmlns", QDjangoWhere::Equals, xmlns), &storage)) { storage.setJid(bareFrom); storage.setXmlns(xmlns); } storage.setXml(request.payload()); storage.save(); // reply QXmppIq response; response.setId(request.id()); response.setTo(request.from()); response.setType(QXmppIq::Result); server()->sendPacket(response); } return true; } return false; }
/** * Dummy database for testing. If you want to edit a data, you also edit * test_models.cpp TestModels::test_common. * */ void dummy_db() { // initially created in initdb() // Unit count; // count.setName("count"); // QCOMPARE(count.save(), true); Unit second; second.setName("second"); QCOMPARE(second.save(), true); const QDjangoQuerySet<Unit> units; Unit* f_second = units.get(QDjangoWhere("name", QDjangoWhere::Equals, "second")); Element pushup; pushup.setName("pushup"); pushup.setPurpose(1000); pushup.setNote("It is pushup note."); pushup.setShortcut("push"); pushup.setUnit(f_second); QCOMPARE(pushup.save(), true); Element* running = new Element; running->setName("running"); running->setPurpose(300); running->setNote("It is running note."); QCOMPARE(running->save(), true); Element breathing; breathing.setName("breathing"); breathing.setPurpose(70000); QCOMPARE(breathing.save(), true); Group daily_group; daily_group.setName("daily"); daily_group.setNote("It is daily."); QCOMPARE(daily_group.save(), true); Group monthly_group; monthly_group.setName("monthly"); monthly_group.setNote("It is monthly"); QCOMPARE(monthly_group.save(), true); const QDjangoQuerySet<Element> elements; Element* f_pushup = elements.get(QDjangoWhere("name", QDjangoWhere::Equals, "pushup")); Element* f_running = elements.get(QDjangoWhere("name", QDjangoWhere::Equals, "running")); const QDjangoQuerySet<Group> groups; Group* f_daily_group = groups.get(QDjangoWhere("name", QDjangoWhere::Equals, "daily")); // It requires free store. It requires more code. ElementGroup daily; daily.setElement(f_pushup); daily.setGroup(f_daily_group); QCOMPARE(daily.save(), true); QDateTime current = QDateTime::currentDateTime(); int year = current.date().year(); int month = current.date().month(); int day = current.date().day(); QDate ddate = QDate(year, month, current.date().addDays(1).day()); QTime dtime = QTime(10, 5, 59); QDateTime start = QDateTime(ddate, dtime); QDateTime end = QDateTime(QDate(year, month, current.date().addDays(3).day()), QTime(15, 6, 5)); Schedule today; // start: today + 1, end: today + 3 today.setStart(start); today.setEnd(end); today.setElement(f_pushup); QCOMPARE(today.save(), true); QDate ddate2 = QDate(year, month, day); QDateTime start2 = QDateTime(ddate2); Schedule today2; // start: today0 today2.setStart(start2); today2.setEnd(start2); today2.setGroup(f_daily_group); QCOMPARE(today2.save(), true); Schedule today3; // start: today0, end: today0 + 1 today3.setStart(start2); today3.setEnd(start2.addDays(1)); today3.setElement(f_pushup); QCOMPARE(today3.save(), true); Schedule today4; // start: today, end: today + 3 today4.setStart(current); today4.setEnd(current.addDays(3)); today4.setElement(f_pushup); QCOMPARE(today4.save(), true); Schedule today5; // start: today0 + 1, end: today0 + 2 today5.setStart(start2.addDays(1)); today5.setEnd(current.addDays(2)); today5.setElement(f_pushup); QCOMPARE(today5.save(), true); Schedule yesterday; // start: today0 - 1, end: today0 + 3 yesterday.setStart(start2.addDays(-1)); yesterday.setEnd(start2.addDays(3)); yesterday.setElement(f_pushup); QCOMPARE(yesterday.save(), true); Schedule yesterday2; yesterday2.setStart(start2.addDays(-1)); yesterday2.setEnd(start2.addDays(4)); yesterday2.setElement(running); QCOMPARE(yesterday2.save(), true); QDateTime rdate = QDateTime(QDate(year, month, day), QTime(18, 0, 0)); Record record_of_today; record_of_today.setDatetime(rdate); record_of_today.setRecord(800); record_of_today.setElement(f_pushup); QCOMPARE(record_of_today.save(), true); }