123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- #include <QtTest>
- #include "EasyQtSql.h"
- using namespace EasyQtSql;
- class TestDDL : public QObject
- {
- Q_OBJECT
- public:
- TestDDL() {}
- ~TestDDL(){}
- private slots:
- void initTestCase();
- void cleanupTestCase();
- void test_case1();
- void test_case2();
- void test_case3();
- void test_case4();
- void test_case5();
- void test_case6();
- void test_case7();
- void test_case8();
- };
- void TestDDL::initTestCase()
- {
- QLatin1Literal driverName("QSQLITE");
- if (!QSqlDatabase::drivers().contains(driverName))
- QFAIL("This test requires the SQLITE database driver");
- QSqlDatabase sdb = QSqlDatabase::addDatabase(driverName);
- sdb.setDatabaseName(":memory:");
- if (!sdb.open())
- {
- QFAIL(sdb.lastError().text().toStdString().c_str());
- }
- }
- void TestDDL::cleanupTestCase()
- {
- {
- QSqlDatabase sdb = QSqlDatabase::database(QSqlDatabase::defaultConnection);
- if (sdb.isOpen())
- {
- sdb.close();
- }
- }
- QSqlDatabase::removeDatabase(QSqlDatabase::defaultConnection);
- }
- void TestDDL::test_case1()
- {
- QVERIFY_EXCEPTION_THROWN({
- Transaction t;
- PreparedQuery query = t.prepare("SELECT * FROM table1"); //there is no table1, so exception expected
- QueryResult res = query.exec();
- }, DBException);
- }
- void TestDDL::test_case2()
- {
- try
- {
- Transaction t;
- t.execNonQuery("CREATE TABLE table2 (a int, b int, c int, d text)"); //create table2
- PreparedQuery query = t.prepare("SELECT * FROM table2"); //...and select from (empty) table2
- QueryResult res = query.exec();
- //transaction is uncommited, so table2 will be removed after rollback
- }
- catch (const DBException &e)
- {
- QFAIL(e.lastError.text().toStdString().c_str());
- }
- }
- void TestDDL::test_case3()
- {
- try
- {
- Transaction t;
- t.execNonQuery("CREATE TABLE table2 (a int, b int, c int, d text)");
- //transaction is uncommited
- }
- catch (const DBException &e)
- {
- QFAIL(e.lastError.text().toStdString().c_str());
- }
- }
- void TestDDL::test_case4()
- {
- try
- {
- Transaction t;
- t.execNonQuery("CREATE TABLE table2 (a int, b int, c int, d text)"); //create table2
- PreparedQuery query = t.prepare("SELECT * FROM table2");
- QueryResult res = query.exec();
- t.commit(); // commit transaction (table2 created and commited)
- }
- catch (const DBException &e)
- {
- QFAIL(e.lastError.text().toStdString().c_str());
- }
- }
- void TestDDL::test_case5()
- {
- QVERIFY_EXCEPTION_THROWN({
- Transaction t;
- t.execNonQuery("CREATE TABLE table2 (a int, b int, c int, d text)"); //cant create existed table2
- }, DBException);
- }
- void TestDDL::test_case6()
- {
- QVERIFY_EXCEPTION_THROWN({
- Transaction t;
- t.execNonQuery("DROP TABLE table4"); //can't drop table4
- }, DBException);
- }
- void TestDDL::test_case7()
- {
- try
- {
- Transaction t;
- t.execNonQuery("DROP TABLE table2"); //can drop table2
- t.commit(); // commit transaction
- }
- catch (const DBException &e)
- {
- QFAIL(e.lastError.text().toStdString().c_str());
- }
- }
- void TestDDL::test_case8()
- {
- QVERIFY_EXCEPTION_THROWN({
- Transaction t;
- t.execNonQuery("DROP TABLE table2"); //there is no table2
- }, DBException);
- }
- QTEST_APPLESS_MAIN(TestDDL)
- #include "tst_testddl.moc"
|