Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a1c198f

Browse files
committedNov 10, 2015
Initial release
0 parents  commit a1c198f

12 files changed

+670
-0
lines changed
 

‎.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
*.log

‎CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
##### 1.0.0 - 09 November 2015
2+
3+
- Initial release

‎README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<img src="https://raw.githubusercontent.com/js-data/js-data/master/js-data.png" alt="js-data logo" title="js-data" align="right" width="64" height="64" />
2+
3+
## js-data-adapter-tests [![Slack Status][sl_b]][sl_l] [![npm version][npm_b]][npm_l] [![Circle CI][circle_b]][circle_l] [![npm downloads][dn_b]][dn_l]
4+
5+
Tests for [js-data](http://www.js-data.io/) adapters.
6+
7+
See [js-data-sql](https://github.com/js-data/js-data-adapter-tests/blob/master/mocha.start.js) for usage.
8+
9+
### Changelog
10+
[CHANGELOG.md](https://github.com/js-data/js-data-adapter-tests/blob/master/CHANGELOG.md)
11+
12+
### Community
13+
- [Slack Channel](http://slack.js-data.io) - Better than IRC!
14+
15+
### License
16+
17+
The MIT License (MIT)
18+
19+
Copyright (c) 2014-2015 Jason Dobry
20+
21+
Permission is hereby granted, free of charge, to any person obtaining a copy
22+
of this software and associated documentation files (the "Software"), to deal
23+
in the Software without restriction, including without limitation the rights
24+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
25+
copies of the Software, and to permit persons to whom the Software is
26+
furnished to do so, subject to the following conditions:
27+
28+
The above copyright notice and this permission notice shall be included in all
29+
copies or substantial portions of the Software.
30+
31+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
34+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
35+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
36+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
37+
SOFTWARE.
38+
39+
[sl_b]: http://slack.js-data.io/badge.svg
40+
[sl_l]: http://slack.js-data.io
41+
[npm_b]: https://img.shields.io/npm/v/js-data-adapter-tests.svg?style=flat
42+
[npm_l]: https://www.npmjs.org/package/js-data-adapter-tests
43+
[circle_b]: https://img.shields.io/circleci/project/js-data/js-data-adapter-tests/master.svg?style=flat
44+
[circle_l]: https://circleci.com/gh/js-data/js-data-adapter-tests/tree/master
45+
[dn_b]: https://img.shields.io/npm/dm/js-data-adapter-tests.svg?style=flat
46+
[dn_l]: https://www.npmjs.org/package/js-data-adapter-tests

‎package.json

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"name": "js-data-adapter-tests",
3+
"description": "Tests for js-data adapters.",
4+
"version": "1.0.0",
5+
"homepage": "http://www.js-data.io",
6+
"repository": {
7+
"type": "git",
8+
"url": "https://github.com/js-data/js-data-adapter-tests.git"
9+
},
10+
"author": {
11+
"name": "Jason Dobry",
12+
"url": "http://www.pseudobry.com",
13+
"email": "jason.dobry@gmail.com"
14+
},
15+
"license": "MIT",
16+
"main": "./src/index.js",
17+
"keywords": [
18+
"js-data",
19+
"adapter",
20+
"test"
21+
],
22+
"devDependencies": {
23+
"babel-eslint": "4.1.3",
24+
"standard": "5.3.1"
25+
},
26+
"standard": {
27+
"parser": "babel-eslint",
28+
"globals": [
29+
"describe",
30+
"it",
31+
"before",
32+
"after",
33+
"beforeEach",
34+
"afterEach",
35+
"assert"
36+
]
37+
},
38+
"scripts": {
39+
"lint": "standard src/**/*.js",
40+
"test": "npm run lint"
41+
},
42+
"peerDependencies": {
43+
"chai": "~3.x"
44+
}
45+
}

‎src/create.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
module.exports = function (adapter) {
2+
describe('Adapter#create', function () {
3+
it('should exist', function * () {
4+
assert.equal(typeof this.$$adapter.create, 'function', 'adapter should have a "create" method')
5+
})
6+
it('should create a user', function * () {
7+
var adapter = this.$$adapter
8+
var User = this.$$User
9+
var createUser = yield adapter.create(User, {name: 'John'})
10+
var id = createUser.id
11+
assert.equal(createUser.name, 'John')
12+
assert.isDefined(createUser.id)
13+
14+
var findUser = yield adapter.find(User, createUser.id)
15+
assert.equal(findUser.name, 'John')
16+
assert.isDefined(findUser.id)
17+
assert.equalObjects(findUser, {id: id, name: 'John', age: null, profileId: null})
18+
19+
var destoryUser = yield adapter.destroy(User, findUser.id)
20+
assert.isFalse(!!destoryUser)
21+
22+
try {
23+
yield adapter.find(User, id)
24+
throw new Error('Should not have reached here!')
25+
} catch (err) {
26+
assert.equal(err.message, 'Not Found!')
27+
}
28+
})
29+
})
30+
}

‎src/destroy.test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module.exports = function (adapter) {
2+
describe('Adapter#destroy', function () {
3+
it('should exist', function * () {
4+
assert.equal(typeof this.$$adapter.destroy, 'function', 'adapter should have a "destroy" method')
5+
})
6+
it('should destroy a user', function * () {
7+
var adapter = this.$$adapter
8+
var User = this.$$User
9+
var createUser = yield adapter.create(User, {name: 'John'})
10+
var id = createUser.id
11+
12+
var destroyUser = yield adapter.destroy(User, createUser.id)
13+
assert.isFalse(!!destroyUser)
14+
15+
try {
16+
yield adapter.find(User, id)
17+
throw new Error('Should not have reached here!')
18+
} catch (err) {
19+
assert.equal(err.message, 'Not Found!')
20+
}
21+
})
22+
})
23+
}

‎src/destroyAll.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module.exports = function (adapter) {
2+
describe('Adapter#destroyAll', function () {
3+
it('should exist', function * () {
4+
assert.equal(typeof this.$$adapter.destroyAll, 'function', 'adapter should have a "destroyAll" method')
5+
})
6+
it('should destroy all users', function * () {
7+
var adapter = this.$$adapter
8+
var User = this.$$User
9+
var createUser = yield adapter.create(User, {name: 'John'})
10+
var id = createUser.id
11+
12+
var findUsers = yield adapter.findAll(User, { name: 'John' })
13+
assert.equal(findUsers.length, 1)
14+
assert.equalObjects(findUsers[0], {id: id, name: 'John', age: null, profileId: null})
15+
16+
yield adapter.destroyAll(User, { name: 'John' })
17+
var findUsers2 = yield adapter.findAll(User, { name: 'John' })
18+
assert.equal(findUsers2.length, 0)
19+
})
20+
})
21+
}

‎src/find.test.js

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+
}

‎src/findAll.test.js

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
module.exports = function (adapter) {
2+
describe('Adapter#findAll', 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.findAll, 'function', 'adapter should have a "findAll" method')
15+
})
16+
17+
it('should filter users', function * () {
18+
var users = yield adapter.findAll(User, { age: 30 })
19+
assert.equal(users.length, 0)
20+
21+
var user = yield adapter.create(User, {name: 'John'})
22+
var id = user.id
23+
24+
var users2 = yield adapter.findAll(User, { name: 'John' })
25+
assert.equal(users2.length, 1)
26+
assert.equalObjects(users2[0], {id: id, name: 'John', age: null, profileId: null})
27+
28+
var destroyedUser = yield adapter.destroy(User, id)
29+
assert.isFalse(!!destroyedUser)
30+
})
31+
32+
it('should filter users using the "in" operator', function * () {
33+
var users = yield adapter.findAll(User, {
34+
where: {
35+
age: {
36+
'in': [30]
37+
}
38+
}
39+
})
40+
assert.equal(users.length, 0)
41+
42+
var user = yield adapter.create(User, {name: 'John'})
43+
var id = user.id
44+
45+
var users2 = yield adapter.findAll(User, { name: 'John' })
46+
assert.equal(users2.length, 1)
47+
assert.equalObjects(users2[0], {id: id, name: 'John', age: null, profileId: null})
48+
49+
var destroyedUser = yield adapter.destroy(User, id)
50+
assert.isFalse(!!destroyedUser)
51+
})
52+
53+
it('should filter users using the "like" operator', function * () {
54+
var users = yield adapter.findAll(User, {
55+
where: {
56+
name: {
57+
'like': '%J%'
58+
}
59+
}
60+
})
61+
assert.equal(users.length, 0)
62+
63+
var user = yield adapter.create(User, {name: 'John'})
64+
var id = user.id
65+
66+
var users2 = yield adapter.findAll(User, {
67+
where: {
68+
name: {
69+
'like': '%J%'
70+
}
71+
}
72+
})
73+
assert.equal(users2.length, 1)
74+
assert.deepEqual(users2[0], {id: id, name: 'John', age: null, profileId: null})
75+
76+
var destroyedUser = yield adapter.destroy(User, id)
77+
assert.isFalse(!!destroyedUser)
78+
})
79+
80+
it('should throw "Operator not found" error', function * () {
81+
assert.throw(function () {
82+
return adapter.findAll(User, {
83+
where: {
84+
name: {
85+
op: 'John'
86+
}
87+
}
88+
})
89+
}, Error, 'Operator not found')
90+
})
91+
92+
it('should load belongsTo relations', function * () {
93+
var profile1 = yield adapter.create(Profile, { email: 'foo@test.com' })
94+
var user1 = yield adapter.create(User, {name: 'John', profileId: profile1.id})
95+
var post1 = yield adapter.create(Post, {content: 'foo', userId: user1.id})
96+
yield adapter.create(Comment, { content: 'test2', postId: post1.id, userId: post1.userId })
97+
98+
var user2 = yield adapter.create(User, {name: 'Sally'})
99+
var post2 = yield adapter.create(Post, {content: 'bar', userId: user2.id})
100+
yield adapter.create(Comment, { content: 'test3', postId: post2.id, userId: post2.userId })
101+
102+
var comments = yield adapter.findAll(Comment, {}, {'with': ['user', 'user.profile', 'post', 'post.user']})
103+
assert.isDefined(comments[0].post)
104+
assert.isDefined(comments[0].post.user)
105+
assert.isDefined(comments[0].user)
106+
assert.isDefined(comments[0].user.profile || comments[1].user.profile)
107+
assert.isDefined(comments[1].post)
108+
assert.isDefined(comments[1].post.user)
109+
assert.isDefined(comments[1].user)
110+
})
111+
112+
it('should load hasMany and belongsTo relations', function * () {
113+
var profile = yield adapter.create(Profile, { email: 'foo@test.com' })
114+
var user1 = yield adapter.create(User, {name: 'John', profileId: profile.id})
115+
var post1 = yield adapter.create(Post, {content: 'foo', userId: user1.id})
116+
yield adapter.create(Comment, { content: 'test2', postId: post1.id, userId: post1.userId })
117+
118+
var user2 = yield adapter.create(User, {name: 'Sally'})
119+
var post2 = yield adapter.create(Post, {content: 'bar', userId: user2.id})
120+
yield adapter.create(Comment, { content: 'test3', postId: post2.id, userId: post2.userId })
121+
122+
var posts = yield adapter.findAll(Post, {}, {'with': ['user', 'comment', 'comment.user', 'comment.user.profile']})
123+
assert.isDefined(posts[0].comments)
124+
assert.isDefined(posts[0].comments[0].user)
125+
assert.isDefined(posts[0].comments[0].user.profile || posts[1].comments[0].user.profile)
126+
assert.isDefined(posts[0].user)
127+
assert.isDefined(posts[1].comments)
128+
assert.isDefined(posts[1].comments[0].user)
129+
assert.isDefined(posts[1].user)
130+
})
131+
132+
it('should filter using belongsTo relation', function * () {
133+
var profile1 = yield adapter.create(Profile, { email: 'foo@test.com' })
134+
var user1 = yield adapter.create(User, {name: 'John', profileId: profile1.id})
135+
var post1 = yield adapter.create(Post, {content: 'foo', userId: user1.id})
136+
yield adapter.create(Comment, {content: 'test1', postId: post1.id, userId: post1.userId})
137+
138+
var user2 = yield adapter.create(User, {name: 'Sally'})
139+
var post2 = yield adapter.create(Post, {content: 'bar', userId: user2.id})
140+
yield adapter.create(Comment, {content: 'test2', postId: post2.id, userId: post2.userId})
141+
142+
var users = yield adapter.findAll(User, {'profile.email': 'foo@test.com'})
143+
assert.equal(users.length, 1)
144+
assert.equal(users[0].profileId, profile1.id)
145+
assert.equal(users[0].name, 'John')
146+
})
147+
148+
it('should filter through multiple hasOne/belongsTo relations', function * () {
149+
var profile1 = yield adapter.create(Profile, { email: 'foo@test.com' })
150+
var user1 = yield adapter.create(User, {name: 'John', profileId: profile1.id})
151+
var post1 = yield adapter.create(Post, {content: 'foo', userId: user1.id})
152+
yield adapter.create(Comment, {content: 'test1', postId: post1.id, userId: post1.userId})
153+
154+
var profile2 = yield adapter.create(Profile, { email: 'bar@test.com' })
155+
var user2 = yield adapter.create(User, {name: 'Sally', profileId: profile2.id})
156+
var post2 = yield adapter.create(Post, {content: 'bar', userId: user2.id})
157+
yield adapter.create(Comment, {content: 'test2', postId: post2.id, userId: post2.userId})
158+
159+
var comments = yield adapter.findAll(Comment, { 'user.profile.email': 'foo@test.com' })
160+
assert.equal(comments.length, 1)
161+
assert.equal(comments[0].userId, user1.id)
162+
assert.equal(comments[0].content, 'test1')
163+
})
164+
165+
it('should filter using multiple hasOne/belongsTo relations', function * () {
166+
var profile1 = yield adapter.create(Profile, { email: 'foo@test.com' })
167+
var user1 = yield adapter.create(User, {name: 'John', profileId: profile1.id})
168+
var post1 = yield adapter.create(Post, {content: 'foo', userId: user1.id})
169+
yield adapter.create(Comment, {content: 'test1', postId: post1.id, userId: post1.userId})
170+
171+
var profile2 = yield adapter.create(Profile, { email: 'bar@test.com' })
172+
var user2 = yield adapter.create(User, {name: 'Sally', profileId: profile2.id})
173+
var post2 = yield adapter.create(Post, {content: 'bar', userId: user2.id})
174+
yield adapter.create(Comment, {content: 'test2', postId: post2.id, userId: post2.userId})
175+
176+
var comments = yield adapter.findAll(Comment, { 'user.name': 'John', 'user.profile.email': 'foo@test.com' })
177+
assert.equal(comments.length, 1)
178+
assert.equal(comments[0].userId, user1.id)
179+
assert.equal(comments[0].content, 'test1')
180+
})
181+
182+
it('should allow passing limit and offset as strings', function * () {
183+
yield adapter.findAll(User, {limit: '10', offset: '20'})
184+
})
185+
})
186+
}

