comparison PostgreSQL/UnitTests/PostgreSQLTests.cpp @ 216:fbb52129158a

TransactionType given to PostgreSQLTransaction constructor
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 26 Mar 2021 17:47:56 +0100
parents b40b30075c51
children dd6cfc250747
comparison
equal deleted inserted replaced
215:b40b30075c51 216:fbb52129158a
204 s.BindInteger(0, 42); 204 s.BindInteger(0, 42);
205 s.BindInteger(1, 4242); 205 s.BindInteger(1, 4242);
206 s.Run(); 206 s.Run();
207 207
208 { 208 {
209 PostgreSQLTransaction t(*pg); 209 PostgreSQLTransaction t(*pg, TransactionType_ReadOnly);
210 s.BindInteger(0, 0);
211 s.BindInteger(1, 1);
212 // Failure, as INSERT in a read-only transaction
213 ASSERT_THROW(s.Run(), Orthanc::OrthancException);
214 }
215
216 {
217 PostgreSQLTransaction t(*pg, TransactionType_ReadWrite);
210 s.BindInteger(0, 43); 218 s.BindInteger(0, 43);
211 s.BindInteger(1, 4343); 219 s.BindInteger(1, 4343);
212 s.Run(); 220 s.Run();
213 s.BindInteger(0, 44); 221 s.BindInteger(0, 44);
214 s.BindInteger(1, 4444); 222 s.BindInteger(1, 4444);
220 228
221 // No commit 229 // No commit
222 } 230 }
223 231
224 { 232 {
233 // Implicit transaction
225 PostgreSQLStatement u(*pg, "SELECT COUNT(*) FROM Test"); 234 PostgreSQLStatement u(*pg, "SELECT COUNT(*) FROM Test");
226 PostgreSQLResult r(u); 235 PostgreSQLResult r(u);
227 ASSERT_EQ(1, r.GetInteger64(0)); // Just "1" because of implicit rollback 236 ASSERT_EQ(1, r.GetInteger64(0)); // Just "1" because of implicit rollback
228 } 237 }
229 238
230 { 239 {
231 PostgreSQLTransaction t(*pg); 240 PostgreSQLTransaction t(*pg, TransactionType_ReadWrite);
232 s.BindInteger(0, 43); 241 s.BindInteger(0, 43);
233 s.BindInteger(1, 4343); 242 s.BindInteger(1, 4343);
234 s.Run(); 243 s.Run();
235 s.BindInteger(0, 44); 244 s.BindInteger(0, 44);
236 s.BindInteger(1, 4444); 245 s.BindInteger(1, 4444);
246 ASSERT_THROW(t.Commit(), Orthanc::OrthancException); 255 ASSERT_THROW(t.Commit(), Orthanc::OrthancException);
247 } 256 }
248 } 257 }
249 258
250 { 259 {
260 PostgreSQLTransaction t(*pg, TransactionType_ReadOnly);
251 PostgreSQLStatement u(*pg, "SELECT COUNT(*) FROM Test"); 261 PostgreSQLStatement u(*pg, "SELECT COUNT(*) FROM Test");
252 PostgreSQLResult r(u); 262 PostgreSQLResult r(u);
253 ASSERT_EQ(3, r.GetInteger64(0)); 263 ASSERT_EQ(3, r.GetInteger64(0));
254 } 264 }
255 } 265 }
274 s.DeclareInputString(0); 284 s.DeclareInputString(0);
275 s.DeclareInputLargeObject(1); 285 s.DeclareInputLargeObject(1);
276 286
277 for (int i = 0; i < 10; i++) 287 for (int i = 0; i < 10; i++)
278 { 288 {
279 PostgreSQLTransaction t(*pg); 289 PostgreSQLTransaction t(*pg, TransactionType_ReadWrite);
280 290
281 std::string value = "Value " + boost::lexical_cast<std::string>(i * 2); 291 std::string value = "Value " + boost::lexical_cast<std::string>(i * 2);
282 PostgreSQLLargeObject obj(*pg, value); 292 PostgreSQLLargeObject obj(*pg, value);
283 293
284 s.BindString(0, "Index " + boost::lexical_cast<std::string>(i)); 294 s.BindString(0, "Index " + boost::lexical_cast<std::string>(i));
295 305
296 306
297 ASSERT_EQ(10, CountLargeObjects(*pg)); 307 ASSERT_EQ(10, CountLargeObjects(*pg));
298 308
299 { 309 {
300 PostgreSQLTransaction t(*pg); 310 PostgreSQLTransaction t(*pg, TransactionType_ReadOnly);
301 PostgreSQLStatement s(*pg, "SELECT * FROM Test ORDER BY name DESC"); 311 PostgreSQLStatement s(*pg, "SELECT * FROM Test ORDER BY name DESC");
302 PostgreSQLResult r(s); 312 PostgreSQLResult r(s);
303 313
304 ASSERT_FALSE(r.IsDone()); 314 ASSERT_FALSE(r.IsDone());
305 315
316 //ASSERT_TRUE(r.IsString(0)); 326 //ASSERT_TRUE(r.IsString(0));
317 } 327 }
318 328
319 329
320 { 330 {
321 PostgreSQLTransaction t(*pg); 331 PostgreSQLTransaction t(*pg, TransactionType_ReadWrite);
322 PostgreSQLStatement s(*pg, "DELETE FROM Test WHERE name='Index 9'"); 332 PostgreSQLStatement s(*pg, "DELETE FROM Test WHERE name='Index 9'");
323 s.Run(); 333 s.Run();
324 t.Commit(); 334 t.Commit();
325 } 335 }
326 336
327 337
328 { 338 {
329 // Count the number of items in the DB 339 // Count the number of items in the DB
330 PostgreSQLTransaction t(*pg); 340 PostgreSQLTransaction t(*pg, TransactionType_ReadOnly);
331 PostgreSQLStatement s(*pg, "SELECT COUNT(*) FROM Test"); 341 PostgreSQLStatement s(*pg, "SELECT COUNT(*) FROM Test");
332 PostgreSQLResult r(s); 342 PostgreSQLResult r(s);
333 ASSERT_EQ(9, r.GetInteger64(0)); 343 ASSERT_EQ(9, r.GetInteger64(0));
334 } 344 }
335 345