~ubuntu-branches/ubuntu/vivid/ruby-sequel/vivid

« back to all changes in this revision

Viewing changes to spec/extensions/serialization_spec.rb

  • Committer: Package Import Robot
  • Author(s): Dmitry Borodaenko, Dmitry Borodaenko, Cédric Boutillier
  • Date: 2013-08-10 18:38:17 UTC
  • mfrom: (1.1.8)
  • Revision ID: package-import@ubuntu.com-20130810183817-iqanz804j32i5myi
Tags: 4.1.1-1
[ Dmitry Borodaenko ]
* New upstream release.
* Standards-Version upgraded to 3.9.4 (no changes).
* Added Build-Depend on ruby-sqlite3.

[ Cédric Boutillier ]
* debian/control: remove obsolete DM-Upload-Allowed flag.
* use canonical URI in Vcs-* fields.
* debian/copyright: use DEP5 copyright-format/1.0 official URL for Format
  field.
* Update debian/watch. Thanks Bart Martens.

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
      no_primary_key
10
10
      columns :id, :abc, :def, :ghi
11
11
    end
12
 
    MODEL_DB.reset
 
12
    DB.reset
13
13
  end
14
14
  
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)?')\)/
19
19
 
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)?')\)/
23
23
    
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\]'\)/
27
27
  end
28
28
 
29
29
  it "should allow serializing attributes to yaml" do
31
31
    @c.create(:abc => 1)
32
32
    @c.create(:abc => "hello")
33
33
 
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')"]
35
35
  end
36
36
 
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')
47
47
 
48
 
    MODEL_DB.sqls.should == [ \
 
48
    DB.sqls.should == [ \
49
49
      "INSERT INTO items (abc) VALUES ('BAhpBg==\n')", \
50
50
      "INSERT INTO items (abc) VALUES ('#{x}')", \
51
51
    ]
57
57
    @c.create(:ghi => ["hello"])
58
58
    
59
59
    x = ["hello"].to_json
60
 
    MODEL_DB.sqls.should == [ \
 
60
    DB.sqls.should == [ \
61
61
      "INSERT INTO items (ghi) VALUES ('[1]')", \
62
62
      "INSERT INTO items (ghi) VALUES ('#{x}')", \
63
63
    ]
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')"]
70
70
  end
71
71
  
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
79
 
    vals = nil
80
79
    @c.dataset._fetch = {:id => 1, :abc => "--- 1\n", :def => "--- hello\n"}
81
80
 
82
81
    o = @c.first
88
87
 
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"]
108
107
 
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"]
115
114
  end
116
115
  
 
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')]
 
120
 
 
121
    o = @c.first
 
122
    o.abc.should == 1
 
123
    o.def.should == "hello"
 
124
  end
 
125
 
 
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']
 
130
 
 
131
    o = @c.first
 
132
    proc{o.abc}.should raise_error
 
133
    proc{o.def}.should raise_error
 
134
  end
 
135
  
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])
131
150
    
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')
152
171
    
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')
174
193
    
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"]
192
211
 
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
210
229
  end
211
230
  
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
217
 
    o.save
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
 
236
    o.save
 
237
    o.deserialized_values.length.should == 2
 
238
  end
 
239
  
 
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
 
247
    o.save
 
248
    o.deserialized_values.length.should == 2
219
249
  end
220
250
  
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
250
280
  end
 
281
 
 
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"}
 
286
 
 
287
    o = @c.first
 
288
    o.freeze
 
289
    o.abc.should == 1
 
290
    o.abc.should == 1
 
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
 
295
  end
251
296
end