JsonTest.C 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /*
  2. * Copyright (C) 2014 Emweb bvba, Herent, Belgium.
  3. *
  4. * See the LICENSE file for terms of use.
  5. */
  6. #ifdef SQLITE3
  7. #include <boost/test/unit_test.hpp>
  8. #include <Wt/Dbo/Dbo>
  9. #include <Wt/Dbo/Json>
  10. #include <Wt/Dbo/backend/Sqlite3>
  11. namespace dbo = Wt::Dbo;
  12. namespace JsonDboTest {
  13. class Post;
  14. class User;
  15. class NestedThing;
  16. class Post {
  17. public:
  18. dbo::ptr<User> user;
  19. dbo::weak_ptr<NestedThing> nestedThing;
  20. std::string title;
  21. std::string body;
  22. template<class Action>
  23. void persist(Action& a)
  24. {
  25. dbo::belongsTo(a, user, "user");
  26. dbo::hasOne(a, nestedThing, "post");
  27. dbo::field(a, title, "title");
  28. dbo::field(a, body, "body");
  29. }
  30. };
  31. class NestedThing {
  32. public:
  33. int one;
  34. int two;
  35. int three;
  36. dbo::ptr<Post> post;
  37. template<class Action>
  38. void persist(Action& a)
  39. {
  40. dbo::field(a, one, "one");
  41. dbo::field(a, two, "two");
  42. dbo::field(a, three, "three");
  43. dbo::belongsTo(a, post, "post");
  44. }
  45. };
  46. class Empty {
  47. public:
  48. template<class Action>
  49. void persist(Action& a)
  50. {
  51. }
  52. };
  53. class Settings {
  54. public:
  55. std::string theme;
  56. dbo::ptr<User> user;
  57. template<class Action>
  58. void persist(Action& a)
  59. {
  60. dbo::field(a, theme, "theme");
  61. dbo::belongsTo(a, user);
  62. }
  63. };
  64. class User {
  65. public:
  66. enum Role {
  67. Visitor = 0,
  68. Admin = 1,
  69. Alien = 42
  70. };
  71. std::string name;
  72. std::string password;
  73. Role role;
  74. int karma;
  75. dbo::collection< dbo::ptr<Post> > posts;
  76. dbo::weak_ptr<Settings> settings;
  77. template<class Action>
  78. void persist(Action& a)
  79. {
  80. dbo::field(a, name, "name");
  81. dbo::field(a, password, "password");
  82. dbo::field(a, role, "role");
  83. dbo::field(a, karma, "karma");
  84. dbo::hasMany(a, posts, dbo::ManyToOne, "user");
  85. dbo::hasOne(a, settings);
  86. }
  87. };
  88. class HasSurrogate {
  89. public:
  90. std::string foo;
  91. template<class Action>
  92. void persist(Action& a)
  93. {
  94. dbo::field(a, foo, "foo");
  95. }
  96. };
  97. class HasNatural {
  98. public:
  99. std::string myNaturalId;
  100. std::string foo;
  101. template<class Action>
  102. void persist(Action& a)
  103. {
  104. dbo::id(a, myNaturalId, "natural_id", 20);
  105. dbo::field(a, foo, "foo");
  106. }
  107. };
  108. struct Coordinate {
  109. int x, y;
  110. Coordinate()
  111. : x(-1), y(-1) { }
  112. Coordinate(int an_x, int an_y)
  113. : x(an_x), y(an_y) { }
  114. bool operator== (const Coordinate& other) const {
  115. return x == other.x && y == other.y;
  116. }
  117. bool operator< (const Coordinate& other) const {
  118. if (x < other.x)
  119. return true;
  120. else if (x == other.x)
  121. return y < other.y;
  122. else
  123. return false;
  124. }
  125. };
  126. std::ostream& operator<< (std::ostream& o, const Coordinate& c)
  127. {
  128. return o << "{" << c.x << ", " << c.y << ")";
  129. }
  130. class HasCoordinateId;
  131. }
  132. namespace Wt {
  133. namespace Dbo {
  134. template <class Action>
  135. void field(Action& action, JsonDboTest::Coordinate& coordinate, const std::string& name, int size = -1)
  136. {
  137. field(action, coordinate.x, name + "_x");
  138. field(action, coordinate.y, name + "_y");
  139. }
  140. template<>
  141. struct dbo_traits<JsonDboTest::HasSurrogate> : public dbo_default_traits {
  142. static const char *surrogateIdField() { return "alternate_id"; }
  143. };
  144. template<>
  145. struct dbo_traits<JsonDboTest::HasNatural> : public dbo_default_traits {
  146. typedef std::string IdType;
  147. static IdType invalidId() {
  148. return std::string();
  149. }
  150. static const char *surrogateIdField() { return 0; }
  151. };
  152. template<>
  153. struct dbo_traits<JsonDboTest::HasCoordinateId> : public dbo_default_traits
  154. {
  155. typedef JsonDboTest::Coordinate IdType;
  156. static IdType invalidId() { return JsonDboTest::Coordinate(); }
  157. static const char *surrogateIdField() { return 0; }
  158. };
  159. }
  160. }
  161. namespace JsonDboTest {
  162. class HasCoordinateId {
  163. public:
  164. Coordinate position;
  165. std::string name;
  166. template <class Action>
  167. void persist(Action& a)
  168. {
  169. dbo::id(a, position, "position");
  170. dbo::field(a, name, "name");
  171. }
  172. };
  173. struct JsonDboFixture
  174. {
  175. JsonDboFixture()
  176. {
  177. static bool logged = false;
  178. if (!logged) {
  179. std::cerr << "JsonTest.C created a Sqlite3 connector" << std::endl;
  180. logged = true;
  181. }
  182. dbo::backend::Sqlite3 *sqlite3 = new dbo::backend::Sqlite3(":memory:");
  183. connection_ = sqlite3;
  184. session_ = new dbo::Session();
  185. session_->setConnection(*connection_);
  186. session_->mapClass<User>("user");
  187. session_->mapClass<Post>("post");
  188. session_->mapClass<NestedThing>("nestedThing");
  189. session_->mapClass<Settings>("settings");
  190. session_->mapClass<Empty>("empty");
  191. session_->mapClass<HasSurrogate>("hasSurrogate");
  192. session_->mapClass<HasNatural>("hasNatural");
  193. session_->mapClass<HasCoordinateId>("hasCoordinateId");
  194. session_->createTables();
  195. }
  196. ~JsonDboFixture()
  197. {
  198. session_->dropTables();
  199. delete session_;
  200. delete connection_;
  201. }
  202. dbo::SqlConnection *connection_;
  203. dbo::Session *session_;
  204. };
  205. BOOST_AUTO_TEST_CASE( dbo_json_empty_test )
  206. {
  207. JsonDboFixture f;
  208. dbo::Session &session = *f.session_;
  209. {
  210. dbo::Transaction transaction(session);
  211. Empty *empty = new Empty();
  212. std::stringstream ss;
  213. dbo::jsonSerialize(*empty, ss);
  214. BOOST_REQUIRE_EQUAL(ss.str(), "{}");
  215. dbo::ptr<Empty> emptyPtr = session.add(empty);
  216. }
  217. dbo::Transaction transaction(session);
  218. dbo::collection<dbo::ptr<Empty> > empties = session.find<Empty>();
  219. std::stringstream ss;
  220. dbo::jsonSerialize(empties.front(), ss);
  221. BOOST_REQUIRE_EQUAL(ss.str(), "{\"id\":1}");
  222. }
  223. BOOST_AUTO_TEST_CASE( dbo_json_empty_and_null_test )
  224. {
  225. JsonDboFixture f;
  226. dbo::Session &session = *f.session_;
  227. {
  228. dbo::Transaction transaction(session);
  229. User *user = new User();
  230. user->name = "John";
  231. user->password = "Something";
  232. user->role = User::Alien;
  233. user->karma = 13;
  234. session.add(user);
  235. }
  236. dbo::Transaction transaction(session);
  237. dbo::ptr<User> user = session.find<User>().where("name = ?").bind("John");
  238. std::string expected = "{\"id\":1,\"name\":\"John\",\"password\":\"Something\",\"role\":42,"
  239. "\"karma\":13,\"posts_user\":[],\"settings_\":null}";
  240. std::stringstream ss;
  241. jsonSerialize(user, ss);
  242. BOOST_REQUIRE_EQUAL(ss.str(), expected);
  243. }
  244. BOOST_AUTO_TEST_CASE( dbo_json_surrogate_id_test )
  245. {
  246. JsonDboFixture f;
  247. dbo::Session &session = *f.session_;
  248. dbo::ptr<HasSurrogate> hasSurrogate;
  249. {
  250. dbo::Transaction transaction(session);
  251. hasSurrogate = session.add(new HasSurrogate());
  252. hasSurrogate.modify()->foo = "bar";
  253. }
  254. dbo::Transaction transaction(session);
  255. std::string expected = "{\"alternate_id\":1,\"foo\":\"bar\"}";
  256. std::stringstream ss;
  257. jsonSerialize(hasSurrogate, ss);
  258. BOOST_REQUIRE_EQUAL(ss.str(), expected);
  259. }
  260. BOOST_AUTO_TEST_CASE( dbo_json_natural_id_test )
  261. {
  262. JsonDboFixture f;
  263. dbo::Session &session = *f.session_;
  264. dbo::ptr<HasNatural> hasNatural;
  265. {
  266. dbo::Transaction transaction(session);
  267. hasNatural = session.add(new HasNatural());
  268. hasNatural.modify()->myNaturalId = "Nature!";
  269. hasNatural.modify()->foo = "bar";
  270. }
  271. dbo::Transaction transaction(session);
  272. std::string expected = "{\"natural_id\":\"Nature!\",\"foo\":\"bar\"}";
  273. std::stringstream ss;
  274. jsonSerialize(hasNatural, ss);
  275. BOOST_REQUIRE_EQUAL(ss.str(), expected);
  276. }
  277. BOOST_AUTO_TEST_CASE( dbo_json_composite_key_test )
  278. {
  279. JsonDboFixture f;
  280. dbo::Session &session = *f.session_;
  281. dbo::ptr<HasCoordinateId> hasCoordinateId;
  282. {
  283. dbo::Transaction transaction(session);
  284. hasCoordinateId = session.add(new HasCoordinateId());
  285. hasCoordinateId.modify()->position = Coordinate(3, 4);
  286. hasCoordinateId.modify()->name = "foo";
  287. }
  288. dbo::Transaction transaction(session);
  289. std::string expected = "{\"position_x\":3,\"position_y\":4,\"name\":\"foo\"}";
  290. std::stringstream ss;
  291. jsonSerialize(hasCoordinateId, ss);
  292. BOOST_REQUIRE_EQUAL(ss.str(), expected);
  293. }
  294. BOOST_AUTO_TEST_CASE( dbo_json_complex_test )
  295. {
  296. JsonDboFixture f;
  297. dbo::Session &session = *f.session_;
  298. {
  299. dbo::Transaction transaction(session);
  300. User *user = new User();
  301. user->name = "Joe";
  302. user->password = "Secret";
  303. user->role = User::Visitor;
  304. user->karma = 13;
  305. dbo::ptr<User> userPtr = session.add(user);
  306. }
  307. dbo::ptr<Post> post;
  308. dbo::ptr<NestedThing> nestedThing;
  309. {
  310. dbo::Transaction transaction(session);
  311. dbo::ptr<User> joe = session.find<User>().where("name = ?").bind("Joe");
  312. post = session.add(new Post());
  313. post.modify()->user = joe;
  314. post.modify()->body = "Lorem ipsum dolor sit amet \"escape me\" something something";
  315. post.modify()->title = "Hello";
  316. nestedThing = session.add(new NestedThing());
  317. nestedThing.modify()->one = 1;
  318. nestedThing.modify()->two = 2;
  319. nestedThing.modify()->three = 3;
  320. nestedThing.modify()->post = post;
  321. }
  322. {
  323. dbo::Transaction transaction(session);
  324. dbo::ptr<User> joe = session.find<User>().where("name = ?").bind("Joe");
  325. dbo::ptr<Settings> settings = session.add(new Settings());
  326. settings.modify()->theme = "fancy-pink";
  327. joe.modify()->settings = settings;
  328. }
  329. dbo::Transaction transaction(session);
  330. dbo::ptr<User> joe = session.find<User>().where("name = ?").bind("Joe");
  331. std::stringstream ss;
  332. dbo::jsonSerialize(joe, ss);
  333. std::string joeString = "{\"id\":1,\"name\":\"Joe\",\"password\":\"Secret\",\"role\":0,\"karma\":13,"
  334. "\"posts_user\":[{\"id\":1,\"user\":1,\"nestedThing_post\":{\"id\":1,\"one\":1,\"two\":2,\"three\":3,\"post\":1},"
  335. "\"title\":\"Hello\",\"body\":\"Lorem ipsum dolor sit amet \\\"escape me\\\" something something\"}],"
  336. "\"settings_\":{\"id\":1,\"theme\":\"fancy-pink\",\"user\":1}}";
  337. BOOST_REQUIRE_EQUAL(ss.str(), joeString);
  338. }
  339. }
  340. #endif