Skip to content

Use co-mocha for tests to simplify async flow. Fixes #19 #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 4, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ When submitting issues on GitHub, please include as much detail as possible to m
1. `cd js-data-sql; npm install; bower install;`
1. Write your code, including relevant documentation and tests
1. Run `grunt test` (build and test)
- You need io.js or Node 4.x that includes generator support without a flag
1. Your code will be linted and checked for formatting, the tests will be run
1. The `dist/` folder & files will be generated, do NOT commit `dist/*`! They will be committed when a release is cut.
1. Submit your PR and we'll review!
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ When submitting issues on GitHub, please include as much detail as possible to m
1. `cd js-data-sql; npm install; bower install;`
1. Write your code, including relevant documentation and tests
1. Run `grunt test` (build and test)
- You need io.js or Node 4.x that includes generator support without a flag
1. Your code will be linted and checked for formatting, the tests will be run
1. The `dist/` folder & files will be generated, do NOT commit `dist/*`! They will be committed when a release is cut.
1. Submit your PR and we'll review!
Expand Down
4 changes: 4 additions & 0 deletions circle.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
machine:
node:
version: 4.1.0

database:
override:
- mysql -u ubuntu circle_test < test/setup.sql
Expand Down
2 changes: 2 additions & 0 deletions mocha.start.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ assert.equalObjects = function (a, b, m) {
assert.deepEqual(JSON.parse(JSON.stringify(a)), JSON.parse(JSON.stringify(b)), m || 'Objects should be equal!');
};
var mocha = require('mocha');
var coMocha = require('co-mocha');
coMocha(mocha);
var sinon = require('sinon');
var JSData = require('js-data');
JSData.DSUtils.Promise = require('bluebird');
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"babel-loader": "5.3.2",
"bluebird": "2.10.0",
"chai": "3.2.0",
"co-mocha": "^1.1.2",
"grunt": "0.4.5",
"grunt-contrib-watch": "0.6.1",
"grunt-karma-coveralls": "2.5.4",
Expand Down
44 changes: 20 additions & 24 deletions test/create.spec.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
describe('DSSqlAdapter#create', function () {
it('should create a user in a sql db', function () {
var id;
return adapter.create(User, {name: 'John'}).then(function (user) {
id = user.id;
assert.equal(user.name, 'John');
assert.isDefined(user.id);
return adapter.find(User, user.id);
})
.then(function (user) {
assert.equal(user.name, 'John');
assert.isDefined(user.id);
assert.equalObjects(user, {id: id, name: 'John', age: null, profileId: null});
return adapter.destroy(User, user.id);
})
.then(function (user) {
assert.isFalse(!!user);
return adapter.find(User, id);
})
.then(function () {
throw new Error('Should not have reached here!');
})
.catch(function (err) {
assert.equal(err.message, 'Not Found!');
});
it('should create a user in a sql db', function* () {
var createUser = yield adapter.create(User, {name: 'John'});
var id = createUser.id;
assert.equal(createUser.name, 'John');
assert.isDefined(createUser.id);

var findUser = yield adapter.find(User, createUser.id);
assert.equal(findUser.name, 'John');
assert.isDefined(findUser.id);
assert.equalObjects(findUser, {id: id, name: 'John', age: null, profileId: null});

var destoryUser = yield adapter.destroy(User, findUser.id);
assert.isFalse(!!destoryUser);

try {
var findUser2 = yield adapter.find(User, id);
throw new Error('Should not have reached here!');
} catch(err) {
assert.equal(err.message, 'Not Found!');
}
});
});
30 changes: 13 additions & 17 deletions test/destroy.spec.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
describe('DSSqlAdapter#destroy', function () {
it('should destroy a user from a Sql db', function () {
var id;
return adapter.create(User, {name: 'John'})
.then(function (user) {
id = user.id;
return adapter.destroy(User, user.id);
})
.then(function (user) {
assert.isFalse(!!user);
return adapter.find(User, id);
})
.then(function () {
throw new Error('Should not have reached here!');
})
.catch(function (err) {
assert.equal(err.message, 'Not Found!');
});
it('should destroy a user from a Sql db', function* () {
var createUser = yield adapter.create(User, {name: 'John'})
var id = createUser.id;

var destroyUser = yield adapter.destroy(User, createUser.id);
assert.isFalse(!!destroyUser);

try {
var findUser = yield adapter.find(User, id);
throw new Error('Should not have reached here!');
} catch (err) {
assert.equal(err.message, 'Not Found!');
}
});
});
32 changes: 11 additions & 21 deletions test/destroyAll.spec.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
describe('DSSqlAdapter#destroyAll', function () {
it('should destroy all items', function () {
var id;
return adapter.create(User, {name: 'John'})
.then(function (user) {
id = user.id;
return adapter.findAll(User, {
name: 'John'
});
}).then(function (users) {
assert.equal(users.length, 1);
assert.equalObjects(users[0], {id: id, name: 'John', age: null, profileId: null});
return adapter.destroyAll(User, {
name: 'John'
});
}).then(function () {
return adapter.findAll(User, {
name: 'John'
});
}).then(function (users) {
assert.equal(users.length, 0);
});
it('should destroy all items', function* () {
var createUser = yield adapter.create(User, {name: 'John'});
var id = createUser.id;

var findUsers = yield adapter.findAll(User, { name: 'John' });
assert.equal(findUsers.length, 1);
assert.equalObjects(findUsers[0], {id: id, name: 'John', age: null, profileId: null});

var destroyUser = yield adapter.destroyAll(User, { name: 'John' });
var findUsers2 = yield adapter.findAll(User, { name: 'John' });
assert.equal(findUsers2.length, 0);
});
});
149 changes: 80 additions & 69 deletions test/find.spec.js
Original file line number Diff line number Diff line change
@@ -1,74 +1,85 @@
var Promise = require('bluebird');
describe('DSSqlAdapter#find', function () {
it('should find a user in a Sql db', function () {
var id, id2, _user, _post, _comments;
return adapter.create(User, {name: 'John'})
.then(function (user) {
_user = user;
id = user.id;
assert.equal(user.name, 'John');
assert.isDefined(user.id);
return adapter.find(User, user.id);
it('should find a user in a Sql db', function* () {
var user = yield adapter.create(User, {name: 'John'});
var userId = user.id;
assert.equal(user.name, 'John');
assert.isDefined(user.id);

var user2 = yield adapter.find(User, user.id);
assert.equal(user2.name, 'John');
assert.isDefined(user2.id);
assert.equalObjects(user2, {id: userId, name: 'John', age: null, profileId: null});

var post = yield adapter.create(Post, { content: 'test', userId: userId });
var postId = post.id;
assert.equal(post.content, 'test');
assert.isDefined(post.id);
assert.isDefined(post.userId);

var comments = yield [
adapter.create(Comment, {
content: 'test2',
postId: post.id,
userId: user.id
}),
adapter.create(Comment, {
content: 'test3',
postId: post.id,
userId: user.id
})
.then(function (user) {
assert.equal(user.name, 'John');
assert.isDefined(user.id);
assert.equalObjects(user, {id: id, name: 'John', age: null, profileId: null});
return adapter.create(Post, {
content: 'test',
userId: user.id
});
})
.then(function (post) {
_post = post;
id2 = post.id;
assert.equal(post.content, 'test');
assert.isDefined(post.id);
assert.isDefined(post.userId);
return Promise.all([
adapter.create(Comment, {
content: 'test2',
postId: post.id,
userId: _user.id
}),
adapter.create(Comment, {
content: 'test3',
postId: post.id,
userId: _user.id
})
]);
})
.then(function (comments) {
_comments = comments;
_comments.sort(function (a, b) {
return a.content > b.content;
});
return adapter.find(Post, _post.id, {with: ['user', 'comment']});
})
.then(function (post) {
post.comments.sort(function (a, b) {
return a.content > b.content;
});
assert.equalObjects(post.user, _user);
assert.equalObjects(post.comments, _comments);
return adapter.destroyAll(Comment);
})
.then(function () {
return adapter.destroy(Post, id2);
})
.then(function () {
return adapter.destroy(User, id);
})
.then(function (user) {
assert.isFalse(!!user);
return adapter.find(User, id);
})
.then(function () {
throw new Error('Should not have reached here!');
})
.catch(function (err) {
console.log(err.stack);
assert.equal(err.message, 'Not Found!');
});
];

comments.sort(function (a, b) {
return a.content > b.content;
});

var findPost = yield adapter.find(Post, postId, {with: ['user', 'comment']});
findPost.comments.sort(function (a, b) {
return a.content > b.content;
});
assert.equalObjects(findPost.user, user);
assert.equalObjects(findPost.comments, comments);

yield adapter.destroyAll(Comment);
yield adapter.destroy(Post, postId);
var destroyUser = yield adapter.destroy(User, userId);
assert.isFalse(!!destroyUser);

try {
yield adapter.find(User, userId);
throw new Error('Should not have reached here!');
} catch (err) {
console.log(err.stack);
assert.equal(err.message, 'Not Found!');
}
});

it('should load belongsTo relations', function* () {
var profile = yield adapter.create(Profile, { email: '[email protected]' });
var user = yield adapter.create(User, {name: 'John', profileId: profile.id});
var post = yield adapter.create(Post, {content: 'foo', userId: user.id});
var comment = yield adapter.create(Comment, { content: 'test2', postId: post.id, userId: post.userId });

var comment = yield adapter.find(Comment, comment.id, {'with': ['user', 'user.profile', 'post', 'post.user']});
assert.isDefined(comment);
assert.isDefined(comment.post);
assert.isDefined(comment.post.user);
assert.isDefined(comment.user);
assert.isDefined(comment.user.profile);
});

it('should load hasMany and belongsTo relations', function* () {
var profile = yield adapter.create(Profile, { email: '[email protected]' });
var user = yield adapter.create(User, {name: 'John', profileId: profile.id});
var post = yield adapter.create(Post, {content: 'foo', userId: user.id});
var comment = yield adapter.create(Comment, { content: 'test2', postId: post.id, userId: post.userId });

var foundPost = yield adapter.find(Post, post.id, {'with': ['user', 'comment', 'comment.user', 'comment.user.profile']});
assert.isDefined(foundPost.comments);
assert.isDefined(foundPost.comments[0].user);
assert.isDefined(foundPost.comments[0].user.profile);
assert.isDefined(foundPost.user);
});

});
Loading