Browse files

add test cases

  • Loading branch information...
1 parent cab2684 commit a70aaf27acdaccd6a789f273587f37fc3b803743 @blacktail committed Sep 16, 2013
Showing with 151 additions and 1 deletion.
  1. +3 −0 .gitignore
  2. +23 −0 Makefile
  3. +4 −0 lib/core.js
  4. +6 −1 package.json
  5. +115 −0 test/core.js
View
3 .gitignore 100644 → 100755
@@ -2,3 +2,6 @@ node_modules
test.js
*.sublime*
.idea
+lib-cov
+coverage.html
+
View
23 Makefile
@@ -0,0 +1,23 @@
+# MOCHA_OPTS= --check-leaks
+REPORTER = spec
+
+check: test
+
+test: test-unit
+
+test-unit:
+ @NODE_ENV=test ./node_modules/.bin/mocha \
+ --reporter $(REPORTER) \
+ $(MOCHA_OPTS)
+
+test-cov: lib-cov
+ @CODE_COV=1 $(MAKE) test REPORTER=html-cov > coverage.html
+
+lib-cov: clean
+ @jscoverage lib lib-cov
+
+clean:
+ rm -f coverage.html
+ rm -rf lib-cov
+
+.PHONY: test test-unit clean
View
4 lib/core.js 100644 → 100755
@@ -218,5 +218,9 @@ var core = module.exports = {
});
return api;
+ },
+
+ getConfig: function () {
+ return cfg;
}
};
View
7 package.json
@@ -2,7 +2,7 @@
"name": "taobao",
"description": "taobao openapi sdk for node",
"keywords": ["taobao", "sdk", "openapi", "node-taobao", "top", "TaobaoClient", "taobaoke"],
- "version": "1.0.2",
+ "version": "1.0.3",
"homepage": "http://github.com/blacktail/taobao.git",
"author": {
"name": "blacktail",
@@ -25,6 +25,11 @@
"moment": "~2.0.0",
"MD5": "~1.0.3"
},
+ "devDependencies": {
+ "chai": "1.7.2",
+ "mocha": "1.12.0",
+ "supertest": "0.7.1"
+ },
"scripts": {
"prepublish": "npm prune"
},
View
115 test/core.js
@@ -0,0 +1,115 @@
+var lib = '../' + (process.env.CODE_COV ? 'lib-cov' : 'lib');
+
+var core = require(lib + '/core'),
+ chai = require('chai'),
+ expect = chai.expect,
+ APP_KEY = '21439599',
+ APP_SECRET = '87786c8e214faa34a137b1def4cd71da';
+
+chai.should();
+
+describe('core.js', function() {
+ describe('config()', function() {
+ it('should change core\'s config successfully', function(done) {
+ core.config({
+ app_key: APP_KEY,
+ app_secret: APP_SECRET
+ });
+
+ var cfg = core.getConfig();
+
+ expect(cfg).to.be.an('object');
+ expect(cfg.app_key).to.equal(APP_KEY);
+ expect(cfg.app_secret).to.equal(APP_SECRET);
+
+ done();
+ });
+ });
+
+ describe('call()', function() {
+ it('should get areas successfully with default http config', function(done) {
+ this.timeout(100000);
+
+ core.call({
+ method: 'taobao.areas.get',
+ fields: 'id,type,name,parent_id,zip'
+ }, function(data) {
+ expect(data).to.be.an('object');
+ expect(data.areas_get_response).to.be.an('object');
+ expect(data.areas_get_response.areas).to.be.an('object');
+ expect(data.areas_get_response.areas.area).to.be.an('array');
+ expect(data.areas_get_response.areas.area).to.have.length.above(0);
+ expect(data.areas_get_response.areas.area[0]).to.contain.keys('id', 'name', 'parent_id', 'type');
+ done();
+ });
+ });
+
+ it('should get areas successfully with https', function(done) {
+ this.timeout(100000);
+
+ core.call({
+ protocol: 'https'
+ }, {
+ method: 'taobao.areas.get',
+ fields: 'id,type,name,parent_id,zip'
+ }, function(data) {
+ expect(data).to.be.an('object');
+ expect(data.areas_get_response).to.be.an('object');
+ expect(data.areas_get_response.areas).to.be.an('object');
+ expect(data.areas_get_response.areas.area).to.be.an('array');
+ expect(data.areas_get_response.areas.area).to.have.length.above(0);
+ expect(data.areas_get_response.areas.area[0]).to.contain.keys('id', 'name', 'parent_id', 'type');
+ done();
+ });
+ });
+
+ it('should fail when get areas using http POST method', function(done) {
+ this.timeout(100000);
+
+ core.call({
+ method: 'post'
+ }, {
+ method: 'taobao.areas.get',
+ fields: 'id,type,name,parent_id,zip'
+ }, function(data) {
+ expect(data).to.be.an('object');
+ expect(data.error_response).to.be.an('object');
+ expect(data.error_response.statusCode).to.equal(411);
+
+ done();
+ });
+ });
+ });
+
+ describe('generateApi()', function() {
+ var api;
+
+ it('should generate api as expected', function(done) {
+ api = core.generateApi([
+ 'areas.get'
+ ]);
+
+ expect(api).to.be.an('object');
+ expect(api).to.contain.keys('areasGet');
+ expect(api.areasGet).to.be.an('function');
+
+ done();
+ });
+
+ it('should success when call with generatedApi', function(done) {
+ this.timeout(100000);
+ api.areasGet({
+ fields: 'id,type,name,parent_id,zip'
+ }, function (data) {
+ expect(data).to.be.an('object');
+ expect(data.areas_get_response).to.be.an('object');
+ expect(data.areas_get_response.areas).to.be.an('object');
+ expect(data.areas_get_response.areas.area).to.be.an('array');
+ expect(data.areas_get_response.areas.area).to.have.length.above(0);
+ expect(data.areas_get_response.areas.area[0]).to.contain.keys('id', 'name', 'parent_id', 'type');
+
+ done();
+ });
+ });
+ });
+});

0 comments on commit a70aaf2

Please sign in to comment.