forked from jnunemaker/toystore
/
serialization_spec.rb
150 lines (129 loc) · 3.8 KB
/
serialization_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
require 'helper'
describe Toy::Serialization do
uses_objects('User', 'Move')
before do
User.attribute :name, String
User.attribute :age, Integer
end
it "serializes to json" do
doc = User.new(:name => 'John', :age => 28)
MultiJson.load(doc.to_json).should == {
'user' => {
'name' => 'John',
'id' => doc.id,
'age' => 28
}
}
end
it "serializes to xml" do
doc = User.new(:name => 'John', :age => 28)
Hash.from_xml(doc.to_xml).should == {
'user' => {
'name' => 'John',
'id' => doc.id,
'age' => 28
}
}
end
it "correctly serializes methods" do
User.class_eval do
def foo
{'foo' => 'bar'}
end
end
json = User.new.to_json(:methods => [:foo])
MultiJson.load(json)['user']['foo'].should == {'foo' => 'bar'}
end
it "allows using :only" do
user = User.new
json = user.to_json(:only => :id)
MultiJson.load(json).should == {'user' => {'id' => user.id}}
end
it "allows using :only with strings" do
user = User.new
json = user.to_json(:only => 'id')
MultiJson.load(json).should == {'user' => {'id' => user.id}}
end
it "allows using :except" do
user = User.new
json = user.to_json(:except => :id)
MultiJson.load(json)['user'].should_not have_key('id')
end
it "allows using :except with strings" do
user = User.new
json = user.to_json(:except => 'id')
MultiJson.load(json)['user'].should_not have_key('id')
end
describe "serializing specific attributes" do
before do
Move.attribute(:index, Integer)
Move.attribute(:points, Integer)
Move.attribute(:words, Array)
end
it "should default to all attributes" do
move = Move.new(:index => 0, :points => 15, :words => ['QI', 'XI'])
move.serializable_attributes.should == [:id, :index, :points, :words]
end
it "should be set per model" do
Move.class_eval do
def serializable_attributes
attribute_names = super - [:index]
attribute_names
end
end
move = Move.new(:index => 0, :points => 15, :words => ['QI', 'XI'])
move.serializable_attributes.should == [:id, :points, :words]
end
it "should only serialize specified attributes" do
Move.class_eval do
def serializable_attributes
attribute_names = super - [:index]
attribute_names
end
end
move = Move.new(:index => 0, :points => 15, :words => ['QI', 'XI'])
MultiJson.load(move.to_json).should == {
'move' => {
'id' => move.id,
'points' => 15,
'words' => ["QI", "XI"]
}
}
end
it "should serialize additional methods along with attributes" do
Move.class_eval do
def serializable_attributes
attribute_names = super + [:calculated_attribute]
attribute_names
end
def calculated_attribute
'some value'
end
end
move = Move.new(:index => 0, :points => 15, :words => ['QI', 'XI'])
MultiJson.load(move.to_json).should == {
'move' => {
'id' => move.id,
'index' => 0,
'points' => 15,
'words' => ["QI", "XI"],
'calculated_attribute' => 'some value'
}
}
end
end
describe "#serializable_hash" do
context "with method that is another toystore object" do
before do
Move.class_eval { attr_accessor :creator }
end
let(:move) { Move.new(:creator => User.new) }
it "returns serializable hash of object" do
move.serializable_hash(:methods => [:creator]).should == {
'id' => move.id,
'creator' => {'id' => move.creator.id}
}
end
end
end
end