Created
November 3, 2010 22:48
-
-
Save Jxck/661854 to your computer and use it in GitHub Desktop.
test-je.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
log = function(a,b){ | |
if(console.log){(!b)? console.log(a) : console.log(a);console.log(b);} | |
}; | |
module('je', { | |
testdata: null, | |
docType: null, | |
docId: null, | |
/** | |
* mock of docId generator | |
* | |
* @return {string} docId | |
*/ | |
_getDocId: function() { | |
var docId = ''; | |
var ALNUMS = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; | |
var digits = 32; | |
for (var i = 0; i < digits; i++) { | |
docId += ALNUMS[Math.floor(Math.random() * (digits + 1))]; | |
} | |
return docId; | |
}, | |
setup: function() { | |
this.testdata = { | |
'key' : 'value', | |
'arr' : ['a', 1, true], | |
'nest' : { 'nested' : {'nested' : 'value' }} | |
}; | |
this.docType = 'test'; | |
}, | |
teardown: function() { | |
this.testdata = null; | |
this.docType = null; | |
this.docId = null; | |
} | |
}); | |
test('test of test', function() { | |
// all five method are exist | |
expect(5); | |
ok(je, 'je'); | |
ok(je.POST, 'je.POST'); | |
ok(je.GET, 'je.GET'); | |
ok(je.PUT, 'je.PUT'); | |
ok(je.DELETE, 'je.DELETE'); | |
}); | |
asyncTest('POST only doc parameter', function() { | |
expect(7); | |
var docType = this.docType; | |
var testdata = this.testdata; | |
var callback = function(data) { | |
setTimeout(function() { | |
start(); | |
ok(data, 'callback catches response data'); | |
same(typeof data.key, 'string'); | |
same(data.key, testdata.key); | |
same(typeof data.arr, 'object'); | |
same(data.arr, testdata.arr); | |
same(typeof data.nest, 'object'); | |
same(data.nest, testdata.nest); | |
},10); | |
}; | |
je.POST(docType, testdata, callback); | |
}); | |
asyncTest('POST with specify the docId in doc parameter', function() { | |
expect(9); | |
var docType = this.docType; | |
var testdata = this.testdata; | |
testdata._docId = this._getDocId(); | |
var callback = function(data) { | |
setTimeout(function() { | |
start(); | |
ok(data, 'callback catches response data'); | |
same(typeof data.key, 'string'); | |
same(data.key, testdata.key); | |
same(typeof data.arr, 'object'); | |
same(data.arr, testdata.arr); | |
same(typeof data.nest, 'object'); | |
same(data.nest, testdata.nest); | |
same(typeof data._docId, 'string'); | |
same(data._docId, testdata._docId); | |
},10); | |
}; | |
je.POST(docType, testdata, callback); | |
}); | |
asyncTest('POST with specify the conflicted docId in doc parameter', function() { | |
// expect(9); | |
var docType = this.docType; | |
var testdata = this.testdata; | |
testdata._docId = this._getDocId(); | |
// var callback = function(data) { | |
// setTimeout(function() { | |
// start(); | |
// ok(data, 'callback catches response data'); | |
// same(typeof data.key, 'string'); | |
// same(data.key, testdata.key); | |
// same(typeof data.arr, 'object'); | |
// same(data.arr, testdata.arr); | |
// same(typeof data.nest, 'object'); | |
// same(data.nest, testdata.nest); | |
// same(typeof data._docId, 'string'); | |
// same(data._docId, testdata._docId); | |
// },10); | |
// }; | |
// function(a,b,c){log(a.status);}); | |
log(JSON.stringify(testdata)); | |
var sameRequest = function(){ | |
setTimeout(function() { | |
start(); | |
je.POST(docType, testdata, function() {}, function(a,b,c) { | |
setTimeout(function() { | |
log(a); | |
}); | |
}); | |
},10); | |
}; | |
je.POST(docType, testdata, sameRequest); | |
}); | |
asyncTest('GET by docId', function() { | |
expect(9); | |
var docType = this.docType; | |
var testdata = this.testdata; | |
var docId = null; | |
var callback = function(data) { | |
setTimeout(function() { | |
start(); | |
ok(data, 'callback catches response data'); | |
same(typeof data.key, 'string'); | |
same(data.key, testdata.key); | |
same(typeof data.arr, 'object'); | |
same(data.arr, testdata.arr); | |
same(typeof data.nest, 'object'); | |
same(data.nest, testdata.nest); | |
same(typeof data._docId, 'string'); | |
same(data._docId, docId); | |
},10); | |
}; | |
je.POST(docType, testdata, function(data) { | |
docId = data._docId; | |
je.GET(docType, docId, callback); | |
}); | |
}); | |
asyncTest('GET by docType', function() { | |
expect(2); | |
var docType = this.docType; | |
var testdata = this.testdata; | |
var callback = function(data) { | |
setTimeout(function() { | |
start(); | |
ok(data, 'callback catches response data'); | |
same(typeof data, 'object'); | |
},10); | |
}; | |
je.POST(docType, testdata, function(data) { | |
je.GET(docType, callback); | |
}); | |
}); | |
asyncTest('DELETE by docType', function() { | |
expect(1); | |
var docType = this.docType; | |
var testdata = this.testdata; | |
var callback = function(data) { | |
setTimeout(function() { | |
start(); | |
same(data, '', 'callback catches empty data'); | |
},50); | |
}; | |
je.DELETE(docType, callback); | |
}); | |
asyncTest('DELETE by docId exist', function() { | |
expect(1); | |
var docType = this.docType; | |
var testdata = this.testdata; | |
var callback = function(resdata) { | |
setTimeout(function() { | |
je.DELETE(docType, resdata._docId, function(data) { | |
setTimeout(function() { | |
start(); | |
same(data, '', 'callback catches empty data'); | |
},30); | |
}); | |
},30); | |
}; | |
je.POST(docType, testdata, callback); | |
}); | |
asyncTest('PUT partial update', function() { | |
expect(4); | |
var docType = this.docType; | |
var testdata = this.testdata; | |
var callback = function(resdata) { | |
setTimeout(function() { | |
var updateTo = { | |
'parcial' : 'update', // add new param | |
'key' : 'values', // update param | |
'nest' : null // update param | |
}; | |
je.PUT(docType, resdata._docId, updateTo, function(data) { | |
setTimeout(function() { | |
start(); | |
same(data.arr, ['a', 1, true], 'added data'); | |
same(data.parcial, 'update', 'added data'); | |
same(data.key, 'values', 'updated data'); | |
same(data.nest, 'null', 'update to null became "null"'); | |
},30); | |
}); | |
},30); | |
}; | |
je.POST(docType, testdata, callback); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment