Skip to content

Commit

Permalink
chore: add test for atomic upsertWithWhere
Browse files Browse the repository at this point in the history
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 <mrbatista@users.noreply.github.com>
  • Loading branch information
mrbatista committed Sep 19, 2020
1 parent 261dd1c commit ef36ded
Showing 1 changed file with 34 additions and 13 deletions.
47 changes: 34 additions & 13 deletions test/manipulation.test.js
Expand Up @@ -2624,22 +2624,43 @@ describe('manipulation', function() {
});
});

it('fails the upsertWithWhere operation when multiple instances are ' +
'retrieved based on the filter criteria', function(done) {
Person.create([
{id: '2', name: 'Howie', city: 'Florida'},
{id: '3', name: 'Kevin', city: 'Florida'},
], function(err, instance) {
if (err) return done(err);
Person.upsertWithWhere({city: 'Florida'}, {
id: '4', name: 'Brian',
}, function(err) {
err.message.should.equal('There are multiple instances found.' +
bdd.itIf(connectorCapabilities.atomicUpsertWithWhere !== true,
'fails the upsertWithWhere operation when multiple instances are ' +
'retrieved based on the filter criteria', function(done) {
Person.create([
{id: '2', name: 'Howie', city: 'Florida'},
{id: '3', name: 'Kevin', city: 'Florida'},
], function(err, instance) {
if (err) return done(err);
Person.upsertWithWhere({city: 'Florida'}, {
id: '4', name: 'Brian',
}, function(err) {
err.message.should.equal('There are multiple instances found.' +
'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: 'Florida'},
{id: '3', name: 'Kevin', city: 'Florida'},
]);
await Person.upsertWithWhere({city: 'Florida'}, {name: 'Brian'});

const updatedInstance = await Person.findById('3');
should.exist(updatedInstance);
updatedInstance.name.should.equal('Brian');

const notUpdatedInstance = await Person.findById('3');
should.exist(notUpdatedInstance);
notUpdatedInstance.name.should.equal('Howie');
});

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

0 comments on commit ef36ded

Please sign in to comment.