10
10
columns :id, :abc, :def, :ghi
15
15
it "should allow setting additional serializable attributes via plugin :serialization call" do
16
16
@c.plugin :serialization, :yaml, :abc
17
17
@c.create(:abc => 1, :def=> 2)
18
MODEL_DB.sqls.last.should =~ /INSERT INTO items \((abc, def|def, abc)\) VALUES \(('--- 1\n(\.\.\.\n)?', 2|2, '--- 1\n(\.\.\.\n)?')\)/
18
DB.sqls.last.should =~ /INSERT INTO items \((abc, def|def, abc)\) VALUES \(('--- 1\n(\.\.\.\n)?', 2|2, '--- 1\n(\.\.\.\n)?')\)/
20
20
@c.plugin :serialization, :marshal, :def
21
21
@c.create(:abc => 1, :def=> 1)
22
MODEL_DB.sqls.last.should =~ /INSERT INTO items \((abc, def|def, abc)\) VALUES \(('--- 1\n(\.\.\.\n)?', 'BAhpBg==\n'|'BAhpBg==\n', '--- 1\n(\.\.\.\n)?')\)/
22
DB.sqls.last.should =~ /INSERT INTO items \((abc, def|def, abc)\) VALUES \(('--- 1\n(\.\.\.\n)?', 'BAhpBg==\n'|'BAhpBg==\n', '--- 1\n(\.\.\.\n)?')\)/
24
24
@c.plugin :serialization, :json, :ghi
25
25
@c.create(:ghi => [123])
26
MODEL_DB.sqls.last.should =~ /INSERT INTO items \((ghi)\) VALUES \('\[123\]'\)/
26
DB.sqls.last.should =~ /INSERT INTO items \((ghi)\) VALUES \('\[123\]'\)/
29
29
it "should allow serializing attributes to yaml" do
31
31
@c.create(:abc => 1)
32
32
@c.create(:abc => "hello")
34
MODEL_DB.sqls.map{|s| s.sub("...\n", '')}.should == ["INSERT INTO items (abc) VALUES ('--- 1\n')", "INSERT INTO items (abc) VALUES ('--- hello\n')"]
34
DB.sqls.map{|s| s.sub("...\n", '')}.should == ["INSERT INTO items (abc) VALUES ('--- 1\n')", "INSERT INTO items (abc) VALUES ('--- hello\n')"]
37
37
it "serialized_columns should be the columns serialized" do
45
45
@c.create(:abc => "hello")
46
46
x = [Marshal.dump("hello")].pack('m')
48
MODEL_DB.sqls.should == [ \
49
49
"INSERT INTO items (abc) VALUES ('BAhpBg==\n')", \
50
50
"INSERT INTO items (abc) VALUES ('#{x}')", \
57
57
@c.create(:ghi => ["hello"])
59
59
x = ["hello"].to_json
60
MODEL_DB.sqls.should == [ \
61
61
"INSERT INTO items (ghi) VALUES ('[1]')", \
62
62
"INSERT INTO items (ghi) VALUES ('#{x}')", \
66
66
it "should allow serializing attributes using arbitrary callable" do
67
67
@c.plugin :serialization, [proc{|s| s.reverse}, proc{}], :abc
68
68
@c.create(:abc => "hello")
69
MODEL_DB.sqls.should == ["INSERT INTO items (abc) VALUES ('olleh')"]
69
DB.sqls.should == ["INSERT INTO items (abc) VALUES ('olleh')"]
72
72
it "should raise an error if specificing serializer as an unregistered symbol" do
76
76
it "should translate values to and from yaml serialization format using accessor methods" do
77
77
@c.set_primary_key :id
78
78
@c.plugin :serialization, :yaml, :abc, :def
80
79
@c.dataset._fetch = {:id => 1, :abc => "--- 1\n", :def => "--- hello\n"}
89
88
o.update(:abc => 23)
90
89
@c.create(:abc => [1, 2, 3])
91
MODEL_DB.sqls.should == ["SELECT * FROM items LIMIT 1",
90
DB.sqls.should == ["SELECT * FROM items LIMIT 1",
92
91
"UPDATE items SET abc = '#{23.to_yaml}' WHERE (id = 1)",
93
92
"INSERT INTO items (abc) VALUES ('#{[1, 2, 3].to_yaml}')",
94
93
"SELECT * FROM items WHERE (id = 10) LIMIT 1"]
109
108
o.update(:abc => 23)
110
109
@c.create(:abc => [1, 2, 3])
111
MODEL_DB.sqls.should == ["SELECT * FROM items LIMIT 1",
110
DB.sqls.should == ["SELECT * FROM items LIMIT 1",
112
111
"UPDATE items SET abc = '#{[Marshal.dump(23)].pack('m')}' WHERE (id = 1)",
113
112
"INSERT INTO items (abc) VALUES ('#{[Marshal.dump([1, 2, 3])].pack('m')}')",
114
113
"SELECT * FROM items WHERE (id = 10) LIMIT 1"]
116
it "should handle old non-base-64 encoded marshal serialization format" do
117
@c.set_primary_key :id
118
@c.plugin :serialization, :marshal, :abc, :def
119
@c.dataset._fetch = [:id => 1, :abc =>Marshal.dump(1), :def =>Marshal.dump('hello')]
123
o.def.should == "hello"
126
it "should raise exception for bad marshal data" do
127
@c.set_primary_key :id
128
@c.plugin :serialization, :marshal, :abc, :def
129
@c.dataset._fetch = [:id => 1, :abc =>'foo', :def =>'bar']
132
proc{o.abc}.should raise_error
133
proc{o.def}.should raise_error
117
136
it "should translate values to and from json serialization format using accessor methods" do
118
137
@c.set_primary_key :id
119
138
@c.plugin :serialization, :json, :abc, :def
129
148
o.update(:abc => [23])
130
149
@c.create(:abc => [1,2,3])
132
MODEL_DB.sqls.should == ["SELECT * FROM items LIMIT 1",
151
DB.sqls.should == ["SELECT * FROM items LIMIT 1",
133
152
"UPDATE items SET abc = '#{[23].to_json}' WHERE (id = 1)",
134
153
"INSERT INTO items (abc) VALUES ('#{[1,2,3].to_json}')",
135
154
"SELECT * FROM items WHERE (id = 10) LIMIT 1"]
150
169
o.update(:abc => 'foo')
151
170
@c.create(:abc => 'bar')
153
MODEL_DB.sqls.should == ["SELECT * FROM items LIMIT 1",
172
DB.sqls.should == ["SELECT * FROM items LIMIT 1",
154
173
"UPDATE items SET abc = 'oof' WHERE (id = 1)",
155
174
"INSERT INTO items (abc) VALUES ('rab')",
156
175
"SELECT * FROM items WHERE (id = 10) LIMIT 1"]
172
191
o.update(:abc => 'foo')
173
192
@c.create(:abc => 'bar')
175
MODEL_DB.sqls.should == ["SELECT * FROM items LIMIT 1",
194
DB.sqls.should == ["SELECT * FROM items LIMIT 1",
176
195
"UPDATE items SET abc = 'oof' WHERE (id = 1)",
177
196
"INSERT INTO items (abc) VALUES ('rab')",
178
197
"SELECT * FROM items WHERE (id = 10) LIMIT 1"]
193
212
o.update(:abc => 23)
194
213
Class.new(@c).create(:abc => [1, 2, 3])
195
MODEL_DB.sqls.should == ["SELECT * FROM items LIMIT 1",
214
DB.sqls.should == ["SELECT * FROM items LIMIT 1",
196
215
"UPDATE items SET abc = '#{23.to_yaml}' WHERE (id = 1)",
197
216
"INSERT INTO items (abc) VALUES ('#{[1, 2, 3].to_yaml}')",
198
217
"SELECT * FROM items WHERE (id = 10) LIMIT 1"]
209
228
o.deserialized_values.length.should == 0
212
it "should clear the deserialized columns when refreshing after saving a new object" do
213
@c.set_primary_key :id
214
@c.plugin :serialization, :yaml, :abc, :def
215
o = @c.new(:abc => "--- 1\n", :def => "--- hello\n")
216
o.deserialized_values.length.should == 2
218
o.deserialized_values.length.should == 0
231
it "should not clear the deserialized columns when refreshing after saving a new object" do
232
@c.set_primary_key :id
233
@c.plugin :serialization, :yaml, :abc, :def
234
o = @c.new(:abc => "--- 1\n", :def => "--- hello\n")
235
o.deserialized_values.length.should == 2
237
o.deserialized_values.length.should == 2
240
it "should not clear the deserialized columns when refreshing after saving a new object with insert_select" do
241
@c.set_primary_key :id
242
@c.plugin :serialization, :yaml, :abc, :def
243
def (@c.instance_dataset).supports_insert_select?() true end
244
def (@c.instance_dataset).insert_select(*) {:id=>1} end
245
o = @c.new(:abc => "--- 1\n", :def => "--- hello\n")
246
o.deserialized_values.length.should == 2
248
o.deserialized_values.length.should == 2
221
251
it "should raise an error if calling internal serialization methods with bad columns" do
248
278
o = @c.load(:abc => 3)
249
279
o.abc.should == 9
282
it "should work correctly with frozen instances" do
283
@c.set_primary_key :id
284
@c.plugin :serialization, :yaml, :abc, :def
285
@c.dataset._fetch = {:id => 1, :abc => "--- 1\n", :def => "--- hello\n"}
291
o.def.should == "hello"
292
o.def.should == "hello"
293
proc{o.abc = 2}.should raise_error
294
proc{o.def = 'h'}.should raise_error