tst_testinsert.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #include <QtTest>
  2. #include "EasyQtSql.h"
  3. #include "../Shared/Shared.h"
  4. using namespace EasyQtSql;
  5. class TestInsert : public QObject
  6. {
  7. Q_OBJECT
  8. public:
  9. TestInsert(){}
  10. ~TestInsert(){}
  11. private slots:
  12. void initTestCase();
  13. void cleanupTestCase();
  14. void test_case1();
  15. void test_case2();
  16. void test_case3();
  17. };
  18. void TestInsert::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 TestInsert::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 TestInsert::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).exec();
  58. query.values(rows[1].a, rows[1].b, rows[1].c, rows[1].d).exec();
  59. query.values(rows[2].a, rows[2].b, rows[2].c, rows[2].d).exec();
  60. query.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. res = t.execQuery("SELECT a, b, c, d FROM testTable");
  67. int i = 0;
  68. while (res.next())
  69. {
  70. int a, b, c;
  71. QString d;
  72. res.fetchVars(a, b, c, d);
  73. QCOMPARE(a, rows[i].a);
  74. QCOMPARE(b, rows[i].b);
  75. QCOMPARE(c, rows[i].c);
  76. QCOMPARE(d, rows[i].d);
  77. ++i;
  78. }
  79. Database sdb;
  80. QueryResult res1 = sdb.execQuery("SELECT a, b, c, d FROM testTable");
  81. QCOMPARE(res1.next(), true);
  82. //transaction rolled back
  83. }
  84. catch (const DBException &ex)
  85. {
  86. QFAIL(ex.lastError.text().toStdString().c_str());
  87. }
  88. Database sdb;
  89. QueryResult res = sdb.execQuery("SELECT a, b, c, d FROM testTable");
  90. QCOMPARE(res.next(), false);
  91. }
  92. void TestInsert::test_case2()
  93. {
  94. const QVector<Row> rows = { {1, 2, 3, "a"}, {4, 5, 6, "b"}, {7, 8, 9, "c"}, {10, 11, 12, "d"}};
  95. try
  96. {
  97. Transaction t;
  98. InsertQuery query = t.insertInto("testTable (a, b, c, d)");
  99. query.values(rows[0].a, rows[0].b, rows[0].c, rows[0].d)
  100. .values(rows[1].a, rows[1].b, rows[1].c, rows[1].d)
  101. .values(rows[2].a, rows[2].b, rows[2].c, rows[2].d)
  102. .values(rows[3].a, rows[3].b, rows[3].c, rows[3].d).exec();
  103. QueryResult res = t.execQuery("SELECT COUNT(*) FROM testTable");
  104. res.next();
  105. int count = -1;
  106. res.fetchVars(count);
  107. QCOMPARE(count, 4);
  108. res = t.execQuery("SELECT a, b, c, d FROM testTable");
  109. int i = 0;
  110. while (res.next())
  111. {
  112. int a, b, c;
  113. QString d;
  114. res.fetchVars(a, b, c, d);
  115. QCOMPARE(a, rows[i].a);
  116. QCOMPARE(b, rows[i].b);
  117. QCOMPARE(c, rows[i].c);
  118. QCOMPARE(d, rows[i].d);
  119. ++i;
  120. }
  121. Database sdb;
  122. QueryResult res1 = sdb.execQuery("SELECT a, b, c, d FROM testTable");
  123. QCOMPARE(res1.next(), true);
  124. //transaction rolled back
  125. }
  126. catch (const DBException &ex)
  127. {
  128. QFAIL(ex.lastError.text().toStdString().c_str());
  129. }
  130. Database sdb;
  131. QueryResult res = sdb.execQuery("SELECT a, b, c, d FROM testTable");
  132. QCOMPARE(res.next(), false);
  133. }
  134. void TestInsert::test_case3()
  135. {
  136. const QVector<Row> rows = { {1, 2, 3, "a"}, {4, 5, 6, "b"}, {7, 8, 9, "c"}, {10, 11, 12, "d"}};
  137. QVERIFY_EXCEPTION_THROWN({
  138. Transaction t;
  139. InsertQuery query = t.insertInto("testTable (a, b, c, d)");
  140. //parameters count mismatch
  141. query.values(rows[0].a, rows[0].b, rows[0].c).exec();
  142. query.values(rows[1].a, rows[1].b, rows[1].c).exec();
  143. query.values(rows[2].a, rows[2].b, rows[2].c).exec();
  144. query.values(rows[3].a, rows[3].b, rows[3].c).exec();
  145. //transaction rolled back
  146. }, DBException);
  147. }
  148. QTEST_APPLESS_MAIN(TestInsert)
  149. #include "tst_testinsert.moc"