Skip to content

Commit c656b9f

Browse files
committed
Merge pull request #20 from techniq/co-mocha
Use co-mocha for tests to simplify async flow. Fixes #19
2 parents 6d8b457 + 1d341ba commit c656b9f

12 files changed

+287
-363
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ When submitting issues on GitHub, please include as much detail as possible to m
1818
1. `cd js-data-sql; npm install; bower install;`
1919
1. Write your code, including relevant documentation and tests
2020
1. Run `grunt test` (build and test)
21+
- You need io.js or Node 4.x that includes generator support without a flag
2122
1. Your code will be linted and checked for formatting, the tests will be run
2223
1. The `dist/` folder & files will be generated, do NOT commit `dist/*`! They will be committed when a release is cut.
2324
1. Submit your PR and we'll review!

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ When submitting issues on GitHub, please include as much detail as possible to m
7676
1. `cd js-data-sql; npm install; bower install;`
7777
1. Write your code, including relevant documentation and tests
7878
1. Run `grunt test` (build and test)
79+
- You need io.js or Node 4.x that includes generator support without a flag
7980
1. Your code will be linted and checked for formatting, the tests will be run
8081
1. The `dist/` folder & files will be generated, do NOT commit `dist/*`! They will be committed when a release is cut.
8182
1. Submit your PR and we'll review!

circle.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
machine:
2+
node:
3+
version: 4.1.0
4+
15
database:
26
override:
37
- mysql -u ubuntu circle_test < test/setup.sql

mocha.start.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ assert.equalObjects = function (a, b, m) {
66
assert.deepEqual(JSON.parse(JSON.stringify(a)), JSON.parse(JSON.stringify(b)), m || 'Objects should be equal!');
77
};
88
var mocha = require('mocha');
9+
var coMocha = require('co-mocha');
10+
coMocha(mocha);
911
var sinon = require('sinon');
1012
var JSData = require('js-data');
1113
JSData.DSUtils.Promise = require('bluebird');

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"babel-loader": "5.3.2",
3131
"bluebird": "2.10.0",
3232
"chai": "3.2.0",
33+
"co-mocha": "^1.1.2",
3334
"grunt": "0.4.5",
3435
"grunt-contrib-watch": "0.6.1",
3536
"grunt-karma-coveralls": "2.5.4",

test/create.spec.js

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,23 @@
11
describe('DSSqlAdapter#create', function () {
2-
it('should create a user in a sql db', function () {
3-
var id;
4-
return adapter.create(User, {name: 'John'}).then(function (user) {
5-
id = user.id;
6-
assert.equal(user.name, 'John');
7-
assert.isDefined(user.id);
8-
return adapter.find(User, user.id);
9-
})
10-
.then(function (user) {
11-
assert.equal(user.name, 'John');
12-
assert.isDefined(user.id);
13-
assert.equalObjects(user, {id: id, name: 'John', age: null, profileId: null});
14-
return adapter.destroy(User, user.id);
15-
})
16-
.then(function (user) {
17-
assert.isFalse(!!user);
18-
return adapter.find(User, id);
19-
})
20-
.then(function () {
21-
throw new Error('Should not have reached here!');
22-
})
23-
.catch(function (err) {
24-
assert.equal(err.message, 'Not Found!');
25-
});
2+
it('should create a user in a sql db', function* () {
3+
var createUser = yield adapter.create(User, {name: 'John'});
4+
var id = createUser.id;
5+
assert.equal(createUser.name, 'John');
6+
assert.isDefined(createUser.id);
7+
8+
var findUser = yield adapter.find(User, createUser.id);
9+
assert.equal(findUser.name, 'John');
10+
assert.isDefined(findUser.id);
11+
assert.equalObjects(findUser, {id: id, name: 'John', age: null, profileId: null});
12+
13+
var destoryUser = yield adapter.destroy(User, findUser.id);
14+
assert.isFalse(!!destoryUser);
15+
16+
try {
17+
var findUser2 = yield adapter.find(User, id);
18+
throw new Error('Should not have reached here!');
19+
} catch(err) {
20+
assert.equal(err.message, 'Not Found!');
21+
}
2622
});
2723
});

test/destroy.spec.js

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
describe('DSSqlAdapter#destroy', function () {
2-
it('should destroy a user from a Sql db', function () {
3-
var id;
4-
return adapter.create(User, {name: 'John'})
5-
.then(function (user) {
6-
id = user.id;
7-
return adapter.destroy(User, user.id);
8-
})
9-
.then(function (user) {
10-
assert.isFalse(!!user);
11-
return adapter.find(User, id);
12-
})
13-
.then(function () {
14-
throw new Error('Should not have reached here!');
15-
})
16-
.catch(function (err) {
17-
assert.equal(err.message, 'Not Found!');
18-
});
2+
it('should destroy a user from a Sql db', function* () {
3+
var createUser = yield adapter.create(User, {name: 'John'})
4+
var id = createUser.id;
5+
6+
var destroyUser = yield adapter.destroy(User, createUser.id);
7+
assert.isFalse(!!destroyUser);
8+
9+
try {
10+
var findUser = yield adapter.find(User, id);
11+
throw new Error('Should not have reached here!');
12+
} catch (err) {
13+
assert.equal(err.message, 'Not Found!');
14+
}
1915
});
2016
});

test/destroyAll.spec.js

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,14 @@
11
describe('DSSqlAdapter#destroyAll', function () {
2-
it('should destroy all items', function () {
3-
var id;
4-
return adapter.create(User, {name: 'John'})
5-
.then(function (user) {
6-
id = user.id;
7-
return adapter.findAll(User, {
8-
name: 'John'
9-
});
10-
}).then(function (users) {
11-
assert.equal(users.length, 1);
12-
assert.equalObjects(users[0], {id: id, name: 'John', age: null, profileId: null});
13-
return adapter.destroyAll(User, {
14-
name: 'John'
15-
});
16-
}).then(function () {
17-
return adapter.findAll(User, {
18-
name: 'John'
19-
});
20-
}).then(function (users) {
21-
assert.equal(users.length, 0);
22-
});
2+
it('should destroy all items', function* () {
3+
var createUser = yield adapter.create(User, {name: 'John'});
4+
var id = createUser.id;
5+
6+
var findUsers = yield adapter.findAll(User, { name: 'John' });
7+
assert.equal(findUsers.length, 1);
8+
assert.equalObjects(findUsers[0], {id: id, name: 'John', age: null, profileId: null});
9+
10+
var destroyUser = yield adapter.destroyAll(User, { name: 'John' });
11+
var findUsers2 = yield adapter.findAll(User, { name: 'John' });
12+
assert.equal(findUsers2.length, 0);
2313
});
2414
});

test/find.spec.js

Lines changed: 80 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,85 @@
11
var Promise = require('bluebird');
22
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
1230
})
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: '[email protected]' });
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: '[email protected]' });
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);
7383
});
84+
7485
});

0 commit comments

Comments
 (0)