‎src/index.js

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
var assert = require('chai').assert
2+
3+
assert.equalObjects = function (a, b, m) {
4+
assert.deepEqual(JSON.parse(JSON.stringify(a)), JSON.parse(JSON.stringify(b)), m || 'Objects should be equal!')
5+
}
6+
7+
var prefix = 'TestRunner.init(options): options'
8+
var methods = [
9+
'create',
10+
'destroy',
11+
'destroyAll',
12+
'find',
13+
'findAll',
14+
'update',
15+
'updateAll'
16+
]
17+
18+
module.exports = {
19+
init: function (options) {
20+
options = options || {}
21+
options.methods = options.methods || 'all'
22+
if (!options.DS || typeof options.DS !== 'function') {
23+
throw new Error(prefix + '.DS: Expected function, Actual: ' + typeof options.DS)
24+
}
25+
if (!options.Adapter || typeof options.Adapter !== 'function') {
26+
throw new Error(prefix + '.Adapter: Expected function, Actual: ' + typeof options.Adapter)
27+
}
28+
beforeEach(function () {
29+
this.$$adapter = new options.Adapter(options.adapterConfig)
30+
this.$$store = new options.DS({
31+
log: false,
32+
debug: false
33+
})
34+
this.$$User = this.$$store.defineResource({
35+
name: 'user',
36+
relations: {
37+
hasMany: {
38+
post: {
39+
localField: 'posts',
40+
foreignKey: 'post'
41+
}
42+
},
43+
hasOne: {
44+
profile: {
45+
localField: 'profile',
46+
localKey: 'profileId'
47+
}
48+
}
49+
}
50+
} || options.userConfig)
51+
this.$$Profile = this.$$store.defineResource({
52+
name: 'profile'
53+
} || options.profileConfig)
54+
this.$$Post = this.$$store.defineResource({
55+
name: 'post',
56+
relations: {
57+
belongsTo: {
58+
user: {
59+
localField: 'user',
60+
localKey: 'userId'
61+
}
62+
},
63+
hasMany: {
64+
comment: {
65+
localField: 'comments',
66+
foreignKey: 'postId'
67+
}
68+
}
69+
}
70+
} || options.postConfig)
71+
this.$$Comment = this.$$store.defineResource({
72+
name: 'comment',
73+
relations: {
74+
belongsTo: {
75+
post: {
76+
localField: 'post',
77+
localKey: 'postId'
78+
},
79+
user: {
80+
localField: 'user',
81+
localKey: 'userId'
82+
}
83+
}
84+
}
85+
})
86+
} || options.commentConfig)
87+
88+
describe('js-data-adapter-tests', function () {
89+
methods.forEach(function (method) {
90+
if (options.methods === 'all' || options.methods.indexOf(method) !== -1) {
91+
require('./' + method + '.test')()
92+
}
93+
}, this)
94+
})
95+
96+
afterEach(function * () {
97+
yield this.$$adapter.destroyAll(this.$$Comment)
98+
yield this.$$adapter.destroyAll(this.$$Post)
99+
yield this.$$adapter.destroyAll(this.$$User)
100+
yield this.$$adapter.destroyAll(this.$$Profile)
101+
})
102+
},
103+
assert: assert,
104+
fail: function (msg) {
105+
assert.equal('should not reach this!: ' + msg, 'failure')
106+
},
107+
TYPES_EXCEPT_STRING: [123, 123.123, null, undefined, {}, [], true, false, function () {
108+
}],
109+
TYPES_EXCEPT_STRING_OR_ARRAY: [123, 123.123, null, undefined, {}, true, false, function () {
110+
}],
111+
TYPES_EXCEPT_STRING_OR_NUMBER: [null, undefined, {}, [], true, false, function () {
112+
}],
113+
TYPES_EXCEPT_STRING_OR_OBJECT: [123, 123.123, null, undefined, [], true, false, function () {
114+
}],
115+
TYPES_EXCEPT_STRING_OR_NUMBER_OBJECT: [null, undefined, [], true, false, function () {
116+
}],
117+
TYPES_EXCEPT_STRING_OR_ARRAY_OR_NUMBER: [null, undefined, {}, true, false, function () {
118+
}],
119+
TYPES_EXCEPT_NUMBER: ['string', null, undefined, {}, [], true, false, function () {
120+
}],
121+
TYPES_EXCEPT_OBJECT: ['string', 123, 123.123, null, undefined, true, false, function () {
122+
}],
123+
TYPES_EXCEPT_BOOLEAN: ['string', 123, 123.123, null, undefined, {}, [], function () {
124+
}],
125+
TYPES_EXCEPT_FUNCTION: ['string', 123, 123.123, null, undefined, {}, [], true, false]
126+
}

