Permalink
Browse files

installSchema.

  • Loading branch information...
1 parent b6d471e commit 89a0fb983de9e6662ffbc9307a3ba4c2cbad4865 @robey committed Apr 12, 2013
Showing with 122 additions and 5 deletions.
  1. +24 −5 src/fauna/fauna_client.coffee
  2. +31 −0 test/test_event_sets.coffee
  3. +67 −0 test/test_fauna_client.coffee
@@ -80,6 +80,7 @@ class FaunaClient
rv
@publisherKeys = plumb _PublisherKeys
@clientKeys = plumb _ClientKeys
+ @eventSets = plumb _EventSets
@classes = plumb _Classes
@_testing = plumb _Testing
@@ -125,13 +126,28 @@ class FaunaClient
# tell fauna to create classes & event sets for us.
# return: { class, event_sets[] }[]
installSchema: ->
+ ###
+ Create (or update) all the custom classes & event sets registered in the
+ schema. On success, returns a map of ref-id to (class or event set).
+ ###
Q.all(
@schema.foreachClass (name, metadata) =>
- @createClass(name).then (klass) =>
- Q.all(for setName in metadata.eventSets then @createEventSet(name, setName)).then (sets) =>
- { "class": klass, "event_sets": sets }
- )
- # XXX
+ if metadata.native
+ Q(null)
+ else
+ @classes.create(name, metadata.data).then (klass) =>
+ Q.all(
+ metadata.eventSets.map (setName) => @eventSets.create(metadata.getClassName(), setName)
+ )
+ .then (sets) =>
+ { "class": klass, eventSets: sets }
+ ).then (results) =>
+ rv = {}
+ results.map (x) =>
+ if x?
+ rv[x["class"]._fauna.id] = x["class"]
+ for set in x.eventSets then rv[set._fauna.id] = set
+ rv
class _PublisherKeys
get: requireOwner asEventArray -> @rest("get", "keys/publisher")
@@ -151,6 +167,9 @@ class FaunaClient
class _ClientKeys
get: requireOwner asEventArray -> @rest("get", "keys/client")
+ class _EventSets
+ create: requirePublisher asObject (className, setName) -> @rest("put", "#{className}/sets/#{setName}/config")
+
class _Classes
get: requirePublisher asEventArray -> @rest("get", "classes")
getByName: requireTokenOrPublisher asObject (name) -> @rest("get", "classes/#{name}/config")
@@ -0,0 +1,31 @@
+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.eventSets", ->
+ it "create", futureTest ->
+ f = new fauna.FaunaClient()
+ f.setPublisherKey("qqq")
+ r = -> f.eventSets.create("users", "spellbook")
+ withSuccessfulRequest(JSON.parse(data1), r).then ([ resp, requests ]) ->
+ requests.length.should.eql(1)
+ requests[0].url.should.match(/\/users\/sets\/spellbook\/config/)
+ resp.name.should.eql("spellbook")
+
+
+data1 = """
+{
+ "resource" : {
+ "ref" : "users/sets/spellbook/config",
+ "class" : "sets/config",
+ "ts" : 1365020937396000,
+ "name" : "spellbook",
+ "deleted" : false
+ }
+}
+"""
@@ -116,6 +116,35 @@ describe "FaunaClient", ->
requests[0].url.should.match(/everything/)
resp.name.should.eql("Sierra Online")
+ it "can install the schema", futureTest ->
+ class Fish extends fauna.Class
+ @eventSet "scales"
+ @data swimming: true
+ class Bird extends fauna.Class
+ @data dinosaur: true
+
+ f = new fauna.FaunaClient()
+ f.addPrototypes Fish, Bird
+ f.setPublisherKey("winner")
+ r = -> f.installSchema()
+ handler = (options) ->
+ if options.url.match(/classes\/fishs\/config/)
+ [ null, 200, data_fish ]
+ else if options.url.match(/classes\/birds\/config/)
+ [ null, 200, data_bird ]
+ else
+ [ null, 200, data_fish_set ]
+ fauna.Rest.withRequestHandler(handler, r).then ([ resp, requests ]) ->
+ requests.length.should.equal(3)
+ Object.keys(resp).sort().should.eql [
+ "classes/birds/config"
+ "classes/fishs/config"
+ "classes/fishs/sets/scales/config"
+ ]
+ resp["classes/birds/config"].name.should.eql("birds")
+ resp["classes/fishs/config"].name.should.eql("fishs")
+ resp["classes/fishs/sets/scales/config"].name.should.eql("scales")
+
# test data from the fauna documentation for GET keys/publisher
data1 = """
@@ -207,3 +236,41 @@ data3 = """
}
}
"""
+
+data_fish = """
+{
+ "resource" : {
+ "ref" : "classes/fishs/config",
+ "class" : "classes/config",
+ "ts" : 9000,
+ "name" : "fishs",
+ "data" : { "swimming": true },
+ "deleted" : false
+ }
+}
+"""
+
+data_fish_set = """
+{
+ "resource" : {
+ "ref" : "classes/fishs/sets/scales/config",
+ "class" : "sets/config",
+ "ts" : 7000,
+ "name" : "scales",
+ "deleted" : false
+ }
+}
+"""
+
+data_bird = """
+{
+ "resource" : {
+ "ref" : "classes/birds/config",
+ "class" : "classes/config",
+ "ts" : 8000,
+ "name" : "birds",
+ "data" : { "dinosaur": true },
+ "deleted" : false
+ }
+}
+"""

0 comments on commit 89a0fb9

Please sign in to comment.