mirror of
https://github.com/meteor/meteor.git
synced 2026-05-02 03:01:46 -04:00
Add upsert tests to help mantain API compatibility
This commit is contained in:
committed by
Ben Newman
parent
906f82e4d9
commit
703e773568
@@ -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']);
|
||||
|
||||
153
packages/mongo/upsert_compatibility_test.js
Normal file
153
packages/mongo/upsert_compatibility_test.js
Normal file
@@ -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);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user