‎src/update.test.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
module.exports = function (adapter) {
2+
describe('Adapter#update', function () {
3+
it('should exist', function * () {
4+
assert.equal(typeof this.$$adapter.update, 'function', 'adapter should have a "update" method')
5+
})
6+
it('should update a user', function * () {
7+
var adapter = this.$$adapter
8+
var User = this.$$User
9+
var user = yield adapter.create(User, {name: 'John'})
10+
var id = user.id
11+
assert.equal(user.name, 'John')
12+
assert.isDefined(user.id)
13+
14+
var foundUser = yield adapter.find(User, user.id)
15+
assert.equal(foundUser.name, 'John')
16+
assert.isDefined(foundUser.id)
17+
assert.equalObjects(foundUser, {id: id, name: 'John', age: null, profileId: null})
18+
19+
var updatedUser = yield adapter.update(User, foundUser.id, {name: 'Johnny'})
20+
assert.equal(updatedUser.name, 'Johnny')
21+
assert.isDefined(updatedUser.id)
22+
assert.equalObjects(updatedUser, {id: id, name: 'Johnny', age: null, profileId: null})
23+
24+
var foundUser2 = yield adapter.find(User, updatedUser.id)
25+
assert.equal(foundUser2.name, 'Johnny')
26+
assert.isDefined(foundUser2.id)
27+
assert.equalObjects(foundUser2, {id: id, name: 'Johnny', age: null, profileId: null})
28+
29+
var destroyUser = yield adapter.destroy(User, foundUser2.id)
30+
assert.isFalse(!!destroyUser)
31+
32+
try {
33+
yield adapter.find(User, id)
34+
throw new Error('Should not have reached here!')
35+
} catch (err) {
36+
assert.equal(err.message, 'Not Found!')
37+
}
38+
})
39+
})
40+
}

