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.
16 #include <dpl/test/test_runner.h>
17 #include <dpl/foreach.h>
18 #include <dpl/db/thread_database_support.h>
19 #include <generator_dpl_orm_test.h>
22 const char* PATH_DB = "/opt/share/wrt/wrt-commons/tests/db/dpl_orm_test.db";
26 #define TEST_REPETITION 16
32 SmartAttach(bool autoattach = true) :
34 DPL::DB::SqlConnection::Flag::UseLucene),
35 m_autoattach(autoattach)
38 m_interface.AttachToThread(DPL::DB::SqlConnection::Flag::RW);
45 m_interface.DetachFromThread();
49 DPL::DB::ThreadDatabaseSupport* get()
55 DPL::DB::ThreadDatabaseSupport m_interface;
59 template<typename ContainerType1, typename ContainerType2>
60 bool ContainerContentsEqual(const ContainerType1& container1,
61 const ContainerType2& container2)
63 using namespace DPL::DB::ORM::dpl_orm_test::TestTableInsert;
64 typedef std::set<typename ContainerType1::value_type> Set1;
65 typedef std::set<typename ContainerType2::value_type> Set2;
66 Set1 set1(container1.begin(), container1.end());
67 Set2 set2(container2.begin(), container2.end());
69 for (typename Set1::iterator it = set1.begin();
73 LogDebug("Set1 element: " << *it);
76 for (typename Set2::iterator it = set2.begin(); it != set2.end(); it++) {
77 LogDebug("Set2 element: " << *it);
84 std::list<T> makeList(const T& a, const T& b)
94 RUNNER_TEST_GROUP_INIT(DPL)
97 Name: ORM_SelectSingleValue
98 Description: tests quering single value of single row from database
99 Expected: Values should match those which were prepared
101 RUNNER_TEST(ORM_SelectSingleValue)
103 SmartAttach interface;
104 using namespace DPL::DB::ORM;
105 using namespace DPL::DB::ORM::dpl_orm_test;
106 //Getting each column
108 TestTable::Select select(interface.get());
109 select.Where(Equals<TestTable::ColumnInt>(8));
111 RUNNER_ASSERT_MSG((result =
112 *select.GetSingleValue<TestTable::ColumnOptInt>())
117 TestTable::Select select(interface.get());
118 select.Where(Equals<TestTable::ColumnInt>(8));
120 RUNNER_ASSERT_MSG((result =
121 *select.GetSingleValue<TestTable::ColumnOptText>(
126 TestTable::Select select(interface.get());
127 select.Where(Equals<TestTable::ColumnInt>(8));
129 RUNNER_ASSERT_MSG((result = select.GetSingleValue<TestTable::ColumnInt>(
130 )) == 8, "Got " << result);
133 TestTable::Select select(interface.get());
134 select.Where(Equals<TestTable::ColumnInt>(8));
136 RUNNER_ASSERT_MSG((result = select.GetSingleValue<TestTable::ColumnInt2>(
137 )) == 9, "Got " << result);
140 TestTable::Select select(interface.get());
141 select.Where(Equals<TestTable::ColumnInt>(8));
143 RUNNER_ASSERT_MSG((result = select.GetSingleValue<TestTable::ColumnText>(
144 )) == L"ten", "Got " << result);
147 //Where on each column
149 TestTable::Select select(interface.get());
150 select.Where(Equals<TestTable::ColumnOptInt>(6));
152 RUNNER_ASSERT_MSG((result =
153 *select.GetSingleValue<TestTable::ColumnOptInt>())
158 TestTable::Select select(interface.get());
159 select.Where(Equals<TestTable::ColumnOptText>(DPL::String(L"seven")));
161 RUNNER_ASSERT_MSG((result =
162 *select.GetSingleValue<TestTable::ColumnOptText>(
167 TestTable::Select select(interface.get());
168 select.Where(Equals<TestTable::ColumnInt>(8));
170 RUNNER_ASSERT_MSG((result = select.GetSingleValue<TestTable::ColumnInt>(
171 )) == 8, "Got " << result);
174 TestTable::Select select(interface.get());
175 select.Where(Equals<TestTable::ColumnInt2>(9));
177 RUNNER_ASSERT_MSG((result = select.GetSingleValue<TestTable::ColumnInt2>(
178 )) == 9, "Got " << result);
181 TestTable::Select select(interface.get());
182 select.Where(Equals<TestTable::ColumnText>(L"ten"));
184 RUNNER_ASSERT_MSG((result = select.GetSingleValue<TestTable::ColumnText>(
185 )) == L"ten", "Got " << result);
190 Name: ORM_SelectSingleRow
191 Description: tests quering single row from database
192 Expected: Values should match those which were prepared
194 RUNNER_TEST(ORM_SelectSingleRow)
196 SmartAttach interface;
197 using namespace DPL::DB::ORM;
198 using namespace DPL::DB::ORM::dpl_orm_test;
200 TestTable::Select select(interface.get());
201 select.Where(Equals<TestTable::ColumnInt>(3));
202 TestTable::Row result = select.GetSingleRow();
203 TestTable::Row expected;
204 expected.Set_ColumnOptInt(1);
205 expected.Set_ColumnOptText(DPL::String(L"two"));
206 expected.Set_ColumnInt(3);
207 expected.Set_ColumnInt2(4);
208 expected.Set_ColumnText(L"five");
209 RUNNER_ASSERT_MSG(result == expected, "Got " << result);
213 TestTable::Select select(interface.get());
214 select.Where(Equals<TestTable::ColumnOptText>(DPL::String(L"seven")));
215 TestTable::Row result = select.GetSingleRow();
216 TestTable::Row expected;
217 expected.Set_ColumnOptInt(6);
218 expected.Set_ColumnOptText(DPL::String(L"seven"));
219 expected.Set_ColumnInt(8);
220 expected.Set_ColumnInt2(9);
221 expected.Set_ColumnText(L"ten");
222 RUNNER_ASSERT_MSG(result == expected, "Got " << result);
227 Name: ORM_SelectRowList
228 Description: tests quering multiple row from database
229 Expected: Values should match those which were prepared
231 RUNNER_TEST(ORM_SelectRowList)
233 SmartAttach interface;
234 using namespace DPL::DB::ORM;
235 using namespace DPL::DB::ORM::dpl_orm_test;
237 TestTable::Select select(interface.get());
238 select.Where(Equals<TestTable::ColumnInt>(3));
239 std::list<TestTable::Row> result = select.GetRowList();
240 RUNNER_ASSERT_MSG(result.size() == 1, "Got " << result.size());
242 TestTable::Row expected;
243 expected.Set_ColumnOptInt(1);
244 expected.Set_ColumnOptText(DPL::String(L"two"));
245 expected.Set_ColumnInt(3);
246 expected.Set_ColumnInt2(4);
247 expected.Set_ColumnText(L"five");
248 RUNNER_ASSERT_MSG(*(result.begin()) == expected, "Got " <<
253 TestTable::Select select(interface.get());
254 select.Where(Equals<TestTable::ColumnOptText>(DPL::String(L"seven")));
255 std::list<TestTable::Row> result = select.GetRowList();
256 RUNNER_ASSERT_MSG(result.size() == 1, "Got " << result.size());
258 TestTable::Row expected;
259 expected.Set_ColumnOptInt(6);
260 expected.Set_ColumnOptText(DPL::String(L"seven"));
261 expected.Set_ColumnInt(8);
262 expected.Set_ColumnInt2(9);
263 expected.Set_ColumnText(L"ten");
264 RUNNER_ASSERT_MSG(*(result.begin()) == expected, "Got " <<
269 TestTable::Select select(interface.get());
270 select.Where(Equals<TestTable::ColumnInt>(99));
271 std::list<TestTable::Row> result = select.GetRowList();
273 TestTable::Row expected1;
274 expected1.Set_ColumnInt(99);
275 expected1.Set_ColumnInt2(11);
276 expected1.Set_ColumnText(L"twelve");
278 TestTable::Row expected2;
279 expected2.Set_ColumnInt(99);
280 expected2.Set_ColumnInt2(13);
281 expected2.Set_ColumnText(L"fourteen");
283 RUNNER_ASSERT(ContainerContentsEqual(makeList(expected1,
284 expected2), result));
289 Name: ORM_SelectValueList
290 Description: tests quering single column from multiple row from database
291 Expected: Values should match those which were prepared
293 RUNNER_TEST(ORM_SelectValueList)
295 SmartAttach interface;
296 using namespace DPL::DB::ORM;
297 using namespace DPL::DB::ORM::dpl_orm_test;
298 //Getting each column
300 TestTable::Select select(interface.get());
301 select.Where(Is<TestTable::ColumnOptInt>(DPL::Optional<int>::Null));
302 RUNNER_ASSERT(ContainerContentsEqual(select.GetValueList<TestTable::
307 TestTable::Select select(interface.get());
308 select.Where(Is<TestTable::ColumnOptInt>(DPL::Optional<int>::Null));
309 RUNNER_ASSERT(ContainerContentsEqual(select.GetValueList<TestTable::
314 TestTable::Select select(interface.get());
315 select.Where(Is<TestTable::ColumnOptInt>(DPL::Optional<int>::Null));
316 RUNNER_ASSERT(ContainerContentsEqual(select.GetValueList<TestTable::
318 makeList(DPL::String(L"twelve"),
319 DPL::String(L"fourteen"))));
322 TestTable::Select select(interface.get());
323 select.Where(Is<TestTable::ColumnOptInt>(DPL::Optional<int>::Null));
324 RUNNER_ASSERT(ContainerContentsEqual(select.GetValueList<TestTable::
326 makeList(DPL::Optional<DPL::String>
328 DPL::Optional<DPL::String>
332 //Where on each column
334 TestTable::Select select(interface.get());
335 select.Where(Is<TestTable::ColumnOptInt>(DPL::Optional<int>::Null));
336 RUNNER_ASSERT(ContainerContentsEqual(select.GetValueList<TestTable::
341 TestTable::Select select(interface.get());
342 select.Where(Is<TestTable::ColumnOptText>(DPL::Optional<DPL::String>::
344 RUNNER_ASSERT(ContainerContentsEqual(select.GetValueList<TestTable::
349 TestTable::Select select(interface.get());
350 select.Where(Is<TestTable::ColumnInt>(99));
351 RUNNER_ASSERT(ContainerContentsEqual(select.GetValueList<TestTable::
358 Name: ORM_MultipleCalls
359 Description: tests sequnece of different queries
360 Expected: Values should match those which were prepared
362 RUNNER_TEST(ORM_MultipleCalls)
364 for (int j = 0; j < TEST_REPETITION; j++) {
365 for (int i = 0; i < TEST_REPETITION; i++) {
366 ORM_SelectSingleValue();
369 for (int i = 0; i < TEST_REPETITION; i++) {
370 ORM_SelectSingleRow();
373 for (int i = 0; i < TEST_REPETITION; i++) {
377 for (int i = 0; i < TEST_REPETITION; i++) {
378 ORM_SelectValueList();
385 Description: tests insering rows into database
386 Expected: Values should be inserted
388 RUNNER_TEST(ORM_Insert)
390 SmartAttach interface;
391 using namespace DPL::DB::ORM;
392 using namespace DPL::DB::ORM::dpl_orm_test;
394 TestTableInsert::Select select1(interface.get());
395 std::list<int> resultList = select1.GetValueList<TestTableInsert::ColumnInt>();
396 RUNNER_ASSERT_MSG(resultList.empty(),
397 "Returned list has wrong size: " << resultList.size());
399 std::list<TestTableInsert::Row> list;
401 TestTableInsert::Insert insert(interface.get());
402 TestTableInsert::Row row;
403 row.Set_ColumnOptInt(1);
404 row.Set_ColumnInt2(2);
405 row.Set_ColumnText(L"three");
409 row.Set_ColumnInt(99);
412 TestTableInsert::Select select2(interface.get());
413 RUNNER_ASSERT_MSG(ContainerContentsEqual(
414 select2.GetRowList(),
415 list), "Returned list doesn't match.");
418 TestTableInsert::Insert insert2(interface.get());
419 TestTableInsert::Row row2;
420 row2.Set_ColumnInt(4);
421 row2.Set_ColumnInt2(5);
422 row2.Set_ColumnText(L"six");
423 insert2.Values(row2);
426 list.push_back(row2);
428 TestTableInsert::Select select(interface.get());
429 RUNNER_ASSERT_MSG(ContainerContentsEqual(
431 list), "Returned list doesn't match.");
434 TestTableInsert::Insert insert3(interface.get());
435 TestTableInsert::Row row3;
436 row3.Set_ColumnOptInt(1);
437 row3.Set_ColumnInt2(7);
438 row3.Set_ColumnText(L"eight");
439 insert3.Values(row3);
442 row3.Set_ColumnInt(99);
443 list.push_back(row3);
445 TestTableInsert::Select select3(interface.get());
446 RUNNER_ASSERT_MSG(ContainerContentsEqual(
447 select3.GetRowList(),
448 list), "Returned list doesn't match.");
451 TestTableInsert::Insert insert4(interface.get());
452 TestTableInsert::Row row4;
453 row4.Set_ColumnOptInt(9);
454 row4.Set_ColumnInt2(10);
455 row4.Set_ColumnText(L"eleven");
456 insert4.Values(row4);
459 row4.Set_ColumnInt(99);
460 list.push_back(row4);
462 TestTableInsert::Select select4(interface.get());
463 RUNNER_ASSERT_MSG(ContainerContentsEqual(
464 select4.GetRowList(),
465 list), "Returned list doesn't match.");
468 // restore original table state
470 TestTableInsert::Delete del(interface.get());
473 TestTableInsert::Select select(interface.get());
474 RUNNER_ASSERT(select.GetRowList().size() == 0);
479 Name: ORM_MultipleBindInsert
480 Description: repeats ORM_Insert test several times
481 Expected: Values should be inserted
483 RUNNER_TEST(ORM_MultipleBindInsert)
485 for (int i = 0; i < TEST_REPETITION; i++) {
492 Description: tests deleting rows from database
493 Expected: deleted rows should not exist
495 RUNNER_TEST(ORM_Delete)
497 SmartAttach interface;
498 using namespace DPL::DB::ORM;
499 using namespace DPL::DB::ORM::dpl_orm_test;
500 TestTableDelete::Select selectStart(interface.get());
501 selectStart.OrderBy(DPL::TypeListDecl<OrderingAscending<TestTableDelete::
503 std::list<TestTableDelete::Row> list = selectStart.GetRowList();
504 std::list<TestTableDelete::Row> originalList = list;
506 std::vector<TestTableDelete::Row> vector(list.begin(), list.end());
508 list.size() == 4, "Returned list has wrong size: " << list.size());
510 typedef DPL::String S;
514 TestTableDelete::Delete del(interface.get());
515 del.Where(And(Equals<TestTableDelete::ColumnOptInt>(1),
516 Equals<TestTableDelete::ColumnOptText>(S(L"seven"))));
519 TestTableDelete::Select select(interface.get());
520 RUNNER_ASSERT_MSG(ContainerContentsEqual(
522 list), "Returned list doesn't match.");
526 TestTableDelete::Delete del(interface.get());
527 del.Where(And(Equals<TestTableDelete::ColumnOptInt>(6),
528 Equals<TestTableDelete::ColumnOptText>(S(L"two"))));
531 TestTableDelete::Select select(interface.get());
532 RUNNER_ASSERT_MSG(ContainerContentsEqual(
534 list), "Returned list doesn't match.");
538 TestTableDelete::Delete del(interface.get());
539 del.Where(Equals<TestTableDelete::ColumnInt2>(10));
542 TestTableDelete::Select select(interface.get());
543 RUNNER_ASSERT_MSG(ContainerContentsEqual(
545 list), "Returned list doesn't match.");
550 list.remove(vector[1]);
552 TestTableDelete::Delete del(interface.get());
553 del.Where(And(Equals<TestTableDelete::ColumnOptInt>(6),
554 Equals<TestTableDelete::ColumnText>(L"ten")));
557 TestTableDelete::Select select(interface.get());
558 RUNNER_ASSERT_MSG(ContainerContentsEqual(
560 list), "Returned list doesn't match.");
564 list.remove(vector[2]);
565 list.remove(vector[3]);
567 TestTableDelete::Delete del(interface.get());
568 del.Where(Is<TestTableDelete::ColumnOptText>(DPL::Optional<DPL::String>
572 TestTableDelete::Select select(interface.get());
573 RUNNER_ASSERT_MSG(ContainerContentsEqual(
575 list), "Returned list doesn't match.");
579 TestTableDelete::Delete del(interface.get());
582 TestTableDelete::Select select(interface.get());
584 select.GetRowList().size() == 0, "Returned list is not empty");
587 // Restore original table state
588 // This also tests if multiple different binds for Insert are working
590 for (std::list<TestTableDelete::Row>::iterator i = originalList.begin();
591 i != originalList.end();
594 TestTableDelete::Insert insert(interface.get());
600 TestTableDelete::Select select(interface.get());
601 RUNNER_ASSERT_MSG(ContainerContentsEqual(
604 "Returned list doesn't match.");
609 Name: ORM_MultipleBindDelete
610 Description: repeats ORM_Delete test several times
611 Expected: Values should be deleted
613 RUNNER_TEST(ORM_MultipleBindDelete)
615 for (int i = 0; i < TEST_REPETITION; i++) {
621 Name: ORM_MultipleBindWhere
622 Description: tests if multiple bind of same query obejct works
623 Expected: Each bind and execution of query should be correct
625 RUNNER_TEST(ORM_MultipleBindWhere)
627 SmartAttach interface;
628 using namespace DPL::DB::ORM;
629 using namespace DPL::DB::ORM::dpl_orm_test;
631 TestTable::Select select(interface.get());
633 select.Where(Equals<TestTable::ColumnInt>(8));
634 RUNNER_ASSERT_MSG((result =
635 *select.GetSingleValue<TestTable::ColumnOptInt>())
639 select.Where(Equals<TestTable::ColumnInt>(3));
640 RUNNER_ASSERT_MSG((result =
641 *select.GetSingleValue<TestTable::ColumnOptInt>())
645 select.Where(Equals<TestTable::ColumnInt>(8));
646 RUNNER_ASSERT_MSG((result =
647 *select.GetSingleValue<TestTable::ColumnOptInt>())
651 select.Where(Equals<TestTable::ColumnInt>(3));
652 RUNNER_ASSERT_MSG((result =
653 *select.GetSingleValue<TestTable::ColumnOptInt>())
659 TestTable::Select select(interface.get());
661 select.Where(And(Equals<TestTable::ColumnInt>(99),
662 Equals<TestTable::ColumnText>(L"fourteen")));
663 RUNNER_ASSERT_MSG((result = select.GetSingleValue<TestTable::ColumnInt2>(
664 )) == 13, "Got " << result);
666 select.Where(And(Equals<TestTable::ColumnInt>(99),
667 Equals<TestTable::ColumnText>(L"twelve")));
668 RUNNER_ASSERT_MSG((result = select.GetSingleValue<TestTable::ColumnInt2>(
669 )) == 11, "Got " << result);
671 select.Where(And(Equals<TestTable::ColumnInt>(99),
672 Equals<TestTable::ColumnText>(L"fourteen")));
673 RUNNER_ASSERT_MSG((result = select.GetSingleValue<TestTable::ColumnInt2>(
674 )) == 13, "Got " << result);
676 select.Where(And(Equals<TestTable::ColumnInt>(99),
677 Equals<TestTable::ColumnText>(L"twelve")));
678 RUNNER_ASSERT_MSG((result = select.GetSingleValue<TestTable::ColumnInt2>(
679 )) == 11, "Got " << result);
683 TestTable::Select select(interface.get());
685 select.Where(And(Equals<TestTable::ColumnText>(L"fourteen"),
686 Equals<TestTable::ColumnInt>(99)));
687 RUNNER_ASSERT_MSG((result = select.GetSingleValue<TestTable::ColumnInt2>(
688 )) == 13, "Got " << result);
690 select.Where(And(Equals<TestTable::ColumnText>(L"twelve"),
691 Equals<TestTable::ColumnInt>(99)));
692 RUNNER_ASSERT_MSG((result = select.GetSingleValue<TestTable::ColumnInt2>(
693 )) == 11, "Got " << result);
695 select.Where(And(Equals<TestTable::ColumnText>(L"fourteen"),
696 Equals<TestTable::ColumnInt>(99)));
697 RUNNER_ASSERT_MSG((result = select.GetSingleValue<TestTable::ColumnInt2>(
698 )) == 13, "Got " << result);
700 select.Where(And(Equals<TestTable::ColumnText>(L"twelve"),
701 Equals<TestTable::ColumnInt>(99)));
702 RUNNER_ASSERT_MSG((result = select.GetSingleValue<TestTable::ColumnInt2>(
703 )) == 11, "Got " << result);
709 Description: tests rows update in database
710 Expected: Successful update
712 RUNNER_TEST(ORM_Update)
714 SmartAttach interface;
715 using namespace DPL::DB::ORM;
716 using namespace DPL::DB::ORM::dpl_orm_test;
718 std::list<TestTableInsert::Row> list;
720 TestTableInsert::Delete del(interface.get());
725 TestTableInsert::Insert insert(interface.get());
726 TestTableInsert::Row row;
727 row.Set_ColumnOptInt(5);
728 row.Set_ColumnInt2(2);
729 row.Set_ColumnText(L"two");
733 row.Set_ColumnInt(99);
737 TestTableInsert::Insert insert(interface.get());
738 TestTableInsert::Row row;
739 row.Set_ColumnOptInt(1);
740 row.Set_ColumnInt2(2);
741 row.Set_ColumnText(L"three");
745 row.Set_ColumnInt(99);
749 TestTableInsert::Insert insert(interface.get());
750 TestTableInsert::Row row;
751 row.Set_ColumnOptInt(2);
752 row.Set_ColumnInt2(3);
753 row.Set_ColumnText(L"three");
757 row.Set_ColumnInt(99);
761 TestTableInsert::Select select(interface.get());
762 RUNNER_ASSERT_MSG(ContainerContentsEqual(
764 list), "Returned list doesn't match.");
768 TestTableInsert::Update update(interface.get());
769 TestTableInsert::Row row;
770 row.Set_ColumnInt2(4);
771 row.Set_ColumnText(L"four");
773 update.Where(Equals<TestTableInsert::ColumnInt2>(12));
777 TestTableInsert::Select select(interface.get());
778 RUNNER_ASSERT_MSG(ContainerContentsEqual(
780 list), "Returned list doesn't match.");
784 TestTableInsert::Update update(interface.get());
785 TestTableInsert::Row row;
786 row.Set_ColumnInt2(2);
787 row.Set_ColumnText(L"four");
789 update.Where(Equals<TestTableInsert::ColumnInt2>(3));
792 list.back().Set_ColumnInt2(2);
793 list.back().Set_ColumnText(L"four");
796 TestTableInsert::Select select(interface.get());
797 RUNNER_ASSERT_MSG(ContainerContentsEqual(
799 list), "Returned list doesn't match.");
803 // UPDATE - multiple rows
804 TestTableInsert::Update update(interface.get());
805 TestTableInsert::Row row;
806 row.Set_ColumnText(L"dup");
808 update.Where(Equals<TestTableInsert::ColumnInt2>(2));
813 it->Set_ColumnText(L"dup");
817 TestTableInsert::Select select(interface.get());
818 RUNNER_ASSERT_MSG(ContainerContentsEqual(
820 list), "Returned list doesn't match.");
823 // restore original table state
825 TestTableInsert::Delete del2(interface.get());
828 TestTableInsert::Select select(interface.get());
829 RUNNER_ASSERT(select.GetRowList().size() == 0);
834 Name: ORM_MultipleBindUpdate
835 Description: repeats ORM_Update severl times
836 Expected: Successful update
838 RUNNER_TEST(ORM_MultipleBindUpdate)
840 for (int i = 0; i < TEST_REPETITION; i++) {
846 Name: ORM_transactions
847 Description: checks creation of transation object
848 Expected: Successful creation of transaction object
850 RUNNER_TEST(ORM_transactions)
852 SmartAttach interface;
853 DPL::DB::ORM::dpl_orm_test::ScopedTransaction transaction(interface.get());
857 Name: ORM_MultiAttach
858 Description: checks correct behaviou in case of multiple tries to attach to database
859 Expected: Methods attaching/dettaching should be prepared for multiple calling
861 RUNNER_TEST(ORM_MultiAttach)
863 SmartAttach interface(false);
865 !interface.get()->IsAttached(), "Is attached, but shouldn't be.");
866 interface.get()->AttachToThread();
868 interface.get()->IsAttached(), "Isn't attached, but should be.");
869 interface.get()->AttachToThread();
871 interface.get()->IsAttached(), "Isn't attached, but should be.");
872 interface.get()->DetachFromThread();
874 interface.get()->IsAttached(), "Isn't attached, but should be.");
875 interface.get()->DetachFromThread();
877 !interface.get()->IsAttached(), "Is attached, but shouldn't be.");
882 Description: tests ORM's join operation
883 Expected: values should insist correct join operation
885 RUNNER_TEST(ORM_Join)
887 SmartAttach interface;
888 using namespace DPL::DB::ORM;
889 using namespace DPL::DB::ORM::dpl_orm_test;
891 typedef DPL::TypeListDecl<TestTableJoin1::TestText,
892 TestTableJoin2::TestText2,
893 DPL::TypeListGuard>::Type JoinColumns;
895 /* Test for correct join:
896 * 5 ids from first table matches 5 ids from second table thus join result
898 TestTableJoin1::Select select(interface.get());
899 select.Join<JoinColumns>(Equal<TestTableJoin1::TestID,
900 TestTableJoin2::TestID>());
901 std::list<CustomRow<JoinColumns> > rowlist =
902 select.GetCustomRowList<JoinColumns, CustomRow<JoinColumns> >();
905 rowlist.size() == 5, "Invalid number of rows fetched: " << rowlist.size());
908 std::ostringstream oss;
910 FOREACH(rowit, rowlist)
915 DPL::ToUTF8String((*rowit).GetColumnData<TestTableJoin1::TestText>());
916 oss << "text val " << cnt;
917 RUNNER_ASSERT_MSG(text.compare(
919 "Invalid value from first column: "
920 << text << " expected: " << oss.str());
921 oss.str(std::string());
924 DPL::ToUTF8String((*rowit).GetColumnData<TestTableJoin2::TestText2>());
925 oss << "text2 " << cnt;
926 RUNNER_ASSERT_MSG(text.compare(
928 "Invalid value from second column: "
929 << text << " expected: " << oss.str());
930 oss.str(std::string());
932 /* Test for empty join:
933 * None of number values from first table matches ids from second table
934 * - join result should be empty */
935 TestTableJoin1::Select select2(interface.get());
936 select2.Join<JoinColumns>(Equal<TestTableJoin1::TestNumber,
937 TestTableJoin2::TestID>());
938 rowlist = select2.GetCustomRowList<JoinColumns, CustomRow<JoinColumns> >();
940 RUNNER_ASSERT_MSG(rowlist.empty(), "Result should be empty but it is not!");
942 /* Test for "converted" join:
943 * - join made with int column and text column as keys
944 * - expected 5 matching rows (one row of 6 should not be matched)*/
945 TestTableJoin1::Select select3(interface.get());
946 select3.Join<JoinColumns>(Equal<TestTableJoin1::TestID,
947 TestTableJoin2::TestText1>());
948 rowlist = select3.GetCustomRowList<JoinColumns, CustomRow<JoinColumns> >();
950 rowlist.size() == 5, "Expected 5 rows while received: " << rowlist.size());
952 FOREACH(rowit, rowlist)
955 // look at last two insertions into TestTableJoin2
956 // for this skip understanding
961 DPL::ToUTF8String((*rowit).GetColumnData<TestTableJoin1::TestText>());
962 oss << "text val " << cnt;
963 RUNNER_ASSERT_MSG(text.compare(
965 "Invalid value from first column: "
966 << text << " expected: " << oss.str() <<
967 " iteration: " << cnt);
968 oss.str(std::string());
971 DPL::ToUTF8String((*rowit).GetColumnData<TestTableJoin2::TestText2>());
972 oss << "text2 " << cnt;
973 RUNNER_ASSERT_MSG(text.compare(
975 "Invalid value from second column: "
976 << text << " expected: " << oss.str() <<
977 " iteration: " << cnt);
978 oss.str(std::string());
981 /* Test for join with non-unique nullable columns given as keys*/
982 typedef DPL::TypeListDecl<TestTableJoin1::TestText,
983 TestTableJoin3::TestText33,
984 DPL::TypeListGuard>::Type JoinTables2;
985 TestTableJoin1::Select select4(interface.get());
986 select4.Join<JoinTables2>(Equal<TestTableJoin1::TestNumber,
987 TestTableJoin3::Value3>());
988 std::list<CustomRow<JoinTables2> > rowlist2 =
989 select4.GetCustomRowList<JoinTables2, CustomRow<JoinTables2> >();
991 rowlist2.size() == 4, "Expected 4 rows while received: " <<
994 DPL::Optional<DPL::String> optext;
995 FOREACH(rowit, rowlist2)
1000 DPL::ToUTF8String((*rowit).GetColumnData<TestTableJoin1::TestText>());
1001 // values expected in subsequent (1,2,3,4) iterations: 1 1 2 2
1002 oss << "text val " << (1 + (int)(cnt / 3));
1003 RUNNER_ASSERT_MSG(text.compare(
1005 "Invalid value from first column: "
1006 << text << " expected: " << oss.str() <<
1007 " iteration: " << cnt);
1008 oss.str(std::string());
1010 optext = (*rowit).GetColumnData<TestTableJoin3::TestText33>();
1011 text = DPL::ToUTF8String(*optext);
1012 oss << "test " << cnt;
1013 RUNNER_ASSERT_MSG(text.compare(
1015 "Invalid value from second column: "
1016 << text << " expected: " << oss.str() <<
1017 " iteration: " << cnt);
1018 oss.str(std::string());
1021 /* Test for join made on three tables:
1022 * - 3 text columns selected for join
1023 * - Equal made for TestID of (table1 and table2) and (table1 and table3) */
1024 typedef DPL::TypeListDecl<TestTableJoin1::TestText,
1025 TestTableJoin2::TestText2,
1026 TestTableJoin3::TestText33,
1027 DPL::TypeListGuard>::Type Join3Tables;
1028 TestTableJoin1::Select select5(interface.get());
1029 select5.Join<Join3Tables>(Equal<TestTableJoin1::TestID,
1030 TestTableJoin2::TestID>());
1031 select5.Join<Join3Tables>(Equal<TestTableJoin1::TestID,
1032 TestTableJoin3::TestID>());
1033 std::list<CustomRow<Join3Tables> > rowlist3tab =
1034 select5.GetCustomRowList<Join3Tables, CustomRow<Join3Tables> >();
1036 rowlist3tab.size() == 3, "Expected 3 rows while received: " <<
1037 rowlist3tab.size());
1039 FOREACH(rowit, rowlist3tab)
1044 DPL::ToUTF8String((*rowit).GetColumnData<TestTableJoin1::TestText>());
1045 oss << "text val " << cnt;
1046 RUNNER_ASSERT_MSG(text.compare(
1048 "Invalid value from first column: "
1049 << text << " expected: " << oss.str() <<
1050 " iteration: " << cnt);
1051 oss.str(std::string());
1054 DPL::ToUTF8String((*rowit).GetColumnData<TestTableJoin2::TestText2>());
1055 oss << "text2 " << cnt;
1056 RUNNER_ASSERT_MSG(text.compare(
1058 "Invalid value from first column: "
1059 << text << " expected: " << oss.str() <<
1060 " iteration: " << cnt);
1061 oss.str(std::string());
1063 optext = (*rowit).GetColumnData<TestTableJoin3::TestText33>();
1064 text = DPL::ToUTF8String(*optext);
1065 oss << "test " << cnt;
1066 RUNNER_ASSERT_MSG(text.compare(
1068 "Invalid value from second column: "
1069 << text << " expected: " << oss.str() <<
1070 " iteration: " << cnt);
1071 oss.str(std::string());
1075 RUNNER_TEST(ORM_SelectOrderByMultipleColumns)
1077 SmartAttach interface;
1078 using namespace DPL::DB::ORM;
1079 using namespace DPL::DB::ORM::dpl_orm_test;
1081 TestTableJoin3::Select select(interface.get());
1083 // testing: " ORDER BY Value3 ASC, TestID DESC, TestID ASC"
1084 select.OrderBy(DPL::TypeListDecl<OrderingAscending<TestTableJoin3::
1086 OrderingDescending<TestTableJoin3::
1088 OrderingAscending<TestTableJoin3::
1091 std::list<TestTableJoin3::Row> result = select.GetRowList();
1092 std::list<TestTableJoin3::Row>::const_iterator iter = result.begin();
1094 RUNNER_ASSERT_MSG(*iter->Get_TestText33() ==
1095 DPL::FromASCIIString(
1096 "test 6"), "Wrong row 1 order");
1097 RUNNER_ASSERT_MSG(iter->Get_TestID() == 10, "Wrong row 1 order");
1101 RUNNER_ASSERT_MSG(*iter->Get_TestText33() ==
1102 DPL::FromASCIIString(
1103 "test 5"), "Wrong row 2 order");
1104 RUNNER_ASSERT_MSG(iter->Get_TestID() == 7, "Wrong row 2 order");
1108 RUNNER_ASSERT_MSG(iter->Get_Value3() == 111, "Wrong row 3 order");
1109 RUNNER_ASSERT_MSG(*iter->Get_TestText33() ==
1110 DPL::FromASCIIString(
1111 "test 2"), "Wrong row 3 order");
1112 RUNNER_ASSERT_MSG(iter->Get_TestID() == 2, "Wrong row 3 order");
1116 RUNNER_ASSERT_MSG(iter->Get_Value3() == 111, "Wrong row 4 order");
1117 RUNNER_ASSERT_MSG(*iter->Get_TestText33() ==
1118 DPL::FromASCIIString(
1119 "test 1"), "Wrong row 4 order");
1120 RUNNER_ASSERT_MSG(iter->Get_TestID() == 1, "Wrong row 4 order");
1124 RUNNER_ASSERT_MSG(iter->Get_Value3() == 222, "Wrong row 5 order");
1125 RUNNER_ASSERT_MSG(*iter->Get_TestText33() ==
1126 DPL::FromASCIIString(
1127 "test 4"), "Wrong row 5 order");
1128 RUNNER_ASSERT_MSG(iter->Get_TestID() == 6, "Wrong row 5 order");
1132 RUNNER_ASSERT_MSG(iter->Get_Value3() == 222, "Wrong row 6 order");
1133 RUNNER_ASSERT_MSG(*iter->Get_TestText33() ==
1134 DPL::FromASCIIString(
1135 "test 3"), "Wrong row 6 order");
1136 RUNNER_ASSERT_MSG(iter->Get_TestID() == 3, "Wrong row 6 order");