tst_testddl.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #include <QtTest>
  2. #include "EasyQtSql.h"
  3. using namespace EasyQtSql;
  4. class TestDDL : public QObject
  5. {
  6. Q_OBJECT
  7. public:
  8. TestDDL() {}
  9. ~TestDDL(){}
  10. private slots:
  11. void initTestCase();
  12. void cleanupTestCase();
  13. void test_case1();
  14. void test_case2();
  15. void test_case3();
  16. void test_case4();
  17. void test_case5();
  18. void test_case6();
  19. void test_case7();
  20. void test_case8();
  21. };
  22. void TestDDL::initTestCase()
  23. {
  24. QLatin1Literal driverName("QSQLITE");
  25. if (!QSqlDatabase::drivers().contains(driverName))
  26. QFAIL("This test requires the SQLITE database driver");
  27. QSqlDatabase sdb = QSqlDatabase::addDatabase(driverName);
  28. sdb.setDatabaseName(":memory:");
  29. if (!sdb.open())
  30. {
  31. QFAIL(sdb.lastError().text().toStdString().c_str());
  32. }
  33. }
  34. void TestDDL::cleanupTestCase()
  35. {
  36. {
  37. QSqlDatabase sdb = QSqlDatabase::database(QSqlDatabase::defaultConnection);
  38. if (sdb.isOpen())
  39. {
  40. sdb.close();
  41. }
  42. }
  43. QSqlDatabase::removeDatabase(QSqlDatabase::defaultConnection);
  44. }
  45. void TestDDL::test_case1()
  46. {
  47. QVERIFY_EXCEPTION_THROWN({
  48. Transaction t;
  49. PreparedQuery query = t.prepare("SELECT * FROM table1"); //there is no table1, so exception expected
  50. QueryResult res = query.exec();
  51. }, DBException);
  52. }
  53. void TestDDL::test_case2()
  54. {
  55. try
  56. {
  57. Transaction t;
  58. t.execNonQuery("CREATE TABLE table2 (a int, b int, c int, d text)"); //create table2
  59. PreparedQuery query = t.prepare("SELECT * FROM table2"); //...and select from (empty) table2
  60. QueryResult res = query.exec();
  61. //transaction is uncommited, so table2 will be removed after rollback
  62. }
  63. catch (const DBException &e)
  64. {
  65. QFAIL(e.lastError.text().toStdString().c_str());
  66. }
  67. }
  68. void TestDDL::test_case3()
  69. {
  70. try
  71. {
  72. Transaction t;
  73. t.execNonQuery("CREATE TABLE table2 (a int, b int, c int, d text)");
  74. //transaction is uncommited
  75. }
  76. catch (const DBException &e)
  77. {
  78. QFAIL(e.lastError.text().toStdString().c_str());
  79. }
  80. }
  81. void TestDDL::test_case4()
  82. {
  83. try
  84. {
  85. Transaction t;
  86. t.execNonQuery("CREATE TABLE table2 (a int, b int, c int, d text)"); //create table2
  87. PreparedQuery query = t.prepare("SELECT * FROM table2");
  88. QueryResult res = query.exec();
  89. t.commit(); // commit transaction (table2 created and commited)
  90. }
  91. catch (const DBException &e)
  92. {
  93. QFAIL(e.lastError.text().toStdString().c_str());
  94. }
  95. }
  96. void TestDDL::test_case5()
  97. {
  98. QVERIFY_EXCEPTION_THROWN({
  99. Transaction t;
  100. t.execNonQuery("CREATE TABLE table2 (a int, b int, c int, d text)"); //cant create existed table2
  101. }, DBException);
  102. }
  103. void TestDDL::test_case6()
  104. {
  105. QVERIFY_EXCEPTION_THROWN({
  106. Transaction t;
  107. t.execNonQuery("DROP TABLE table4"); //can't drop table4
  108. }, DBException);
  109. }
  110. void TestDDL::test_case7()
  111. {
  112. try
  113. {
  114. Transaction t;
  115. t.execNonQuery("DROP TABLE table2"); //can drop table2
  116. t.commit(); // commit transaction
  117. }
  118. catch (const DBException &e)
  119. {
  120. QFAIL(e.lastError.text().toStdString().c_str());
  121. }
  122. }
  123. void TestDDL::test_case8()
  124. {
  125. QVERIFY_EXCEPTION_THROWN({
  126. Transaction t;
  127. t.execNonQuery("DROP TABLE table2"); //there is no table2
  128. }, DBException);
  129. }
  130. QTEST_APPLESS_MAIN(TestDDL)
  131. #include "tst_testddl.moc"