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