forked from mikowals/batch-insert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
batch-insert-tests.js
124 lines (117 loc) · 5.09 KB
/
batch-insert-tests.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Write your tests here!
// Here is an example.
var col;
Meteor.methods({
testDefineBatchInsert: function( s ){
if ( ! this.isSimulation ){
var colServer = new Meteor.Collection( s );
colServer.allow({
insert: function(){ return true;}
});
}
return true;
},
testDefineBatchInsertDisallow: function( s ){
if ( ! this.isSimulation ){
var colServer2 = new Meteor.Collection( s );
colServer2.allow({
insert: function(){ return false;}
});
}
return true;
},
insertOnServer: function (name, docs) {
if (! this.isSimulation) {
var col = new Mongo.Collection(name);
col.deny({
insert: function (){return true}
});
return col.batchInsert(docs);
}
}
});
if (Meteor.isClient ){
var colName = Random.secret( 10 );
Meteor.apply( 'testDefineBatchInsert',[colName], {wait: true, returnStubValue: true});
var col = new Meteor.Collection( colName );
Tinytest.add( 'mikowals:batch-insert - client side insert', function( test ){
var ids = col.batchInsert( [{_id: "1", name: 'phil'}, {_id: "2", name: 'sally'}] );
test.equal(ids, ["1","2"], 'client side batchInsert');
});
Tinytest.add( 'mikowals:batch-insert - collection is instance of Mongo.Collection', function( test ){
test.instanceOf( col, Mongo.Collection);
});
testAsyncMulti( 'mikowals:batch-insert - server _ids match client and allow rules manage security', [
function( test, expect ) {
var ids = col.batchInsert( [{name:'john'}, {name: 'jerry'} ], expect( function( err, res ){
test.equal( ids, res, 'server and client ids match');
//test.equal( col.findOne( ids[0] ).name, 'john', 'able to retrieve new obj from db');
}));
},
function( test, expect ){
var testColName = Random.secret( 10 );
Meteor.apply( 'testDefineBatchInsertDisallow',[testColName], {wait: true, returnStubValue: true});
var testCol = new Meteor.Collection( testColName );
testCol.batchInsert( [{name: Random.secret(5) }, {name: Random.secret(5)}], expect( function( err, res){
var expectedErr = JSON.stringify( {error:403,reason:"Access denied",message:"Access denied [403]",errorType:"Meteor.Error"} );
var actualErr = JSON.stringify( err );
test.equal( err.reason, "Access denied" , 'insert should fail based on allow rule');
}));
},
function (test, expect){
var colName = Random.secret(10);
var docs = [{name: Random.id(5)},{name: Random.id(5)}];
Meteor.apply('insertOnServer',[colName, docs],{wait: true}, expect(function(err, res){
test.equal(err, undefined);
test.equal(res.length, 2, 'two docs inserted successfully');
}));
}
]);
} else {
//server only tests
Tinytest.add( 'mikowals:batch-insert - test error on duplicate id', function( test ){
//this test is a problem. Individual docs can be inserted while others fail.
var newColName = Random.secret( 10 );
var col = new Meteor.Collection( newColName );
col.batchInsert( [{_id:1}, {_id:2}] );
function insertAgain(){
var err = col.batchInsert([ {_id:3, name: 'shouldFail'}, {_id:1} ]);
}
var msg = 'E11000 duplicate key error collection: meteor.'+ newColName + ' index: _id_ dup key: { : 1 }';
test.throws( insertAgain, msg, 'insert should fail with duplicate ids');
});
Tinytest.add( 'mikowals:batch-insert - collection is instance of Mongo.Collection', function( test ){
//this test is a problem. Individual docs can be inserted while others fail.
var newColName = Random.secret( 10 );
var col = new Meteor.Collection( newColName );
test.instanceOf( col, Mongo.Collection);
});
Tinytest.add( 'mikowals:batch-insert - batch insert on server collection', function( test ){
var serverCol = new Meteor.Collection(Random.secret( 10 ));
serverCol.deny({
insert: function(){ return false; }
});
var ids = serverCol.batchInsert([{_id: 'a', name: 'earl'}, {_id: 'b', name: 'brian'}]);
test.equal( ids, ['a','b'], 'should return given ids' );
});
testAsyncMulti( 'mikowals:batch-insert - server callback gets error or result', [
function( test, expect ) {
var serverCol = new Meteor.Collection(Random.secret( 10 ));
serverCol.batchInsert( [{_id: "a", name:'john'}, {_id: "b", name: 'jerry'} ], expect( function( err, res ){
test.equal( ["a", "b"], res, 'server and client ids match');
//test.equal( col.findOne( ids[0] ).name, 'john', 'able to retrieve new obj from db');
}));
},
function (test, expect) {
//this test is a problem. Individual docs can be inserted while others fail.
var newColName = Random.secret( 10 );
var col = new Meteor.Collection( newColName );
col.batchInsert( [{_id:1}, {_id:2}] );
col.batchInsert([ {_id:3, name: 'shouldFail'}, {_id:1} ], expect(function (err, res){
//console.log(err.errmsg);
var msg = 'E11000 duplicate key error collection: meteor.' + newColName + ' index: _id_ dup key: { : 1 }';
test.equal(msg, err.message, 'insert should fail with duplicate ids');
}));
}
]);
}