Skip to content

Commit

Permalink
add upsertGraph tests for extra properties
Browse files Browse the repository at this point in the history
  • Loading branch information
koskimas committed Dec 28, 2018
1 parent 53fed10 commit 20e3eea
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 15 deletions.
1 change: 1 addition & 0 deletions lib/queryBuilder/graph/patch/GraphPatchAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class GraphPatchAction extends GraphAction {
shouldPatch || shouldUpdate
? changedProps
: [...changedPropsBecauseOfBelongsToOneInsert, ...changePropsBecauseOfBelongsToOneDelete],

// Remove id properties from the props to update. With upsertGraph
// it never makes sense to change the id.
node.modelClass.getIdPropertyArray()
Expand Down
14 changes: 10 additions & 4 deletions setup-test-db.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,22 @@ const mysql = knex({
}
});

Promise.all([
[
postgres.raw('DROP DATABASE IF EXISTS objection_test'),
postgres.raw('DROP USER IF EXISTS objection'),
postgres.raw('CREATE USER objection SUPERUSER'),
postgres.raw('CREATE DATABASE objection_test'),

mysql.raw('DROP DATABASE IF EXISTS objection_test'),
mysql.raw('DROP USER IF EXISTS objection'),
mysql.raw('CREATE USER objection'),
mysql.raw('GRANT ALL PRIVILEGES ON *.* TO objection'),
mysql.raw('CREATE DATABASE objection_test')
]).then(() => {
Promise.all([
].reduce((promise, query) => {
return promise.then(() => query);
}, Promise.resolve()).then(() => {
return Promise.all([
postgres.destroy(),
mysql.destroy()
]);
});
});
7 changes: 7 additions & 0 deletions testUtils/TestSession.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class TestSession {

static get namedFilters() {
return {
'orderById': builder => builder.orderBy('Model1.id'),
'select:id': builder => builder.select(this.ref('id')),
'select:model1Prop1': builder => builder.select('model1Prop1'),
'select:model1Prop1Aliased': builder => builder.select('model1Prop1 as aliasedInFilter'),
Expand Down Expand Up @@ -112,6 +113,12 @@ class TestSession {
return snakeCaseMappers();
}

static get modifiers() {
return {
orderById: builder => builder.orderBy('model2.id_col')
};
}

static get relationMappings() {
return {
model2Relation1: {
Expand Down
3 changes: 2 additions & 1 deletion tests/integration/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -1705,7 +1705,8 @@ module.exports = session => {
});
});

it('namedFilters should work as an alias for modifiers', () => {
// No need for this test since the test models use both `modifiers` and `namedFilters`.
it.skip('namedFilters should work as an alias for modifiers', () => {
class TestModel2 extends Model2 {
static get namedFilters() {
return {
Expand Down
124 changes: 114 additions & 10 deletions tests/integration/upsertGraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -729,14 +729,18 @@ module.exports = session => {
const upsert = {
id: 1,

// Should relate these.
// relate 1, 2
// insert 'new'
model1Relation2: [
{
idCol: 1,
model2Prop1: 'also update'
},
{
idCol: 2
},
{
model2Prop1: 'new'
}
]
};
Expand All @@ -749,7 +753,8 @@ module.exports = session => {
.eager('model1Relation2');
})
.then(result => {
expect(result.model1Relation2).to.have.length(2);
expect(result.model1Relation2).to.have.length(3);

chai.expect(result).to.containSubset({
id: 1,
model1Id: null,
Expand All @@ -764,6 +769,10 @@ module.exports = session => {
idCol: 2,
model1Id: 1,
model2Prop1: 'hasMany 2'
},
{
model1Id: 1,
model2Prop1: 'new'
}
]
});
Expand Down Expand Up @@ -2698,19 +2707,114 @@ module.exports = session => {
});
});

// describe.skip('HasMany relates', () => {});
describe('manytoManyRelation extra properties', () => {
it('insert', () => {
const upsert = {
idCol: 2,

describe.skip('manytoManyRelation extra properties', () => {
it('test inserts', () => {
throw new Error();
model2Relation1: [
// Do nothing.
{
id: 6
},
// Do nothing.
{
id: 7
},
// Insert.
{
aliasedExtra: 'foo'
}
]
};

return transaction(session.knex, trx => {
return Model2.query(trx)
.upsertGraph(upsert)
.then(result => {
expect(result.model2Relation1[2].aliasedExtra).to.equal('foo');
});
})
.then(() => {
return Model2.query(session.knex)
.findById(2)
.eager('model2Relation1(orderById)');
})
.then(model => {
expect(model.model2Relation1[2].aliasedExtra).to.equal('foo');
});
});

it('test relates', () => {
throw new Error();
it('relate', () => {
const upsert = {
idCol: 2,

// delete 6
model2Relation1: [
// relate
{
id: 5,
aliasedExtra: 'foo'
},
// do nothing.
{
id: 7
}
]
};

return transaction(session.knex, trx => {
return Model2.query(trx)
.upsertGraph(upsert, { relate: true })
.then(result => {
expect(result.model2Relation1[0].id).to.equal(5);
expect(result.model2Relation1[0].aliasedExtra).to.equal('foo');
});
})
.then(() => {
return Model2.query(session.knex)
.findById(2)
.eager('model2Relation1(orderById)');
})
.then(model => {
expect(model.model2Relation1[0].id).to.equal(5);
expect(model.model2Relation1[0].aliasedExtra).to.equal('foo');
});
});

it('test updates', () => {
throw new Error();
it('update', () => {
const upsert = {
idCol: 2,

model2Relation1: [
{
id: 6,
aliasedExtra: 'hello extra 1'
},
{
id: 7,
aliasedExtra: 'hello extra 2'
}
]
};

return transaction(session.knex, trx => {
return Model2.query(trx)
.upsertGraph(upsert)
.then(result => {
expect(result.model2Relation1[0].aliasedExtra).to.equal('hello extra 1');
expect(result.model2Relation1[1].aliasedExtra).to.equal('hello extra 2');
});
})
.then(() => {
return Model2.query(session.knex)
.findById(2)
.eager('model2Relation1(orderById)');
})
.then(model => {
expect(model.model2Relation1[0].aliasedExtra).to.equal('hello extra 1');
expect(model.model2Relation1[1].aliasedExtra).to.equal('hello extra 2');
});
});
});
});
Expand Down

0 comments on commit 20e3eea

Please sign in to comment.