The Battle for Wesnoth  1.15.3+dev
RollbackTest.cpp
Go to the documentation of this file.
1 //
2 // M A R I A D B + +
3 //
4 // Copyright The ViaDuck Project 2016 - 2018.
5 // Distributed under the Boost Software License, Version 1.0.
6 // (See accompanying file LICENSE or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
8 
9 #include "RollbackTest.h"
10 
11 TEST_F(RollbackTest, testTransactCommit) {
12  transaction_ref trx = m_con->create_transaction();
13 
14  u64 id = m_con->insert("INSERT INTO " + m_table_name + "(str) VALUES('test');");
15  EXPECT_NE(0, id);
16  trx->commit();
17 
18  result_set_ref rs = m_con->query("SELECT COUNT(*) FROM " + m_table_name + ";");
19  EXPECT_TRUE(!!rs);
20  EXPECT_TRUE(rs->next());
21  EXPECT_EQ(1, rs->get_unsigned64(0));
22 }
23 
24 TEST_F(RollbackTest, testTransactionRollback) {
25  // force transaction out of scope to destruct it and trigger automatic rollback
26  {
27  transaction_ref trx = m_con->create_transaction();
28  m_con->insert("INSERT INTO " + m_table_name + "(str) VALUES('test2');");
29  }
30 
31  result_set_ref rs = m_con->query("SELECT COUNT(*) FROM " + m_table_name + ";");
32  EXPECT_TRUE(!!rs);
33  EXPECT_TRUE(rs->next());
34  EXPECT_EQ(0, rs->get_unsigned64(0));
35 }
36 
37 TEST_F(RollbackTest, testSavePointCommit) {
38  transaction_ref trx = m_con->create_transaction();
39  {
40  save_point_ref sp = trx->create_save_point();
41  u64 id = m_con->insert("INSERT INTO " + m_table_name + "(str) VALUES('test');");
42  EXPECT_NE(0, id);
43  sp->commit();
44  }
45  trx->commit();
46 
47  result_set_ref rs = m_con->query("SELECT COUNT(*) FROM " + m_table_name + ";");
48  EXPECT_TRUE(!!rs);
49  EXPECT_TRUE(rs->next());
50  EXPECT_EQ(1, rs->get_unsigned64(0));
51 }
52 
53 TEST_F(RollbackTest, testSavePointNoCommit) {
54  transaction_ref trx = m_con->create_transaction();
55  {
56  save_point_ref sp = trx->create_save_point();
57  u64 id = m_con->insert("INSERT INTO " + m_table_name + "(str) VALUES('test');");
58  EXPECT_NE(0, id);
59  }
60  trx->commit();
61 
62  result_set_ref rs = m_con->query("SELECT COUNT(*) FROM " + m_table_name + ";");
63  EXPECT_TRUE(!!rs);
64  EXPECT_TRUE(rs->next());
65  EXPECT_EQ(0, rs->get_unsigned64(0));
66 }
67 
68 TEST_F(RollbackTest, testMultiInsertIntegration) {
69  std::string queries[] = {
70  "INSERT INTO " + m_table_name + " (data) VALUES(?);",
71  "INSERT INTO " + m_table_name + " (data, str) VALUES(?, ?);",
72  "INSERT INTO " + m_table_name + " (data, str, dt) VALUES(?, ?, ?);",
73  "INSERT INTO " + m_table_name + " (data, str, dt, t) VALUES(?, ?, ?, ?);",
74  "INSERT INTO " + m_table_name + " (data, str, dt, t, value) VALUES(?, ?, ?, ?, ?);",
75  "INSERT INTO " + m_table_name +
76  " (data, str, dt, t, value, deci) VALUES(?, ?, ?, ?, ?, ?);",
77  ""};
78 
79  const char *content =
80  "01234567890123456789012345678901234567890123456789"
81  "01234567890123456789012345678901234567890123456789";
82 
83  u32 index = 0;
84 
85  while (queries[index] != "") {
86  statement_ref sta = m_con->create_statement(queries[index]);
87  data_ref data = data_ref(new mariadb::data<char>(content, 100));
88 
89  sta->set_data(0, data);
90 
91  if (index >= 1) sta->set_string(1, "test");
92  if (index >= 2) sta->set_date_time(2, mariadb::date_time(2000, 1, 2, 3, 4, 5));
93  if (index >= 3) sta->set_time(3, mariadb::time(11, 22, 33));
94  if (index >= 4) sta->set_signed32(4, 666);
95  if (index >= 5) sta->set_decimal(5, decimal("1.1234"));
96  EXPECT_NE(0, sta->insert());
97 
98  index++;
99  result_set_ref rs = m_con->query("SELECT COUNT(*) FROM " + m_table_name + ";");
100  EXPECT_TRUE(!!rs);
101  EXPECT_TRUE(rs->next());
102  EXPECT_EQ(index, rs->get_unsigned64(0));
103  }
104 
105  //
106  // Validate the inserted value from last test
107  //
108 
109  result_set_ref rs = m_con->query("SELECT data, str, dt, t, value, deci FROM " + m_table_name +
110  " ORDER BY id DESC LIMIT 1;");
111 
112  EXPECT_TRUE(!!rs);
113  EXPECT_TRUE(rs->next());
114 
115  data_ref data = rs->get_data("data");
116  EXPECT_TRUE(!!data);
117  EXPECT_EQ(100, data->size());
118 
119  EXPECT_EQ("test", rs->get_string("str"));
120  EXPECT_EQ(666, rs->get_signed32("value"));
121  EXPECT_EQ("1.1234", rs->get_decimal("deci").str());
122  EXPECT_EQ("2000-01-02 03:04:05", rs->get_date_time("dt").str());
123  EXPECT_EQ("11:22:33", rs->get_time("t").str_time());
124 }
std::shared_ptr< transaction > transaction_ref
Definition: transaction.hpp:78
std::shared_ptr< result_set > result_set_ref
Definition: result_set.hpp:219
Class representing SQL time.
Definition: time.hpp:23
return decimal(std::string(m_row[index], column_size(index)))
TEST_F(RollbackTest, testTransactCommit)
std::shared_ptr< ::mariadb::data< char > > data_ref
Definition: data.hpp:170
Class used to represent SQL date_time.
Definition: date_time.hpp:20
std::shared_ptr< save_point > save_point_ref
Definition: save_point.hpp:50
std::size_t index(const std::string &str, const std::size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
Definition: unicode.cpp:71
unsigned long long u64
Definition: types.hpp:34
std::shared_ptr< statement > statement_ref
Definition: statement.hpp:109
unsigned int u32
Definition: types.hpp:22