chore: add test for atomic upsertWithWhere (#1864) (#1871)

Introduce the new property atomicUpsertWithWhere for
connector that implement specific method.
See https://github.com/strongloop/loopback-connector-mongodb/pull/563
for mongodb implementation.

Signed-off-by: Matteo Padovano <mrbatista@users.noreply.github.com>

Co-authored-by: Matteo Padovano <mrbatista@users.noreply.github.com>
This commit is contained in:
Janny 2020-11-24 09:53:21 -05:00 committed by GitHub
parent e89429af8b
commit 7058c52265
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 34 additions and 13 deletions

View File

@ -2620,22 +2620,43 @@ describe('manipulation', function() {
}); });
}); });
it('fails the upsertWithWhere operation when multiple instances are ' + bdd.itIf(!connectorCapabilities.atomicUpsertWithWhere,
'retrieved based on the filter criteria', function(done) { 'fails the upsertWithWhere operation when multiple instances are ' +
Person.create([ 'retrieved based on the filter criteria', function(done) {
{id: '2', name: 'Howie', city: 'Florida'}, Person.create([
{id: '3', name: 'Kevin', city: 'Florida'}, {id: '2', name: 'Howie', city: 'Florida'},
], function(err, instance) { {id: '3', name: 'Kevin', city: 'Florida'},
if (err) return done(err); ], function(err, instance) {
Person.upsertWithWhere({city: 'Florida'}, { if (err) return done(err);
id: '4', name: 'Brian', Person.upsertWithWhere({city: 'Florida'}, {
}, function(err) { id: '4', name: 'Brian',
err.message.should.equal('There are multiple instances found.' + }, function(err) {
err.message.should.equal('There are multiple instances found.' +
'Upsert Operation will not be performed!'); 'Upsert Operation will not be performed!');
done(); done();
});
}); });
}); });
});
bdd.itIf(connectorCapabilities.atomicUpsertWithWhere === true,
'upsertWithWhere update the first matching instance when multiple instances are ' +
'retrieved based on the filter criteria', async () => {
// The first matching instance is determinate from specific connector implementation
// For example for mongodb connector the sort parameter is used (default to _id asc)
await Person.create([
{id: '4', name: 'Howie', city: 'Turin'},
{id: '3', name: 'Kevin', city: 'Turin'},
]);
await Person.upsertWithWhere({city: 'Turin'}, {name: 'Brian'});
const updatedInstance = await Person.findById('3');
should.exist(updatedInstance);
updatedInstance.name.should.equal('Brian');
const notUpdatedInstance = await Person.findById('4');
should.exist(notUpdatedInstance);
notUpdatedInstance.name.should.equal('Howie');
});
it('updates the record when one matching instance is found ' + it('updates the record when one matching instance is found ' +
'based on the filter criteria', function(done) { 'based on the filter criteria', function(done) {