Mercurial > hg > orthanc-databases
comparison PostgreSQL/UnitTests/PostgreSQLTests.cpp @ 214:ab96698c73a3
removed useless information about read-only in ITransaction and IPrecompiledStatement
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 25 Mar 2021 13:56:26 +0100 |
parents | d9ef3f16e6a2 |
children | b40b30075c51 |
comparison
equal
deleted
inserted
replaced
213:c2e4a909de0e | 214:ab96698c73a3 |
---|---|
64 | 64 |
65 | 65 |
66 static int64_t CountLargeObjects(PostgreSQLDatabase& db) | 66 static int64_t CountLargeObjects(PostgreSQLDatabase& db) |
67 { | 67 { |
68 // Count the number of large objects in the DB | 68 // Count the number of large objects in the DB |
69 PostgreSQLStatement s(db, "SELECT COUNT(*) FROM pg_catalog.pg_largeobject", true); | 69 PostgreSQLStatement s(db, "SELECT COUNT(*) FROM pg_catalog.pg_largeobject"); |
70 PostgreSQLResult r(s); | 70 PostgreSQLResult r(s); |
71 return r.GetInteger64(0); | 71 return r.GetInteger64(0); |
72 } | 72 } |
73 | 73 |
74 | 74 |
78 | 78 |
79 ASSERT_FALSE(pg->DoesTableExist("Test")); | 79 ASSERT_FALSE(pg->DoesTableExist("Test")); |
80 pg->Execute("CREATE TABLE Test(name INTEGER, value BIGINT)"); | 80 pg->Execute("CREATE TABLE Test(name INTEGER, value BIGINT)"); |
81 ASSERT_TRUE(pg->DoesTableExist("Test")); | 81 ASSERT_TRUE(pg->DoesTableExist("Test")); |
82 | 82 |
83 PostgreSQLStatement s(*pg, "INSERT INTO Test VALUES ($1,$2)", false); | 83 PostgreSQLStatement s(*pg, "INSERT INTO Test VALUES ($1,$2)"); |
84 s.DeclareInputInteger(0); | 84 s.DeclareInputInteger(0); |
85 s.DeclareInputInteger64(1); | 85 s.DeclareInputInteger64(1); |
86 | 86 |
87 s.BindInteger(0, 43); | 87 s.BindInteger(0, 43); |
88 s.BindNull(0); | 88 s.BindNull(0); |
97 s.BindNull(0); | 97 s.BindNull(0); |
98 s.BindInteger64(1, 4444); | 98 s.BindInteger64(1, 4444); |
99 s.Run(); | 99 s.Run(); |
100 | 100 |
101 { | 101 { |
102 PostgreSQLStatement t(*pg, "SELECT name, value FROM Test ORDER BY name", true); | 102 PostgreSQLStatement t(*pg, "SELECT name, value FROM Test ORDER BY name"); |
103 PostgreSQLResult r(t); | 103 PostgreSQLResult r(t); |
104 | 104 |
105 ASSERT_FALSE(r.IsDone()); | 105 ASSERT_FALSE(r.IsDone()); |
106 ASSERT_FALSE(r.IsNull(0)); ASSERT_EQ(42, r.GetInteger(0)); | 106 ASSERT_FALSE(r.IsNull(0)); ASSERT_EQ(42, r.GetInteger(0)); |
107 ASSERT_FALSE(r.IsNull(1)); ASSERT_EQ(-4242, r.GetInteger64(1)); | 107 ASSERT_FALSE(r.IsNull(1)); ASSERT_EQ(-4242, r.GetInteger64(1)); |
119 r.Next(); | 119 r.Next(); |
120 ASSERT_TRUE(r.IsDone()); | 120 ASSERT_TRUE(r.IsDone()); |
121 } | 121 } |
122 | 122 |
123 { | 123 { |
124 PostgreSQLStatement t(*pg, "SELECT name, value FROM Test WHERE name=$1", true); | 124 PostgreSQLStatement t(*pg, "SELECT name, value FROM Test WHERE name=$1"); |
125 t.DeclareInputInteger(0); | 125 t.DeclareInputInteger(0); |
126 | 126 |
127 { | 127 { |
128 t.BindInteger(0, 42); | 128 t.BindInteger(0, 42); |
129 PostgreSQLResult r(t); | 129 PostgreSQLResult r(t); |
149 { | 149 { |
150 std::unique_ptr<PostgreSQLDatabase> pg(CreateTestDatabase()); | 150 std::unique_ptr<PostgreSQLDatabase> pg(CreateTestDatabase()); |
151 | 151 |
152 pg->Execute("CREATE TABLE Test(name INTEGER, value VARCHAR(40))"); | 152 pg->Execute("CREATE TABLE Test(name INTEGER, value VARCHAR(40))"); |
153 | 153 |
154 PostgreSQLStatement s(*pg, "INSERT INTO Test VALUES ($1,$2)", false); | 154 PostgreSQLStatement s(*pg, "INSERT INTO Test VALUES ($1,$2)"); |
155 s.DeclareInputInteger(0); | 155 s.DeclareInputInteger(0); |
156 s.DeclareInputString(1); | 156 s.DeclareInputString(1); |
157 | 157 |
158 s.BindInteger(0, 42); | 158 s.BindInteger(0, 42); |
159 s.BindString(1, "Hello"); | 159 s.BindString(1, "Hello"); |
166 s.BindNull(0); | 166 s.BindNull(0); |
167 s.BindString(1, ""); | 167 s.BindString(1, ""); |
168 s.Run(); | 168 s.Run(); |
169 | 169 |
170 { | 170 { |
171 PostgreSQLStatement t(*pg, "SELECT name, value FROM Test ORDER BY name", true); | 171 PostgreSQLStatement t(*pg, "SELECT name, value FROM Test ORDER BY name"); |
172 PostgreSQLResult r(t); | 172 PostgreSQLResult r(t); |
173 | 173 |
174 ASSERT_FALSE(r.IsDone()); | 174 ASSERT_FALSE(r.IsDone()); |
175 ASSERT_FALSE(r.IsNull(0)); ASSERT_EQ(42, r.GetInteger(0)); | 175 ASSERT_FALSE(r.IsNull(0)); ASSERT_EQ(42, r.GetInteger(0)); |
176 ASSERT_FALSE(r.IsNull(1)); ASSERT_EQ("Hello", r.GetString(1)); | 176 ASSERT_FALSE(r.IsNull(1)); ASSERT_EQ("Hello", r.GetString(1)); |
196 std::unique_ptr<PostgreSQLDatabase> pg(CreateTestDatabase()); | 196 std::unique_ptr<PostgreSQLDatabase> pg(CreateTestDatabase()); |
197 | 197 |
198 pg->Execute("CREATE TABLE Test(name INTEGER, value INTEGER)"); | 198 pg->Execute("CREATE TABLE Test(name INTEGER, value INTEGER)"); |
199 | 199 |
200 { | 200 { |
201 PostgreSQLStatement s(*pg, "INSERT INTO Test VALUES ($1,$2)", false); | 201 PostgreSQLStatement s(*pg, "INSERT INTO Test VALUES ($1,$2)"); |
202 s.DeclareInputInteger(0); | 202 s.DeclareInputInteger(0); |
203 s.DeclareInputInteger(1); | 203 s.DeclareInputInteger(1); |
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(); |
212 s.Run(); | 212 s.Run(); |
213 s.BindInteger(0, 44); | 213 s.BindInteger(0, 44); |
214 s.BindInteger(1, 4444); | 214 s.BindInteger(1, 4444); |
215 s.Run(); | 215 s.Run(); |
216 | 216 |
217 PostgreSQLStatement u(*pg, "SELECT COUNT(*) FROM Test", true); | 217 PostgreSQLStatement u(*pg, "SELECT COUNT(*) FROM Test"); |
218 PostgreSQLResult r(u); | 218 PostgreSQLResult r(u); |
219 ASSERT_EQ(3, r.GetInteger64(0)); | 219 ASSERT_EQ(3, r.GetInteger64(0)); |
220 | 220 |
221 // No commit | 221 // No commit |
222 } | 222 } |
223 | 223 |
224 { | 224 { |
225 PostgreSQLStatement u(*pg, "SELECT COUNT(*) FROM Test", true); | 225 PostgreSQLStatement u(*pg, "SELECT COUNT(*) FROM Test"); |
226 PostgreSQLResult r(u); | 226 PostgreSQLResult r(u); |
227 ASSERT_EQ(1, r.GetInteger64(0)); // Just "1" because of implicit rollback | 227 ASSERT_EQ(1, r.GetInteger64(0)); // Just "1" because of implicit rollback |
228 } | 228 } |
229 | 229 |
230 { | 230 { |
235 s.BindInteger(0, 44); | 235 s.BindInteger(0, 44); |
236 s.BindInteger(1, 4444); | 236 s.BindInteger(1, 4444); |
237 s.Run(); | 237 s.Run(); |
238 | 238 |
239 { | 239 { |
240 PostgreSQLStatement u(*pg, "SELECT COUNT(*) FROM Test", true); | 240 PostgreSQLStatement u(*pg, "SELECT COUNT(*) FROM Test"); |
241 PostgreSQLResult r(u); | 241 PostgreSQLResult r(u); |
242 ASSERT_EQ(3, r.GetInteger64(0)); | 242 ASSERT_EQ(3, r.GetInteger64(0)); |
243 | 243 |
244 t.Commit(); | 244 t.Commit(); |
245 ASSERT_THROW(t.Rollback(), Orthanc::OrthancException); | 245 ASSERT_THROW(t.Rollback(), Orthanc::OrthancException); |
246 ASSERT_THROW(t.Commit(), Orthanc::OrthancException); | 246 ASSERT_THROW(t.Commit(), Orthanc::OrthancException); |
247 } | 247 } |
248 } | 248 } |
249 | 249 |
250 { | 250 { |
251 PostgreSQLStatement u(*pg, "SELECT COUNT(*) FROM Test", true); | 251 PostgreSQLStatement u(*pg, "SELECT COUNT(*) FROM Test"); |
252 PostgreSQLResult r(u); | 252 PostgreSQLResult r(u); |
253 ASSERT_EQ(3, r.GetInteger64(0)); | 253 ASSERT_EQ(3, r.GetInteger64(0)); |
254 } | 254 } |
255 } | 255 } |
256 } | 256 } |
268 | 268 |
269 // Automatically remove the large objects associated with the table | 269 // Automatically remove the large objects associated with the table |
270 pg->Execute("CREATE RULE TestDelete AS ON DELETE TO Test DO SELECT lo_unlink(old.value);"); | 270 pg->Execute("CREATE RULE TestDelete AS ON DELETE TO Test DO SELECT lo_unlink(old.value);"); |
271 | 271 |
272 { | 272 { |
273 PostgreSQLStatement s(*pg, "INSERT INTO Test VALUES ($1,$2)", false); | 273 PostgreSQLStatement s(*pg, "INSERT INTO Test VALUES ($1,$2)"); |
274 s.DeclareInputString(0); | 274 s.DeclareInputString(0); |
275 s.DeclareInputLargeObject(1); | 275 s.DeclareInputLargeObject(1); |
276 | 276 |
277 for (int i = 0; i < 10; i++) | 277 for (int i = 0; i < 10; i++) |
278 { | 278 { |
296 | 296 |
297 ASSERT_EQ(10, CountLargeObjects(*pg)); | 297 ASSERT_EQ(10, CountLargeObjects(*pg)); |
298 | 298 |
299 { | 299 { |
300 PostgreSQLTransaction t(*pg); | 300 PostgreSQLTransaction t(*pg); |
301 PostgreSQLStatement s(*pg, "SELECT * FROM Test ORDER BY name DESC", true); | 301 PostgreSQLStatement s(*pg, "SELECT * FROM Test ORDER BY name DESC"); |
302 PostgreSQLResult r(s); | 302 PostgreSQLResult r(s); |
303 | 303 |
304 ASSERT_FALSE(r.IsDone()); | 304 ASSERT_FALSE(r.IsDone()); |
305 | 305 |
306 ASSERT_FALSE(r.IsNull(0)); | 306 ASSERT_FALSE(r.IsNull(0)); |
317 } | 317 } |
318 | 318 |
319 | 319 |
320 { | 320 { |
321 PostgreSQLTransaction t(*pg); | 321 PostgreSQLTransaction t(*pg); |
322 PostgreSQLStatement s(*pg, "DELETE FROM Test WHERE name='Index 9'", false); | 322 PostgreSQLStatement s(*pg, "DELETE FROM Test WHERE name='Index 9'"); |
323 s.Run(); | 323 s.Run(); |
324 t.Commit(); | 324 t.Commit(); |
325 } | 325 } |
326 | 326 |
327 | 327 |
328 { | 328 { |
329 // Count the number of items in the DB | 329 // Count the number of items in the DB |
330 PostgreSQLTransaction t(*pg); | 330 PostgreSQLTransaction t(*pg); |
331 PostgreSQLStatement s(*pg, "SELECT COUNT(*) FROM Test", true); | 331 PostgreSQLStatement s(*pg, "SELECT COUNT(*) FROM Test"); |
332 PostgreSQLResult r(s); | 332 PostgreSQLResult r(s); |
333 ASSERT_EQ(9, r.GetInteger64(0)); | 333 ASSERT_EQ(9, r.GetInteger64(0)); |
334 } | 334 } |
335 | 335 |
336 ASSERT_EQ(9, CountLargeObjects(*pg)); | 336 ASSERT_EQ(9, CountLargeObjects(*pg)); |