1 | // |
2 | // MySQLTest.cpp |
3 | // |
4 | // Copyright (c) 2008, Applied Informatics Software Engineering GmbH. |
5 | // and Contributors. |
6 | // |
7 | // SPDX-License-Identifier: BSL-1.0 |
8 | // |
9 | |
10 | |
11 | #include "MySQLTest.h" |
12 | #include "Poco/CppUnit/TestCaller.h" |
13 | #include "Poco/CppUnit/TestSuite.h" |
14 | #include "Poco/Environment.h" |
15 | #include "Poco/String.h" |
16 | #include "Poco/Format.h" |
17 | #include "Poco/Tuple.h" |
18 | #include "Poco/NamedTuple.h" |
19 | #include "Poco/Exception.h" |
20 | #include "Poco/SQL/LOB.h" |
21 | #include "Poco/SQL/StatementImpl.h" |
22 | #include "Poco/SQL/MySQL/Connector.h" |
23 | #include "Poco/SQL/MySQL/Utility.h" |
24 | #include "Poco/SQL/MySQL/MySQLException.h" |
25 | #include "Poco/Nullable.h" |
26 | #include "Poco/SQL/SQLException.h" |
27 | #include <iostream> |
28 | |
29 | using namespace Poco::SQL; |
30 | using namespace Poco::SQL::Keywords; |
31 | using Poco::SQL::MySQL::ConnectionException; |
32 | using Poco::SQL::MySQL::Utility; |
33 | using Poco::SQL::MySQL::StatementException; |
34 | using Poco::format; |
35 | using Poco::Environment; |
36 | using Poco::NotFoundException; |
37 | using Poco::Int32; |
38 | using Poco::Nullable; |
39 | using Poco::Tuple; |
40 | using Poco::NamedTuple; |
41 | |
42 | Poco::SharedPtr<Poco::SQL::Session> MySQLTest::_pSession = 0; |
43 | Poco::SharedPtr<SQLExecutor> MySQLTest::_pExecutor = 0; |
44 | |
45 | // |
46 | // Parameters for barebone-test |
47 | |
48 | std::string MySQLTest::getHost() |
49 | { |
50 | if (Environment::has("MYSQL_HOST" )) |
51 | return Environment::get("MYSQL_HOST" ); |
52 | else |
53 | return "127.0.0.1" ; //do not change to "localhost"! |
54 | } |
55 | |
56 | |
57 | std::string MySQLTest::getPort() |
58 | { |
59 | return "3306" ; |
60 | } |
61 | |
62 | |
63 | std::string MySQLTest::getUser() |
64 | { |
65 | return "root" ; |
66 | } |
67 | |
68 | |
69 | std::string MySQLTest::getPass() |
70 | { |
71 | if (Environment::has("APPVEYOR" )) |
72 | return "Password12!" ; |
73 | else if (Environment::has("TRAVIS" )) |
74 | return "" ; |
75 | else |
76 | return "poco" ; |
77 | } |
78 | |
79 | |
80 | std::string MySQLTest::getBase() |
81 | { |
82 | return "pocotestdb" ; |
83 | } |
84 | |
85 | |
86 | std::string MySQLTest::_dbConnString; |
87 | |
88 | |
89 | // |
90 | // Connection string |
91 | // |
92 | |
93 | MySQLTest::MySQLTest(const std::string& name): |
94 | CppUnit::TestCase(name) |
95 | { |
96 | MySQL::Connector::registerConnector(); |
97 | } |
98 | |
99 | |
100 | MySQLTest::~MySQLTest() |
101 | { |
102 | MySQL::Connector::unregisterConnector(); |
103 | } |
104 | |
105 | |
106 | void MySQLTest::dbInfo(Session& session) |
107 | { |
108 | std::cout << "Server Info: " << Utility::serverInfo(session) << std::endl; |
109 | std::cout << "Server Version: " << Utility::serverVersion(session) << std::endl; |
110 | std::cout << "Host Info: " << Utility::hostInfo(session) << std::endl; |
111 | } |
112 | |
113 | |
114 | void MySQLTest::connectNoDB() |
115 | { |
116 | std::string dbConnString; |
117 | dbConnString = "host=" + getHost(); |
118 | dbConnString += ";user=" + getUser(); |
119 | dbConnString += ";password=" + getPass(); |
120 | dbConnString += ";compress=true;auto-reconnect=true;protocol=tcp" ; |
121 | |
122 | try |
123 | { |
124 | Session session(MySQL::Connector::KEY, dbConnString); |
125 | std::cout << "Connected to [" << "MySQL" << "] without database." << std::endl; |
126 | dbInfo(session); |
127 | session << "CREATE DATABASE IF NOT EXISTS " + getBase() + ";" , now; |
128 | std::cout << "Disconnecting ..." << std::endl; |
129 | session.close(); |
130 | std::cout << "Disconnected." << std::endl; |
131 | } |
132 | catch (ConnectionFailedException& ex) |
133 | { |
134 | std::cout << ex.displayText() << std::endl; |
135 | } |
136 | } |
137 | |
138 | |
139 | void MySQLTest::testBareboneMySQL() |
140 | { |
141 | if (!_pSession) fail ("Test not available." ); |
142 | |
143 | std::string tableCreateString = "CREATE TABLE Test " |
144 | "(First VARCHAR(30)," |
145 | "Second VARCHAR(30)," |
146 | "Third VARBINARY(30)," |
147 | "Fourth INTEGER," |
148 | "Fifth FLOAT)" ; |
149 | |
150 | _pExecutor->bareboneMySQLTest(getHost(), getUser(), getPass(), getBase(), getPort(), tableCreateString.c_str()); |
151 | } |
152 | |
153 | |
154 | void MySQLTest::testSimpleAccess() |
155 | { |
156 | if (!_pSession) fail ("Test not available." ); |
157 | |
158 | recreatePersonTable(); |
159 | _pExecutor->simpleAccess(); |
160 | } |
161 | |
162 | |
163 | void MySQLTest::testComplexType() |
164 | { |
165 | if (!_pSession) fail ("Test not available." ); |
166 | |
167 | recreatePersonTable(); |
168 | _pExecutor->complexType(); |
169 | } |
170 | |
171 | |
172 | void MySQLTest::testSimpleAccessVector() |
173 | { |
174 | if (!_pSession) fail ("Test not available." ); |
175 | |
176 | recreatePersonTable(); |
177 | _pExecutor->simpleAccessVector(); |
178 | } |
179 | |
180 | |
181 | void MySQLTest::testComplexTypeVector() |
182 | { |
183 | if (!_pSession) fail ("Test not available." ); |
184 | |
185 | recreatePersonTable(); |
186 | _pExecutor->complexTypeVector(); |
187 | } |
188 | |
189 | |
190 | void MySQLTest::testInsertVector() |
191 | { |
192 | if (!_pSession) fail ("Test not available." ); |
193 | |
194 | recreateStringsTable(); |
195 | _pExecutor->insertVector(); |
196 | } |
197 | |
198 | |
199 | void MySQLTest::testInsertEmptyVector() |
200 | { |
201 | if (!_pSession) fail ("Test not available." ); |
202 | |
203 | recreateStringsTable(); |
204 | _pExecutor->insertEmptyVector(); |
205 | } |
206 | |
207 | |
208 | void MySQLTest::testInsertSingleBulk() |
209 | { |
210 | if (!_pSession) fail ("Test not available." ); |
211 | |
212 | recreateStringsTable(); |
213 | _pExecutor->insertSingleBulk(); |
214 | } |
215 | |
216 | |
217 | void MySQLTest::testInsertSingleBulkVec() |
218 | { |
219 | if (!_pSession) fail ("Test not available." ); |
220 | |
221 | recreateStringsTable(); |
222 | _pExecutor->insertSingleBulkVec(); |
223 | } |
224 | |
225 | |
226 | void MySQLTest::testLimit() |
227 | { |
228 | if (!_pSession) fail ("Test not available." ); |
229 | |
230 | recreateStringsTable(); |
231 | _pExecutor->limits(); |
232 | } |
233 | |
234 | |
235 | void MySQLTest::testLimitZero() |
236 | { |
237 | if (!_pSession) fail ("Test not available." ); |
238 | |
239 | recreateStringsTable(); |
240 | _pExecutor->limitZero(); |
241 | } |
242 | |
243 | |
244 | void MySQLTest::testLimitOnce() |
245 | { |
246 | if (!_pSession) fail ("Test not available." ); |
247 | |
248 | recreateStringsTable(); |
249 | _pExecutor->limitOnce(); |
250 | } |
251 | |
252 | |
253 | void MySQLTest::testLimitPrepare() |
254 | { |
255 | if (!_pSession) fail ("Test not available." ); |
256 | |
257 | recreateStringsTable(); |
258 | _pExecutor->limitPrepare(); |
259 | } |
260 | |
261 | |
262 | |
263 | void MySQLTest::testPrepare() |
264 | { |
265 | if (!_pSession) fail ("Test not available." ); |
266 | |
267 | recreateStringsTable(); |
268 | _pExecutor->prepare(); |
269 | } |
270 | |
271 | |
272 | void MySQLTest::testSetSimple() |
273 | { |
274 | if (!_pSession) fail ("Test not available." ); |
275 | |
276 | recreatePersonTable(); |
277 | _pExecutor->setSimple(); |
278 | } |
279 | |
280 | |
281 | void MySQLTest::testSetComplex() |
282 | { |
283 | if (!_pSession) fail ("Test not available." ); |
284 | |
285 | recreatePersonTable(); |
286 | _pExecutor->setComplex(); |
287 | } |
288 | |
289 | |
290 | void MySQLTest::testSetComplexUnique() |
291 | { |
292 | if (!_pSession) fail ("Test not available." ); |
293 | |
294 | recreatePersonTable(); |
295 | _pExecutor->setComplexUnique(); |
296 | } |
297 | |
298 | void MySQLTest::testMultiSetSimple() |
299 | { |
300 | if (!_pSession) fail ("Test not available." ); |
301 | |
302 | recreatePersonTable(); |
303 | _pExecutor->multiSetSimple(); |
304 | } |
305 | |
306 | |
307 | void MySQLTest::testMultiSetComplex() |
308 | { |
309 | if (!_pSession) fail ("Test not available." ); |
310 | |
311 | recreatePersonTable(); |
312 | _pExecutor->multiSetComplex(); |
313 | } |
314 | |
315 | |
316 | void MySQLTest::testMapComplex() |
317 | { |
318 | if (!_pSession) fail ("Test not available." ); |
319 | |
320 | recreatePersonTable(); |
321 | _pExecutor->mapComplex(); |
322 | } |
323 | |
324 | |
325 | void MySQLTest::testMapComplexUnique() |
326 | { |
327 | if (!_pSession) fail ("Test not available." ); |
328 | |
329 | recreatePersonTable(); |
330 | _pExecutor->mapComplexUnique(); |
331 | } |
332 | |
333 | |
334 | void MySQLTest::testMultiMapComplex() |
335 | { |
336 | if (!_pSession) fail ("Test not available." ); |
337 | |
338 | recreatePersonTable(); |
339 | _pExecutor->multiMapComplex(); |
340 | } |
341 | |
342 | |
343 | void MySQLTest::testSelectIntoSingle() |
344 | { |
345 | if (!_pSession) fail ("Test not available." ); |
346 | |
347 | recreatePersonTable(); |
348 | _pExecutor->selectIntoSingle(); |
349 | } |
350 | |
351 | |
352 | void MySQLTest::testSelectIntoSingleStep() |
353 | { |
354 | if (!_pSession) fail ("Test not available." ); |
355 | |
356 | recreatePersonTable(); |
357 | _pExecutor->selectIntoSingleStep(); |
358 | } |
359 | |
360 | |
361 | void MySQLTest::testSelectIntoSingleFail() |
362 | { |
363 | if (!_pSession) fail ("Test not available." ); |
364 | |
365 | recreatePersonTable(); |
366 | _pExecutor->selectIntoSingleFail(); |
367 | } |
368 | |
369 | |
370 | void MySQLTest::testLowerLimitOk() |
371 | { |
372 | if (!_pSession) fail ("Test not available." ); |
373 | |
374 | recreatePersonTable(); |
375 | _pExecutor->lowerLimitOk(); |
376 | } |
377 | |
378 | |
379 | void MySQLTest::testSingleSelect() |
380 | { |
381 | if (!_pSession) fail ("Test not available." ); |
382 | |
383 | recreatePersonTable(); |
384 | _pExecutor->singleSelect(); |
385 | } |
386 | |
387 | |
388 | void MySQLTest::testLowerLimitFail() |
389 | { |
390 | if (!_pSession) fail ("Test not available." ); |
391 | |
392 | recreatePersonTable(); |
393 | _pExecutor->lowerLimitFail(); |
394 | } |
395 | |
396 | |
397 | void MySQLTest::testCombinedLimits() |
398 | { |
399 | if (!_pSession) fail ("Test not available." ); |
400 | |
401 | recreatePersonTable(); |
402 | _pExecutor->combinedLimits(); |
403 | } |
404 | |
405 | |
406 | |
407 | void MySQLTest::testRange() |
408 | { |
409 | if (!_pSession) fail ("Test not available." ); |
410 | |
411 | recreatePersonTable(); |
412 | _pExecutor->ranges(); |
413 | } |
414 | |
415 | |
416 | void MySQLTest::testCombinedIllegalLimits() |
417 | { |
418 | if (!_pSession) fail ("Test not available." ); |
419 | |
420 | recreatePersonTable(); |
421 | _pExecutor->combinedIllegalLimits(); |
422 | } |
423 | |
424 | |
425 | |
426 | void MySQLTest::testIllegalRange() |
427 | { |
428 | if (!_pSession) fail ("Test not available." ); |
429 | |
430 | recreatePersonTable(); |
431 | _pExecutor->illegalRange(); |
432 | } |
433 | |
434 | |
435 | void MySQLTest::testEmptyDB() |
436 | { |
437 | if (!_pSession) fail ("Test not available." ); |
438 | |
439 | recreatePersonTable(); |
440 | _pExecutor->emptyDB(); |
441 | } |
442 | |
443 | |
444 | void MySQLTest::testDateTime() |
445 | { |
446 | if (!_pSession) fail ("Test not available." ); |
447 | |
448 | recreatePersonDateTimeTable(); |
449 | _pExecutor->dateTime(); |
450 | recreatePersonDateTable(); |
451 | _pExecutor->date(); |
452 | recreatePersonTimeTable(); |
453 | _pExecutor->time(); |
454 | } |
455 | |
456 | |
457 | void MySQLTest::testBLOB() |
458 | { |
459 | if (!_pSession) fail ("Test not available." ); |
460 | |
461 | recreatePersonBLOBTable(); |
462 | _pExecutor->blob(); |
463 | |
464 | const std::size_t maxFldSize = 65534; |
465 | _pSession->setProperty("maxFieldSize" , Poco::Any(maxFldSize-1)); |
466 | recreatePersonBLOBTable(); |
467 | |
468 | try |
469 | { |
470 | _pExecutor->blob(maxFldSize); |
471 | fail ("must fail" ); |
472 | } |
473 | catch (SQLException&) |
474 | { |
475 | _pSession->setProperty("maxFieldSize" , Poco::Any(maxFldSize)); |
476 | } |
477 | |
478 | recreatePersonBLOBTable(); |
479 | _pExecutor->blob(maxFldSize); |
480 | |
481 | recreatePersonBLOBTable(); |
482 | |
483 | try |
484 | { |
485 | _pExecutor->blob(maxFldSize+1); |
486 | fail ("must fail" ); |
487 | } |
488 | catch (SQLException&) { } |
489 | } |
490 | |
491 | |
492 | void MySQLTest::testBLOBStmt() |
493 | { |
494 | if (!_pSession) fail ("Test not available." ); |
495 | |
496 | recreatePersonBLOBTable(); |
497 | _pExecutor->blobStmt(); |
498 | } |
499 | |
500 | |
501 | void MySQLTest::testLongText() |
502 | { |
503 | if (!_pSession) fail ("Test not available." ); |
504 | |
505 | recreatePersonLongTextTable(); |
506 | _pExecutor->longText(); |
507 | } |
508 | |
509 | void MySQLTest::testUnsignedInts() |
510 | { |
511 | if (!_pSession) fail ("Test not available." ); |
512 | |
513 | recreateUnsignedIntsTable(); |
514 | _pExecutor->unsignedInts(); |
515 | } |
516 | |
517 | |
518 | void MySQLTest::testFloat() |
519 | { |
520 | if (!_pSession) fail ("Test not available." ); |
521 | |
522 | recreateFloatsTable(); |
523 | _pExecutor->floats(); |
524 | } |
525 | |
526 | |
527 | void MySQLTest::testDouble() |
528 | { |
529 | if (!_pSession) fail ("Test not available." ); |
530 | |
531 | recreateFloatsTable(); |
532 | _pExecutor->doubles(); |
533 | } |
534 | |
535 | void MySQLTest::testAny() |
536 | { |
537 | if (!_pSession) fail ("Test not available." ); |
538 | |
539 | recreateAnyTable(); |
540 | _pExecutor->any(); |
541 | } |
542 | |
543 | void MySQLTest::testDynamicAny() |
544 | { |
545 | if (!_pSession) fail ("Test not available." ); |
546 | |
547 | recreateAnyTable(); |
548 | _pExecutor->dynamicAny(); |
549 | } |
550 | |
551 | |
552 | void MySQLTest::testTuple() |
553 | { |
554 | if (!_pSession) fail ("Test not available." ); |
555 | |
556 | recreateTuplesTable(); |
557 | _pExecutor->tuples(); |
558 | } |
559 | |
560 | |
561 | void MySQLTest::testTupleVector() |
562 | { |
563 | if (!_pSession) fail ("Test not available." ); |
564 | |
565 | recreateTuplesTable(); |
566 | _pExecutor->tupleVector(); |
567 | } |
568 | |
569 | |
570 | void MySQLTest::testStdTuple() |
571 | { |
572 | if (!_pSession) fail ("Test not available." ); |
573 | |
574 | recreateTuplesTable(); |
575 | _pExecutor->stdTuples(); |
576 | } |
577 | |
578 | |
579 | void MySQLTest::testStdTupleVector() |
580 | { |
581 | if (!_pSession) fail ("Test not available." ); |
582 | |
583 | recreateTuplesTable(); |
584 | _pExecutor->stdTupleVector(); |
585 | } |
586 | |
587 | |
588 | void MySQLTest::() |
589 | { |
590 | if (!_pSession) fail ("Test not available." ); |
591 | |
592 | recreateVectorsTable(); |
593 | _pExecutor->internalExtraction(); |
594 | } |
595 | |
596 | |
597 | void MySQLTest::testNull() |
598 | { |
599 | if (!_pSession) fail ("Test not available." ); |
600 | |
601 | recreateVectorsTable(); |
602 | _pExecutor->doNull(); |
603 | } |
604 | |
605 | |
606 | void MySQLTest::testSessionTransaction() |
607 | { |
608 | if (!_pSession) fail ("Test not available." ); |
609 | |
610 | recreatePersonTable(); |
611 | _pExecutor->sessionTransaction(_dbConnString); |
612 | } |
613 | |
614 | |
615 | void MySQLTest::testTransaction() |
616 | { |
617 | if (!_pSession) fail ("Test not available." ); |
618 | |
619 | recreatePersonTable(); |
620 | _pExecutor->transaction(_dbConnString); |
621 | } |
622 | |
623 | |
624 | void MySQLTest::testReconnect() |
625 | { |
626 | if (!_pSession) fail ("Test not available." ); |
627 | |
628 | recreatePersonTable(); |
629 | _pExecutor->reconnect(); |
630 | } |
631 | |
632 | |
633 | void MySQLTest::testNullableInt() |
634 | { |
635 | if (!_pSession) fail ("Test not available." ); |
636 | |
637 | recreateNullableIntTable(); |
638 | |
639 | Nullable<Int32> i1(1); |
640 | Nullable<Int32> i2; |
641 | |
642 | int id = 1; |
643 | *_pSession << "INSERT INTO NullableIntTest VALUES(?, ?)" , use(id), use(i1), now; |
644 | id = 2; |
645 | *_pSession << "INSERT INTO NullableIntTest VALUES(?, ?)" , use(id), use(i2), now; |
646 | id = 3; |
647 | i2 = 3; |
648 | *_pSession << "INSERT INTO NullableIntTest VALUES(?, ?)" , use(id), use(i2), now; |
649 | |
650 | int count = 0; |
651 | *_pSession << "SELECT COUNT(*) FROM NullableIntTest" , into(count), now; |
652 | assertTrue (count == 3); |
653 | |
654 | Nullable<Int32> ci1; |
655 | Nullable<Int32> ci2; |
656 | Nullable<Int32> ci3; |
657 | id = 1; |
658 | *_pSession << "SELECT Value FROM NullableIntTest WHERE Id = ?" , into(ci1), use(id), now; |
659 | assertTrue (ci1 == i1); |
660 | id = 2; |
661 | *_pSession << "SELECT Value FROM NullableIntTest WHERE Id = ?" , into(ci2), use(id), now; |
662 | assertTrue (ci2.isNull()); |
663 | assertTrue (!(0 == ci2)); |
664 | assertTrue (0 != ci2); |
665 | assertTrue (!(ci2 == 0)); |
666 | assertTrue (ci2 != 0); |
667 | ci2 = 10; |
668 | assertTrue (10 == ci2); |
669 | assertTrue (ci2 == 10); |
670 | assertTrue (!ci2.isNull()); |
671 | id = 3; |
672 | *_pSession << "SELECT Value FROM NullableIntTest WHERE Id = ?" , into(ci3), use(id), now; |
673 | assertTrue (!ci3.isNull()); |
674 | assertTrue (ci3 == 3); |
675 | assertTrue (3 == ci3); |
676 | } |
677 | |
678 | |
679 | void MySQLTest::testNullableString() |
680 | { |
681 | if (!_pSession) fail ("Test not available." ); |
682 | |
683 | recreateNullableStringTable(); |
684 | |
685 | Int32 id = 0; |
686 | Nullable<std::string> address("Address" ); |
687 | Nullable<Int32> age = 10; |
688 | *_pSession << "INSERT INTO NullableStringTest VALUES(?, ?, ?)" , use(id), use(address), use(age), now; |
689 | id++; |
690 | address = null; |
691 | age = null; |
692 | *_pSession << "INSERT INTO NullableStringTest VALUES(?, ?, ?)" , use(id), use(address), use(age), now; |
693 | |
694 | Nullable<std::string> resAddress; |
695 | Nullable<Int32> resAge; |
696 | *_pSession << "SELECT Address, Age FROM NullableStringTest WHERE Id = ?" , into(resAddress), into(resAge), use(id), now; |
697 | assertTrue (resAddress == address); |
698 | assertTrue (resAge == age); |
699 | assertTrue (resAddress.isNull()); |
700 | assertTrue (null == resAddress); |
701 | assertTrue (resAddress == null); |
702 | |
703 | resAddress = std::string("Test" ); |
704 | assertTrue (!resAddress.isNull()); |
705 | assertTrue (resAddress == std::string("Test" )); |
706 | assertTrue (std::string("Test" ) == resAddress); |
707 | assertTrue (null != resAddress); |
708 | assertTrue (resAddress != null); |
709 | } |
710 | |
711 | |
712 | void MySQLTest::testTupleWithNullable() |
713 | { |
714 | if (!_pSession) fail ("Test not available." ); |
715 | |
716 | recreateNullableStringTable(); |
717 | |
718 | typedef Poco::Tuple<Int32, Nullable<std::string>, Nullable<Int32> > Info; |
719 | |
720 | Info info(0, std::string("Address" ), 10); |
721 | *_pSession << "INSERT INTO NullableStringTest VALUES(?, ?, ?)" , use(info), now; |
722 | |
723 | info.set<0>(info.get<0>()++); |
724 | info.set<1>(null); |
725 | *_pSession << "INSERT INTO NullableStringTest VALUES(?, ?, ?)" , use(info), now; |
726 | |
727 | info.set<0>(info.get<0>()++); |
728 | info.set<1>(std::string("Address!" )); |
729 | info.set<2>(null); |
730 | *_pSession << "INSERT INTO NullableStringTest VALUES(?, ?, ?)" , use(info), now; |
731 | |
732 | std::vector<Info> infos; |
733 | infos.push_back(Info(10, std::string("A" ), 0)); |
734 | infos.push_back(Info(11, null, 12)); |
735 | infos.push_back(Info(12, std::string("B" ), null)); |
736 | |
737 | *_pSession << "INSERT INTO NullableStringTest VALUES(?, ?, ?)" , use(infos), now; |
738 | |
739 | std::vector<Info> result; |
740 | |
741 | *_pSession << "SELECT Id, Address, Age FROM NullableStringTest" , into(result), now; |
742 | |
743 | assertTrue (result.size() == 6); |
744 | assertTrue (result[0].get<1>() == std::string("Address" )); |
745 | assertTrue (result[0].get<2>() == 10); |
746 | |
747 | assertTrue (result[1].get<1>() == null); |
748 | assertTrue (result[1].get<2>() == 10); |
749 | |
750 | assertTrue (result[2].get<1>() == std::string("Address!" )); |
751 | assertTrue (result[2].get<2>() == null); |
752 | |
753 | assertTrue (result[3].get<1>() == std::string("A" )); |
754 | assertTrue (result[3].get<2>() == 0); |
755 | |
756 | assertTrue (result[4].get<1>() == null); |
757 | assertTrue (result[4].get<2>() == 12); |
758 | |
759 | assertTrue (result[5].get<1>() == std::string("B" )); |
760 | assertTrue (result[5].get<2>() == null); |
761 | |
762 | } |
763 | |
764 | |
765 | void MySQLTest::dropTable(const std::string& tableName) |
766 | { |
767 | try { *_pSession << format("DROP TABLE IF EXISTS %s" , tableName), now; } |
768 | catch(ConnectionException& ce){ std::cout << ce.displayText() << std::endl; fail ("dropTable()" ); } |
769 | catch(StatementException& se){ std::cout << se.displayText() << std::endl; fail ("dropTable()" ); } |
770 | } |
771 | |
772 | |
773 | void MySQLTest::recreatePersonTable() |
774 | { |
775 | dropTable("Person" ); |
776 | try { *_pSession << "CREATE TABLE Person (LastName VARCHAR(30), FirstName VARCHAR(30), Address VARCHAR(30), Age INTEGER)" , now; } |
777 | catch(ConnectionException& ce){ std::cout << ce.displayText() << std::endl; fail ("recreatePersonTable()" ); } |
778 | catch(StatementException& se){ std::cout << se.displayText() << std::endl; fail ("recreatePersonTable()" ); } |
779 | } |
780 | |
781 | |
782 | void MySQLTest::recreatePersonBLOBTable() |
783 | { |
784 | dropTable("Person" ); |
785 | try { *_pSession << "CREATE TABLE Person (LastName VARCHAR(30), FirstName VARCHAR(30), Address VARCHAR(30), Image BLOB)" , now; } |
786 | catch(ConnectionException& ce){ std::cout << ce.displayText() << std::endl; fail ("recreatePersonBLOBTable()" ); } |
787 | catch(StatementException& se){ std::cout << se.displayText() << std::endl; fail ("recreatePersonBLOBTable()" ); } |
788 | } |
789 | |
790 | void MySQLTest::recreatePersonLongTextTable() |
791 | { |
792 | dropTable("Person" ); |
793 | try { *_pSession << "CREATE TABLE Person (LastName VARCHAR(30), FirstName VARCHAR(30), Address VARCHAR(30), Info LONGTEXT)" , now; } |
794 | catch(ConnectionException& ce){ std::cout << ce.displayText() << std::endl; fail ("recreatePersonBLOBTable()" ); } |
795 | catch(StatementException& se){ std::cout << se.displayText() << std::endl; fail ("recreatePersonBLOBTable()" ); } |
796 | } |
797 | |
798 | void MySQLTest::recreatePersonDateTimeTable() |
799 | { |
800 | dropTable("Person" ); |
801 | try { *_pSession << "CREATE TABLE Person (LastName VARCHAR(30), FirstName VARCHAR(30), Address VARCHAR(30), Birthday DATETIME(6))" , now; } |
802 | catch(ConnectionException& ce){ std::cout << ce.displayText() << std::endl; fail ("recreatePersonDateTimeTable()" ); } |
803 | catch(StatementException& se){ std::cout << se.displayText() << std::endl; fail ("recreatePersonDateTimeTable()" ); } |
804 | } |
805 | |
806 | |
807 | void MySQLTest::recreatePersonDateTable() |
808 | { |
809 | dropTable("Person" ); |
810 | try { *_pSession << "CREATE TABLE Person (LastName VARCHAR(30), FirstName VARCHAR(30), Address VARCHAR(30), Birthday DATE)" , now; } |
811 | catch(ConnectionException& ce){ std::cout << ce.displayText() << std::endl; fail ("recreatePersonDateTable()" ); } |
812 | catch(StatementException& se){ std::cout << se.displayText() << std::endl; fail ("recreatePersonDateTable()" ); } |
813 | } |
814 | |
815 | |
816 | void MySQLTest::recreatePersonTimeTable() |
817 | { |
818 | dropTable("Person" ); |
819 | try { *_pSession << "CREATE TABLE Person (LastName VARCHAR(30), FirstName VARCHAR(30), Address VARCHAR(30), Birthday TIME)" , now; } |
820 | catch(ConnectionException& ce){ std::cout << ce.displayText() << std::endl; fail ("recreatePersonTimeTable()" ); } |
821 | catch(StatementException& se){ std::cout << se.displayText() << std::endl; fail ("recreatePersonTimeTable()" ); } |
822 | } |
823 | |
824 | |
825 | void MySQLTest::recreateIntsTable() |
826 | { |
827 | dropTable("Ints" ); |
828 | try { *_pSession << "CREATE TABLE Ints (str INTEGER)" , now; } |
829 | catch(ConnectionException& ce){ std::cout << ce.displayText() << std::endl; fail ("recreateIntsTable()" ); } |
830 | catch(StatementException& se){ std::cout << se.displayText() << std::endl; fail ("recreateIntsTable()" ); } |
831 | } |
832 | |
833 | |
834 | void MySQLTest::recreateStringsTable() |
835 | { |
836 | dropTable("Strings" ); |
837 | try { *_pSession << "CREATE TABLE Strings (str VARCHAR(30))" , now; } |
838 | catch(ConnectionException& ce){ std::cout << ce.displayText() << std::endl; fail ("recreateStringsTable()" ); } |
839 | catch(StatementException& se){ std::cout << se.displayText() << std::endl; fail ("recreateStringsTable()" ); } |
840 | } |
841 | |
842 | |
843 | void MySQLTest::recreateUnsignedIntsTable() |
844 | { |
845 | dropTable("Strings" ); |
846 | try { *_pSession << "CREATE TABLE Strings (str INTEGER UNSIGNED)" , now; } |
847 | catch(ConnectionException& ce){ std::cout << ce.displayText() << std::endl; fail ("recreateUnsignedIntegersTable()" ); } |
848 | catch(StatementException& se){ std::cout << se.displayText() << std::endl; fail ("recreateUnsignedIntegersTable()" ); } |
849 | } |
850 | |
851 | |
852 | void MySQLTest::recreateFloatsTable() |
853 | { |
854 | dropTable("Strings" ); |
855 | try { *_pSession << "CREATE TABLE Strings (str FLOAT)" , now; } |
856 | catch(ConnectionException& ce){ std::cout << ce.displayText() << std::endl; fail ("recreateFloatsTable()" ); } |
857 | catch(StatementException& se){ std::cout << se.displayText() << std::endl; fail ("recreateFloatsTable()" ); } |
858 | } |
859 | |
860 | |
861 | void MySQLTest::recreateTuplesTable() |
862 | { |
863 | dropTable("Tuples" ); |
864 | try { *_pSession << "CREATE TABLE Tuples " |
865 | "(i0 INTEGER, i1 INTEGER, i2 INTEGER, i3 INTEGER, i4 INTEGER, i5 INTEGER, i6 INTEGER, " |
866 | "i7 INTEGER, i8 INTEGER, i9 INTEGER, i10 INTEGER, i11 INTEGER, i12 INTEGER, i13 INTEGER," |
867 | "i14 INTEGER, i15 INTEGER, i16 INTEGER, i17 INTEGER, i18 INTEGER, i19 INTEGER)" , now; } |
868 | catch(ConnectionException& ce){ std::cout << ce.displayText() << std::endl; fail ("recreateTuplesTable()" ); } |
869 | catch(StatementException& se){ std::cout << se.displayText() << std::endl; fail ("recreateTuplesTable()" ); } |
870 | } |
871 | |
872 | |
873 | void MySQLTest::recreateNullableIntTable() |
874 | { |
875 | dropTable("NullableIntTest" ); |
876 | try { |
877 | *_pSession << "CREATE TABLE NullableIntTest (Id INTEGER(10), Value INTEGER(10))" , now; |
878 | } |
879 | catch(ConnectionException& ce){ std::cout << ce.displayText() << std::endl; fail ("recreateNullableIntTable()" ); } |
880 | catch(StatementException& se){ std::cout << se.displayText() << std::endl; fail ("recreateNullableIntTable()" ); } |
881 | } |
882 | |
883 | |
884 | void MySQLTest::recreateNullableStringTable() |
885 | { |
886 | dropTable("NullableStringTest" ); |
887 | try { |
888 | *_pSession << "CREATE TABLE NullableStringTest (Id INTEGER(10), Address VARCHAR(30), Age INTEGER(10))" , now; |
889 | } |
890 | catch(ConnectionException& ce){ std::cout << ce.displayText() << std::endl; fail ("recreateNullableStringTable()" ); } |
891 | catch(StatementException& se){ std::cout << se.displayText() << std::endl; fail ("recreateNullableStringTable()" ); } |
892 | } |
893 | |
894 | void MySQLTest::recreateAnyTable() |
895 | { |
896 | dropTable("Anys" ); |
897 | try { |
898 | *_pSession << "CREATE TABLE Anys (int_8 TINYINT, int_16 SMALLINT, int_32 MEDIUMINT, int_64 BIGINT, flt FLOAT, dbl DOUBLE, " |
899 | "str0 VARCHAR(255), str1 TEXT, date0 DATE, time0 TIME, date_time0 DATETIME(6), empty INTEGER)" , now; |
900 | } |
901 | catch(ConnectionException& ce){ std::cout << ce.displayText() << std::endl; fail ("recreateAnyTable()" ); } |
902 | catch(StatementException& se){ std::cout << se.displayText() << std::endl; fail ("recreateAnyTable()" ); } |
903 | } |
904 | |
905 | |
906 | void MySQLTest::recreateVectorsTable() |
907 | { |
908 | dropTable("Vectors" ); |
909 | try { *_pSession << "CREATE TABLE Vectors (i0 INTEGER, flt0 FLOAT, str0 VARCHAR(30))" , now; } |
910 | catch(ConnectionException& ce){ std::cout << ce.displayText() << std::endl; fail ("recreateVectorsTable()" ); } |
911 | catch(StatementException& se){ std::cout << se.displayText() << std::endl; fail ("recreateVectorsTable()" ); } |
912 | } |
913 | |
914 | |
915 | void MySQLTest::setUp() |
916 | { |
917 | } |
918 | |
919 | |
920 | void MySQLTest::tearDown() |
921 | { |
922 | dropTable("Person" ); |
923 | dropTable("Strings" ); |
924 | } |
925 | |
926 | |
927 | CppUnit::Test* MySQLTest::suite() |
928 | { |
929 | MySQL::Connector::registerConnector(); |
930 | |
931 | _dbConnString = "host=" + getHost(); |
932 | _dbConnString += ";user=" + getUser(); |
933 | _dbConnString += ";password=" + getPass(); |
934 | _dbConnString += ";db=" + getBase(); |
935 | _dbConnString += ";compress=true" ; |
936 | _dbConnString += ";auto-reconnect=true" ; |
937 | _dbConnString += ";secure-auth=true" ; |
938 | _dbConnString += ";protocol=tcp" ; |
939 | |
940 | try |
941 | { |
942 | _pSession = new Session(MySQL::Connector::KEY, _dbConnString); |
943 | } |
944 | catch (ConnectionFailedException& ex) |
945 | { |
946 | std::cout << ex.displayText() << std::endl; |
947 | std::cout << "Trying to connect without DB and create one ..." << std::endl; |
948 | connectNoDB(); |
949 | try |
950 | { |
951 | _pSession = new Session(MySQL::Connector::KEY, _dbConnString); |
952 | } |
953 | catch (ConnectionFailedException& ex) |
954 | { |
955 | std::cout << ex.displayText() << std::endl; |
956 | return 0; |
957 | } |
958 | } |
959 | |
960 | std::cout << "*** Connected to [" << "MySQL" << "] test database." << std::endl; |
961 | dbInfo(*_pSession); |
962 | |
963 | _pExecutor = new SQLExecutor("MySQL SQL Executor" , _pSession); |
964 | |
965 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("MySQLTest" ); |
966 | |
967 | CppUnit_addTest(pSuite, MySQLTest, testBareboneMySQL); |
968 | CppUnit_addTest(pSuite, MySQLTest, testSimpleAccess); |
969 | CppUnit_addTest(pSuite, MySQLTest, testComplexType); |
970 | CppUnit_addTest(pSuite, MySQLTest, testSimpleAccessVector); |
971 | CppUnit_addTest(pSuite, MySQLTest, testComplexTypeVector); |
972 | CppUnit_addTest(pSuite, MySQLTest, testInsertVector); |
973 | CppUnit_addTest(pSuite, MySQLTest, testInsertEmptyVector); |
974 | CppUnit_addTest(pSuite, MySQLTest, testInsertSingleBulk); |
975 | CppUnit_addTest(pSuite, MySQLTest, testInsertSingleBulkVec); |
976 | CppUnit_addTest(pSuite, MySQLTest, testLimit); |
977 | CppUnit_addTest(pSuite, MySQLTest, testLimitOnce); |
978 | CppUnit_addTest(pSuite, MySQLTest, testLimitPrepare); |
979 | CppUnit_addTest(pSuite, MySQLTest, testLimitZero); |
980 | CppUnit_addTest(pSuite, MySQLTest, testPrepare); |
981 | CppUnit_addTest(pSuite, MySQLTest, testSetSimple); |
982 | CppUnit_addTest(pSuite, MySQLTest, testSetComplex); |
983 | CppUnit_addTest(pSuite, MySQLTest, testSetComplexUnique); |
984 | CppUnit_addTest(pSuite, MySQLTest, testMultiSetSimple); |
985 | CppUnit_addTest(pSuite, MySQLTest, testMultiSetComplex); |
986 | CppUnit_addTest(pSuite, MySQLTest, testMapComplex); |
987 | CppUnit_addTest(pSuite, MySQLTest, testMapComplexUnique); |
988 | CppUnit_addTest(pSuite, MySQLTest, testMultiMapComplex); |
989 | CppUnit_addTest(pSuite, MySQLTest, testSelectIntoSingle); |
990 | CppUnit_addTest(pSuite, MySQLTest, testSelectIntoSingleStep); |
991 | CppUnit_addTest(pSuite, MySQLTest, testSelectIntoSingleFail); |
992 | CppUnit_addTest(pSuite, MySQLTest, testLowerLimitOk); |
993 | CppUnit_addTest(pSuite, MySQLTest, testLowerLimitFail); |
994 | CppUnit_addTest(pSuite, MySQLTest, testCombinedLimits); |
995 | CppUnit_addTest(pSuite, MySQLTest, testCombinedIllegalLimits); |
996 | CppUnit_addTest(pSuite, MySQLTest, testRange); |
997 | CppUnit_addTest(pSuite, MySQLTest, testIllegalRange); |
998 | CppUnit_addTest(pSuite, MySQLTest, testSingleSelect); |
999 | CppUnit_addTest(pSuite, MySQLTest, testEmptyDB); |
1000 | CppUnit_addTest(pSuite, MySQLTest, testDateTime); |
1001 | //CppUnit_addTest(pSuite, MySQLTest, testBLOB); |
1002 | CppUnit_addTest(pSuite, MySQLTest, testBLOBStmt); |
1003 | CppUnit_addTest(pSuite, MySQLTest, testLongText); |
1004 | CppUnit_addTest(pSuite, MySQLTest, testUnsignedInts); |
1005 | CppUnit_addTest(pSuite, MySQLTest, testFloat); |
1006 | CppUnit_addTest(pSuite, MySQLTest, testDouble); |
1007 | CppUnit_addTest(pSuite, MySQLTest, testAny); |
1008 | CppUnit_addTest(pSuite, MySQLTest, testDynamicAny); |
1009 | CppUnit_addTest(pSuite, MySQLTest, testTuple); |
1010 | CppUnit_addTest(pSuite, MySQLTest, testTupleVector); |
1011 | CppUnit_addTest(pSuite, MySQLTest, testStdTuple); |
1012 | CppUnit_addTest(pSuite, MySQLTest, testStdTupleVector); |
1013 | CppUnit_addTest(pSuite, MySQLTest, testInternalExtraction); |
1014 | CppUnit_addTest(pSuite, MySQLTest, testNull); |
1015 | CppUnit_addTest(pSuite, MySQLTest, testNullableInt); |
1016 | CppUnit_addTest(pSuite, MySQLTest, testNullableString); |
1017 | CppUnit_addTest(pSuite, MySQLTest, testTupleWithNullable); |
1018 | CppUnit_addTest(pSuite, MySQLTest, testSessionTransaction); |
1019 | CppUnit_addTest(pSuite, MySQLTest, testTransaction); |
1020 | CppUnit_addTest(pSuite, MySQLTest, testReconnect); |
1021 | |
1022 | return pSuite; |
1023 | } |
1024 | |