Skip to content

Commit

Permalink
Merge pull request #9 from manubb/testFix
Browse files Browse the repository at this point in the history
Tests fix and use recent objection BelongsToOne and HasMany relations
  • Loading branch information
kapouer authored Jan 13, 2017
2 parents c52c3c9 + 735a0f0 commit cdb1b1a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 21 deletions.
67 changes: 49 additions & 18 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,25 +364,46 @@ describe('integration tests', function () {
res.body = integerIds(res.body, 'id', 'ownerId');

expect(res.body).to.have.length(4);
expect(_.sortBy(res.body, 'id')).to.eql([
{name: 'Updated name 1', id: 34, ownerId: 4},
{name: 'Updated name 2', id: 37, ownerId: 4},
{name: 'New 1', id: numPersons * numAnimalsPerPerson + 1, ownerId: 4},
{name: 'New 2', id: numPersons * numAnimalsPerPerson + 2, ownerId: 4}
]);
var items = _.sortBy(res.body, 'id');
if (items[2].name === 'New 1') {
expect(items).to.eql([
{name: 'Updated name 1', id: 34, ownerId: 4},
{name: 'Updated name 2', id: 37, ownerId: 4},
{name: 'New 1', id: numPersons * numAnimalsPerPerson + 1, ownerId: 4},
{name: 'New 2', id: numPersons * numAnimalsPerPerson + 2, ownerId: 4}
]);
} else {
expect(items).to.eql([
{name: 'Updated name 1', id: 34, ownerId: 4},
{name: 'Updated name 2', id: 37, ownerId: 4},
{name: 'New 2', id: numPersons * numAnimalsPerPerson + 1, ownerId: 4},
{name: 'New 1', id: numPersons * numAnimalsPerPerson + 2, ownerId: 4}
]);
}

return session.knex('Animal');
})
.then(function (rows) {
rows = integerIds(rows, 'id', 'ownerId');

expect(rows).to.have.length(numPersons * numMoviesPerPerson - (numAnimalsPerPerson - 4));
expect(_.sortBy(_.where(rows, {ownerId: 4}), 'id')).to.eql([
{name: 'Updated name 1', id: 34, ownerId: 4},
{name: 'Updated name 2', id: 37, ownerId: 4},
{name: 'New 1', id: numPersons * numAnimalsPerPerson + 1, ownerId: 4},
{name: 'New 2', id: numPersons * numAnimalsPerPerson + 2, ownerId: 4}
]);
var items = _.sortBy(_.where(rows, {ownerId: 4}), 'id');
expect(items).to.have.length(4);
if (items[2].name === 'New 1') {
expect(items).to.eql([
{name: 'Updated name 1', id: 34, ownerId: 4},
{name: 'Updated name 2', id: 37, ownerId: 4},
{name: 'New 1', id: numPersons * numAnimalsPerPerson + 1, ownerId: 4},
{name: 'New 2', id: numPersons * numAnimalsPerPerson + 2, ownerId: 4}
]);
} else {
expect(items).to.eql([
{name: 'Updated name 1', id: 34, ownerId: 4},
{name: 'Updated name 2', id: 37, ownerId: 4},
{name: 'New 2', id: numPersons * numAnimalsPerPerson + 1, ownerId: 4},
{name: 'New 1', id: numPersons * numAnimalsPerPerson + 2, ownerId: 4}
]);
}
});
});

Expand Down Expand Up @@ -481,12 +502,22 @@ describe('integration tests', function () {
res.body = integerIds(res.body, 'id');

expect(res.body).to.have.length(4);
expect(_.sortBy(res.body, 'id')).to.eql([
{name: 'Updated name 1', id: 64},
{name: 'Updated name 2', id: 67},
{name: 'New 1', id: numPersons * numMoviesPerPerson + 1},
{name: 'New 2', id: numPersons * numMoviesPerPerson + 2}
]);
var items = _.sortBy(res.body, 'id');
if (items[2].name === 'New 1') {
expect(items).to.eql([
{name: 'Updated name 1', id: 64},
{name: 'Updated name 2', id: 67},
{name: 'New 1', id: numPersons * numMoviesPerPerson + 1},
{name: 'New 2', id: numPersons * numMoviesPerPerson + 2}
]);
} else {
expect(items).to.eql([
{name: 'Updated name 1', id: 64},
{name: 'Updated name 2', id: 67},
{name: 'New 2', id: numPersons * numMoviesPerPerson + 1},
{name: 'New 1', id: numPersons * numMoviesPerPerson + 2}
]);
}

return session.knex('Movie');
})
Expand Down
6 changes: 3 additions & 3 deletions tests/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ function createModels(knex) {

Person.relationMappings = {
parent: {
relation: objection.OneToOneRelation,
relation: objection.BelongsToOneRelation,
modelClass: Person,
join: {
from: 'Person.pid',
Expand All @@ -209,7 +209,7 @@ function createModels(knex) {
},

children: {
relation: objection.OneToManyRelation,
relation: objection.HasManyRelation,
modelClass: Person,
join: {
from: 'Person.id',
Expand All @@ -218,7 +218,7 @@ function createModels(knex) {
},

pets: {
relation: objection.OneToManyRelation,
relation: objection.HasManyRelation,
modelClass: Animal,
join: {
from: 'Person.id',
Expand Down

0 comments on commit cdb1b1a

Please sign in to comment.