Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a1de652

Browse files
igor-savin-htkoskimas
authored andcommitted
Test to illustrate problem with children instantiation
refs: Vincit#777
1 parent eaab724 commit a1de652

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

tests/integration/upsertGraph.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,63 @@ module.exports = session => {
897897
});
898898
});
899899

900+
it('should be able to automatically convert children that are plain JS objects into entities', () => {
901+
const parent = Model1.fromJson({
902+
id: 2,
903+
model1Prop1: null
904+
});
905+
906+
const children = [
907+
{
908+
idCol: 1,
909+
model2Relation1: [
910+
{
911+
id: 4,
912+
model1Prop1: null
913+
}
914+
]
915+
}
916+
];
917+
parent.model1Relation2 = children;
918+
919+
return transaction(session.knex, trx => {
920+
return Model1.query(trx)
921+
.upsertGraph(parent, { unrelate: true, relate: true })
922+
.then(result => {
923+
// Fetch the graph from the database.
924+
return Model1.query(trx)
925+
.findById(2)
926+
.eager('[model1Relation2.model2Relation1]')
927+
.modifyEager('model1Relation2', qb => qb.orderBy('id_col'))
928+
.modifyEager('model1Relation2.model2Relation1', qb => qb.orderBy('id'));
929+
})
930+
.then(omitIrrelevantProps)
931+
.then(result => {
932+
expect(result).to.eql({
933+
id: 2,
934+
model1Id: 3,
935+
model1Prop1: null,
936+
937+
model1Relation2: [
938+
{
939+
idCol: 1,
940+
model1Id: 2,
941+
model2Prop1: 'hasMany 1',
942+
943+
model2Relation1: [
944+
{
945+
id: 4,
946+
model1Id: null,
947+
model1Prop1: null
948+
}
949+
]
950+
}
951+
]
952+
});
953+
});
954+
});
955+
});
956+
900957
it('should respect noRelate and noUnrelate flags', () => {
901958
const upsert = {
902959
// the root gets updated because it has an id

0 commit comments

Comments
 (0)