tst_testdelete.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. #include <QtTest>
  2. #include "EasyQtSql.h"
  3. #include "../Shared/Shared.h"
  4. using namespace EasyQtSql;
  5. class TestDelete : public QObject
  6. {
  7. Q_OBJECT
  8. public:
  9. TestDelete(){}
  10. ~TestDelete(){}
  11. private slots:
  12. void initTestCase();
  13. void cleanupTestCase();
  14. void test_case1();
  15. void test_case2();
  16. void test_case3();
  17. void test_case4();
  18. //====================================================
  19. // Test data
  20. const QList<Row> &testData() const
  21. {
  22. const static QList<Row> rows =
  23. { {1, 2, 3, "a"}
  24. , {4, 5, 6, "b"}
  25. , {7, 8, 9, "c"}
  26. };
  27. return rows;
  28. }
  29. //====================================================
  30. int colCount() const
  31. {
  32. static Row sampleRow;
  33. return sampleRow.staticMetaObject.propertyCount();
  34. }
  35. int rowCount() const
  36. {
  37. return testData().count();
  38. }
  39. };
  40. void TestDelete::initTestCase()
  41. {
  42. QLatin1Literal driverName("QSQLITE");
  43. if (!QSqlDatabase::drivers().contains(driverName))
  44. QFAIL("This test requires the SQLITE database driver");
  45. QSqlDatabase sdb = QSqlDatabase::addDatabase(driverName);
  46. sdb.setDatabaseName(":memory:");
  47. if (!sdb.open())
  48. {
  49. QFAIL(sdb.lastError().text().toStdString().c_str());
  50. }
  51. Transaction t(sdb);
  52. t.execNonQuery("CREATE TABLE testTable (a int, b int, c int, d text)");
  53. const auto &rows = testData();
  54. //multi insert
  55. NonQueryResult res1 = t.insertInto("testTable (a, b, c, d)")
  56. .values(rows[0].a, rows[0].b, rows[0].c, rows[0].d)
  57. .values(rows[1].a, rows[1].b, rows[1].c, rows[1].d)
  58. .values(rows[2].a, rows[2].b, rows[2].c, rows[2].d)
  59. .exec();
  60. t.commit();
  61. }
  62. void TestDelete::cleanupTestCase()
  63. {
  64. {
  65. QSqlDatabase sdb = QSqlDatabase::database(QSqlDatabase::defaultConnection);
  66. if (sdb.isOpen())
  67. {
  68. sdb.close();
  69. }
  70. }
  71. QSqlDatabase::removeDatabase(QSqlDatabase::defaultConnection);
  72. }
  73. void TestDelete::test_case1()
  74. {
  75. try
  76. {
  77. Transaction t;
  78. {
  79. QueryResult res = t.execQuery("SELECT COUNT (*) FROM testTable");
  80. if (res.next())
  81. {
  82. int count = -1;
  83. res.fetchVars(count);
  84. QCOMPARE(count, rowCount());
  85. }
  86. else
  87. {
  88. QFAIL(res.lastError().text().toStdString().c_str());
  89. }
  90. }
  91. t.deleteFrom("testTable").exec();
  92. {
  93. QueryResult res = t.execQuery("SELECT COUNT (*) FROM testTable");
  94. if (res.next())
  95. {
  96. int count = -1;
  97. res.fetchVars(count);
  98. QCOMPARE(count, 0);
  99. }
  100. else
  101. {
  102. QFAIL(res.lastError().text().toStdString().c_str());
  103. }
  104. }
  105. }
  106. catch (const DBException &e)
  107. {
  108. QFAIL(e.lastError.text().toStdString().c_str());
  109. }
  110. }
  111. void TestDelete::test_case2()
  112. {
  113. try
  114. {
  115. Transaction t;
  116. {
  117. QueryResult res = t.execQuery("SELECT COUNT (*) FROM testTable");
  118. if (res.next())
  119. {
  120. int count = -1;
  121. res.fetchVars(count);
  122. QCOMPARE(count, rowCount());
  123. }
  124. else
  125. {
  126. QFAIL(res.lastError().text().toStdString().c_str());
  127. }
  128. }
  129. t.deleteFrom("testTable").where("a=1");
  130. {
  131. QueryResult res = t.execQuery("SELECT COUNT (*) FROM testTable");
  132. if (res.next())
  133. {
  134. int count = -1;
  135. res.fetchVars(count);
  136. QCOMPARE(count, rowCount() - 1);
  137. }
  138. else
  139. {
  140. QFAIL(res.lastError().text().toStdString().c_str());
  141. }
  142. }
  143. }
  144. catch (const DBException &e)
  145. {
  146. QFAIL(e.lastError.text().toStdString().c_str());
  147. }
  148. }
  149. void TestDelete::test_case3()
  150. {
  151. try
  152. {
  153. Transaction t;
  154. {
  155. QueryResult res = t.execQuery("SELECT COUNT (*) FROM testTable");
  156. if (res.next())
  157. {
  158. int count = -1;
  159. res.fetchVars(count);
  160. QCOMPARE(count, rowCount());
  161. }
  162. else
  163. {
  164. QFAIL(res.lastError().text().toStdString().c_str());
  165. }
  166. }
  167. t.deleteFrom("testTable").where("a=?", 1);
  168. {
  169. QueryResult res = t.execQuery("SELECT COUNT (*) FROM testTable");
  170. if (res.next())
  171. {
  172. int count = -1;
  173. res.fetchVars(count);
  174. QCOMPARE(count, rowCount() - 1);
  175. }
  176. else
  177. {
  178. QFAIL(res.lastError().text().toStdString().c_str());
  179. }
  180. }
  181. t.deleteFrom("testTable").where("a=? OR b=?", 1, 5);
  182. {
  183. QueryResult res = t.execQuery("SELECT COUNT (*) FROM testTable");
  184. if (res.next())
  185. {
  186. int count = -1;
  187. res.fetchVars(count);
  188. QCOMPARE(count, rowCount() - 2);
  189. }
  190. else
  191. {
  192. QFAIL(res.lastError().text().toStdString().c_str());
  193. }
  194. }
  195. QCOMPARE(t.commited(), false);
  196. }
  197. catch (const DBException &e)
  198. {
  199. QFAIL(e.lastError.text().toStdString().c_str());
  200. }
  201. }
  202. void TestDelete::test_case4()
  203. {
  204. try
  205. {
  206. Transaction t;
  207. {
  208. QueryResult res = t.execQuery("SELECT COUNT (*) FROM testTable");
  209. if (res.next())
  210. {
  211. int count = -1;
  212. res.fetchVars(count);
  213. QCOMPARE(count, rowCount());
  214. }
  215. else
  216. {
  217. QFAIL(res.lastError().text().toStdString().c_str());
  218. }
  219. }
  220. t.deleteFrom("testTable").where("a=?", 1);
  221. {
  222. QueryResult res = t.execQuery("SELECT COUNT (*) FROM testTable");
  223. if (res.next())
  224. {
  225. int count = -1;
  226. res.fetchVars(count);
  227. QCOMPARE(count, rowCount() - 1);
  228. }
  229. else
  230. {
  231. QFAIL(res.lastError().text().toStdString().c_str());
  232. }
  233. }
  234. t.deleteFrom("testTable").where("a=? OR b=?", 1, 5);
  235. {
  236. QueryResult res = t.execQuery("SELECT COUNT (*) FROM testTable");
  237. if (res.next())
  238. {
  239. int count = -1;
  240. res.fetchVars(count);
  241. QCOMPARE(count, rowCount() - 2);
  242. }
  243. else
  244. {
  245. QFAIL(res.lastError().text().toStdString().c_str());
  246. }
  247. }
  248. t.commit();
  249. QCOMPARE(t.commited(), true);
  250. }
  251. catch (const DBException &e)
  252. {
  253. QFAIL(e.lastError.text().toStdString().c_str());
  254. }
  255. Transaction t2;
  256. QueryResult res = t2.execQuery("SELECT COUNT (*) FROM testTable");
  257. if (res.next())
  258. {
  259. int count = -1;
  260. res.fetchVars(count);
  261. QCOMPARE(count, rowCount() - 2);
  262. }
  263. QCOMPARE(t2.commited(), false);
  264. }
  265. QTEST_APPLESS_MAIN(TestDelete)
  266. #include "tst_testdelete.moc"