You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
4.8 KiB

  1. #include <cpphibernate/driver/mariadb.h>
  2. #include "test_helper.h"
  3. #include "test_schema.h"
  4. #include "mariadb_mock.h"
  5. using namespace ::testing;
  6. using namespace ::cpphibernate;
  7. using namespace ::cpphibernate::modifier;
  8. using namespace ::boost::hana::literals;
  9. TEST(CppHibernateTests, read_test1)
  10. {
  11. StrictMock<mariadb_mock> mock;
  12. expect_query(mock, "START TRANSACTION");
  13. expect_query(mock, "SELECT "
  14. "`tbl_test1`.`tbl_test1_id`, "
  15. "`tbl_test1`.`str_data`, "
  16. "`tbl_test1`.`str64_data`, "
  17. "`tbl_test1`.`u32_nullable`, "
  18. "`tbl_test1`.`u32_ptr_u`, "
  19. "`tbl_test1`.`u32_ptr_s` "
  20. "FROM "
  21. "`tbl_test1` ",
  22. result_used({
  23. { "3d12697a-abb9-11e8-98d0-529269fb1459", "str_data of class `test1` object `t1`", "str64_data of class `test1` object `t1`", nullptr, "123", "456" }
  24. }));
  25. expect_query(mock, "COMMIT");
  26. EXPECT_CALL(
  27. mock,
  28. mysql_close(
  29. reinterpret_cast<MYSQL*>(0x1111)));
  30. EXPECT_CALL(
  31. mock,
  32. mysql_real_escape_string(reinterpret_cast<MYSQL*>(0x1111), _, _, _))
  33. .Times(AnyNumber())
  34. .WillRepeatedly(WithArgs<1, 2, 3>(EscapeString()));
  35. test1 t1;
  36. t1.id = uuid("3d12697a-abb9-11e8-98d0-529269fb1459");
  37. ::cppmariadb::connection connection(reinterpret_cast<MYSQL*>(0x1111));
  38. auto context = make_context<driver::mariadb>(test_schema, connection);
  39. using namespace modifier;
  40. context.read(t1);
  41. EXPECT_EQ (t1.str_data, "str_data of class `test1` object `t1`");
  42. EXPECT_EQ (t1.str64_data, "str64_data of class `test1` object `t1`");
  43. EXPECT_FALSE(static_cast<bool>(t1.u32_nullable));
  44. ASSERT_TRUE (static_cast<bool>(t1.u32_ptr_u));
  45. EXPECT_EQ (*t1.u32_ptr_u, 123);
  46. ASSERT_TRUE (static_cast<bool>(t1.u32_ptr_s));
  47. EXPECT_EQ (*t1.u32_ptr_s, 456);
  48. }
  49. TEST(CppHibernateTests, read_test2)
  50. {
  51. StrictMock<mariadb_mock> mock;
  52. expect_query(mock, "START TRANSACTION");
  53. expect_query(mock, "SELECT "
  54. "`tbl_test2`.`tbl_test2_id`, "
  55. "`tbl_test2`.`u8_data`, "
  56. "`tbl_test2`.`i8_data`, "
  57. "`tbl_test2`.`u16_data`, "
  58. "`tbl_test2`.`i16_data` "
  59. "FROM "
  60. "`tbl_test2` ",
  61. result_used({
  62. { "3d1270dc-abb9-11e8-98d0-529269fb1459", "1", "2", "3", "4" }
  63. }));
  64. expect_query(mock, "COMMIT");
  65. EXPECT_CALL(
  66. mock,
  67. mysql_real_escape_string(reinterpret_cast<MYSQL*>(0x1111), _, _, _))
  68. .Times(AnyNumber())
  69. .WillRepeatedly(WithArgs<1, 2, 3>(EscapeString()));
  70. EXPECT_CALL(
  71. mock,
  72. mysql_close(
  73. reinterpret_cast<MYSQL*>(0x1111)));
  74. ::cppmariadb::connection connection(reinterpret_cast<MYSQL*>(0x1111));
  75. auto context = make_context<driver::mariadb>(test_schema, connection);
  76. constexpr decltype(auto) test2_key_field = test_schema.tables[1_c].fields[0_c];
  77. test2 t2;
  78. context.read(t2, where(equal(test2_key_field, "3d1270dc-abb9-11e8-98d0-529269fb1459")));
  79. EXPECT_EQ(1, t2.u8_data);
  80. EXPECT_EQ(2, t2.i8_data);
  81. EXPECT_EQ(3, t2.u16_data);
  82. EXPECT_EQ(4, t2.i16_data);
  83. }
  84. TEST(CppHibernateTests, read_test3)
  85. {
  86. StrictMock<mariadb_mock> mock;
  87. expect_query(mock, "START TRANSACTION");
  88. expect_query(mock, "SELECT "
  89. "`tbl_test3`.`tbl_test3_id`, "
  90. "`tbl_test3`.`u32_data`, "
  91. "`tbl_test3`.`i32_data`, "
  92. "`tbl_test3`.`u64_data`, "
  93. "`tbl_test3`.`i64_data` "
  94. "FROM "
  95. "`tbl_test3` ",
  96. result_used({
  97. { "3d12737a-abb9-11e8-98d0-529269fb1459", "5", "6", "7", "8" }
  98. }));
  99. expect_query(mock, "COMMIT");
  100. EXPECT_CALL(
  101. mock,
  102. mysql_real_escape_string(reinterpret_cast<MYSQL*>(0x1111), _, _, _))
  103. .Times(AnyNumber())
  104. .WillRepeatedly(WithArgs<1, 2, 3>(EscapeString()));
  105. EXPECT_CALL(
  106. mock,
  107. mysql_close(
  108. reinterpret_cast<MYSQL*>(0x1111)));
  109. ::cppmariadb::connection connection(reinterpret_cast<MYSQL*>(0x1111));
  110. auto context = make_context<driver::mariadb>(test_schema, connection);
  111. test3 t3;
  112. t3.id = uuid("3d12737a-abb9-11e8-98d0-529269fb1459");
  113. context.read(t3);
  114. EXPECT_EQ(5, t3.u32_data);
  115. EXPECT_EQ(6, t3.i32_data);
  116. EXPECT_EQ(7, t3.u64_data);
  117. EXPECT_EQ(8, t3.i64_data);
  118. }