Merge pull request #462 from strongloop/feature/fix-ids-tests

Feature/fix ids tests
This commit is contained in:
Raymond Feng 2015-02-20 21:46:44 -08:00
commit d92323a40c
3 changed files with 160 additions and 64 deletions

View File

@ -60,9 +60,8 @@ describe('basic-querying', function () {
});
describe('findByIds', function () {
var createdUsers;
before(function(done) {
// FIXME: [rfeng] The autogenerated ids are not always 1-6
var people = [
{ id: 1, name: 'a', vip: true },
{ id: 2, name: 'b' },
@ -71,33 +70,52 @@ describe('basic-querying', function () {
{ id: 5, name: 'e' },
{ id: 6, name: 'f' }
];
// Use automigrate so that serial keys are 1-6
db.automigrate(['User'], function(err) {
User.create(people, function(err, users) {
should.not.exist(err);
// Users might be created in parallel and the generated ids can be
// out of sequence
createdUsers = users;
done();
});
});
});
it('should query by ids', function (done) {
User.findByIds([3, 2, 1], function (err, users) {
should.exist(users);
should.not.exist(err);
var names = users.map(function(u) { return u.name; });
names.should.eql(['c', 'b', 'a']);
done();
});
it('should query by ids', function(done) {
User.findByIds(
[createdUsers[2].id, createdUsers[1].id, createdUsers[0].id],
function(err, users) {
should.exist(users);
should.not.exist(err);
var names = users.map(function(u) {
return u.name;
});
names.should.eql(
[createdUsers[2].name, createdUsers[1].name, createdUsers[0].name]);
done();
});
});
it('should query by ids and condition', function (done) {
User.findByIds([4, 3, 2, 1],
{ where: { vip: true } }, function (err, users) {
should.exist(users);
should.not.exist(err);
var names = users.map(function(u) { return u.name; });
names.should.eql(['d', 'a']);
done();
});
it('should query by ids and condition', function(done) {
User.findByIds([
createdUsers[0].id,
createdUsers[1].id,
createdUsers[2].id,
createdUsers[3].id],
{ where: { vip: true } }, function(err, users) {
should.exist(users);
should.not.exist(err);
var names = users.map(function(u) {
return u.name;
});
names.should.eql(createdUsers.slice(0, 4).
filter(function(u) {
return u.vip;
}).map(function(u) {
return u.name;
}));
done();
});
});
});

View File

@ -447,68 +447,109 @@ describe('manipulation', function () {
person.isNewRecord().should.be.true;
});
it('should report current date when using $now as default value for date property',
function (done) {
var CustomModel = db.define('CustomModel', {
describe('Date $now function', function() {
var CustomModel;
before(function(done) {
CustomModel = db.define('CustomModel1', {
createdAt: { type: Date, default: '$now' }
});
db.automigrate('CustomModel1', done);
});
var now = Date.now();
it('should report current date as default value for date property',
function(done) {
var now = Date.now();
var myCustomModel = CustomModel.create(function (err, m) {
m.createdAt.should.be.instanceOf(Date);
(m.createdAt >= now).should.be.true;
var myCustomModel = CustomModel.create(function(err, m) {
should.not.exists(err);
m.createdAt.should.be.instanceOf(Date);
(m.createdAt >= now).should.be.true;
});
done();
});
done();
});
it('should report \'$now\' when using $now as default value for string property',
function (done) {
var CustomModel = db.define('CustomModel', {
describe('Date $now function', function() {
var CustomModel;
before(function(done) {
CustomModel = db.define('CustomModel2', {
now: { type: String, default: '$now' }
});
db.automigrate('CustomModel2', done);
});
var myCustomModel = CustomModel.create(function (err, m) {
m.now.should.be.instanceOf(String);
m.now.should.equal('$now');
it('should report \'$now\' as default value for string property',
function(done) {
var myCustomModel = CustomModel.create(function(err, m) {
should.not.exists(err);
m.now.should.be.instanceOf(String);
m.now.should.equal('$now');
});
done();
});
done();
});
it('should generate a new id when "defaultFn" is "guid"', function (done) {
var CustomModel = db.define('CustomModel', {
guid: { type: String, defaultFn: 'guid' }
describe('now defaultFn', function() {
var CustomModel;
before(function(done) {
CustomModel = db.define('CustomModel3', {
now: { type: Date, defaultFn: 'now' }
});
db.automigrate('CustomModel3', done);
});
var inst = CustomModel.create(function (err, m) {
m.guid.should.match(UUID_REGEXP);
done();
it('should generate current time when "defaultFn" is "now"',
function(done) {
var now = Date.now();
var inst = CustomModel.create(function(err, m) {
should.not.exists(err);
m.now.should.be.instanceOf(Date);
m.now.should.be.within(now, now + 200);
done();
});
});
});
describe('guid defaultFn', function() {
var CustomModel;
before(function(done) {
CustomModel = db.define('CustomModel4', {
guid: { type: String, defaultFn: 'guid' }
});
db.automigrate('CustomModel4', done);
});
it('should generate a new id when "defaultFn" is "guid"', function(done) {
var inst = CustomModel.create(function(err, m) {
should.not.exists(err);
m.guid.should.match(UUID_REGEXP);
done();
});
});
});
it('should generate a new id when "defaultfn" is "uuid"', function (done) {
var CustomModel = db.define('custommodel', {
guid: { type: String, defaultFn: 'uuid' }
describe('uuid defaultFn', function() {
var CustomModel;
before(function(done) {
CustomModel = db.define('CustomModel5', {
guid: { type: String, defaultFn: 'uuid' }
});
db.automigrate('CustomModel5', done);
});
var inst = CustomModel.create(function (err, m) {
m.guid.should.match(UUID_REGEXP);
done();
});
});
it('should generate current time when "defaultFn" is "now"', function (done) {
var CustomModel = db.define('CustomModel', {
now: { type: Date, defaultFn: 'now' }
});
var now = Date.now();
var inst = CustomModel.create(function (err, m) {
m.now.should.be.instanceOf(Date);
m.now.should.be.within(now, now + 200);
done();
it('should generate a new id when "defaultfn" is "uuid"', function(done) {
var inst = CustomModel.create(function(err, m) {
should.not.exists(err);
m.guid.should.match(UUID_REGEXP);
done();
});
});
});

View File

@ -756,10 +756,13 @@ describe('relations', function () {
it('should create record on scope', function (done) {
Category.create(function (err, c) {
should.not.exists(err);
c.jobs.create({ type: 'book' }, function(err, p) {
should.not.exists(err);
p.categoryId.should.equal(c.id);
p.type.should.equal('book');
c.jobs.create({ type: 'widget' }, function(err, p) {
should.not.exists(err);
p.categoryId.should.equal(c.id);
p.type.should.equal('widget');
done();
@ -770,7 +773,9 @@ describe('relations', function () {
it('should find records on scope', function (done) {
Category.findOne(function (err, c) {
should.not.exists(err);
c.jobs(function(err, jobs) {
should.not.exists(err);
jobs.should.have.length(2);
done();
});
@ -779,7 +784,9 @@ describe('relations', function () {
it('should find record on scope - filtered', function (done) {
Category.findOne(function (err, c) {
should.not.exists(err);
c.jobs({ where: { type: 'book' } }, function(err, jobs) {
should.not.exists(err);
jobs.should.have.length(1);
jobs[0].type.should.equal('book');
done();
@ -798,6 +805,7 @@ describe('relations', function () {
it('should create record on scope - properties', function (done) {
Category.findOne(function (err, c) {
should.not.exists(err);
c.jobType = 'tool'; // temporary
c.jobs.create(function(err, p) {
p.categoryId.should.equal(c.id);
@ -809,7 +817,9 @@ describe('relations', function () {
it('should find records on scope', function (done) {
Category.findOne(function (err, c) {
should.not.exists(err);
c.jobs(function(err, jobs) {
should.not.exists(err);
jobs.should.have.length(3);
done();
});
@ -818,8 +828,10 @@ describe('relations', function () {
it('should find record on scope - scoped', function (done) {
Category.findOne(function (err, c) {
should.not.exists(err);
c.jobType = 'book'; // temporary, for scoping
c.jobs(function(err, jobs) {
should.not.exists(err);
jobs.should.have.length(1);
jobs[0].type.should.equal('book');
done();
@ -829,8 +841,10 @@ describe('relations', function () {
it('should find record on scope - scoped', function (done) {
Category.findOne(function (err, c) {
should.not.exists(err);
c.jobType = 'tool'; // temporary, for scoping
c.jobs(function(err, jobs) {
should.not.exists(err);
jobs.should.have.length(1);
jobs[0].type.should.equal('tool');
done();
@ -840,8 +854,10 @@ describe('relations', function () {
it('should find count of records on scope - scoped', function (done) {
Category.findOne(function (err, c) {
should.not.exists(err);
c.jobType = 'tool'; // temporary, for scoping
c.jobs.count(function(err, count) {
should.not.exists(err);
count.should.equal(1);
done();
});
@ -850,16 +866,19 @@ describe('relations', function () {
it('should delete records on scope - scoped', function (done) {
Category.findOne(function (err, c) {
should.not.exists(err);
c.jobType = 'tool'; // temporary, for scoping
c.jobs.destroyAll(function(err, result) {
done();
done(err);
});
});
});
it('should find record on scope - verify', function (done) {
Category.findOne(function (err, c) {
should.not.exists(err);
c.jobs(function(err, jobs) {
should.not.exists(err);
jobs.should.have.length(2);
done();
});
@ -893,6 +912,7 @@ describe('relations', function () {
it('should create polymorphic relation - author', function (done) {
Author.create({name: 'Author 1' }, function (err, author) {
should.not.exists(err);
author.avatar.create({ name: 'Avatar' }, function (err, p) {
should.not.exist(err);
should.exist(p);
@ -905,6 +925,7 @@ describe('relations', function () {
it('should create polymorphic relation - reader', function (done) {
Reader.create({name: 'Reader 1' }, function (err, reader) {
should.not.exists(err);
reader.mugshot.create({ name: 'Mugshot' }, function (err, p) {
should.not.exist(err);
should.exist(p);
@ -917,6 +938,7 @@ describe('relations', function () {
it('should find polymorphic relation - author', function (done) {
Author.findOne(function (err, author) {
should.not.exists(err);
author.avatar(function (err, p) {
should.not.exist(err);
@ -933,6 +955,7 @@ describe('relations', function () {
it('should find polymorphic relation - reader', function (done) {
Reader.findOne(function (err, reader) {
should.not.exists(err);
reader.mugshot(function (err, p) {
should.not.exist(err);
p.name.should.equal('Mugshot');
@ -945,6 +968,7 @@ describe('relations', function () {
it('should find inverse polymorphic relation - author', function (done) {
Picture.findOne({ where: { name: 'Avatar' } }, function (err, p) {
should.not.exists(err);
p.imageable(function (err, imageable) {
should.not.exist(err);
imageable.should.be.instanceof(Author);
@ -956,6 +980,7 @@ describe('relations', function () {
it('should find inverse polymorphic relation - reader', function (done) {
Picture.findOne({ where: { name: 'Mugshot' } }, function (err, p) {
should.not.exists(err);
p.imageable(function (err, imageable) {
should.not.exist(err);
imageable.should.be.instanceof(Reader);
@ -1017,6 +1042,7 @@ describe('relations', function () {
it('should create polymorphic relation - author', function (done) {
Author.create({name: 'Author 1' }, function (err, author) {
should.not.exists(err);
author.avatar.create({ name: 'Avatar' }, function (err, p) {
should.not.exist(err);
should.exist(p);
@ -1029,6 +1055,7 @@ describe('relations', function () {
it('should create polymorphic relation - reader', function (done) {
Reader.create({name: 'Reader 1' }, function (err, reader) {
should.not.exists(err);
reader.mugshot.create({ name: 'Mugshot' }, function (err, p) {
should.not.exist(err);
should.exist(p);
@ -1041,6 +1068,7 @@ describe('relations', function () {
it('should find polymorphic relation - author', function (done) {
Author.findOne(function (err, author) {
should.not.exists(err);
author.avatar(function (err, p) {
should.not.exist(err);
@ -1057,6 +1085,7 @@ describe('relations', function () {
it('should find polymorphic relation - reader', function (done) {
Reader.findOne(function (err, reader) {
should.not.exists(err);
reader.mugshot(function (err, p) {
should.not.exist(err);
p.name.should.equal('Mugshot');
@ -1069,6 +1098,7 @@ describe('relations', function () {
it('should find inverse polymorphic relation - author', function (done) {
Picture.findOne({ where: { name: 'Avatar' } }, function (err, p) {
should.not.exists(err);
p.owner(function (err, owner) {
should.not.exist(err);
owner.should.be.instanceof(Author);
@ -1080,6 +1110,7 @@ describe('relations', function () {
it('should find inverse polymorphic relation - reader', function (done) {
Picture.findOne({ where: { name: 'Mugshot' } }, function (err, p) {
should.not.exists(err);
p.owner(function (err, owner) {
should.not.exist(err);
owner.should.be.instanceof(Reader);
@ -1150,6 +1181,7 @@ describe('relations', function () {
it('should create polymorphic relation - author', function (done) {
Author.create({ name: 'Author 1' }, function (err, author) {
should.not.exists(err);
author.pictures.create({ name: 'Author Pic' }, function (err, p) {
should.not.exist(err);
should.exist(p);
@ -1162,6 +1194,7 @@ describe('relations', function () {
it('should create polymorphic relation - reader', function (done) {
Reader.create({ name: 'Reader 1' }, function (err, reader) {
should.not.exists(err);
reader.pictures.create({ name: 'Reader Pic' }, function (err, p) {
should.not.exist(err);
should.exist(p);
@ -1174,6 +1207,7 @@ describe('relations', function () {
it('should find polymorphic items - author', function (done) {
Author.findOne(function (err, author) {
should.not.exists(err);
author.pictures(function (err, pics) {
should.not.exist(err);
@ -1189,6 +1223,7 @@ describe('relations', function () {
it('should find polymorphic items - reader', function (done) {
Reader.findOne(function (err, reader) {
should.not.exists(err);
reader.pictures(function (err, pics) {
should.not.exist(err);
pics.should.have.length(1);
@ -1238,6 +1273,7 @@ describe('relations', function () {
it('should assign a polymorphic relation', function(done) {
Author.create({ name: 'Author 2' }, function(err, author) {
should.not.exists(err);
var p = new Picture({ name: 'Sample' });
p.imageable(author); // assign
p.imageableId.should.eql(author.id);
@ -1248,6 +1284,7 @@ describe('relations', function () {
it('should find polymorphic items - author', function (done) {
Author.findOne({ where: { name: 'Author 2' } }, function (err, author) {
should.not.exists(err);
author.pictures(function (err, pics) {
should.not.exist(err);
pics.should.have.length(1);