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

Skip to content

Commit 1536f96

Browse files
committed
add tests for some BelongsToOne upsertGraph corner cases
1 parent 20e3eea commit 1536f96

File tree

4 files changed

+168
-23
lines changed

4 files changed

+168
-23
lines changed

lib/queryBuilder/graph/GraphOptions.js

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,31 @@ class GraphOptions {
5656
return !this._hasOption(node, NO_INSERT) && this.shouldInsertIgnoreDisable(node, currentGraph);
5757
}
5858

59+
shouldPatchOrUpdateIgnoreDisable(node, currentGraph) {
60+
if (this.shouldRelate(node)) {
61+
// We should update all nodes that are going to be related. Note that
62+
// we don't actually update anything unless there is something to update
63+
// so this is just a preliminary test.
64+
return true;
65+
}
66+
67+
return !!getCurrentNode(node, currentGraph);
68+
}
69+
5970
shouldPatch(node, currentGraph) {
60-
return this._shouldPatch(node, currentGraph) && !this._hasOption(node, UPDATE);
71+
return (
72+
this.shouldPatchOrUpdateIgnoreDisable(node, currentGraph) &&
73+
!this._hasOption(node, NO_UPDATE) &&
74+
!this._hasOption(node, UPDATE)
75+
);
6176
}
6277

6378
shouldUpdate(node, currentGraph) {
64-
return this._shouldPatch(node, currentGraph) && this._hasOption(node, UPDATE);
79+
return (
80+
this.shouldPatchOrUpdateIgnoreDisable(node, currentGraph) &&
81+
!this._hasOption(node, NO_UPDATE) &&
82+
this._hasOption(node, UPDATE)
83+
);
6584
}
6685

