tst_testupdate.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. #include <QtTest>
  2. #include "EasyQtSql.h"
  3. #include "../Shared/Shared.h"
  4. using namespace EasyQtSql;
  5. class TestUpdate : public QObject
  6. {
  7. Q_OBJECT
  8. public:
  9. TestUpdate(){}
  10. ~TestUpdate(){}
  11. private slots:
  12. void initTestCase();
  13. void cleanupTestCase();
  14. void test_case1();
  15. void test_case2();
  16. void test_case3();
  17. };
  18. void TestUpdate::initTestCase()
  19. {
  20. QLatin1Literal driverName("QSQLITE");
  21. if (!QSqlDatabase::drivers().contains(driverName))
  22. QFAIL("This test requires the SQLITE database driver");
  23. QSqlDatabase sdb = QSqlDatabase::addDatabase(driverName);
  24. sdb.setDatabaseName(":memory:");
  25. if (!sdb.open())
  26. {
  27. QFAIL(sdb.lastError().text().toStdString().c_str());
  28. }
  29. try {
  30. Transaction t(sdb);
  31. t.execNonQuery("CREATE TABLE testTable (a int, b int, c int, d text)");
  32. t.commit();
  33. }
  34. catch (const DBException &ex)
  35. {
  36. QFAIL(ex.lastError.text().toStdString().c_str());
  37. }
  38. }
  39. void TestUpdate::cleanupTestCase()
  40. {
  41. {
  42. QSqlDatabase sdb = QSqlDatabase::database(QSqlDatabase::defaultConnection);
  43. if (sdb.isOpen())
  44. {
  45. sdb.close();
  46. }
  47. }
  48. QSqlDatabase::removeDatabase(QSqlDatabase::defaultConnection);
  49. }
  50. void TestUpdate::test_case1()
  51. {
  52. const QVector<Row> rows = { {1, 2, 3, "a"}, {4, 5, 6, "b"}, {7, 8, 9, "c"}, {10, 11, 12, "d"}};
  53. try
  54. {
  55. Transaction t;
  56. InsertQuery query = t.insertInto("testTable (a, b, c, d)");
  57. query.values(rows[0].a, rows[0].b, rows[0].c, rows[0].d)
  58. .values(rows[1].a, rows[1].b, rows[1].c, rows[1].d)
  59. .values(rows[2].a, rows[2].b, rows[2].c, rows[2].d)
  60. .values(rows[3].a, rows[3].b, rows[3].c, rows[3].d).exec();
  61. QueryResult res = t.execQuery("SELECT COUNT(*) FROM testTable");
  62. res.next();
  63. int count = -1;
  64. res.fetchVars(count);
  65. QCOMPARE(count, 4);
  66. {
  67. res = t.execQuery("SELECT a, b, c, d FROM testTable");
  68. int i = 0;
  69. while (res.next())
  70. {
  71. int a, b, c;
  72. QString d;
  73. res.fetchVars(a, b, c, d);
  74. QCOMPARE(a, rows[i].a);
  75. QCOMPARE(b, rows[i].b);
  76. QCOMPARE(c, rows[i].c);
  77. QCOMPARE(d, rows[i].d);
  78. ++i;
  79. }
  80. }
  81. {
  82. t.update("testTable").set("a", 10).set("b", 20).set("c", 30).exec();
  83. res = t.execQuery("SELECT a, b, c, d FROM testTable");
  84. int i = 0;
  85. while (res.next())
  86. {
  87. int a, b, c;
  88. QString d;
  89. res.fetchVars(a, b, c, d);
  90. qDebug() << res.toMap();
  91. QCOMPARE(a, 10);
  92. QCOMPARE(b, 20);
  93. QCOMPARE(c, 30);
  94. QCOMPARE(d, rows[i].d);
  95. ++i;
  96. }
  97. }
  98. //transaction rolled back
  99. }
  100. catch (const DBException &ex)
  101. {
  102. QFAIL(ex.lastError.text().toStdString().c_str());
  103. }
  104. Database sdb;
  105. QueryResult res = sdb.execQuery("SELECT a, b, c, d FROM testTable");
  106. QCOMPARE(res.next(), false);
  107. }
  108. void TestUpdate::test_case2()
  109. {
  110. const QVector<Row> rows = { {1, 2, 3, "a"}, {4, 5, 6, "b"}, {7, 8, 9, "c"}, {10, 11, 12, "d"}};
  111. const QVector<Row> expd = { {10, 20, 30, "a"}, {10, 20, 30, "b"}, {7, 8, 9, "c"}, {10, 11, 12, "d"}};
  112. try
  113. {
  114. Transaction t;
  115. InsertQuery query = t.insertInto("testTable (a, b, c, d)");
  116. query.values(rows[0].a, rows[0].b, rows[0].c, rows[0].d)
  117. .values(rows[1].a, rows[1].b, rows[1].c, rows[1].d)
  118. .values(rows[2].a, rows[2].b, rows[2].c, rows[2].d)
  119. .values(rows[3].a, rows[3].b, rows[3].c, rows[3].d).exec();
  120. QueryResult res = t.execQuery("SELECT COUNT(*) FROM testTable");
  121. res.next();
  122. int count = -1;
  123. res.fetchVars(count);
  124. QCOMPARE(count, 4);
  125. {
  126. res = t.execQuery("SELECT a, b, c, d FROM testTable");
  127. int i = 0;
  128. while (res.next())
  129. {
  130. int a, b, c;
  131. QString d;
  132. res.fetchVars(a, b, c, d);
  133. QCOMPARE(a, rows[i].a);
  134. QCOMPARE(b, rows[i].b);
  135. QCOMPARE(c, rows[i].c);
  136. QCOMPARE(d, rows[i].d);
  137. ++i;
  138. }
  139. }
  140. {
  141. t.update("testTable").set("a", 10).set("b", 20).set("c", 30).where("a=? OR a=?", 1, 4);
  142. res = t.execQuery("SELECT a, b, c, d FROM testTable");
  143. int i = 0;
  144. while (res.next())
  145. {
  146. int a, b, c;
  147. QString d;
  148. res.fetchVars(a, b, c, d);
  149. qDebug() << res.toMap();
  150. QCOMPARE(a, expd[i].a);
  151. QCOMPARE(b, expd[i].b);
  152. QCOMPARE(c, expd[i].c);
  153. QCOMPARE(d, expd[i].d);
  154. ++i;
  155. }
  156. }
  157. //transaction rolled back
  158. }
  159. catch (const DBException &ex)
  160. {
  161. QFAIL(ex.lastError.text().toStdString().c_str());
  162. }
  163. Database sdb;
  164. QueryResult res = sdb.execQuery("SELECT a, b, c, d FROM testTable");
  165. QCOMPARE(res.next(), false);
  166. }
  167. void TestUpdate::test_case3()
  168. {
  169. const QVector<Row> rows = { {1, 2, 3, "a"}, {4, 5, 6, "b"}, {7, 8, 9, "c"}, {10, 11, 12, "d"}};
  170. const QVector<Row> expd = { {10, 20, 30, "a"}, {10, 20, 30, "b"}, {7, 8, 9, "c"}, {10, 11, 12, "d"}};
  171. try
  172. {
  173. Transaction t;
  174. InsertQuery query = t.insertInto("testTable (a, b, c, d)");
  175. query.values(rows[0].a, rows[0].b, rows[0].c, rows[0].d)
  176. .values(rows[1].a, rows[1].b, rows[1].c, rows[1].d)
  177. .values(rows[2].a, rows[2].b, rows[2].c, rows[2].d)
  178. .values(rows[3].a, rows[3].b, rows[3].c, rows[3].d).exec();
  179. QueryResult res = t.execQuery("SELECT COUNT(*) FROM testTable");
  180. res.next();
  181. int count = -1;
  182. res.fetchVars(count);
  183. QCOMPARE(count, 4);
  184. {
  185. res = t.execQuery("SELECT a, b, c, d FROM testTable");
  186. int i = 0;
  187. while (res.next())
  188. {
  189. int a, b, c;
  190. QString d;
  191. res.fetchVars(a, b, c, d);
  192. QCOMPARE(a, rows[i].a);
  193. QCOMPARE(b, rows[i].b);
  194. QCOMPARE(c, rows[i].c);
  195. QCOMPARE(d, rows[i].d);
  196. ++i;
  197. }
  198. }
  199. {
  200. t.update("testTable").set("a", 10).set("b", 20).set("c", 30).where("a = 1 OR a = 4");
  201. res = t.execQuery("SELECT a, b, c, d FROM testTable");
  202. int i = 0;
  203. while (res.next())
  204. {
  205. int a, b, c;
  206. QString d;
  207. res.fetchVars(a, b, c, d);
  208. qDebug() << res.toMap();
  209. QCOMPARE(a, expd[i].a);
  210. QCOMPARE(b, expd[i].b);
  211. QCOMPARE(c, expd[i].c);
  212. QCOMPARE(d, expd[i].d);
  213. ++i;
  214. }
  215. }
  216. //transaction rolled back
  217. }
  218. catch (const DBException &ex)
  219. {
  220. QFAIL(ex.lastError.text().toStdString().c_str());
  221. }
  222. Database sdb;
  223. QueryResult res = sdb.execQuery("SELECT a, b, c, d FROM testTable");
  224. QCOMPARE(res.next(), false);
  225. }
  226. QTEST_APPLESS_MAIN(TestUpdate)
  227. #include "tst_testupdate.moc"