The Battle for Wesnoth  1.15.3+dev
GeneralTest.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 "GeneralTest.h"
11 
12 TEST_F(GeneralTest, testCreateFail) {
13  // intended syntax error
14  ASSERT_ANY_THROW(m_con->execute("CREATE TAVBEL testtest ();"));
15  ASSERT_ANY_THROW(m_con->execute("CREATE TABLE testtest (\");"));
16  ASSERT_ANY_THROW(m_con->execute("CREATE TABLE testtest¸ ()"));
17  ASSERT_ANY_THROW(m_con->execute("CREATE TABLE ()"));
18 }
19 
20 TEST_F(GeneralTest, testMissingConnection) {
21  // create connection without connecting
22  account_ref no_acc = account::create("0.0.0.0", "", "");
23  connection_ref no_conn = connection::create(no_acc);
24 
25  ASSERT_FALSE(no_conn->connected());
26  EXPECT_ANY_THROW(no_conn->execute("CREATE TABLE asdf;"));
27  EXPECT_ANY_THROW(no_conn->query("SELECT * FROM asdf;"));
28  EXPECT_ANY_THROW(no_conn->insert("INSERT INTO asdf (a) VALUES ('a');"));
29 
30  EXPECT_ANY_THROW(no_conn->create_statement("SELECT * FROM asdf;"));
31 }
32 
33 TEST_F(GeneralTest, testDuplicateTable) {
34  EXPECT_ANY_THROW(m_con->execute("CREATE TABLE " + m_table_name +
35  " (id INT AUTO_INCREMENT, PRIMARY KEY(id));"));
36 }
37 
38 TEST_F(GeneralTest, testConcurrentInsert) {
39  constexpr int num_results = 100;
40 
41  std::vector<handle> handles;
42  std::set<u64> results;
43 
44  concurrency::set_account(m_account_setup);
45 
46  // launch all queries
47  for (int i = 0; i < num_results; i++) {
48  auto hndl = concurrency::insert("INSERT INTO " + m_table_name + "(str) VALUES('teest');", true);
49  handles.push_back(hndl);
50  }
51 
52  // wait for all queries
53  for (auto h : handles) EXPECT_TRUE(concurrency::wait_handle(h));
54 
55  // get all results
56  for (auto h : handles) {
58  auto set_result = results.insert(res);
59 
60  // fail if this result already existed (insert returns false as second)
61  EXPECT_TRUE(set_result.second);
62  }
63 
64  // release all handles
65  for (auto h : handles) concurrency::release_handle(h);
66 
67  EXPECT_EQ(num_results, results.size());
68 }
void set_account(account_ref &account)
std::shared_ptr< account > account_ref
Definition: account.hpp:19
#define h
TEST_F(GeneralTest, testCreateFail)
Definition: GeneralTest.cpp:12
std::size_t i
Definition: function.cpp:933
bool wait_handle(handle h, u64 wait_time_ms=100)
unsigned long long u64
Definition: types.hpp:34
std::shared_ptr< connection > connection_ref
Definition: statement.hpp:33
void release_handle(handle h)
u64 get_execute_result(handle h)