From a00f69b28f949c8c1b2b9d0ba7f8a068168596c4 Mon Sep 17 00:00:00 2001 From: Phil Jackson Date: Tue, 1 Apr 2014 12:05:42 +0100 Subject: [PATCH] Added documentation routines and some tests. --- Makefile | 9 +- axle.coffee | 9 +- package.json | 3 +- test/v1_test.coffee | 252 +++++--------------------------------------- 4 files changed, 38 insertions(+), 235 deletions(-) diff --git a/Makefile b/Makefile index 10aa664..832f76c 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,4 @@ JS=axle.js lib/client.js index.js -TWERP=`which twerp` %.js: %.coffee coffee -b -c $< @@ -12,11 +11,9 @@ publish: clean $(JS) npminstall: npm install -test: - $(TWERP) $(MY_TWERP_OPTIONS) `find test -name '*_test.coffee'` - -stublesstest: - NO_STUB=1 $(MAKE) test +test: $(JS) + mocha --compilers coffee:coffee-script/register \ + -b -u tdd --recursive -R spec -C test clean: @rm -fr $(JS) diff --git a/axle.coffee b/axle.coffee index 91d6949..62e33bb 100644 --- a/axle.coffee +++ b/axle.coffee @@ -132,9 +132,6 @@ class exports.V1 extends Client rawRequest: ( path, options, cb ) -> super "/v1#{ path }", options, cb - request: ( path, options, cb ) -> - super path, options, cb - ping: ( cb ) -> @rawRequest "/ping", {}, cb @@ -171,6 +168,12 @@ class exports.V1 extends Client return cb null, meta, instanciated + modelDocs: ( cb ) -> + @request "/docs/models", {}, cb + + controllerDocs: ( cb ) -> + @request "/docs/controllers", {}, cb + findKey: ( name, cb ) -> @request "/key/#{ name }", {}, ( err, meta, details ) => return cb err if err diff --git a/package.json b/package.json index c7bc1d2..40a849d 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,8 @@ "request": "~2.16.6" }, "devDependencies": { - "twerp": "=1.0.7", + "mocha": "~1.18.2", + "should": "~3.2.0", "sinon": "1.5.0" }, "author": "Phil Jackson", diff --git a/test/v1_test.coffee b/test/v1_test.coffee index 436ee33..7fa17ca 100644 --- a/test/v1_test.coffee +++ b/test/v1_test.coffee @@ -1,230 +1,32 @@ -_ = require "lodash" -sinon = require "sinon" +{ V1 } = require "../index" +should = require "should" -time = Date.now() +describe "The Client", -> + v1 = null -{ TwerpTest } = require "twerp" -{ Client, AxleObject, V1, Api, Key } = require "../index" + before -> v1 = new V1 "http://localhost:3000" -class AxleTest extends TwerpTest - stubRespose: ( err, meta, results ) -> - if process.env.NO_STUB - return {}= - restore: ( ) -> - calledOnce: ( ) -> true - - return sinon.stub Client::, "request", ( path, options, cb ) -> - return cb err, meta, results - -class exports.Basics extends AxleTest - "test object creation": ( done ) -> - @ok axle = new V1 "localhost", 3001 - - done 1 - - "test #getRangeOptions": ( done ) -> - @ok axle = new V1 "localhost", 3001 - - res = axle.getRangeOptions { query_params: { from: 3 } } - - @equal res.query_params.from, 3 - @equal res.query_params.to, 20 - @equal res.query_params.resolve, true - - done 4 - -class exports.ApiTest extends AxleTest - "setup axle": ( done ) -> - @axle = new V1 "localhost", 3001 + it "should have a valid client", ( done ) -> + v1.should.be.ok done() - "test updating an API": ( done ) -> - api = new Api @axle, "facebook-#{ time }", - endPoint: "graph.facebook.com" - apiFormat: "json" - - stub = @stubRespose null, {}, apiFormat: "json" - api.save ( err ) => - @ok not err - @ok stub.calledOnce - stub.restore() - - stub = @stubRespose null, {}, - old: - apiFormat: "json" - new: - apiFormat: "xml", - - api.update { apiFormat: "xml" }, ( err, meta, results ) => - @ok not err - - @ok stub.calledOnce - stub.restore() - - @equal results.new.apiFormat, "xml" - @equal results.old.apiFormat, "json" - - done 6 - - "test finding an API": ( done ) -> - stub = @stubRespose null, {}, endPoint: "graph.facebook.com" - - @axle.findApi "facebook-#{ time }", ( err, meta, api ) => - @ok stub.calledOnce - stub.restore() - - @ok not err - - @ok api - @equal api.id, "facebook-#{ time }" - @equal api.endPoint, "graph.facebook.com" - - done 5 - - "test creating keys on an API": ( done ) -> - key = new Key @axle, "hello-#{ time }", - qps: 2 - qpd: 10 - - stub = @stubRespose null, {}, { qps: 20, qpd: 10 } - - key.save ( err ) => - @ok not err - @ok stub.calledOnce - stub.restore() - - done 2 - - "test linking keys to an API": ( done ) -> - api = new Api @axle, "facebook-#{ time }", - endPoint: "graph.facebook.com" - - stub = @stubRespose null, {}, { qps: 2, qpd: 10 } - api.linkKey "hello-#{ time }", ( err, meta, key ) => - @ok not err - @ok stub.calledOnce - stub.restore() - - @ok key - - @equal key.id, "hello-#{ time }" - @equal key.qps, 2 - @equal key.qpd, 10 - - done 6 - - "test listing keys": ( done ) -> - api = new Api @axle, "facebook-#{ time }", - endPoint: "graph.facebook.com" - - # save another key - key = new Key @axle, "hello2-#{ time }", - qps: 20 - qpd: 10 - - # save another key - stub = @stubRespose null, {}, { qps: 20, qpd: 10 } - key.save ( err ) => - @ok not err - @ok stub.calledOnce - stub.restore() - - # link it to facebook - stub = @stubRespose null, {}, {} - api.linkKey "hello2-#{ time }", ( err, key ) => - @ok not err - @ok stub.calledOnce - stub.restore() - - res = {} - res["hello2-#{ time }"] = { qpd: 10, qps: 20 } - res["hello-#{ time }"] = { qpd: 172800, qps: 2 } - - stub = @stubRespose null, {}, res - - api.keys {}, ( err, meta, [ key1, key2 ] ) => - @ok not err - @ok stub.calledOnce - stub.restore() - - @equal key1.id, "hello2-#{ time }" - @equal key1.qps, 20 - - @equal key2.id, "hello-#{ time }" - @equal key2.qps, 2 - - done 10 - - "test all apis": ( done ) -> - # save another api - api = new Api @axle, "superduper3-#{ time }", - qps: 20 - qpd: 10 - - # save another api - stub = @stubRespose null, {}, { qps: 20, qpd: 10 } - api.save ( err ) => - @ok not err - @ok stub.calledOnce - stub.restore() - - data = {} - data["superduper1"] = { qps: 20, qpd: 10 } - data["superduper2"] = { qps: 10, qpd: 10 } - data["superduper3"] = { qps: 20, qpd: 1 } - - # link it to facebook - stub = @stubRespose null, {}, data - - @axle.apis {}, ( err, meta, [ superduper1, superduper2, superduper3 ] ) => - @ok not err - @ok stub.calledOnce - stub.restore() - - @deepEqual superduper1.data, { qps: 20, qpd: 10 } - @ok superduper1.client - - @deepEqual superduper2.data, { qps: 10, qpd: 10 } - @ok superduper2.client - - @deepEqual superduper3.data, { qps: 20, qpd: 1 } - @ok superduper3.client - - done 10 - - "test all keys": ( done ) -> - # save another key - key = new Key @axle, "hello3-#{ time }", - qps: 20 - qpd: 10 - - # save another key - stub = @stubRespose null, {}, { qps: 20, qpd: 10 } - key.save ( err ) => - @ok not err - @ok stub.calledOnce - stub.restore() - - data = {} - data["hello1"] = { qps: 20, qpd: 10 } - data["hello2"] = { qps: 10, qpd: 10 } - data["hello3"] = { qps: 20, qpd: 1 } - - # link it to facebook - stub = @stubRespose null, {}, data - - @axle.keys {}, ( err, meta, [ hello1, hello2, hello3 ] ) => - @ok not err - @ok stub.calledOnce - stub.restore() - - @deepEqual hello1.data, { qps: 20, qpd: 10 } - @ok hello1.client - - @deepEqual hello2.data, { qps: 10, qpd: 10 } - @ok hello2.client - - @deepEqual hello3.data, { qps: 20, qpd: 1 } - @ok hello3.client - - done 10 + it "can ping the API", ( done ) -> + v1.ping ( err, res ) -> + return done err if err + res.body.should.equal "pong" + + done() + + it "can fetch keys", ( done ) -> + v1.keys {}, ( err, meta, keys ) -> + return done err if err + keys.should.be.ok + done() + + it "can fetch the model documentation", ( done ) -> + v1.modelDocs ( err, meta, docs ) -> + return done err if err + docs.should.be.ok + docs.keyfactory.should.be.ok + docs.keyfactory.properties.disabled.type.should.equal "boolean" + done()