Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add one clientKeys method. factor tests.

  • Loading branch information...
commit 89f5e8a1f0635e4a1695d78b527b551365b674b0 1 parent c2ba5d9
@robey authored
View
5 src/fauna/fauna_client.coffee
@@ -44,6 +44,8 @@ class FaunaClient
# hook up nested namespaces
@publisherKeys = new _PublisherKeys
for k, v of @publisherKeys then @publisherKeys[k] = v.bind(@)
+ @clientKeys = new _ClientKeys
+ for k, v of @clientKeys then @clientKeys[k] = v.bind(@)
debug: (message) -> Rest.debug(message)
@@ -73,6 +75,9 @@ class FaunaClient
getById: requireOwner asObject (id) -> @rest("get", "#{id}")
delete: requireOwner (id) -> @rest("delete", "#{id}")
+ class _ClientKeys
+ get: requireOwner asEventArray -> @rest("get", "keys/client")
+
# ----- internals
View
65 test/test_client_keys.coffee
@@ -0,0 +1,65 @@
+should = require 'should'
+Q = require 'q'
+util = require 'util'
+
+fauna = require("../lib/fauna")
+test_util = require("./test_util")
+withSuccessfulRequest = test_util.withSuccessfulRequest
+futureTest = test_util.futureTest
+
+describe "FaunaClient.clientKeys", ->
+ it "get", futureTest ->
+ f = new fauna.FaunaClient()
+ f.setOwnerAuth("u", "p")
+ r = -> f.clientKeys.get()
+ withSuccessfulRequest(JSON.parse(data1), r).then ([ resp, requests ]) ->
+ requests.length.should.eql(1)
+ requests[0].url.should.match(/keys\/client/)
+ resp.map((item) -> item.key).should.eql [
+ "AQEAayWp-xAAAQBrJamcsAABodTOVpMJU4EvMU6paEgrHA"
+ "AQEAayWp97AAAQBrJamcsAABR0HPBQQdosjqwCGI7q1cMg"
+ ]
+
+
+data1 = """
+{
+ "resource" : {
+ "ref" : "keys/client",
+ "class" : "sets",
+ "after" : 9223372036854775,
+ "creates" : 2,
+ "updates" : 0,
+ "deletes" : 0,
+ "events" : [
+ {
+ "ts" : 1365020937349000,
+ "action" : "create",
+ "resource" : "keys/client/30159234499870721",
+ "set" : "keys/client"
+ },
+ {
+ "ts" : 1365020937292003,
+ "action" : "create",
+ "resource" : "keys/client/30159234443247617",
+ "set" : "keys/client"
+ }
+ ]
+ },
+ "references" : {
+ "keys/client/30159234499870721" : {
+ "ref" : "keys/client/30159234499870721",
+ "class" : "keys/client",
+ "ts" : 1365020937349000,
+ "key" : "AQEAayWp-xAAAQBrJamcsAABodTOVpMJU4EvMU6paEgrHA",
+ "deleted" : false
+ },
+ "keys/client/30159234443247617" : {
+ "ref" : "keys/client/30159234443247617",
+ "class" : "keys/client",
+ "ts" : 1365020937292003,
+ "key" : "AQEAayWp97AAAQBrJamcsAABR0HPBQQdosjqwCGI7q1cMg",
+ "deleted" : false
+ }
+ }
+}
+"""
View
62 test/test_fauna_client.coffee
@@ -3,13 +3,9 @@ Q = require 'q'
util = require 'util'
fauna = require("../lib/fauna")
-
-withSuccessfulRequest = (json, f) ->
- fauna.Rest.withRequestHandler(((options) -> [ null, 200, JSON.stringify(json) ]), f)
-
-futureTest = (f) ->
- (done) ->
- f().then((-> done()), ((error) -> done(error)))
+test_util = require("./test_util")
+withSuccessfulRequest = test_util.withSuccessfulRequest
+futureTest = test_util.futureTest
describe "FaunaClient", ->
@@ -74,46 +70,6 @@ describe "FaunaClient", ->
requests.length.should.eql(1)
requests[0].url.should.match(/u:p@/)
- describe "publisherKeys", ->
- it "get", futureTest ->
- f = new fauna.FaunaClient()
- f.setOwnerAuth("u", "p")
- r = -> f.publisherKeys.get()
- withSuccessfulRequest(JSON.parse(data1), r).then ([ resp, requests ]) ->
- requests.length.should.eql(1)
- requests[0].url.should.match(/keys\/publisher/)
- resp.map((item) -> item.key).should.eql [
- "AQAAayWp_qAAAQBrJamcsAABzAFAUI2ckXGpAt2VjWsyiA"
- "AQAAayWp97AEAQBrJamcsAABlnqXMsjdfw3kJU44o1dpDg"
- "AQAAayWpw9AAAQBrJamcsAABfoxWbkY-6Vd8d_er_VP_NA"
- ]
-
- it "create", futureTest ->
- f = new fauna.FaunaClient()
- f.setOwnerAuth("u", "p")
- r = -> f.publisherKeys.create()
- withSuccessfulRequest(JSON.parse(data3), r).then ([ resp, requests ]) ->
- requests.length.should.eql(1)
- requests[0].url.should.match(/keys\/publisher/)
- resp.key.should.eql "AQAAayWp97AEAQBrJamcsAABlnqXMsjdfw3kJU44o1dpDg"
-
- it "getById", futureTest ->
- f = new fauna.FaunaClient()
- f.setOwnerAuth("u", "p")
- r = -> f.publisherKeys.getById("keys/publisher/30159234443248641")
- withSuccessfulRequest(JSON.parse(data3), r).then ([ resp, requests ]) ->
- requests.length.should.eql(1)
- requests[0].url.should.match(/keys\/publisher\/30159234443248641/)
- resp.key.should.eql "AQAAayWp97AEAQBrJamcsAABlnqXMsjdfw3kJU44o1dpDg"
-
- it "delete", futureTest ->
- f = new fauna.FaunaClient()
- f.setOwnerAuth("u", "p")
- r = -> f.publisherKeys.delete("keys/publisher/30159234443248641")
- fauna.Rest.withRequestHandler(((options) -> [ null, 204, null ]), r).then ([ resp, requests ]) ->
- requests.length.should.eql(1)
- requests[0].url.should.match(/keys\/publisher\/30159234443248641/)
- (resp?).should.eql(false)
# test data from the fauna documentation for GET keys/publisher
data1 = """
@@ -192,15 +148,3 @@ data2 =
class: "cats"
data: { name: "Simba" }
references: { friend: "spooky" }
-
-data3 = """
-{
- "resource" : {
- "ref" : "keys/publisher/30159234443248641",
- "class" : "keys/publisher",
- "ts" : 1365020937292002,
- "key" : "AQAAayWp97AEAQBrJamcsAABlnqXMsjdfw3kJU44o1dpDg",
- "deleted" : false
- }
-}
-"""
View
140 test/test_publisher_keys.coffee
@@ -0,0 +1,140 @@
+should = require 'should'
+Q = require 'q'
+util = require 'util'
+
+fauna = require("../lib/fauna")
+test_util = require("./test_util")
+withSuccessfulRequest = test_util.withSuccessfulRequest
+futureTest = test_util.futureTest
+
+
+describe "FaunaClient.publisherKeys", ->
+ it "get", futureTest ->
+ f = new fauna.FaunaClient()
+ f.setOwnerAuth("u", "p")
+ r = -> f.publisherKeys.get()
+ withSuccessfulRequest(JSON.parse(data1), r).then ([ resp, requests ]) ->
+ requests.length.should.eql(1)
+ requests[0].url.should.match(/keys\/publisher/)
+ resp.map((item) -> item.key).should.eql [
+ "AQAAayWp_qAAAQBrJamcsAABzAFAUI2ckXGpAt2VjWsyiA"
+ "AQAAayWp97AEAQBrJamcsAABlnqXMsjdfw3kJU44o1dpDg"
+ "AQAAayWpw9AAAQBrJamcsAABfoxWbkY-6Vd8d_er_VP_NA"
+ ]
+
+ it "create", futureTest ->
+ f = new fauna.FaunaClient()
+ f.setOwnerAuth("u", "p")
+ r = -> f.publisherKeys.create()
+ withSuccessfulRequest(JSON.parse(data3), r).then ([ resp, requests ]) ->
+ requests.length.should.eql(1)
+ requests[0].url.should.match(/keys\/publisher/)
+ resp.key.should.eql "AQAAayWp97AEAQBrJamcsAABlnqXMsjdfw3kJU44o1dpDg"
+
+ it "getById", futureTest ->
+ f = new fauna.FaunaClient()
+ f.setOwnerAuth("u", "p")
+ r = -> f.publisherKeys.getById("keys/publisher/30159234443248641")
+ withSuccessfulRequest(JSON.parse(data3), r).then ([ resp, requests ]) ->
+ requests.length.should.eql(1)
+ requests[0].url.should.match(/keys\/publisher\/30159234443248641/)
+ resp.key.should.eql "AQAAayWp97AEAQBrJamcsAABlnqXMsjdfw3kJU44o1dpDg"
+
+ it "delete", futureTest ->
+ f = new fauna.FaunaClient()
+ f.setOwnerAuth("u", "p")
+ r = -> f.publisherKeys.delete("keys/publisher/30159234443248641")
+ fauna.Rest.withRequestHandler(((options) -> [ null, 204, null ]), r).then ([ resp, requests ]) ->
+ requests.length.should.eql(1)
+ requests[0].url.should.match(/keys\/publisher\/30159234443248641/)
+ (resp?).should.eql(false)
+
+# test data from the fauna documentation for GET keys/publisher
+data1 = """
+{
+ "resource" : {
+ "ref" : "keys/publisher",
+ "class" : "sets",
+ "after" : 9223372036854775,
+ "creates" : 3,
+ "updates" : 0,
+ "deletes" : 0,
+ "events" : [
+ {
+ "ts" : 1365020937405000,
+ "action" : "create",
+ "resource" : "keys/publisher/30159234559639553",
+ "set" : "keys/publisher"
+ },
+ {
+ "ts" : 1365020937292002,
+ "action" : "create",
+ "resource" : "keys/publisher/30159234443248641",
+ "set" : "keys/publisher"
+ },
+ {
+ "ts" : 1365020936500000,
+ "action" : "create",
+ "resource" : "keys/publisher/30159233572929537",
+ "set" : "keys/publisher"
+ }
+ ]
+ },
+ "references" : {
+ "keys/publisher/30159234559639553" : {
+ "ref" : "keys/publisher/30159234559639553",
+ "class" : "keys/publisher",
+ "ts" : 1365020937405000,
+ "key" : "AQAAayWp_qAAAQBrJamcsAABzAFAUI2ckXGpAt2VjWsyiA",
+ "deleted" : false
+ },
+ "keys/publisher/30159234443248641" : {
+ "ref" : "keys/publisher/30159234443248641",
+ "class" : "keys/publisher",
+ "ts" : 1365020937292002,
+ "key" : "AQAAayWp97AEAQBrJamcsAABlnqXMsjdfw3kJU44o1dpDg",
+ "deleted" : false
+ },
+ "keys/publisher/30159233572929537" : {
+ "ref" : "keys/publisher/30159233572929537",
+ "class" : "keys/publisher",
+ "ts" : 1365020936500000,
+ "key" : "AQAAayWpw9AAAQBrJamcsAABfoxWbkY-6Vd8d_er_VP_NA",
+ "deleted" : false
+ }
+ }
+}
+"""
+
+data2 =
+ resource:
+ class: "sets"
+ events: [
+ { ts: 4, action: "create", resource: "spooky", set: "_" }
+ { ts: 3, action: "create", resource: "simba", set: "_" }
+ ]
+ references:
+ spooky:
+ class: "cats"
+ data: { name: "Spooky" }
+ references: { friend: "commie" }
+ commie:
+ class: "cats"
+ data: { name: "Commie" }
+ references: {}
+ simba:
+ class: "cats"
+ data: { name: "Simba" }
+ references: { friend: "spooky" }
+
+data3 = """
+{
+ "resource" : {
+ "ref" : "keys/publisher/30159234443248641",
+ "class" : "keys/publisher",
+ "ts" : 1365020937292002,
+ "key" : "AQAAayWp97AEAQBrJamcsAABlnqXMsjdfw3kJU44o1dpDg",
+ "deleted" : false
+ }
+}
+"""
View
11 test/test_util.coffee
@@ -0,0 +1,11 @@
+
+fauna = require("../lib/fauna")
+
+# simplified version of 'withRequestHandler'
+exports.withSuccessfulRequest = (json, f) ->
+ fauna.Rest.withRequestHandler(((options) -> [ null, 200, JSON.stringify(json) ]), f)
+
+# run a test as a future, and call mocha's 'done' method at the end of the chain.
+exports.futureTest = (f) ->
+ (done) ->
+ f().then((-> done()), ((error) -> done(error)))
Please sign in to comment.
Something went wrong with that request. Please try again.