diff --git a/packages/mongo/package.js b/packages/mongo/package.js index dbcfec4a04..92f0540b96 100644 --- a/packages/mongo/package.js +++ b/packages/mongo/package.js @@ -84,6 +84,7 @@ Package.onTest(function (api) { // XXX test order dependency: the allow_tests "partial allow" test // fails if it is run before mongo_livedata_tests. api.addFiles('mongo_livedata_tests.js', ['client', 'server']); + api.addFiles('upsert_compatibility_test.js', ['server']); api.addFiles('allow_tests.js', ['client', 'server']); api.addFiles('collection_tests.js', ['client', 'server']); api.addFiles('observe_changes_tests.js', ['client', 'server']); diff --git a/packages/mongo/upsert_compatibility_test.js b/packages/mongo/upsert_compatibility_test.js new file mode 100644 index 0000000000..8c608eb836 --- /dev/null +++ b/packages/mongo/upsert_compatibility_test.js @@ -0,0 +1,153 @@ +if (Meteor.isServer) { + Tinytest.add('mongo livedata - native upsert - id type MONGO with MODIFIERS update', function (test) { + var collName = Random.id(); + var coll = new Mongo.Collection('native_upsert_'+collName, {idGeneration: 'MONGO'}); + + coll.insert({foo: 1}); + var result = coll.upsert({foo: 1}, {$set: {foo:2}}); + var updated = coll.findOne({foo: 2}); + + test.equal(result.insertedId, undefined); + test.equal(result.numberAffected, 1); + + test.isTrue(updated._id instanceof Mongo.ObjectID); + + delete updated['_id']; + test.equal(EJSON.equals(updated, {foo: 2}), true); + }); + + Tinytest.add('mongo livedata - native upsert - id type MONGO with MODIFIERS insert', function (test) { + var collName = Random.id(); + var coll = new Mongo.Collection('native_upsert_'+collName, {idGeneration: 'MONGO'}); + + var result = coll.upsert({foo: 1}, {$set: {bar:2}}); + var inserted = coll.findOne({foo: 1}); + + test.isTrue(result.insertedId !== undefined); + test.equal(result.numberAffected, 1); + + test.isTrue(inserted._id instanceof Mongo.ObjectID); + test.equal(inserted._id, result.insertedId) + + delete inserted['_id']; + test.equal(EJSON.equals(inserted, {foo: 1, bar: 2}), true); + }); + + Tinytest.add('mongo livedata - native upsert - id type MONGO PLAIN OBJECT update', function (test) { + var collName = Random.id(); + var coll = new Mongo.Collection('native_upsert_'+collName, {idGeneration: 'MONGO'}); + + coll.insert({foo: 1, baz: 42}); + var result = coll.upsert({foo: 1}, {bar:2}); + var updated = coll.findOne({bar: 2}); + + test.isTrue(result.insertedId === undefined); + test.equal(result.numberAffected, 1); + + test.isTrue(updated._id instanceof Mongo.ObjectID); + + delete updated['_id']; + test.equal(EJSON.equals(updated, {bar: 2}), true); + }); + + Tinytest.add('mongo livedata - native upsert - id type MONGO PLAIN OBJECT insert', function (test) { + var collName = Random.id(); + var coll = new Mongo.Collection('native_upsert_'+collName, {idGeneration: 'MONGO'}); + + var result = coll.upsert({foo: 1}, {bar:2}); + var inserted = coll.findOne({bar: 2}); + + test.isTrue(result.insertedId !== undefined); + test.equal(result.numberAffected, 1); + + test.isTrue(inserted._id instanceof Mongo.ObjectID); + test.isTrue(result.insertedId instanceof Mongo.ObjectID); + test.equal(inserted._id, result.insertedId); + + delete inserted['_id']; + test.equal(EJSON.equals(inserted, {bar: 2}), true); + }); + + Tinytest.add('mongo livedata - native upsert - id type STRING with MODIFIERS update', function (test) { + var collName = Random.id(); + var coll = new Mongo.Collection('native_upsert_'+collName, {idGeneration: 'STRING'}); + + coll.insert({foo: 1}); + var result = coll.upsert({foo: 1}, {$set: {foo:2}}); + var updated = coll.findOne({foo: 2}); + + test.equal(result.insertedId, undefined); + test.equal(result.numberAffected, 1); + + test.isTrue(typeof updated._id === 'string'); + + delete updated['_id']; + test.equal(EJSON.equals(updated, {foo: 2}), true); + }); + + Tinytest.add('mongo livedata - native upsert - id type STRING with MODIFIERS insert', function (test) { + var collName = Random.id(); + var coll = new Mongo.Collection('native_upsert_'+collName, {idGeneration: 'STRING'}); + + var result = coll.upsert({foo: 1}, {$set: {bar:2}}); + var inserted = coll.findOne({foo: 1}); + + test.isTrue(result.insertedId !== undefined); + test.equal(result.numberAffected, 1); + + test.isTrue(typeof inserted._id === 'string'); + test.equal(inserted._id, result.insertedId); + + delete inserted['_id']; + test.equal(EJSON.equals(inserted, {foo: 1, bar: 2}), true); + }); + + Tinytest.add('mongo livedata - native upsert - id type STRING PLAIN OBJECT update', function (test) { + var collName = Random.id(); + var coll = new Mongo.Collection('native_upsert_'+collName, {idGeneration: 'STRING'}); + + coll.insert({foo: 1, baz: 42}); + var result = coll.upsert({foo: 1}, {bar:2}); + var updated = coll.findOne({bar: 2}); + + test.isTrue(result.insertedId === undefined); + test.equal(result.numberAffected, 1); + + test.isTrue(typeof updated._id === 'string'); + + delete updated['_id']; + test.equal(EJSON.equals(updated, {bar: 2}), true); + }); + + Tinytest.add('mongo livedata - native upsert - id type STRING PLAIN OBJECT insert', function (test) { + var collName = Random.id(); + var coll = new Mongo.Collection('native_upsert_'+collName, {idGeneration: 'STRING'}); + + var result = coll.upsert({foo: 1}, {bar:2}); + var inserted = coll.findOne({bar: 2}); + + test.isTrue(result.insertedId !== undefined); + test.equal(result.numberAffected, 1); + + test.isTrue(typeof inserted._id === 'string'); + test.equal(inserted._id, result.insertedId); + + delete inserted['_id']; + test.equal(EJSON.equals(inserted, {bar: 2}), true); + }); + + Tinytest.add('mongo livedata - native upsert - MONGO passing id insert', function (test) { + var collName = Random.id(); + var coll = new Mongo.Collection('native_upsert_'+collName, {idGeneration: 'MONGO'}); + + var result = coll.upsert({foo: 1}, {_id: 'meu id'}); + var inserted = coll.findOne({_id: 'meu id'}); + + test.equal(result.insertedId, 'meu id'); + test.equal(result.numberAffected, 1); + + test.isTrue(typeof inserted._id === 'string'); + + test.equal(EJSON.equals(inserted, {_id: 'meu id'}), true); + }); +}