Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
  • 6 commits
  • 4 files changed
  • 0 commit comments
  • 1 contributor
View
26 app/controllers/request_controller.coffee
@@ -10,7 +10,7 @@ db = connection.database("cozy")
# GET /request/:req_name
action 'access', ->
- db.view 'cozy-request/' + params.req_name, (err, res) ->
+ db.view "cozy-request/#{params.req_name}", (err, res) ->
if err
send 404
else
@@ -18,6 +18,26 @@ action 'access', ->
delete value._rev # CouchDB specific, user don't need it
send res
-# PUT /request/:id
+# PUT /request/:req_name
+action 'definition', ->
+ db.get "_design/cozy-request", (err, res) ->
+ if err && err.error is 'not_found'
+ design_doc = {}
+ design_doc[params.req_name] = body
+ db.save "_design/cozy-request", design_doc, (err, res) ->
+ if err
+ console.log "[Definition] err: " + JSON.stringify err
+ send 500
+ else
+ send 200
+ else
+ views = res.views
+ views[params.req_name] = body
+ db.merge "_design/cozy-request", {views:views}, (err, res) ->
+ if err
+ console.log "[Definition] err: " + JSON.stringify err
+ send 500
+ else
+ send 200
-# DELETE /request/:id
+# DELETE /request/:req_name
View
2  config/routes.coffee
@@ -14,3 +14,5 @@ exports.routes = (map) ->
#Request handling
map.get '/request/:req_name/', 'request#access'
+
+ map.put 'request/:req_name/', 'request#definition'
View
6 test/data_tests.coffee
@@ -8,7 +8,7 @@ client = new Client("http://localhost:8888/")
# connection to DB for "hand work"
cradle = require 'cradle'
connection = new cradle.Connection
- cache: true,
+ cache: false,
raw: false
db = connection.database('cozy')
@@ -166,13 +166,13 @@ describe "Data handling tests", ->
@_id = @body._id
it "Then the Document should exist in Database", (done) ->
- client.get "data/exist/" + @_id + "/", (error, response, body) =>
+ client.get "data/exist/#{@_id}/", (error, response, body) =>
@body = parseBody response, body
@body.exist.should.be.true
done()
it "Then the Document in DB should equal the sent Document", (done) ->
- client.get "data/" + @_id + "/", (error, response, body) =>
+ client.get "data/#{@_id}/", (error, response, body) =>
@body = parseBody response, body
@body.should.have.property 'value', @randomValue
done()
View
113 test/request_tests.coffee
@@ -8,7 +8,7 @@ client = new Client("http://localhost:8888/")
# connection to DB for "hand work"
cradle = require 'cradle'
connection = new cradle.Connection
- cache: true,
+ cache: false,
raw: false
db = connection.database('cozy')
@@ -36,15 +36,7 @@ describe "Request handling tests", ->
console.log 'DB recreated'
docs = ({'type':'dumb_doc', 'num':num} for num in [0..100])
db.save docs, ->
- map_no_doc = (doc) ->
- map_every_docs = (doc) ->
- emit doc._id, null
-
- views = {no_doc:{map:map_no_doc}, \
- every_docs:{map:map_every_docs}}
-
- db.save "_design/cozy-request", views, ->
- done()
+ done()
# Start application before starting tests.
before (done) ->
@@ -58,6 +50,52 @@ describe "Request handling tests", ->
+ describe "View creation", ->
+ describe "Creation of the first view + design document creation", ->
+ before cleanRequest
+
+ it "When I send a request to create view every_docs", (done) ->
+ map = (doc) ->
+ emit doc._id, doc
+ return
+ @viewAll = {map:map.toString()}
+
+ client.put 'request/every_docs/', @viewAll, \
+ (error, response, body) =>
+ response.statusCode.should.equal 200
+ done()
+
+ it "Then the design document should exist and contain the view", \
+ (done) ->
+ db.get '_design/cozy-request', (err, res) ->
+ should.not.exist err
+ should.exist res
+ res.views.should.have.property 'every_docs', @viewAll
+ done()
+
+ describe "Creation of a new view", ->
+ before cleanRequest
+
+ it "When I send a request to create view even_num", (done) ->
+ map = (doc) ->
+ emit doc._id, doc if (doc.num? && (doc.num % 2) is 0)
+ return
+ @viewEven = {map:map.toString()}
+
+ client.put 'request/even_num/', @viewEven, \
+ (error, response, body) =>
+ response.statusCode.should.equal 200
+ done()
+
+ it "Then the design document should exist and contain the views", \
+ (done) ->
+ db.get '_design/cozy-request', (err, res) ->
+ should.not.exist err
+ should.exist res
+ res.views.should.have.property 'every_docs', @viewAll
+ res.views.should.have.property 'even_num', @viewEven
+ done()
+
describe "Access to a view without option", ->
describe "Access to a non existing view", ->
before cleanRequest
@@ -68,28 +106,59 @@ describe "Request handling tests", ->
done()
it "Then error 404 should be returned", ->
- @response.statusCode.should.equal 404
-
- describe "Access to an existing view : no_doc", ->
+ @response.statusCode.should.equal
+
+ describe "Access to an existing view : every_docs", (done) ->
before cleanRequest
- it "When I send a request to access view no_doc", (done) ->
- client.get "request/no_doc/", (error, response, body) =>
+ it "When I send a request to access view every_docs", (done) ->
+ client.get "request/every_docs/", (error, response, body) =>
response.statusCode.should.equal 200
@body = parseBody response, body
done()
- it "Then I should have no document returned", ->
- @body.should.have.length 0
+ it "Then I should have 101 documents returned", ->
+ @body.should.have.length 101
- describe "Access to an existing view : every_docs", (done) ->
+ describe "Access to an existing view : even_num", (done) ->
before cleanRequest
- it "When I send a request to access view every-docs", (done) ->
- client.get "request/every_docs/", (error, response, body) =>
+ it "When I send a request to access view every_docs", (done) ->
+ client.get "request/even_num/", (error, response, body) =>
response.statusCode.should.equal 200
@body = parseBody response, body
done()
- it "Then I should have 101 documents returned", ->
- @body.should.have.length 101
+ it "Then I should have 51 documents returned", ->
+ @body.should.have.length 51
+
+ describe "Update of an existing view", ->
+ describe "Redefinition of existing view even_num", ->
+ before cleanRequest
+
+ it "When I send a request to create view even_num", (done) ->
+ map = (doc) ->
+ emit doc._id, doc if (doc.num? && (doc.num % 2) isnt 0)
+ return
+ @viewEven = {map:map.toString()}
+
+ client.put 'request/even_num/', @viewEven, \
+ (error, response, body) =>
+ response.statusCode.should.equal 200
+ done()
+
+ it "Then the design document should exist and contain the views", \
+ (done) ->
+ db.get '_design/cozy-request', (err, res) ->
+ should.not.exist err
+ should.exist res
+ res.views.should.have.property 'every_docs', @viewAll
+ res.views.should.have.property 'even_num', @viewEven
+ done()
+
+ it "And I should retrieve the good values", (done) ->
+ client.get "request/even_num/", (error, response, body) =>
+ response.statusCode.should.equal 200
+ @body = parseBody response, body
+ @body.should.have.length 50
+ done()

No commit comments for this range

Something went wrong with that request. Please try again.