UpdateTest.cc 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright: All Rights Reserved
  2. // Author: egametang@gmail.com (tanghai)
  3. #include <gtest/gtest.h>
  4. #include <glog/logging.h>
  5. #include <gflags/gflags.h>
  6. #include "Orm/Column.h"
  7. #include "Orm/Exception.h"
  8. #include "Orm/Update.h"
  9. #include "Orm/Person.pb.h"
  10. namespace Egametang {
  11. class UpdateTest: public testing::Test
  12. {
  13. };
  14. TEST_F(UpdateTest, Update_OneField)
  15. {
  16. std::string expectedSql;
  17. expectedSql = "update Egametang.Person set guid = 1";
  18. Person person;
  19. person.set_guid(1);
  20. Update update(person);
  21. EXPECT_EQ(expectedSql, update.ToString());
  22. }
  23. TEST_F(UpdateTest, Update_MutiField)
  24. {
  25. std::string expectedSql;
  26. expectedSql =
  27. "update Egametang.Person "
  28. "set guid = 1, age = 18, comment = 'a good student!'";
  29. Person person;
  30. person.set_guid(1);
  31. person.set_age(18);
  32. person.set_comment("a good student!");
  33. Update update(person);
  34. EXPECT_EQ(expectedSql, update.ToString());
  35. }
  36. TEST_F(UpdateTest, Update_Where)
  37. {
  38. std::string expectedSql;
  39. expectedSql =
  40. "update Egametang.Person "
  41. "set guid = 1, age = 18, comment = 'a good student!' "
  42. "where age > 10";
  43. Person person;
  44. person.set_guid(1);
  45. person.set_age(18);
  46. person.set_comment("a good student!");
  47. Update update(person);
  48. update.Where(Column("age") > 10);
  49. EXPECT_EQ(expectedSql, update.ToString());
  50. }
  51. TEST_F(UpdateTest, Update_ThrowException)
  52. {
  53. std::string expectedSql;
  54. Person person;
  55. Update update1(person);
  56. EXPECT_THROW(update1.Where(Column("age") == 1).ToString(), MessageNoFeildIsSetException);
  57. person.set_guid(1);
  58. Update update2(person);
  59. EXPECT_THROW(update2.Where(Column("color") == 1), MessageHasNoSuchFeildException);
  60. }
  61. } // namespace Egametang
  62. int main(int argc, char* argv[])
  63. {
  64. testing::InitGoogleTest(&argc, argv);
  65. google::InitGoogleLogging(argv[0]);
  66. google::ParseCommandLineFlags(&argc, &argv, true);
  67. return RUN_ALL_TESTS();
  68. }