/** Test case-insensitive "=" comparison. */ void tst_QDjangoWhere::iEqualsWhere() { QDjangoDatabase::DatabaseType databaseType = QDjangoDatabase::databaseType(QDjango::database()); if (databaseType == QDjangoDatabase::PostgreSQL) { QDjangoWhere testQuery = QDjangoWhere("name", QDjangoWhere::IEquals, "abc"); CHECKWHERE(testQuery, QLatin1String("UPPER(name::text) LIKE UPPER(?)"), QVariantList() << "abc"); testQuery = !QDjangoWhere("name", QDjangoWhere::IEquals, "abc"); CHECKWHERE(testQuery, QLatin1String("UPPER(name::text) NOT LIKE UPPER(?)"), QVariantList() << "abc"); } else { QDjangoWhere testQuery = QDjangoWhere("name", QDjangoWhere::IEquals, "abc"); CHECKWHERE(testQuery, QLatin1String("name LIKE ?"), QVariantList() << "abc"); testQuery = !QDjangoWhere("name", QDjangoWhere::IEquals, "abc"); CHECKWHERE(testQuery, QLatin1String("name NOT LIKE ?"), QVariantList() << "abc"); } }
/** Test "contains" comparison. */ void tst_QDjangoWhere::contains() { QDjangoDatabase::DatabaseType databaseType = QDjangoDatabase::databaseType(QDjango::database()); if (databaseType == QDjangoDatabase::MySqlServer) { QDjangoWhere testQuery = QDjangoWhere("name", QDjangoWhere::Contains, "abc"); CHECKWHERE(testQuery, QLatin1String("name LIKE BINARY ?"), QVariantList() << "%abc%"); testQuery = !QDjangoWhere("name", QDjangoWhere::Contains, "abc"); CHECKWHERE(testQuery, QLatin1String("name NOT LIKE BINARY ?"), QVariantList() << "%abc%"); } else { QDjangoWhere testQuery = QDjangoWhere("name", QDjangoWhere::Contains, "abc"); CHECKWHERE(testQuery, QLatin1String("name LIKE ?"), QVariantList() << "%abc%"); testQuery = !QDjangoWhere("name", QDjangoWhere::Contains, "abc"); CHECKWHERE(testQuery, QLatin1String("name NOT LIKE ?"), QVariantList() << "%abc%"); } }
/** Test compound where clause, using both the AND and the OR operators. */ void tst_QDjangoWhere::complexWhere() { QDjangoWhere testQuery; const QDjangoWhere queryId("id", QDjangoWhere::Equals, 1); const QDjangoWhere queryUsername("username", QDjangoWhere::Equals, "foouser"); const QDjangoWhere queryPassword("password", QDjangoWhere::Equals, "foopass"); testQuery = (queryId || queryUsername) && queryPassword; CHECKWHERE(testQuery, QLatin1String("(id = ? OR username = ?) AND password = ?"), QVariantList() << 1 << "foouser" << "foopass"); testQuery = queryId || (queryUsername && queryPassword); CHECKWHERE(testQuery, QLatin1String("id = ? OR (username = ? AND password = ?)"), QVariantList() << 1 << "foouser" << "foopass"); testQuery = queryId && queryUsername && queryPassword; CHECKWHERE(testQuery, QLatin1String("id = ? AND username = ? AND password = ?"), QVariantList() << 1 << "foouser" << "foopass"); testQuery = queryId || queryUsername || queryPassword; CHECKWHERE(testQuery, QLatin1String("id = ? OR username = ? OR password = ?"), QVariantList() << 1 << "foouser" << "foopass"); }
/** Test compound where clause, using the OR operator. */ void tst_QDjangoWhere::orWhere() { QDjangoWhere testQuery; const QDjangoWhere queryId("id", QDjangoWhere::Equals, 1); const QDjangoWhere queryUsername("username", QDjangoWhere::Equals, "foo"); testQuery = queryId || queryUsername; CHECKWHERE(testQuery, QLatin1String("id = ? OR username = ?"), QVariantList() << 1 << "foo"); // or with "all" queryset testQuery = QDjangoWhere() || queryId; QCOMPARE(testQuery.isAll(), true); CHECKWHERE(testQuery, QString(), QVariantList()); testQuery = queryId || QDjangoWhere(); QCOMPARE(testQuery.isAll(), true); CHECKWHERE(testQuery, QString(), QVariantList()); // or with "none" queryset testQuery = !QDjangoWhere() || queryId; CHECKWHERE(testQuery, QLatin1String("id = ?"), QVariantList() << 1); testQuery = queryId || !QDjangoWhere(); CHECKWHERE(testQuery, QLatin1String("id = ?"), QVariantList() << 1); // negation testQuery = !(queryId || queryUsername); CHECKWHERE(testQuery, QLatin1String("NOT (id = ? OR username = ?)"), QVariantList() << 1 << "foo"); }
/** Test compound where clause, using the AND operator. */ void tst_QDjangoWhere::andWhere() { QDjangoWhere testQuery; const QDjangoWhere queryId("id", QDjangoWhere::Equals, 1); const QDjangoWhere queryUsername("username", QDjangoWhere::Equals, "foo"); testQuery = queryId && queryUsername; CHECKWHERE(testQuery, QLatin1String("id = ? AND username = ?"), QVariantList() << 1 << "foo"); // and with "all" queryset testQuery = QDjangoWhere() && queryId; CHECKWHERE(testQuery, QLatin1String("id = ?"), QVariantList() << 1); testQuery = queryId && QDjangoWhere(); CHECKWHERE(testQuery, QLatin1String("id = ?"), QVariantList() << 1); // and with "none" queryset testQuery = !QDjangoWhere() && queryId; QCOMPARE(testQuery.isNone(), true); CHECKWHERE(testQuery, QLatin1String("1 != 0"), QVariantList()); testQuery = queryId && !QDjangoWhere(); QCOMPARE(testQuery.isNone(), true); CHECKWHERE(testQuery, QLatin1String("1 != 0"), QVariantList()); // negation testQuery = !(queryId && queryUsername); CHECKWHERE(testQuery, QLatin1String("NOT (id = ? AND username = ?)"), QVariantList() << 1 << "foo"); }
void tst_QDjangoCompiler::resolve() { QSqlDatabase db = QDjango::database(); QDjangoCompiler compiler("Owner", db); QDjangoWhere where("name", QDjangoWhere::Equals, "foo"); compiler.resolve(where); CHECKWHERE(where, QLatin1String("\"owner\".\"name\" = ?"), QVariantList() << "foo"); QCOMPARE(compiler.fromSql(), QLatin1String("\"owner\"")); compiler = QDjangoCompiler("Owner", db); where = QDjangoWhere("item1__name", QDjangoWhere::Equals, "foo"); compiler.resolve(where); CHECKWHERE(where, QLatin1String("T0.\"name\" = ?"), QVariantList() << "foo"); QCOMPARE(compiler.fromSql(), QLatin1String("\"owner\" INNER JOIN \"item\" T0 ON T0.\"id\" = \"owner\".\"item1_id\"")); compiler = QDjangoCompiler("Owner", db); where = QDjangoWhere("item1__name", QDjangoWhere::Equals, "foo") && QDjangoWhere("item2__name", QDjangoWhere::Equals, "bar"); compiler.resolve(where); CHECKWHERE(where, QLatin1String("T0.\"name\" = ? AND T1.\"name\" = ?"), QVariantList() << "foo" << "bar"); QCOMPARE(compiler.fromSql(), QLatin1String("\"owner\" INNER JOIN \"item\" T0 ON T0.\"id\" = \"owner\".\"item1_id\" INNER JOIN \"item\" T1 ON T1.\"id\" = \"owner\".\"item2_id\"")); }
/** Test "contains" comparison. */ void tst_QDjangoWhere::contains() { QDjangoWhere testQuery = QDjangoWhere("name", QDjangoWhere::Contains, "abc"); CHECKWHERE(testQuery, QLatin1String("name LIKE ? ESCAPE ?"), QVariantList() << "%abc%" << "\\"); }
/** Test "endswith" comparison. */ void tst_QDjangoWhere::endsWith() { QDjangoWhere testQuery = QDjangoWhere("name", QDjangoWhere::EndsWith, "abc"); CHECKWHERE(testQuery, QLatin1String("name LIKE ? ESCAPE ?"), QVariantList() << "%abc" << "\\"); }
/** Test "in" comparison. */ void tst_QDjangoWhere::isIn() { QDjangoWhere testQuery = QDjangoWhere("id", QDjangoWhere::IsIn, QVariantList() << 1 << 2); CHECKWHERE(testQuery, QLatin1String("id IN (?, ?)"), QVariantList() << 1 << 2); }