UpdateTest.cc 1.6 KB

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