Skip to content

Instantly share code, notes, and snippets.

@dmuskat
Created August 2, 2012 18:14
Show Gist options
  • Save dmuskat/3239295 to your computer and use it in GitHub Desktop.
Save dmuskat/3239295 to your computer and use it in GitHub Desktop.
MODEL COFFEE FILE image.coffee :
window.models = {} unless window.models?
imageSchema =
"type":"object"
"id":"#image"
"properties":
"name": { "type":"string" }
"encoding": { "type":"string" }
"drcid": { "type":"string" }
"type": { "type":"string" }
"environment": { "type":"string" }
"content": { "type":"string" }
class models.Image extends Backbone.Model
idAttribute: "name"
urlRoot: "/images"
defaults:
name: ""
encoding: ""
drcid: ""
type: ""
environment: ""
content: ""
validate: (attrs) ->
JSV = require("./jsv").JSV
jsvEnv = JSV.createEnvironment()
unless jsvEnv.validate(this, imageSchema) is 0
return "Image must follow schema."
unless typeof attrs.encoding is "string"
return "Encoding must be a string."
unless typeof attrs.drcid is "string"
return "Drcid must be a string."
unless typeof attrs.type is "string"
return "Type must be a string."
unless typeof attrs.environment is "string"
return "Environment must be a string."
SPEC COFFEE FILE image.spec.coffee:
describe "Image testing environment", ->
it "is sane", ->
expect(true).toBe(true)
describe "Image model", ->
myImage = null
beforeEach ->
myImage = new models.Image
afterEach ->
myImage = null
it "follows schema", ->
expect(myImage.get 'name').toBeDefined()
expect(myImage.get 'encoding').toBeDefined()
expect(myImage.get 'drcid').toBeDefined()
expect(myImage.get 'type').toBeDefined()
expect(myImage.get 'environment').toBeDefined()
expect(myImage.get 'content').toBeDefined()
it "knows that drcid is its identifier", ->
myImageWithData = new models.Image({name: "test1"})
expect(myImageWithData.id).toBe('test1')
it "knows that /images is its urlRoot", ->
myImageWithUrl = new models.Image({name: "test1"})
expect(myImageWithUrl.urlRoot).toBe("/images")
it "generates error when data with invalid schema is bound", ->
myImageWithInvalidData = new models.Image({name: true, encoding: 10})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment