2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 * @file test_sql_connection.cpp
18 * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
20 * @brief This file is the implementation file of sql connection tests
22 #include <dpl/test/test_runner.h>
23 #include <dpl/db/sql_connection.h>
24 #include <dpl/db/naive_synchronization_object.h>
26 #include <dpl/log/log.h>
32 extern const char* PATH_DB;
34 RUNNER_TEST_GROUP_INIT(DPL)
36 class AbstractSynchronizationObjectGenerator
39 virtual ~AbstractSynchronizationObjectGenerator() {}
41 virtual DPL::DB::SqlConnection::SynchronizationObject *Create() = 0;
44 class NaiveSynchronizationObjectGenerator :
45 public AbstractSynchronizationObjectGenerator
48 virtual DPL::DB::SqlConnection::SynchronizationObject *Create()
50 return new DPL::DB::NaiveSynchronizationObject();
54 void MassiveReadWriteTest(AbstractSynchronizationObjectGenerator *generator);
56 class StressGenerator :
61 std::string m_dbFileName;
62 AbstractSynchronizationObjectGenerator *m_generator;
65 virtual int ThreadEntry()
67 DPL::DB::SqlConnection connection(
69 DPL::DB::SqlConnection::Flag::None,
70 DPL::DB::SqlConnection::Flag::RW,
71 m_generator->Create());
73 DPL::DB::SqlConnection::DataCommandAutoPtr countCommand =
74 connection.PrepareDataCommand(
75 "SELECT COUNT(*) FROM test WHERE value=?");
77 for (size_t i = 0; i < 10; ++i) {
78 std::ostringstream valueStream;
80 valueStream << "value_";
81 valueStream << static_cast<unsigned long>(m_prefix);
83 valueStream << static_cast<unsigned long>(i);
85 std::string value = valueStream.str();
87 connection.ExecCommand(
88 "INSERT INTO test VALUES ('%s');",
91 countCommand->BindString(1, value.c_str());
93 RUNNER_ASSERT(countCommand->Step());
95 RUNNER_ASSERT(countCommand->GetColumnString(0) == "1");
97 countCommand->Reset();
100 countCommand.reset();
106 StressGenerator(size_t prefix,
107 const std::string &dbFileName,
108 AbstractSynchronizationObjectGenerator *generator) :
110 m_dbFileName(dbFileName),
111 m_generator(generator)
115 typedef std::shared_ptr<DPL::Thread> ThreadPtr;
117 void MassiveReadWriteTest(AbstractSynchronizationObjectGenerator *generator)
119 DPL::DB::SqlConnection connection(PATH_DB,
120 DPL::DB::SqlConnection::Flag::UseLucene,
121 DPL::DB::SqlConnection::Flag::RW);
123 connection.ExecCommand("CREATE TABLE test(value TEXT);");
125 const size_t STRESS_GENERATOR_COUNT = 5;
126 ThreadPtr stressGenerators[STRESS_GENERATOR_COUNT];
128 for (size_t i = 0; i < STRESS_GENERATOR_COUNT; ++i) {
129 stressGenerators[i].reset(
130 new StressGenerator(i, PATH_DB, generator));
132 stressGenerators[i]->Run();
135 for (size_t i = 0; i < STRESS_GENERATOR_COUNT; ++i) {
136 stressGenerators[i]->Quit();
139 connection.ExecCommand("DROP TABLE test;");
143 Name: SqlConnection_MassiveReadWrite_NaiveSynchronization
144 Description: tests massive multiple quiries from many threads
145 Expected: no ORM/db failures
147 RUNNER_TEST(SqlConnection_MassiveReadWrite_NaiveSynchronization)
151 NaiveSynchronizationObjectGenerator m_generator;
152 MassiveReadWriteTest(&m_generator);
156 Name: SqlConnection_Not_Connected_Lucene
157 Description: tests connection to not existing database with Lucene option
158 Expected: exception throw
160 RUNNER_TEST(SqlConnection_Not_Connected_Lucene)
163 DPL::DB::SqlConnection connection(
164 "/notexisitingdirectiory/foo",
165 DPL::DB::SqlConnection::Flag::
167 DPL::DB::SqlConnection::Flag::RW);
168 RUNNER_ASSERT_MSG(false,
169 "connection should throw on accessing "
170 "nonexistent file as a database");
172 Catch(DPL::DB::SqlConnection::Exception::ConnectionBroken)
175 } catch (DPL::Exception) {
176 RUNNER_ASSERT_MSG(false, "Wrong exception found");
181 Name: SqlConnection_Not_Connected
182 Description: tests connection to not existing database without Lucene option
183 Expected: exception throw
185 RUNNER_TEST(SqlConnection_Not_Connected)
188 DPL::DB::SqlConnection connection("/notexisitingdirectiory/foo",
189 DPL::DB::SqlConnection::Flag::None,
190 DPL::DB::SqlConnection::Flag::RW);
191 RUNNER_ASSERT_MSG(false,
192 "connection should throw on accessing "
193 "nonexistent file as a database");
195 Catch(DPL::DB::SqlConnection::Exception::ConnectionBroken)
198 } catch (DPL::Exception) {
199 RUNNER_ASSERT_MSG(false, "Wrong exception found");
204 Name: SqlConnection_Null_Query
205 Description: tests resistance to passing NULL as query in ExecCommand
206 Expected: exception throw
208 RUNNER_TEST(SqlConnection_Null_Query)
210 DPL::DB::SqlConnection connection(PATH_DB,
211 DPL::DB::SqlConnection::Flag::UseLucene,
212 DPL::DB::SqlConnection::Flag::RW);
215 connection.ExecCommand(NULL);
216 RUNNER_ASSERT_MSG(false,
217 "Null pointer should not be accepted");
219 Catch(DPL::DB::SqlConnection::Exception::SyntaxError)
222 } catch (DPL::Exception) {
223 RUNNER_ASSERT_MSG(false, "Wrong exception found");
228 Name: SqlConnection_Bad_Query
229 Description: tests resistance to passing trash as query in ExecCommand
230 Expected: exception throw
232 RUNNER_TEST(SqlConnection_Bad_Query)
234 DPL::DB::SqlConnection connection(PATH_DB,
235 DPL::DB::SqlConnection::Flag::UseLucene,
236 DPL::DB::SqlConnection::Flag::RW);
239 connection.ExecCommand("Some stupid string");
240 RUNNER_ASSERT_MSG(false, "This string should not be accepted");
242 Catch(DPL::DB::SqlConnection::Exception::SyntaxError)
245 } catch (DPL::Exception) {
246 RUNNER_ASSERT_MSG(false, "Wrong exception found");