it('run, config, root_path', function(done){ var instance = new Template(); var flag = false; muk(think, 'npm', function(){ return { configure: function(root_path){ assert.equal(root_path, __dirname); flag = true; }, render: function(filepath, conf){ var content = fs.readFileSync(filepath, 'utf8') assert.equal(content.indexOf("describe('adapter/template/nunjucks.js'") > -1, true); assert.deepEqual(conf,{ name: 'welefen' }) return content; } } }) muk(think, 'log', function(){}) instance.run(__filename, { name: 'welefen' }, { type: 'swig', root_path: __dirname, options: { test: 'haha' } }).then(function(data){ assert.equal(data.indexOf("describe('adapter/template/base.js'") > -1, true); muk.restore(); assert.equal(flag, true) done(); }).catch(function(err){ console.log(err.stack) }) })
before(function () { muk(urllib, 'request', function (url, args, callback) { var resp = { "access_token":"ACCESS_TOKEN", "expires_in":7200, "refresh_token":"REFRESH_TOKEN", "openid":"OPENID", "scope":"SCOPE" }; process.nextTick(function () { callback(null, resp); }); }); muk(api, '_getUser', function (openid, accessToken, callback) { process.nextTick(function () { callback(null, { "openid": "OPENID", "nickname": "NICKNAME", "sex": "1", "province": "PROVINCE", "city": "CITY", "country": "COUNTRY", "headimgurl": "http://wx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/46", "privilege": [ "PRIVILEGE1", "PRIVILEGE2" ] }); }); }); });
it('before', function(done){ RestController = thinkRequire('RestController'); MysqlSocket = thinkRequire('MysqlSocket'); MysqlDb = thinkRequire('MysqlDb') muk(RestController.prototype, 'success', function(data){ return data; }) muk(RestController.prototype, 'error', function(data){ return getPromise(data, true); }); muk(MysqlSocket.prototype, 'query', function(sql){ if(sql.indexOf('SHOW COLUMNS ') > -1){ var data = [{"Field":"id","Type":"int(11) unsigned","Null":"NO","Key":"PRI","Default":null,"Extra":"auto_increment"},{"Field":"title","Type":"varchar(255)","Null":"NO","Key":"UNI","Default":null,"Extra":""},{"Field":"cate_id","Type":"tinyint(255)","Null":"NO","Key":"MUL","Default":"1","Extra":""},{"Field":"cate_no","Type":"int(11)","Null":"YES","Key":"","Default":null,"Extra":""},{"Field":"md5","Type":"varchar(255)","Null":"NO","Key":"","Default":null,"Extra":""},{"Field":"width","Type":"int(11)","Null":"NO","Key":"","Default":"0","Extra":""},{"Field":"height","Type":"int(11)","Null":"NO","Key":"","Default":"0","Extra":""},{"Field":"pic_nums","Type":"int(11)","Null":"NO","Key":"MUL","Default":"0","Extra":""},{"Field":"view_nums","Type":"int(11)","Null":"NO","Key":"MUL","Default":"0","Extra":""},{"Field":"content","Type":"text","Null":"NO","Key":"","Default":null,"Extra":""},{"Field":"date","Type":"int(11)","Null":"NO","Key":"MUL","Default":null,"Extra":""},{"Field":"is_hide","Type":"tinyint(11)","Null":"YES","Key":"MUL","Default":"0","Extra":""},{"Field":"is_safe","Type":"tinyint(11)","Null":"YES","Key":"MUL","Default":"1","Extra":""}]; return getPromise(data); }else if (sql.indexOf('SELECT ') > -1) { var data = [{"id":7565,"title":"米兰达·可儿干练服装写真大片","cate_id":1,"cate_no":0,"md5":"27e562c50195153d89c52072bd4c8d5a","width":936,"height":1177,"pic_nums":11,"view_nums":1965,"content":"","date":20140526,"is_hide":0,"is_safe":0},{"id":7564,"title":"[Beautyleg]2014.05.21 No.977 Cindy","cate_id":2,"cate_no":977,"md5":"e31b3202f3cefe9944fb0e086064694f","width":1600,"height":2400,"pic_nums":30,"view_nums":1558,"content":"","date":20140524,"is_hide":0,"is_safe":0},{"id":7563,"title":"[DISI第四印象]2014.05.21 NO.281","cate_id":7,"cate_no":281,"md5":"ea455be2cab6813964543662b462a550","width":960,"height":640,"pic_nums":30,"view_nums":2335,"content":"","date":20140524,"is_hide":0,"is_safe":0},{"id":7562,"title":"[PANS写真]2014-05-20 NO.242 小倩 套图","cate_id":6,"cate_no":242,"md5":"de775e0785c379ad853b2a299e4736cd","width":1600,"height":2397,"pic_nums":30,"view_nums":2013,"content":"","date":20140524,"is_hide":0,"is_safe":0},{"id":7561,"title":"[ROSI写真]2014.05.20 NO.896","cate_id":3,"cate_no":896,"md5":"65e4a012a123190184a07d87c21e9dec","width":1600,"height":1067,"pic_nums":30,"view_nums":1425,"content":"","date":20140524,"is_hide":0,"is_safe":0},{"id":7560,"title":"[ROSI写真]2014.05.21 NO.897","cate_id":3,"cate_no":897,"md5":"51477ddac8a1d67d30f83da66e0b9ff5","width":1600,"height":1067,"pic_nums":22,"view_nums":1433,"content":"","date":20140524,"is_hide":0,"is_safe":0},{"id":7559,"title":"[ROSI写真]2014.05.22 NO.898","cate_id":3,"cate_no":898,"md5":"a74d4a187ae126b1446503da309b56c9","width":796,"height":531,"pic_nums":30,"view_nums":1994,"content":"","date":20140524,"is_hide":0,"is_safe":0},{"id":7558,"title":"[ru1mm写真] 2014-05-20 NO.151","cate_id":17,"cate_no":151,"md5":"4818e310b0c983e17ae87414a808879d","width":1200,"height":1800,"pic_nums":30,"view_nums":2035,"content":"","date":20140524,"is_hide":0,"is_safe":0},{"id":7557,"title":"[ru1mm写真] 2014-05-22 NO.152","cate_id":17,"cate_no":152,"md5":"af8b033373b1526bd8a28053f065359f","width":1600,"height":1067,"pic_nums":30,"view_nums":1118,"content":"","date":20140524,"is_hide":0,"is_safe":0}] return getPromise(data); }else if(sql.indexOf('INSERT INTO ') > -1){ return getPromise({insertId: 100, affectedRows: 1}); }else if (sql.indexOf('DELETE ') > -1) { return getPromise({affectedRows: 1}); }else if(sql.indexOf('UPDATE ') > -1){ return getPromise({affectedRows: 1}); }else{ return getPromise([]); } }) done(); })
before(function () { muk(api, 'getToken', function (openid, callback) { process.nextTick(function () { callback(null, { access_token: 'access_token', create_at: new Date().getTime(), expires_in: 60 }); }); }); muk(api, '_getUser', function (openid, accessToken, callback) { process.nextTick(function () { callback(null, { "openid": "OPENID", "nickname": "NICKNAME", "sex": "1", "province": "PROVINCE", "city": "CITY", "country": "COUNTRY", "headimgurl": "http://wx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/46", "privilege": [ "PRIVILEGE1", "PRIVILEGE2" ] }); }); }); });
it('set data error', function(done){ muk(think, 'npm', function(name){ return { Client: { create: function(str){ assert.equal(str, 'welefen:suredy@127.0.0.1:11211'); return { connection: 'connection', set: function(key, value, callback, timeout){ assert.equal(key, 'welefen'); assert.equal(value, 'suredy'); assert.equal(timeout, 1000); callback && callback(new Error('memcache set data error')) } } } } } }); muk(think, 'reject', function(err){ return Promise.reject(err); }) var instance = new memcacheSocket({ username: '******', password: '******' }); instance.set('welefen', 'suredy', 1000).catch(function(){ muk.restore(); done(); }) })
it('get data error', function(done){ muk(think, 'npm', function(name){ return { Client: { create: function(str){ assert.equal(str, 'welefen:suredy@127.0.0.1:11211'); return { connection: 'connection', get: function(key, callback){ callback && callback(new Error('suredy')) } } } } } }); muk(think, 'reject', function(err){ return Promise.reject(err); }) var instance = new memcacheSocket({ username: '******', password: '******' }); instance.get('welefen').catch(function(err){ assert.deepEqual(err.message, 'suredy') muk.restore(); done(); }) })
it('run, config', function(done){ var instance = new Template(); muk(think, 'npm', function(){ return { setDefaults: function(){}, compileFile: function(filepath, conf){ var content = fs.readFileSync(filepath, 'utf8') assert.equal(content.indexOf("describe('adapter/template/swig.js'") > -1, true); assert.deepEqual(conf, undefined) return function(data){ assert.deepEqual(data, {name: 'welefen'}) return content; } } } }) muk(think, 'log', function(){}) instance.run(__filename, { name: 'welefen' }, { type: 'swig', options: { test: 'haha' } }).then(function(data){ assert.equal(data.indexOf("describe('adapter/template/base.js'") > -1, true); muk.restore(); done(); }).catch(function(err){ console.log(err.stack) }) })
it('run, config', function(done){ var instance = new Template(); muk(think, 'npm', function(){ return { configure: function(){ return { addGlobal: function(){} } }, render: function(filepath, conf, callback){ var content = fs.readFileSync(filepath, 'utf8') assert.equal(content.indexOf("describe('adapter/template/nunjucks.js'") > -1, true); assert.deepEqual(conf,{ name: 'welefen' }) callback(null, content) } } }) muk(think, 'log', function(){}) instance.run(__filename, { name: 'welefen' }, { type: 'swig', options: { test: 'haha' } }).then(function(data){ assert.equal(data.indexOf("describe('adapter/template/base.js'") > -1, true); muk.restore(); done(); }).catch(function(err){ console.log(err.stack) }) })
it('run, config, cache_compile, no cache data', function(done){ var instance = new Template(); muk(think, 'npm', function(){ return { compile: function(content, conf){ assert.equal(content.indexOf("describe('adapter/template/jade.js'") > -1, true); assert.equal(conf.test, 'haha') //assert.deepEqual(conf, { filename: __filename , test: 'haha'}) return function(data){ assert.deepEqual(data, {name: 'welefen'}) return content; } } } }) muk(think, 'log', function(){}) instance.run(__filename, { name: 'welefen' }, { type: 'jade', cache_compile: true, options: { test: 'haha' } }).then(function(data){ assert.equal(data.indexOf("describe('adapter/template/jade.js'") > -1, true); muk.restore(); done(); }) })
it('decrement error', function(done){ muk(think, 'npm', function(name){ return { Client: { create: function(str){ assert.equal(str, 'welefen:suredy@127.0.0.1:11211'); return { connection: 'connection', decrement: function(key, amount, callback){ assert.equal(key, 'welefen'); assert.equal(amount, 1000) callback && callback(new Error('descrement error')) } } } } } }); muk(think, 'reject', function(err){ return Promise.reject(err); }) var instance = new memcacheSocket({ username: '******', password: '******' }); instance.decrement('welefen', 1000).catch(function(){ muk.restore(); done(); }) })
before(function() { muk(fs, 'readFileSync', function(filename, encoding) { throw new Error('mock readFileSync error'); }); muk(fs, 'readFile', function(filename, callback) { callback(new Error('mock readFile error')); }) });
before(function () { muk(urllib, 'request', function (url, opts, callback) { process.nextTick(function () { callback(new Error('mock err')); }); }); });
before(function () { muk(api, 'sendMusic', function (openid, music, callback) { process.nextTick(function () { callback(null, {errcode: 0, errmsg: 'ok'}); }); }); });
before(function () { muk(api, 'sendVideo', function (openid, movieId, thumbId, callback) { process.nextTick(function () { callback(null, {errcode: 0, errmsg: 'ok'}); }); }); });
it('run, options', function(done){ muk(think, 'npm', function(){ return { createServer: function(options){ options.log(); return { on: function(type, callback){ }, installHandlers: function(ser, options){ assert.deepEqual(options, { prefix: '/websocket' }) } } } } }) var server = {}; var config = {sockjs_url: '/websocket', path: '/websocket'}; var app = {}; var instance = new WebSocket(server, config, app); instance.run().then(function(){ muk.restore(); done(); }).catch(function(err){ console.log(err.stack) }); })
it('getConnection config, connection exist', function(done){ muk(think, 'npm', function(name){ return { Client: { create: function(str){ assert.equal(str, 'welefen:suredy@127.0.0.1:11211'); return { connection: 'connection' } } } } }) var instance = new memcacheSocket({ username: '******', password: '******' }); instance.getConnection().then(function(){ muk.restore(); return instance.getConnection(); }).then(function(connection){ assert.deepEqual(connection, {connection: 'connection'}) done(); }) })
it('close', function(done){ muk(think, 'npm', function(name){ return { Client: { create: function(str){ assert.equal(str, 'welefen:suredy@127.0.0.1:11211'); return { connection: 'connection', close: function(key, amount, callback){ } } } } } }) var instance = new memcacheSocket({ username: '******', password: '******' }); instance.getConnection().then(function(){ instance.close(); muk.restore(); done(); }) })
it('decrement timeout', function(done){ muk(think, 'npm', function(name){ return { Client: { create: function(str){ assert.equal(str, 'welefen:suredy@127.0.0.1:11211'); return { connection: 'connection', decrement: function(key, amount, callback, timeout){ assert.equal(key, 'welefen'); assert.equal(amount, 1000) assert.equal(timeout, 2000) callback && callback() } } } } } }) var instance = new memcacheSocket({ username: '******', password: '******' }); instance.decrement('welefen', 1000, 2000).then(function(){ muk.restore(); done(); }) })
it('delete data', function(done){ muk(think, 'npm', function(name){ return { Client: { create: function(str){ assert.equal(str, 'welefen:suredy@127.0.0.1:11211'); return { connection: 'connection', delete: function(key, callback){ assert.equal(key, 'welefen'); callback && callback(null) } } } } } }) var instance = new memcacheSocket({ username: '******', password: '******' }); instance.delete('welefen').then(function(){ muk.restore(); done(); }) })
before(function () { muk(urllib, 'request', function (url, args, callback) { process.nextTick(function () { callback(null, {"access_token": "ACCESS_TOKEN","expires_in": 7200}); }); }); });
before(function () { muk(urllib, 'request', function (url, opts) { process.nextTick(function () { callback(new Error('mock invalid media_id')); }); }); });
before(function () { muk(api, 'getToken', function (callback) { process.nextTick(function () { callback(new Error('mock getToken error')); }); }); });
it('run', function(done){ var instance = new Template(); muk(think, 'npm', function(){ return { configure: function(){ return { addGlobal: function(){} } }, render: function(filepath, conf, callback){ var content = fs.readFileSync(filepath, 'utf8') assert.equal(content.indexOf("describe('adapter/template/nunjucks.js'") > -1, true); assert.deepEqual(conf, undefined) callback(null, content); } } }) instance.run(__filename).then(function(data){ assert.equal(data.indexOf("describe('adapter/template/nunjucks.js'") > -1, true); muk.restore(); done(); }).catch(function(err){ console.log(err.stack) }) })
before(function () { muk(urllib, 'request', function (url, args, callback) { var err = new Error('Urllib Error'); err.name = 'UrllibError'; callback(err); }); });
before(function () { muk(api, 'saveToken', function (token, callback) { process.nextTick(function () { callback(new Error('mock saveToken err')); }); }); });
it('set data, timeout', function(done){ muk(think, 'npm', function(name){ return { Client: { create: function(str){ assert.equal(str, 'welefen:suredy@127.0.0.1:11211'); return { connection: 'connection', set: function(key, value, callback, timeout){ assert.equal(key, 'welefen'); assert.equal(value, 'suredy'); assert.equal(timeout, 0); callback && callback(null, new Buffer('suredy')) } } } } } }) var instance = new memcacheSocket({ username: '******', password: '******', timeout: 0 }); instance.set('welefen', 'suredy').then(function(){ muk.restore(); done(); }) })
before(function () { muk(api, 'getToken', function (callback) { process.nextTick(function () { callback(null, {accessToken: 'token', expireTime: (new Date().getTime() + 10000)}); }); }); });
before(function () { muk(api, 'sendImage', function (openid, mediaId, callback) { process.nextTick(function () { callback(null, {errcode: 0, errmsg: 'ok'}); }); }); });
before(function () { muk(urllib, 'request', function (url, args, callback) { process.nextTick(function () { callback(null, {'access_token': 'ACCESS_TOKEN','expires_in': 7200}); }); }); });
it('get data', function(done){ muk(think, 'npm', function(name){ return { Client: { create: function(str){ assert.equal(str, 'welefen:suredy@127.0.0.1:11211'); return { connection: 'connection', get: function(key, callback){ callback && callback(null, new Buffer('suredy')) } } } } } }) var instance = new memcacheSocket({ username: '******', password: '******' }); instance.get('welefen').then(function(data){ assert.deepEqual(data, 'suredy') muk.restore(); done(); }) })