passivemodel
Version:
Model that can be used on both Client & Server
103 lines (82 loc) • 2.82 kB
text/coffeescript
require './helper'
require '../lib/conversion'
describe 'Attribute Conversion', ->
it "should update only typed attribytes", ->
class Tmp.User extends Model
name : String
hasMail : Boolean
age : Number
u = new Tmp.User()
u.set {name: 'Alex', hasMail: 'true', age: '31'}, cast: true
expect([u.name, u.hasMail, u.age]).to.eql ['Alex', true, 31]
# Should skip attributes if type not specified.
u.set {unknown: false}, cast: true
expect(u.unknown).to.be undefined
it "should inherit attribute types", ->
class Tmp.User extends Model
age: Number
class Tmp.Writer extends Tmp.User
posts: Number
u = new Tmp.Writer()
u.set {age: '20', posts: '12'}, cast: true
expect([u.age, u.posts]).to.eql [20, 12]
it 'should parse string values', ->
cases = [
[Boolean, 'true', true]
[Number, '12', 12]
[String, 'Hi', 'Hi']
]
for cse in cases
[type, raw, expected] = cse
expect(Model._cast(raw, type)).to.eql expected
expect(Model._cast('2011-08-23', Date)).to.eql (new Date('2011-08-23'))
describe "Model Conversion", ->
it "should convert object to and from hash & array", ->
class Tmp.Post extends Model
'tags', 'comments'
class Tmp.Comment extends Model
# Should aslo allow to use models that
# are saved to array.
# To do so we need to use toArray and afterFromHash.
class Tmp.Tags extends Model
constructor: -> = []
push: (args...) -> .push args...
toArray: ->
Tmp.Post.afterFromHash = (obj, hash) ->
obj.tags = new Tmp.Tags
obj.tags.array = hash.tags
# Creating some data.
comment = new Tmp.Comment()
comment.text = 'Some text'
tags = new Tmp.Tags()
tags.push 'a', 'b'
post = new Tmp.Post()
post.title = 'Some title'
post.comments = [comment]
post.tags = tags
hash = {
_class : 'Post',
title : 'Some title',
comments : [{text: 'Some text', _class: 'Comment'}],
tags : ['a', 'b']
}
# Converting to Hash.
expect(post.toHash()).to.eql hash
[post.id, hash.id] = ['some id', 'some id']
expect(post.toHash()).to.eql hash
# Converting from mongo.
expect(Model.fromHash(hash).toHash()).to.eql hash
it "chldren should have `_parent` reference to the main object", ->
class Tmp.Unit extends Model
'items'
class Tmp.Item extends Model
unit = new Tmp.Unit()
unit.items = [
new Tmp.Item(name: 'Psionic blade')
new Tmp.Item(name: 'Plasma shield')
]
hash = unit.toHash()
unit = Model.fromHash(hash)
expect(unit._parent).to.be undefined
expect(unit.items[0]._parent).to.eql unit