Skip to content

Commit 76acf33

Browse files
authored
chore: add test for atomic upsertWithWhere (#1864)
Introduce the new property atomicUpsertWithWhere for connector that implement specific method. See loopbackio/loopback-connector-mongodb#563 for mongodb implementation. Signed-off-by: Matteo Padovano <[email protected]>
1 parent 261dd1c commit 76acf33

File tree

1 file changed

+34
-13
lines changed

1 file changed

+34
-13
lines changed

test/manipulation.test.js

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2624,22 +2624,43 @@ describe('manipulation', function() {
26242624
});
26252625
});
26262626

2627-
it('fails the upsertWithWhere operation when multiple instances are ' +
2628-
'retrieved based on the filter criteria', function(done) {
2629-
Person.create([
2630-
{id: '2', name: 'Howie', city: 'Florida'},
2631-
{id: '3', name: 'Kevin', city: 'Florida'},
2632-
], function(err, instance) {
2633-
if (err) return done(err);
2634-
Person.upsertWithWhere({city: 'Florida'}, {
2635-
id: '4', name: 'Brian',
2636-
}, function(err) {
2637-
err.message.should.equal('There are multiple instances found.' +
2627+
bdd.itIf(connectorCapabilities.atomicUpsertWithWhere !== true,
2628+
'fails the upsertWithWhere operation when multiple instances are ' +
2629+
'retrieved based on the filter criteria', function(done) {
2630+
Person.create([
2631+
{id: '2', name: 'Howie', city: 'Florida'},
2632+
{id: '3', name: 'Kevin', city: 'Florida'},
2633+
], function(err, instance) {
2634+
if (err) return done(err);
2635+
Person.upsertWithWhere({city: 'Florida'}, {
2636+
id: '4', name: 'Brian',
2637+
}, function(err) {
2638+
err.message.should.equal('There are multiple instances found.' +
26382639
'Upsert Operation will not be performed!');
2639-
done();
2640+
done();
2641+
});
26402642
});
26412643
});
2642-
});
2644+
2645+
bdd.itIf(connectorCapabilities.atomicUpsertWithWhere === true,
2646+
'upsertWithWhere update the first matching instance when multiple instances are ' +
2647+
'retrieved based on the filter criteria', async () => {
2648+
// The first matching instance is determinate from specific connector implementation
2649+
// For example for mongodb connector the sort parameter is used (default to _id asc)
2650+
await Person.create([
2651+
{id: '4', name: 'Howie', city: 'Turin'},
2652+
{id: '3', name: 'Kevin', city: 'Turin'},
2653+
]);
2654+
await Person.upsertWithWhere({city: 'Turin'}, {name: 'Brian'});
2655+
2656+
const updatedInstance = await Person.findById('3');
2657+
should.exist(updatedInstance);
2658+
updatedInstance.name.should.equal('Brian');
2659+
2660+
const notUpdatedInstance = await Person.findById('4');
2661+
should.exist(notUpdatedInstance);
2662+
notUpdatedInstance.name.should.equal('Howie');
2663+
});
26432664

26442665
it('updates the record when one matching instance is found ' +
26452666
'based on the filter criteria', function(done) {

0 commit comments

Comments
 (0)