‎src/updateAll.test.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
module.exports = function (adapter) {
2+
describe('Adapter#updateAll', function () {
3+
it('should exist', function * () {
4+
assert.equal(typeof this.$$adapter.updateAll, 'function', 'adapter should have a "updateAll" method')
5+
})
6+
it('should update multiple users', function * () {
7+
var adapter = this.$$adapter
8+
var User = this.$$User
9+
var user1 = yield adapter.create(User, {name: 'John', age: 20})
10+
var userId1 = user1.id
11+
12+
var user2 = yield adapter.create(User, {name: 'John', age: 30})
13+
var userId2 = user2.id
14+
15+
var users = yield adapter.findAll(User, { name: 'John' })
16+
users.sort(function (a, b) {
17+
return a.age - b.age
18+
})
19+
assert.equalObjects(users, [
20+
{id: userId1, name: 'John', age: 20, profileId: null},
21+
{id: userId2, name: 'John', age: 30, profileId: null}
22+
])
23+
24+
var users2 = yield adapter.updateAll(User, { name: 'Johnny' }, { name: 'John' })
25+
users2.sort(function (a, b) {
26+
return a.age - b.age
27+
})
28+
assert.equalObjects(users2, [
29+
{id: userId1, name: 'Johnny', age: 20, profileId: null},
30+
{id: userId2, name: 'Johnny', age: 30, profileId: null}
31+
])
32+
33+
var users3 = yield adapter.findAll(User, { name: 'John' })
34+
assert.equalObjects(users3, [])
35+
assert.equal(users3.length, 0)
36+
37+
var users4 = yield adapter.findAll(User, { name: 'Johnny' })
38+
users4.sort(function (a, b) {
39+
return a.age - b.age
40+
})
41+
assert.equalObjects(users4, [
42+
{id: userId1, name: 'Johnny', age: 20, profileId: null},
43+
{id: userId2, name: 'Johnny', age: 30, profileId: null}
44+
])
45+
46+
var destroyedUser = yield adapter.destroyAll(User)
47+
assert.isFalse(!!destroyedUser)
48+
})
49+
})
50+
}

0 commit comments

Comments
 (0)
Please sign in to comment.