6786
shouldUnrelateIgnoreDisable(currentNode) {
@@ -92,10 +111,6 @@ class GraphOptions {
92111
return this.shouldDelete(currentNode, graph) || this.shouldUnrelate(currentNode, graph);
93112
}
94113

95-
shouldPatchOrUpdate(node, currentGraph) {
96-
return this.shouldPatch(node, currentGraph) || this.shouldUpdate(node, currentGraph);
97-
}
98-
99114
rebasedOptions(newRoot) {
100115
const newOpt = {};
101116
const newRootRelationPath = newRoot.relationPathKey;
@@ -116,17 +131,6 @@ class GraphOptions {
116131
return new GraphOptions(newOpt);
117132
}
118133

119-
_shouldPatch(node, currentGraph) {
120-
if (this.shouldRelate(node)) {
121-
// We should update all nodes that are going to be related. Note that
122-
// we don't actually update anything unless there is something to update
123-
// so this is just a preliminary test.
124-
return true;
125-
}
126-
127-
return !!getCurrentNode(node, currentGraph) && !this._hasOption(node, NO_UPDATE);
128-
}
129-
130134
_hasOption(node, optionName) {
131135
const option = this.options[optionName];
132136

lib/queryBuilder/graph/patch/GraphPatch.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class GraphPatch extends GraphOperation {
66
return [
77
new GraphPatchAction({
88
nodes: this.graph.nodes.filter(node =>
9-
this.graphOptions.shouldPatchOrUpdate(node, this.currentGraph)
9+
this.graphOptions.shouldPatchOrUpdateIgnoreDisable(node, this.currentGraph)
1010
),
1111

1212
graph: this.graph,

lib/queryBuilder/graph/patch/GraphPatchAction.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ class GraphPatchAction extends GraphAction {
5050
}
5151

5252
delete node.obj[node.modelClass.uidRefProp];
53+
delete node.obj[node.modelClass.dbRefProp];
54+
5355
node.obj.$validate(null, {
5456
dataPath: node.dataPathKey,
5557
patch: shouldPatch || (!shouldPatch && !shouldUpdate)
@@ -78,9 +80,10 @@ class GraphPatchAction extends GraphAction {
7880

7981
for (const edge of node.edges) {
8082
if (
81-
edge.isOwnerNode(currentNode) &&
83+
edge.isOwnerNode(node) &&
84+
edge.relation &&
8285
edge.relation.isObjectionBelongsToOneRelation &&
83-
this.graphOptions.shouldInsertOrRelate(edge.relatedNode, this.currentGraph)
86+
edge.relation.relatedProp.hasProps(edge.relatedNode.obj)
8487
) {
8588
const { relation } = edge;
8689

tests/integration/upsertGraph.js

Lines changed: 141 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,9 +1420,147 @@ module.exports = session => {
14201420
});
14211421
});
14221422

1423-
it.skip('should not update other properties than the related ones when belongsToOneRelation is inserted but the parent has noDelete: true', () => {});
1423+
it('should not update other than the relation properties when belongsToOneRelation is inserted but the parent has noUpdate: true', () => {
1424+
const upsert = {
1425+
id: 2,
1426+
1427+
model1Relation1: {
1428+
id: 3,
1429+
model1Prop1: 'this should not be written to db',
1430+
1431+
// This should cause the id=3 to be updated with the new
1432+
// model1Id property.
1433+
model1Relation1: {
1434+
model1Prop1: 'inserted'
1435+
}
1436+
}
1437+
};
1438+
1439+
return Model1.query(session.knex)
1440+
.upsertGraph(upsert, {
1441+
noUpdate: ['model1Relation1']
1442+
})
1443+
.then(() => {
1444+
// Fetch the graph from the database.
1445+
return Model1.query(session.knex)
1446+
.findById(2)
1447+
.eager('model1Relation1.model1Relation1');
1448+
})
1449+
.then(result => {
1450+
chai.expect(result).to.containSubset({
1451+
id: 2,
1452+
1453+
model1Relation1: {
1454+
id: 3,
1455+
model1Prop1: 'belongsToOne',
1456+
1457+
model1Relation1: {
1458+
model1Prop1: 'inserted'
1459+
}
1460+
}
1461+
});
1462+
});
1463+
});
1464+
1465+
it('should not update other than the relation properties when belongsToOneRelation is related but the parent has noUpdate: true', () => {
1466+
const upsert = {
1467+
id: 2,
1468+
1469+
model1Relation1: {
1470+
id: 3,
1471+
model1Prop1: 'this should not be written to db',
1472+
1473+
// This should cause the id=3 to be updated with the new
1474+
// model1Id property.
1475+
model1Relation1: {
1476+
id: 1
1477+
}
1478+
}
1479+
};
1480+
1481+
return Model1.query(session.knex)
1482+
.upsertGraph(upsert, {
1483+
noUpdate: ['model1Relation1'],
1484+
relate: ['model1Relation1.model1Relation1']
1485+
})
1486+
.then(() => {
1487+
// Fetch the graph from the database.
1488+
return Model1.query(session.knex)
1489+
.findById(2)
1490+
.eager('model1Relation1.model1Relation1');
1491+
})
1492+
.then(result => {
1493+
chai.expect(result).to.containSubset({
1494+
id: 2,
14241495

1425-
it.skip('should not update other properties than the related ones when belongsToOneRelation is deleted but the parent has noDelete: true', () => {});
1496+
model1Relation1: {
1497+
id: 3,
1498+
model1Prop1: 'belongsToOne',
1499+
1500+
model1Relation1: {
1501+
id: 1,
1502+
model1Prop1: 'root 1'
1503+
}
1504+
}
1505+
});
1506+
});
1507+
});
1508+
1509+
it('should not update other than the relation properties when belongsToOneRelation is unrelated but the parent has noUpdate: true', () => {
1510+
const upsert1 = {
1511+
id: 2,
1512+
1513+
model1Relation1: {
1514+
id: 3,
1515+
1516+
model1Relation1: {
1517+
id: 1
1518+
}
1519+
}
1520+
};
1521+
1522+
const upsert2 = {
1523+
id: 2,
1524+
1525+
model1Relation1: {
1526+
id: 3,
1527+
model1Prop1: 'this should not be written to db',
1528+
1529+
model1Relation1: null
1530+
}
1531+
};
1532+
1533+
return Model1.query(session.knex)
1534+
.upsertGraph(upsert1, {
1535+
noUpdate: ['model1Relation1'],
1536+
relate: ['model1Relation1.model1Relation1']
1537+
})
1538+
.then(() => {
1539+
return Model1.query(session.knex).upsertGraph(upsert2, {
1540+
noUpdate: ['model1Relation1'],
1541+
unrelate: ['model1Relation1.model1Relation1']
1542+
});
1543+
})
1544+
.then(() => {
1545+
// Fetch the graph from the database.
1546+
return Model1.query(session.knex)
1547+
.findById(2)
1548+
.eager('model1Relation1.model1Relation1');
1549+
})
1550+
.then(result => {
1551+
chai.expect(result).to.containSubset({
1552+
id: 2,
1553+
1554+
model1Relation1: {
1555+
id: 3,
1556+
model1Prop1: 'belongsToOne',
1557+
1558+
model1Id: null,
1559+
model1Relation1: null
1560+
}
1561+
});
1562+
});
1563+
});
14261564

14271565
it('should insert with an id instead of throwing an error if `insertMissing` option is true', () => {
14281566
const upsert = {
@@ -1462,7 +1600,7 @@ module.exports = session => {
14621600
return transaction(session.knex, trx => {
14631601
return Model1.query(trx).upsertGraph(upsert, { insertMissing: true });
14641602
})
1465-
.then(result => {
1603+
.then(() => {
14661604
// Fetch the graph from the database.
14671605
return Model1.query(session.knex)
14681606
.findById(2)

0 commit comments

Comments
 (0)