|
1 | 1 | var Promise = require('bluebird'); |
2 | 2 | describe('DSSqlAdapter#find', function () { |
3 | | - it('should find a user in a Sql db', function () { |
4 | | - var id, id2, _user, _post, _comments; |
5 | | - return adapter.create(User, {name: 'John'}) |
6 | | - .then(function (user) { |
7 | | - _user = user; |
8 | | - id = user.id; |
9 | | - assert.equal(user.name, 'John'); |
10 | | - assert.isDefined(user.id); |
11 | | - return adapter.find(User, user.id); |
| 3 | + it('should find a user in a Sql db', function* () { |
| 4 | + var user = yield adapter.create(User, {name: 'John'}); |
| 5 | + var userId = user.id; |
| 6 | + assert.equal(user.name, 'John'); |
| 7 | + assert.isDefined(user.id); |
| 8 | + |
| 9 | + var user2 = yield adapter.find(User, user.id); |
| 10 | + assert.equal(user2.name, 'John'); |
| 11 | + assert.isDefined(user2.id); |
| 12 | + assert.equalObjects(user2, {id: userId, name: 'John', age: null, profileId: null}); |
| 13 | + |
| 14 | + var post = yield adapter.create(Post, { content: 'test', userId: userId }); |
| 15 | + var postId = post.id; |
| 16 | + assert.equal(post.content, 'test'); |
| 17 | + assert.isDefined(post.id); |
| 18 | + assert.isDefined(post.userId); |
| 19 | + |
| 20 | + var comments = yield [ |
| 21 | + adapter.create(Comment, { |
| 22 | + content: 'test2', |
| 23 | + postId: post.id, |
| 24 | + userId: user.id |
| 25 | + }), |
| 26 | + adapter.create(Comment, { |
| 27 | + content: 'test3', |
| 28 | + postId: post.id, |
| 29 | + userId: user.id |
12 | 30 | }) |
13 | | - .then(function (user) { |
14 | | - assert.equal(user.name, 'John'); |
15 | | - assert.isDefined(user.id); |
16 | | - assert.equalObjects(user, {id: id, name: 'John', age: null, profileId: null}); |
17 | | - return adapter.create(Post, { |
18 | | - content: 'test', |
19 | | - userId: user.id |
20 | | - }); |
21 | | - }) |
22 | | - .then(function (post) { |
23 | | - _post = post; |
24 | | - id2 = post.id; |
25 | | - assert.equal(post.content, 'test'); |
26 | | - assert.isDefined(post.id); |
27 | | - assert.isDefined(post.userId); |
28 | | - return Promise.all([ |
29 | | - adapter.create(Comment, { |
30 | | - content: 'test2', |
31 | | - postId: post.id, |
32 | | - userId: _user.id |
33 | | - }), |
34 | | - adapter.create(Comment, { |
35 | | - content: 'test3', |
36 | | - postId: post.id, |
37 | | - userId: _user.id |
38 | | - }) |
39 | | - ]); |
40 | | - }) |
41 | | - .then(function (comments) { |
42 | | - _comments = comments; |
43 | | - _comments.sort(function (a, b) { |
44 | | - return a.content > b.content; |
45 | | - }); |
46 | | - return adapter.find(Post, _post.id, {with: ['user', 'comment']}); |
47 | | - }) |
48 | | - .then(function (post) { |
49 | | - post.comments.sort(function (a, b) { |
50 | | - return a.content > b.content; |
51 | | - }); |
52 | | - assert.equalObjects(post.user, _user); |
53 | | - assert.equalObjects(post.comments, _comments); |
54 | | - return adapter.destroyAll(Comment); |
55 | | - }) |
56 | | - .then(function () { |
57 | | - return adapter.destroy(Post, id2); |
58 | | - }) |
59 | | - .then(function () { |
60 | | - return adapter.destroy(User, id); |
61 | | - }) |
62 | | - .then(function (user) { |
63 | | - assert.isFalse(!!user); |
64 | | - return adapter.find(User, id); |
65 | | - }) |
66 | | - .then(function () { |
67 | | - throw new Error('Should not have reached here!'); |
68 | | - }) |
69 | | - .catch(function (err) { |
70 | | - console.log(err.stack); |
71 | | - assert.equal(err.message, 'Not Found!'); |
72 | | - }); |
| 31 | + ]; |
| 32 | + |
| 33 | + comments.sort(function (a, b) { |
| 34 | + return a.content > b.content; |
| 35 | + }); |
| 36 | + |
| 37 | + var findPost = yield adapter.find(Post, postId, {with: ['user', 'comment']}); |
| 38 | + findPost.comments.sort(function (a, b) { |
| 39 | + return a.content > b.content; |
| 40 | + }); |
| 41 | + assert.equalObjects(findPost.user, user); |
| 42 | + assert.equalObjects(findPost.comments, comments); |
| 43 | + |
| 44 | + yield adapter.destroyAll(Comment); |
| 45 | + yield adapter.destroy(Post, postId); |
| 46 | + var destroyUser = yield adapter.destroy(User, userId); |
| 47 | + assert.isFalse(!!destroyUser); |
| 48 | + |
| 49 | + try { |
| 50 | + yield adapter.find(User, userId); |
| 51 | + throw new Error('Should not have reached here!'); |
| 52 | + } catch (err) { |
| 53 | + console.log(err.stack); |
| 54 | + assert.equal(err.message, 'Not Found!'); |
| 55 | + } |
| 56 | + }); |
| 57 | + |
| 58 | + it('should load belongsTo relations', function* () { |
| 59 | + var profile = yield adapter.create(Profile, { email: 'foo@test.com' }); |
| 60 | + var user = yield adapter.create(User, {name: 'John', profileId: profile.id}); |
| 61 | + var post = yield adapter.create(Post, {content: 'foo', userId: user.id}); |
| 62 | + var comment = yield adapter.create(Comment, { content: 'test2', postId: post.id, userId: post.userId }); |
| 63 | + |
| 64 | + var comment = yield adapter.find(Comment, comment.id, {'with': ['user', 'user.profile', 'post', 'post.user']}); |
| 65 | + assert.isDefined(comment); |
| 66 | + assert.isDefined(comment.post); |
| 67 | + assert.isDefined(comment.post.user); |
| 68 | + assert.isDefined(comment.user); |
| 69 | + assert.isDefined(comment.user.profile); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should load hasMany and belongsTo relations', function* () { |
| 73 | + var profile = yield adapter.create(Profile, { email: 'foo@test.com' }); |
| 74 | + var user = yield adapter.create(User, {name: 'John', profileId: profile.id}); |
| 75 | + var post = yield adapter.create(Post, {content: 'foo', userId: user.id}); |
| 76 | + var comment = yield adapter.create(Comment, { content: 'test2', postId: post.id, userId: post.userId }); |
| 77 | + |
| 78 | + var foundPost = yield adapter.find(Post, post.id, {'with': ['user', 'comment', 'comment.user', 'comment.user.profile']}); |
| 79 | + assert.isDefined(foundPost.comments); |
| 80 | + assert.isDefined(foundPost.comments[0].user); |
| 81 | + assert.isDefined(foundPost.comments[0].user.profile); |
| 82 | + assert.isDefined(foundPost.user); |
73 | 83 | }); |
| 84 | + |
74 | 85 | }); |
0 commit comments