Skip to content

Commit

Permalink
Adding a unit test for binary IDs and relations... (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
6utt3rfly committed Nov 19, 2018
1 parent 8b00040 commit 98701df
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
17 changes: 14 additions & 3 deletions test/integration/helpers/migration.js
Expand Up @@ -7,6 +7,8 @@ var drops = [
'admins_sites',
'authors',
'authors_posts',
'critics',
'critics_comments',
'blogs',
'posts',
'tags',
Expand Down Expand Up @@ -99,6 +101,15 @@ module.exports = function(Bookshelf) {
table.string('first_name');
table.string('last_name');
})
.createTable('critics', function(table) {
table.binary('id', 16).primary();
table.string('name');
})
.createTable('critics_comments', function(table) {
table.increments();
table.binary('critic_id', 16).notNullable();
table.string('comment');
})
.createTable('posts', function(table) {
table.increments('id');
table.integer('owner_id').notNullable();
Expand Down Expand Up @@ -147,9 +158,9 @@ module.exports = function(Bookshelf) {
table.string('imageable_type');
})
/* The following table is for testing non-standard morphTo column name
* specification. The breaking of naming convention is intentional.
* Changing it back to snake_case will break the tests!
*/
* specification. The breaking of naming convention is intentional.
* Changing it back to snake_case will break the tests!
*/
.createTable('thumbnails', function(table) {
table.increments('id');
table.string('url');
Expand Down
17 changes: 17 additions & 0 deletions test/integration/helpers/objects.js
Expand Up @@ -121,6 +121,21 @@ module.exports = function(Bookshelf) {
}
});

// Critic uses binary ID
var Critic = Bookshelf.Model.extend({
tableName: 'critics',
comments: function() {
return this.hasMany(CriticComment);
}
});

var CriticComment = Bookshelf.Model.extend({
tableName: 'critics_comments',
critic: function() {
return this.belongsTo(Critic);
}
});

// A blog for a site.
var Blog = Bookshelf.Model.extend({
tableName: 'blogs',
Expand Down Expand Up @@ -414,6 +429,8 @@ module.exports = function(Bookshelf) {
TestAuthor: TestAuthor,
Author: Author,
AuthorParsed: AuthorParsed,
Critic: Critic,
CriticComment: CriticComment,
Backup: Backup,
BackupType: BackupType,
Blog: Blog,
Expand Down
33 changes: 33 additions & 0 deletions test/integration/relations.js
Expand Up @@ -29,6 +29,8 @@ module.exports = function(Bookshelf) {
var Site = Models.Site;
var Admin = Models.Admin;
var Author = Models.Author;
var Critic = Models.Critic;
var CriticComment = Models.CriticComment;
var Blog = Models.Blog;
var Post = Models.Post;
var Comment = Models.Comment;
Expand Down Expand Up @@ -1388,5 +1390,36 @@ module.exports = function(Bookshelf) {
return new Locale({isoCode: 'en'}).fetch({withRelated: 'customersThrough'}).then(checkTest(this));
});
});

describe.only('Issue #xx - Binary ID relations', function() {
it('should group relations properly with binary ID columns', function() {
const critic1Id = new Buffer('93', 'hex');
const critic2Id = new Buffer('90', 'hex');
const critic1 = new Critic({id: critic1Id, name: '1'});
const critic2 = new Critic({id: critic2Id, name: '2'});
const comment1 = new CriticComment({critic_id: critic1Id, comment: 'c1-1'});
const comment2 = new CriticComment({critic_id: critic1Id, comment: 'c1-2'});
const comment3 = new CriticComment({critic_id: critic2Id, comment: 'c2-1'});
return Promise.all([
critic1.save(null, {method: 'insert'}),
critic2.save(null, {method: 'insert'}),
comment1.save(),
comment2.save(),
comment3.save()
])
.then(function() {
return Critic.where('name', 'IN', ['1', '2'])
.orderBy('name', 'ASC')
.fetchAll({debug: true, withRelated: 'comments'});
})
.then(function(critics) {
critics = critics.serialize();
console.log(critics);
expect(critics).to.have.lengthOf(2);
expect(critics[0].comments).to.have.lengthOf(2);
expect(critics[1].comments).to.have.lengthOf(1);
});
});
});
});
};

0 comments on commit 98701df

Please sign in to comment.