Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Finished tests and implementation of api.coffee

  • Loading branch information...
commit 0992542d4ccf1c5ec36af2c52f9626caeda4031a 1 parent 7ed77b4
jward jward authored
28 README.md
Source Rendered
@@ -18,13 +18,13 @@ Schemas
18 18
19 19 Root URL: /1/schemas
20 20
21   -| Command | HTTP Verb | URL |
22   -| :------ | :-------: | :--------------------------------- |
23   -| Create | POST | /1/schemas/(schemaName) |
24   -| Get One | GET | /1/schemas/(schemaName)/(objectId) |
25   -| Update | PUT | /1/schemas/(schemaName)/(objectId) |
26   -| Query | GET | /1/schemas/(schemaName) |
27   -| Delete | DELETE | /1/schemas/(schemaName)/(objectId) |
  21 +| Command | HTTP Verb | URL |
  22 +| :------ | :-------: | :------------------- |
  23 +| Create | POST | /1/schemas |
  24 +| Get One | GET | /1/schemas/:objectId |
  25 +| Update | PUT | /1/schemas/:objectId |
  26 +| Query | GET | /1/schemas |
  27 +| Delete | DELETE | /1/schemas/:objectId |
28 28
29 29
30 30 Models
@@ -32,11 +32,11 @@ Models
32 32
33 33 Root URL: /1/models
34 34
35   -| Command | HTTP Verb | URL |
36   -| :------ | :-------: | :------------------------------- |
37   -| Create | POST | /1/models/(modelName) |
38   -| Get One | GET | /1/models/(modelName)/(objectId) |
39   -| Update | PUT | /1/models/(modelName)/(objectId) |
40   -| Query | GET | /1/models/(modelName) |
41   -| Delete | DELETE | /1/models/(modelName)/(objectId) |
  35 +| Command | HTTP Verb | URL |
  36 +| :------ | :-------: | :----------------------------- |
  37 +| Create | POST | /1/models/:modelName |
  38 +| Get One | GET | /1/models/:modelName/:objectId |
  39 +| Update | PUT | /1/models/:modelName/:objectId |
  40 +| Query | GET | /1/models/:modelName |
  41 +| Delete | DELETE | /1/models/:modelName/:objectId |
42 42
18 api.coffee
... ... @@ -1,11 +1,19 @@
1 1 class Api
2 2 constructor: (@app)->
3 3 expose: (urlRoot, handlers)->
4   - # CREATE
5   - # UPDATE
6   - # GET
7   - # QUERY
8   - # DELETE
  4 +
  5 + @app.post(urlRoot, handlers.create)
  6 +
  7 + @app.put(@objectId(urlRoot), handlers.update)
  8 +
  9 + @app.get(urlRoot, handlers.query)
  10 +
  11 + @app.get(@objectId(urlRoot), handlers.get)
  12 +
  13 + @app.delete(@objectId(urlRoot), handlers.delete)
  14 +
  15 + objectId: (urlRoot)->
  16 + urlRoot + '/:objectId'
9 17
10 18 module.exports = (app)->
11 19 new Api(app)
4 package.json
@@ -15,7 +15,9 @@
15 15 "cli-color": "~0.2.1"
16 16 },
17 17 "devDependencies": {
18   - "mocha": "~1.7.4"
  18 + "mocha": "~1.7.4",
  19 + "sinon": "~1.5.2",
  20 + "chai": "~1.4.0"
19 21 },
20 22 "optionalDependencies": {},
21 23 "engines": {
3  schemas.coffee
... ... @@ -1,4 +1 @@
1 1
2   -
3   -module.export = (app)->
4   -
43 test/api_tests.coffee
... ... @@ -0,0 +1,43 @@
  1 +Api = require('../api')
  2 +
  3 +describe 'Api', ->
  4 +
  5 + describe 'expose', ->
  6 +
  7 + app = {
  8 + post: sinon.spy()
  9 + put: sinon.spy()
  10 + get: sinon.spy()
  11 + delete: sinon.spy()
  12 + }
  13 + handlers = {
  14 + create: ()->
  15 + update: ()->
  16 + get: ()->
  17 + query: ()->
  18 + delete: ()->
  19 + }
  20 + api = new Api(app)
  21 +
  22 + api.expose('/1/schemas', handlers)
  23 +
  24 + it 'Create: POST /1/schemas', ->
  25 + assert app.post.called, 'app.post should be called'
  26 + assert app.post.calledWith('/1/schemas', handlers.create), 'app.post has wrong params'
  27 +
  28 + it 'Update: PUT /1/schemas/:objectId', ->
  29 + assert app.put.called, 'app.put should be called'
  30 + assert app.put.calledWith('/1/schemas/:objectId', handlers.update), 'app.put has wrong params'
  31 +
  32 + it 'Query: GET /1/schemas', ->
  33 + console.log app.put.args
  34 + assert app.get.called, 'app.get (query) should be called'
  35 + assert app.get.calledWith('/1/schemas', handlers.query), 'app.get (query) has wrong params'
  36 +
  37 + it 'Get: GET /1/schemas/:objectId', ->
  38 + assert app.get.called, 'app.get (single) should be called'
  39 + assert app.get.calledWith('/1/schemas/:objectId', handlers.get), 'app.get (single) has wrong params'
  40 +
  41 + it 'Delete: DELETE /1/schemas/:objectId', ->
  42 + assert app.delete.called, 'app.delete should be called'
  43 + assert app.delete.calledWith('/1/schemas/:objectId', handlers.delete), 'app.delete has wrong params'
1  test/mocha.opts
... ... @@ -1 +1,2 @@
1 1 --compilers coffee:coffee-script
  2 +-r test/test_helper.js
3  test/test_helper.js
... ... @@ -0,0 +1,3 @@
  1 +// this file is automatically included before running tests (see mocha.opts file)
  2 +global.sinon = require('sinon')
  3 +global.assert = require('chai').assert

0 comments on commit 0992542

Please sign in to comment.
Something went wrong with that request. Please try again.