colincasey-sequel 2.10.0 → 2.10.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +7 -1
- data/doc/advanced_associations.rdoc +614 -0
- data/doc/cheat_sheet.rdoc +223 -0
- data/doc/dataset_filtering.rdoc +158 -0
- data/doc/prepared_statements.rdoc +104 -0
- data/doc/release_notes/1.0.txt +38 -0
- data/doc/release_notes/1.1.txt +143 -0
- data/doc/release_notes/1.3.txt +101 -0
- data/doc/release_notes/1.4.0.txt +53 -0
- data/doc/release_notes/1.5.0.txt +155 -0
- data/doc/release_notes/2.0.0.txt +298 -0
- data/doc/release_notes/2.1.0.txt +271 -0
- data/doc/release_notes/2.10.0.txt +328 -0
- data/doc/release_notes/2.2.0.txt +253 -0
- data/doc/release_notes/2.3.0.txt +88 -0
- data/doc/release_notes/2.4.0.txt +106 -0
- data/doc/release_notes/2.5.0.txt +137 -0
- data/doc/release_notes/2.6.0.txt +157 -0
- data/doc/release_notes/2.7.0.txt +166 -0
- data/doc/release_notes/2.8.0.txt +171 -0
- data/doc/release_notes/2.9.0.txt +97 -0
- data/doc/schema.rdoc +29 -0
- data/doc/sharding.rdoc +113 -0
- data/lib/sequel.rb +1 -0
- data/lib/sequel_core/adapters/ado.rb +89 -0
- data/lib/sequel_core/adapters/db2.rb +143 -0
- data/lib/sequel_core/adapters/dbi.rb +112 -0
- data/lib/sequel_core/adapters/do/mysql.rb +38 -0
- data/lib/sequel_core/adapters/do/postgres.rb +92 -0
- data/lib/sequel_core/adapters/do/sqlite.rb +31 -0
- data/lib/sequel_core/adapters/do.rb +205 -0
- data/lib/sequel_core/adapters/firebird.rb +298 -0
- data/lib/sequel_core/adapters/informix.rb +85 -0
- data/lib/sequel_core/adapters/jdbc/h2.rb +69 -0
- data/lib/sequel_core/adapters/jdbc/mysql.rb +66 -0
- data/lib/sequel_core/adapters/jdbc/oracle.rb +23 -0
- data/lib/sequel_core/adapters/jdbc/postgresql.rb +113 -0
- data/lib/sequel_core/adapters/jdbc/sqlite.rb +43 -0
- data/lib/sequel_core/adapters/jdbc.rb +491 -0
- data/lib/sequel_core/adapters/mysql.rb +369 -0
- data/lib/sequel_core/adapters/odbc.rb +174 -0
- data/lib/sequel_core/adapters/openbase.rb +68 -0
- data/lib/sequel_core/adapters/oracle.rb +107 -0
- data/lib/sequel_core/adapters/postgres.rb +456 -0
- data/lib/sequel_core/adapters/shared/ms_access.rb +110 -0
- data/lib/sequel_core/adapters/shared/mssql.rb +102 -0
- data/lib/sequel_core/adapters/shared/mysql.rb +325 -0
- data/lib/sequel_core/adapters/shared/oracle.rb +61 -0
- data/lib/sequel_core/adapters/shared/postgres.rb +715 -0
- data/lib/sequel_core/adapters/shared/progress.rb +31 -0
- data/lib/sequel_core/adapters/shared/sqlite.rb +265 -0
- data/lib/sequel_core/adapters/sqlite.rb +248 -0
- data/lib/sequel_core/connection_pool.rb +258 -0
- data/lib/sequel_core/core_ext.rb +217 -0
- data/lib/sequel_core/core_sql.rb +202 -0
- data/lib/sequel_core/database/schema.rb +164 -0
- data/lib/sequel_core/database.rb +691 -0
- data/lib/sequel_core/dataset/callback.rb +13 -0
- data/lib/sequel_core/dataset/convenience.rb +237 -0
- data/lib/sequel_core/dataset/pagination.rb +96 -0
- data/lib/sequel_core/dataset/prepared_statements.rb +220 -0
- data/lib/sequel_core/dataset/query.rb +41 -0
- data/lib/sequel_core/dataset/schema.rb +15 -0
- data/lib/sequel_core/dataset/sql.rb +1010 -0
- data/lib/sequel_core/dataset/stored_procedures.rb +75 -0
- data/lib/sequel_core/dataset/unsupported.rb +43 -0
- data/lib/sequel_core/dataset.rb +511 -0
- data/lib/sequel_core/deprecated.rb +26 -0
- data/lib/sequel_core/exceptions.rb +44 -0
- data/lib/sequel_core/migration.rb +212 -0
- data/lib/sequel_core/object_graph.rb +230 -0
- data/lib/sequel_core/pretty_table.rb +71 -0
- data/lib/sequel_core/schema/generator.rb +320 -0
- data/lib/sequel_core/schema/sql.rb +325 -0
- data/lib/sequel_core/schema.rb +2 -0
- data/lib/sequel_core/sql.rb +887 -0
- data/lib/sequel_core/version.rb +11 -0
- data/lib/sequel_core.rb +172 -0
- data/lib/sequel_model/association_reflection.rb +267 -0
- data/lib/sequel_model/associations.rb +499 -0
- data/lib/sequel_model/base.rb +523 -0
- data/lib/sequel_model/caching.rb +82 -0
- data/lib/sequel_model/dataset_methods.rb +26 -0
- data/lib/sequel_model/eager_loading.rb +370 -0
- data/lib/sequel_model/exceptions.rb +7 -0
- data/lib/sequel_model/hooks.rb +101 -0
- data/lib/sequel_model/inflector.rb +281 -0
- data/lib/sequel_model/plugins.rb +62 -0
- data/lib/sequel_model/record.rb +568 -0
- data/lib/sequel_model/schema.rb +49 -0
- data/lib/sequel_model/validations.rb +429 -0
- data/lib/sequel_model.rb +91 -0
- data/spec/adapters/ado_spec.rb +46 -0
- data/spec/adapters/firebird_spec.rb +376 -0
- data/spec/adapters/informix_spec.rb +96 -0
- data/spec/adapters/mysql_spec.rb +881 -0
- data/spec/adapters/oracle_spec.rb +244 -0
- data/spec/adapters/postgres_spec.rb +687 -0
- data/spec/adapters/spec_helper.rb +10 -0
- data/spec/adapters/sqlite_spec.rb +555 -0
- data/spec/integration/dataset_test.rb +134 -0
- data/spec/integration/eager_loader_test.rb +696 -0
- data/spec/integration/prepared_statement_test.rb +130 -0
- data/spec/integration/schema_test.rb +180 -0
- data/spec/integration/spec_helper.rb +58 -0
- data/spec/integration/type_test.rb +96 -0
- data/spec/rcov.opts +6 -0
- data/spec/sequel_core/connection_pool_spec.rb +526 -0
- data/spec/sequel_core/core_ext_spec.rb +156 -0
- data/spec/sequel_core/core_sql_spec.rb +522 -0
- data/spec/sequel_core/database_spec.rb +1188 -0
- data/spec/sequel_core/dataset_spec.rb +3481 -0
- data/spec/sequel_core/expression_filters_spec.rb +363 -0
- data/spec/sequel_core/migration_spec.rb +261 -0
- data/spec/sequel_core/object_graph_spec.rb +272 -0
- data/spec/sequel_core/pretty_table_spec.rb +58 -0
- data/spec/sequel_core/schema_generator_spec.rb +167 -0
- data/spec/sequel_core/schema_spec.rb +780 -0
- data/spec/sequel_core/spec_helper.rb +55 -0
- data/spec/sequel_core/version_spec.rb +7 -0
- data/spec/sequel_model/association_reflection_spec.rb +93 -0
- data/spec/sequel_model/associations_spec.rb +1767 -0
- data/spec/sequel_model/base_spec.rb +419 -0
- data/spec/sequel_model/caching_spec.rb +215 -0
- data/spec/sequel_model/dataset_methods_spec.rb +78 -0
- data/spec/sequel_model/eager_loading_spec.rb +1165 -0
- data/spec/sequel_model/hooks_spec.rb +485 -0
- data/spec/sequel_model/inflector_spec.rb +119 -0
- data/spec/sequel_model/model_spec.rb +588 -0
- data/spec/sequel_model/plugins_spec.rb +80 -0
- data/spec/sequel_model/record_spec.rb +1184 -0
- data/spec/sequel_model/schema_spec.rb +90 -0
- data/spec/sequel_model/spec_helper.rb +78 -0
- data/spec/sequel_model/validations_spec.rb +1067 -0
- data/spec/spec.opts +0 -0
- data/spec/spec_config.rb.example +10 -0
- metadata +177 -3
@@ -0,0 +1,419 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "spec_helper")
|
2
|
+
|
3
|
+
describe "Model attribute setters" do
|
4
|
+
|
5
|
+
before(:each) do
|
6
|
+
MODEL_DB.reset
|
7
|
+
|
8
|
+
@c = Class.new(Sequel::Model(:items)) do
|
9
|
+
columns :id, :x, :y
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should mark the column value as changed" do
|
14
|
+
o = @c.new
|
15
|
+
o.changed_columns.should == []
|
16
|
+
|
17
|
+
o.x = 2
|
18
|
+
o.changed_columns.should == [:x]
|
19
|
+
|
20
|
+
o.y = 3
|
21
|
+
o.changed_columns.should == [:x, :y]
|
22
|
+
|
23
|
+
o.changed_columns.clear
|
24
|
+
|
25
|
+
o[:x] = 2
|
26
|
+
o.changed_columns.should == [:x]
|
27
|
+
|
28
|
+
o[:y] = 3
|
29
|
+
o.changed_columns.should == [:x, :y]
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "Model#serialize" do
|
35
|
+
|
36
|
+
before(:each) do
|
37
|
+
MODEL_DB.reset
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should translate values to YAML when creating records" do
|
41
|
+
@c = Class.new(Sequel::Model(:items)) do
|
42
|
+
no_primary_key
|
43
|
+
serialize :abc
|
44
|
+
columns :abc
|
45
|
+
end
|
46
|
+
|
47
|
+
@c.create(:abc => 1)
|
48
|
+
@c.create(:abc => "hello")
|
49
|
+
|
50
|
+
MODEL_DB.sqls.should == [ \
|
51
|
+
"INSERT INTO items (abc) VALUES ('--- 1\n')", \
|
52
|
+
"INSERT INTO items (abc) VALUES ('--- hello\n')", \
|
53
|
+
]
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should support calling after the class is defined" do
|
57
|
+
@c = Class.new(Sequel::Model(:items)) do
|
58
|
+
no_primary_key
|
59
|
+
columns :def
|
60
|
+
end
|
61
|
+
|
62
|
+
@c.serialize :def
|
63
|
+
|
64
|
+
@c.create(:def => 1)
|
65
|
+
@c.create(:def => "hello")
|
66
|
+
|
67
|
+
MODEL_DB.sqls.should == [ \
|
68
|
+
"INSERT INTO items (def) VALUES ('--- 1\n')", \
|
69
|
+
"INSERT INTO items (def) VALUES ('--- hello\n')", \
|
70
|
+
]
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should support using the Marshal format" do
|
74
|
+
@c = Class.new(Sequel::Model(:items)) do
|
75
|
+
no_primary_key
|
76
|
+
serialize :abc, :format => :marshal
|
77
|
+
columns :abc
|
78
|
+
end
|
79
|
+
|
80
|
+
@c.create(:abc => 1)
|
81
|
+
@c.create(:abc => "hello")
|
82
|
+
x = [Marshal.dump("hello")].pack('m')
|
83
|
+
|
84
|
+
MODEL_DB.sqls.should == [ \
|
85
|
+
"INSERT INTO items (abc) VALUES ('BAhpBg==\n')", \
|
86
|
+
"INSERT INTO items (abc) VALUES ('#{x}')", \
|
87
|
+
]
|
88
|
+
end
|
89
|
+
|
90
|
+
it "should translate values to and from YAML using accessor methods" do
|
91
|
+
@c = Class.new(Sequel::Model(:items)) do
|
92
|
+
serialize :abc, :def
|
93
|
+
columns :abc, :def
|
94
|
+
end
|
95
|
+
|
96
|
+
ds = @c.dataset
|
97
|
+
ds.extend(Module.new {
|
98
|
+
attr_accessor :raw
|
99
|
+
|
100
|
+
def fetch_rows(sql, &block)
|
101
|
+
block.call(@raw)
|
102
|
+
end
|
103
|
+
|
104
|
+
@@sqls = nil
|
105
|
+
|
106
|
+
def insert(*args)
|
107
|
+
@@sqls = insert_sql(*args)
|
108
|
+
end
|
109
|
+
|
110
|
+
def update(*args)
|
111
|
+
@@sqls = update_sql(*args)
|
112
|
+
end
|
113
|
+
|
114
|
+
def sqls
|
115
|
+
@@sqls
|
116
|
+
end
|
117
|
+
|
118
|
+
def columns
|
119
|
+
[:id, :abc, :def]
|
120
|
+
end
|
121
|
+
}
|
122
|
+
)
|
123
|
+
|
124
|
+
ds.raw = {:id => 1, :abc => "--- 1\n", :def => "--- hello\n"}
|
125
|
+
o = @c.first
|
126
|
+
o.id.should == 1
|
127
|
+
o.abc.should == 1
|
128
|
+
o.def.should == "hello"
|
129
|
+
|
130
|
+
o.update_values(:abc => 23)
|
131
|
+
ds.sqls.should == "UPDATE items SET abc = '#{23.to_yaml}' WHERE (id = 1)"
|
132
|
+
|
133
|
+
ds.raw = {:id => 1, :abc => "--- 1\n", :def => "--- hello\n"}
|
134
|
+
o = @c.create(:abc => [1, 2, 3])
|
135
|
+
ds.sqls.should == "INSERT INTO items (abc) VALUES ('#{[1, 2, 3].to_yaml}')"
|
136
|
+
end
|
137
|
+
|
138
|
+
end
|
139
|
+
|
140
|
+
describe Sequel::Model, "dataset" do
|
141
|
+
before do
|
142
|
+
@a = Class.new(Sequel::Model(:items))
|
143
|
+
@b = Class.new(Sequel::Model)
|
144
|
+
|
145
|
+
class Elephant < Sequel::Model(:ele1)
|
146
|
+
end
|
147
|
+
|
148
|
+
class Maggot < Sequel::Model
|
149
|
+
end
|
150
|
+
|
151
|
+
class ShoeSize < Sequel::Model
|
152
|
+
end
|
153
|
+
|
154
|
+
class BootSize < ShoeSize
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
specify "should default to the plural of the class name" do
|
159
|
+
Maggot.dataset.sql.should == 'SELECT * FROM maggots'
|
160
|
+
ShoeSize.dataset.sql.should == 'SELECT * FROM shoe_sizes'
|
161
|
+
end
|
162
|
+
|
163
|
+
specify "should return the dataset for the superclass if available" do
|
164
|
+
BootSize.dataset.sql.should == 'SELECT * FROM shoe_sizes'
|
165
|
+
end
|
166
|
+
|
167
|
+
specify "should return the correct dataset if set explicitly" do
|
168
|
+
Elephant.dataset.sql.should == 'SELECT * FROM ele1'
|
169
|
+
@a.dataset.sql.should == 'SELECT * FROM items'
|
170
|
+
end
|
171
|
+
|
172
|
+
specify "should raise if no dataset is explicitly set and the class is anonymous" do
|
173
|
+
proc {@b.dataset}.should raise_error(Sequel::Error)
|
174
|
+
end
|
175
|
+
|
176
|
+
specify "should disregard namespaces for the table name" do
|
177
|
+
module BlahBlah
|
178
|
+
class MwaHaHa < Sequel::Model
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
BlahBlah::MwaHaHa.dataset.sql.should == 'SELECT * FROM mwa_ha_has'
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
describe Sequel::Model, ".def_dataset_method" do
|
187
|
+
setup do
|
188
|
+
@c = Class.new(Sequel::Model(:items)) do
|
189
|
+
@dataset = Object.new
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
it "should add a method to the dataset and model if called with a block argument" do
|
194
|
+
@c.instance_eval do
|
195
|
+
def_dataset_method(:return_3){3}
|
196
|
+
end
|
197
|
+
@c.return_3.should == 3
|
198
|
+
@c.dataset.return_3.should == 3
|
199
|
+
end
|
200
|
+
|
201
|
+
it "should add all passed methods to the model if called without a block argument" do
|
202
|
+
@c.instance_eval do
|
203
|
+
def_dataset_method(:return_3, :return_4)
|
204
|
+
end
|
205
|
+
proc{@c.return_3}.should raise_error(NoMethodError)
|
206
|
+
proc{@c.return_4}.should raise_error(NoMethodError)
|
207
|
+
@c.dataset.instance_eval do
|
208
|
+
def return_3; 3; end
|
209
|
+
def return_4; 4; end
|
210
|
+
end
|
211
|
+
@c.return_3.should == 3
|
212
|
+
@c.return_4.should == 4
|
213
|
+
end
|
214
|
+
|
215
|
+
it "should cache calls and readd methods if set_dataset is used" do
|
216
|
+
@c.instance_eval do
|
217
|
+
def_dataset_method(:return_3){3}
|
218
|
+
end
|
219
|
+
@c.set_dataset :items
|
220
|
+
@c.return_3.should == 3
|
221
|
+
@c.dataset.return_3.should == 3
|
222
|
+
end
|
223
|
+
|
224
|
+
it "should readd methods to subclasses, if set_dataset is used in a subclass" do
|
225
|
+
@c.instance_eval do
|
226
|
+
def_dataset_method(:return_3){3}
|
227
|
+
end
|
228
|
+
c = Class.new(@c)
|
229
|
+
c.set_dataset :items
|
230
|
+
c.return_3.should == 3
|
231
|
+
c.dataset.return_3.should == 3
|
232
|
+
end
|
233
|
+
end
|
234
|
+
|
235
|
+
describe "A model class with implicit table name" do
|
236
|
+
setup do
|
237
|
+
class Donkey < Sequel::Model
|
238
|
+
end
|
239
|
+
end
|
240
|
+
|
241
|
+
specify "should have a dataset associated with the model class" do
|
242
|
+
Donkey.dataset.model_classes.should == {nil => Donkey}
|
243
|
+
end
|
244
|
+
end
|
245
|
+
|
246
|
+
describe "A model inheriting from a model" do
|
247
|
+
setup do
|
248
|
+
class Feline < Sequel::Model
|
249
|
+
end
|
250
|
+
|
251
|
+
class Leopard < Feline
|
252
|
+
end
|
253
|
+
end
|
254
|
+
|
255
|
+
specify "should have a dataset associated with itself" do
|
256
|
+
Feline.dataset.model_classes.should == {nil => Feline}
|
257
|
+
Leopard.dataset.model_classes.should == {nil => Leopard}
|
258
|
+
end
|
259
|
+
end
|
260
|
+
|
261
|
+
describe "Model.db=" do
|
262
|
+
setup do
|
263
|
+
$db1 = MockDatabase.new
|
264
|
+
$db2 = MockDatabase.new
|
265
|
+
|
266
|
+
class BlueBlue < Sequel::Model(:items)
|
267
|
+
set_dataset $db1[:blue]
|
268
|
+
end
|
269
|
+
end
|
270
|
+
|
271
|
+
specify "should affect the underlying dataset" do
|
272
|
+
BlueBlue.db = $db2
|
273
|
+
|
274
|
+
BlueBlue.dataset.db.should === $db2
|
275
|
+
BlueBlue.dataset.db.should_not === $db1
|
276
|
+
end
|
277
|
+
end
|
278
|
+
|
279
|
+
describe Sequel::Model, ".(allowed|restricted)_columns " do
|
280
|
+
setup do
|
281
|
+
@c = Class.new(Sequel::Model(:blahblah)) do
|
282
|
+
columns :x, :y, :z
|
283
|
+
def refresh
|
284
|
+
self
|
285
|
+
end
|
286
|
+
end
|
287
|
+
@c.strict_param_setting = false
|
288
|
+
@c.instance_variable_set(:@columns, [:x, :y, :z])
|
289
|
+
end
|
290
|
+
|
291
|
+
it "should set the allowed columns correctly" do
|
292
|
+
@c.allowed_columns.should == nil
|
293
|
+
@c.set_allowed_columns :x
|
294
|
+
@c.allowed_columns.should == [:x]
|
295
|
+
@c.set_allowed_columns :x, :y
|
296
|
+
@c.allowed_columns.should == [:x, :y]
|
297
|
+
end
|
298
|
+
|
299
|
+
it "should set the restricted columns correctly" do
|
300
|
+
@c.restricted_columns.should == nil
|
301
|
+
@c.set_restricted_columns :x
|
302
|
+
@c.restricted_columns.should == [:x]
|
303
|
+
@c.set_restricted_columns :x, :y
|
304
|
+
@c.restricted_columns.should == [:x, :y]
|
305
|
+
end
|
306
|
+
|
307
|
+
it "should only set allowed columns by default" do
|
308
|
+
@c.set_allowed_columns :x, :y
|
309
|
+
i = @c.new(:x => 1, :y => 2, :z => 3)
|
310
|
+
i.values.should == {:x => 1, :y => 2}
|
311
|
+
i.set(:x => 4, :y => 5, :z => 6)
|
312
|
+
i.values.should == {:x => 4, :y => 5}
|
313
|
+
i.update(:x => 7, :y => 8, :z => 9)
|
314
|
+
i.values.delete(:id) # stupid specs
|
315
|
+
i.values.should == {:x => 7, :y => 8}
|
316
|
+
end
|
317
|
+
|
318
|
+
it "should not set restricted columns by default" do
|
319
|
+
@c.set_restricted_columns :z
|
320
|
+
i = @c.new(:x => 1, :y => 2, :z => 3)
|
321
|
+
i.values.should == {:x => 1, :y => 2}
|
322
|
+
i.set(:x => 4, :y => 5, :z => 6)
|
323
|
+
i.values.should == {:x => 4, :y => 5}
|
324
|
+
i.update(:x => 7, :y => 8, :z => 9)
|
325
|
+
i.values.delete(:id) # stupid specs
|
326
|
+
i.values.should == {:x => 7, :y => 8}
|
327
|
+
end
|
328
|
+
|
329
|
+
it "should have allowed take precedence over restricted" do
|
330
|
+
@c.set_allowed_columns :x, :y
|
331
|
+
@c.set_restricted_columns :y, :z
|
332
|
+
i = @c.new(:x => 1, :y => 2, :z => 3)
|
333
|
+
i.values.should == {:x => 1, :y => 2}
|
334
|
+
i.set(:x => 4, :y => 5, :z => 6)
|
335
|
+
i.values.should == {:x => 4, :y => 5}
|
336
|
+
i.update(:x => 7, :y => 8, :z => 9)
|
337
|
+
i.values.delete(:id) # stupid specs
|
338
|
+
i.values.should == {:x => 7, :y => 8}
|
339
|
+
end
|
340
|
+
end
|
341
|
+
|
342
|
+
describe Sequel::Model, ".(un)?restrict_primary_key\\??" do
|
343
|
+
setup do
|
344
|
+
@c = Class.new(Sequel::Model(:blahblah)) do
|
345
|
+
set_primary_key :id
|
346
|
+
columns :x, :y, :z, :id
|
347
|
+
def refresh
|
348
|
+
self
|
349
|
+
end
|
350
|
+
end
|
351
|
+
@c.strict_param_setting = false
|
352
|
+
@c.instance_variable_set(:@columns, [:x, :y, :z])
|
353
|
+
end
|
354
|
+
|
355
|
+
it "should restrict updates to primary key by default" do
|
356
|
+
i = @c.new(:x => 1, :y => 2, :id => 3)
|
357
|
+
i.values.should == {:x => 1, :y => 2}
|
358
|
+
i.set(:x => 4, :y => 5, :id => 6)
|
359
|
+
i.values.should == {:x => 4, :y => 5}
|
360
|
+
end
|
361
|
+
|
362
|
+
it "should allow updates to primary key if unrestrict_primary_key is used" do
|
363
|
+
@c.unrestrict_primary_key
|
364
|
+
i = @c.new(:x => 1, :y => 2, :id => 3)
|
365
|
+
i.values.should == {:x => 1, :y => 2, :id=>3}
|
366
|
+
i.set(:x => 4, :y => 5, :id => 6)
|
367
|
+
i.values.should == {:x => 4, :y => 5, :id=>6}
|
368
|
+
end
|
369
|
+
|
370
|
+
it "should have restrict_primary_key? return true or false depending" do
|
371
|
+
@c.restrict_primary_key?.should == true
|
372
|
+
@c.unrestrict_primary_key
|
373
|
+
@c.restrict_primary_key?.should == false
|
374
|
+
c1 = Class.new(@c)
|
375
|
+
c1.restrict_primary_key?.should == false
|
376
|
+
@c.restrict_primary_key
|
377
|
+
@c.restrict_primary_key?.should == true
|
378
|
+
c1.restrict_primary_key?.should == false
|
379
|
+
c2 = Class.new(@c)
|
380
|
+
c2.restrict_primary_key?.should == true
|
381
|
+
end
|
382
|
+
end
|
383
|
+
|
384
|
+
describe Sequel::Model, ".strict_param_setting" do
|
385
|
+
setup do
|
386
|
+
@c = Class.new(Sequel::Model(:blahblah)) do
|
387
|
+
columns :x, :y, :z, :id
|
388
|
+
set_restricted_columns :z
|
389
|
+
def refresh
|
390
|
+
self
|
391
|
+
end
|
392
|
+
end
|
393
|
+
@c.instance_variable_set(:@columns, [:x, :y, :z])
|
394
|
+
end
|
395
|
+
|
396
|
+
it "should be enabled by default" do
|
397
|
+
@c.strict_param_setting.should == true
|
398
|
+
end
|
399
|
+
|
400
|
+
it "should raise an error if a missing/restricted column/method is accessed" do
|
401
|
+
proc{@c.new(:z=>1)}.should raise_error(Sequel::Error)
|
402
|
+
proc{@c.create(:z=>1)}.should raise_error(Sequel::Error)
|
403
|
+
c = @c.new
|
404
|
+
proc{c.set(:z=>1)}.should raise_error(Sequel::Error)
|
405
|
+
proc{c.set_all(:id=>1)}.should raise_error(Sequel::Error)
|
406
|
+
proc{c.set_only({:x=>1}, :y)}.should raise_error(Sequel::Error)
|
407
|
+
proc{c.set_except({:x=>1}, :x)}.should raise_error(Sequel::Error)
|
408
|
+
proc{c.update(:z=>1)}.should raise_error(Sequel::Error)
|
409
|
+
proc{c.update_all(:id=>1)}.should raise_error(Sequel::Error)
|
410
|
+
proc{c.update_only({:x=>1}, :y)}.should raise_error(Sequel::Error)
|
411
|
+
proc{c.update_except({:x=>1}, :x)}.should raise_error(Sequel::Error)
|
412
|
+
end
|
413
|
+
|
414
|
+
it "should be disabled by strict_param_setting = false" do
|
415
|
+
@c.strict_param_setting = false
|
416
|
+
@c.strict_param_setting.should == false
|
417
|
+
proc{@c.new(:z=>1)}.should_not raise_error
|
418
|
+
end
|
419
|
+
end
|
@@ -0,0 +1,215 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "spec_helper")
|
2
|
+
|
3
|
+
describe Sequel::Model, "caching" do
|
4
|
+
before do
|
5
|
+
MODEL_DB.reset
|
6
|
+
|
7
|
+
@cache_class = Class.new(Hash) do
|
8
|
+
attr_accessor :ttl
|
9
|
+
def set(k, v, ttl); self[k] = v; @ttl = ttl; end
|
10
|
+
def get(k); self[k]; end
|
11
|
+
end
|
12
|
+
cache = @cache_class.new
|
13
|
+
@cache = cache
|
14
|
+
|
15
|
+
@c = Class.new(Sequel::Model(:items))
|
16
|
+
@c.class_eval do
|
17
|
+
set_cache cache
|
18
|
+
def self.name; 'Item' end
|
19
|
+
|
20
|
+
columns :name, :id
|
21
|
+
end
|
22
|
+
|
23
|
+
$cache_dataset_row = {:name => 'sharon', :id => 1}
|
24
|
+
@dataset = @c.dataset
|
25
|
+
$sqls = []
|
26
|
+
@dataset.extend(Module.new {
|
27
|
+
def fetch_rows(sql)
|
28
|
+
$sqls << sql
|
29
|
+
yield $cache_dataset_row
|
30
|
+
end
|
31
|
+
|
32
|
+
def update(values)
|
33
|
+
$sqls << update_sql(values)
|
34
|
+
$cache_dataset_row.merge!(values)
|
35
|
+
end
|
36
|
+
|
37
|
+
def delete
|
38
|
+
$sqls << delete_sql
|
39
|
+
end
|
40
|
+
})
|
41
|
+
@c2 = Class.new(@c) do
|
42
|
+
def self.name; 'SubItem' end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should set the model's cache store" do
|
47
|
+
@c.cache_store.should be(@cache)
|
48
|
+
@c2.cache_store.should be(@cache)
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should have a default ttl of 3600" do
|
52
|
+
@c.cache_ttl.should == 3600
|
53
|
+
@c2.cache_ttl.should == 3600
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should take a ttl option" do
|
57
|
+
@c.set_cache @cache, :ttl => 1234
|
58
|
+
@c.cache_ttl.should == 1234
|
59
|
+
Class.new(@c).cache_ttl.should == 1234
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should offer a set_cache_ttl method for setting the ttl" do
|
63
|
+
@c.cache_ttl.should == 3600
|
64
|
+
@c.set_cache_ttl 1234
|
65
|
+
@c.cache_ttl.should == 1234
|
66
|
+
Class.new(@c).cache_ttl.should == 1234
|
67
|
+
end
|
68
|
+
|
69
|
+
it "should generate a cache key appropriate to the class" do
|
70
|
+
m = @c.new
|
71
|
+
m.values[:id] = 1
|
72
|
+
m.cache_key.should == "#{m.class}:1"
|
73
|
+
m = @c2.new
|
74
|
+
m.values[:id] = 1
|
75
|
+
m.cache_key.should == "#{m.class}:1"
|
76
|
+
|
77
|
+
# custom primary key
|
78
|
+
@c.set_primary_key :ttt
|
79
|
+
m = @c.new
|
80
|
+
m.values[:ttt] = 333
|
81
|
+
m.cache_key.should == "#{m.class}:333"
|
82
|
+
c = Class.new(@c)
|
83
|
+
m = c.new
|
84
|
+
m.values[:ttt] = 333
|
85
|
+
m.cache_key.should == "#{m.class}:333"
|
86
|
+
|
87
|
+
# composite primary key
|
88
|
+
@c.set_primary_key [:a, :b, :c]
|
89
|
+
m = @c.new
|
90
|
+
m.values[:a] = 123
|
91
|
+
m.values[:c] = 456
|
92
|
+
m.values[:b] = 789
|
93
|
+
m.cache_key.should == "#{m.class}:123,789,456"
|
94
|
+
c = Class.new(@c)
|
95
|
+
m = c.new
|
96
|
+
m.values[:a] = 123
|
97
|
+
m.values[:c] = 456
|
98
|
+
m.values[:b] = 789
|
99
|
+
m.cache_key.should == "#{m.class}:123,789,456"
|
100
|
+
end
|
101
|
+
|
102
|
+
it "should raise error if attempting to generate cache_key and primary key value is null" do
|
103
|
+
m = @c.new
|
104
|
+
proc {m.cache_key}.should raise_error(Sequel::Error)
|
105
|
+
m.values[:id] = 1
|
106
|
+
proc {m.cache_key}.should_not raise_error(Sequel::Error)
|
107
|
+
|
108
|
+
m = @c2.new
|
109
|
+
proc {m.cache_key}.should raise_error(Sequel::Error)
|
110
|
+
m.values[:id] = 1
|
111
|
+
proc {m.cache_key}.should_not raise_error(Sequel::Error)
|
112
|
+
end
|
113
|
+
|
114
|
+
it "should not raise error if trying to save a new record" do
|
115
|
+
proc {@c.new(:name=>'blah').save}.should_not raise_error
|
116
|
+
proc {@c.create(:name=>'blah')}.should_not raise_error
|
117
|
+
proc {@c2.new(:name=>'blah').save}.should_not raise_error
|
118
|
+
proc {@c2.create(:name=>'blah')}.should_not raise_error
|
119
|
+
end
|
120
|
+
|
121
|
+
it "should set the cache when reading from the database" do
|
122
|
+
$sqls.should == []
|
123
|
+
@cache.should be_empty
|
124
|
+
|
125
|
+
m = @c[1]
|
126
|
+
$sqls.should == ['SELECT * FROM items WHERE (id = 1) LIMIT 1']
|
127
|
+
m.values.should == $cache_dataset_row
|
128
|
+
@cache[m.cache_key].should == m
|
129
|
+
m2 = @c[1]
|
130
|
+
$sqls.should == ['SELECT * FROM items WHERE (id = 1) LIMIT 1']
|
131
|
+
m2.should == m
|
132
|
+
m2.values.should == $cache_dataset_row
|
133
|
+
|
134
|
+
$sqls.clear
|
135
|
+
m = @c2[1]
|
136
|
+
$sqls.should == ['SELECT * FROM items WHERE (id = 1) LIMIT 1']
|
137
|
+
m.values.should == $cache_dataset_row
|
138
|
+
@cache[m.cache_key].should == m
|
139
|
+
m2 = @c2[1]
|
140
|
+
$sqls.should == ['SELECT * FROM items WHERE (id = 1) LIMIT 1']
|
141
|
+
m2.should == m
|
142
|
+
m2.values.should == $cache_dataset_row
|
143
|
+
end
|
144
|
+
|
145
|
+
it "should delete the cache when writing to the database" do
|
146
|
+
m = @c[1]
|
147
|
+
@cache[m.cache_key].should == m
|
148
|
+
|
149
|
+
m.update_values(:name => 'tutu')
|
150
|
+
@cache.has_key?(m.cache_key).should be_false
|
151
|
+
$sqls.last.should == "UPDATE items SET name = 'tutu' WHERE (id = 1)"
|
152
|
+
|
153
|
+
m = @c[1]
|
154
|
+
@cache[m.cache_key].should == m
|
155
|
+
m.name = 'hey'
|
156
|
+
m.save
|
157
|
+
@cache.has_key?(m.cache_key).should be_false
|
158
|
+
$sqls.last.should == "UPDATE items SET name = 'hey', id = 1 WHERE (id = 1)"
|
159
|
+
|
160
|
+
m = @c2[1]
|
161
|
+
@cache[m.cache_key].should == m
|
162
|
+
|
163
|
+
m.update_values(:name => 'tutu')
|
164
|
+
@cache.has_key?(m.cache_key).should be_false
|
165
|
+
$sqls.last.should == "UPDATE items SET name = 'tutu' WHERE (id = 1)"
|
166
|
+
|
167
|
+
m = @c2[1]
|
168
|
+
@cache[m.cache_key].should == m
|
169
|
+
m.name = 'hey'
|
170
|
+
m.save
|
171
|
+
@cache.has_key?(m.cache_key).should be_false
|
172
|
+
$sqls.last.should == "UPDATE items SET name = 'hey', id = 1 WHERE (id = 1)"
|
173
|
+
end
|
174
|
+
|
175
|
+
it "should delete the cache when deleting the record" do
|
176
|
+
m = @c[1]
|
177
|
+
@cache[m.cache_key].should == m
|
178
|
+
m.delete
|
179
|
+
@cache.has_key?(m.cache_key).should be_false
|
180
|
+
$sqls.last.should == "DELETE FROM items WHERE (id = 1)"
|
181
|
+
|
182
|
+
m = @c2[1]
|
183
|
+
@cache[m.cache_key].should == m
|
184
|
+
m.delete
|
185
|
+
@cache.has_key?(m.cache_key).should be_false
|
186
|
+
$sqls.last.should == "DELETE FROM items WHERE (id = 1)"
|
187
|
+
end
|
188
|
+
|
189
|
+
it "should support #[] as a shortcut to #find with hash" do
|
190
|
+
m = @c[:id => 3]
|
191
|
+
@cache[m.cache_key].should be_nil
|
192
|
+
$sqls.last.should == "SELECT * FROM items WHERE (id = 3) LIMIT 1"
|
193
|
+
m = @c[1]
|
194
|
+
@cache[m.cache_key].should == m
|
195
|
+
$sqls.should == ["SELECT * FROM items WHERE (id = 3) LIMIT 1", \
|
196
|
+
"SELECT * FROM items WHERE (id = 1) LIMIT 1"]
|
197
|
+
@c[:id => 4]
|
198
|
+
$sqls.should == ["SELECT * FROM items WHERE (id = 3) LIMIT 1", \
|
199
|
+
"SELECT * FROM items WHERE (id = 1) LIMIT 1", \
|
200
|
+
"SELECT * FROM items WHERE (id = 4) LIMIT 1"]
|
201
|
+
|
202
|
+
$sqls.clear
|
203
|
+
m = @c2[:id => 3]
|
204
|
+
@cache[m.cache_key].should be_nil
|
205
|
+
$sqls.last.should == "SELECT * FROM items WHERE (id = 3) LIMIT 1"
|
206
|
+
m = @c2[1]
|
207
|
+
@cache[m.cache_key].should == m
|
208
|
+
$sqls.should == ["SELECT * FROM items WHERE (id = 3) LIMIT 1", \
|
209
|
+
"SELECT * FROM items WHERE (id = 1) LIMIT 1"]
|
210
|
+
@c2[:id => 4]
|
211
|
+
$sqls.should == ["SELECT * FROM items WHERE (id = 3) LIMIT 1", \
|
212
|
+
"SELECT * FROM items WHERE (id = 1) LIMIT 1", \
|
213
|
+
"SELECT * FROM items WHERE (id = 4) LIMIT 1"]
|
214
|
+
end
|
215
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "spec_helper")
|
2
|
+
|
3
|
+
describe Sequel::Model::DatasetMethods, "#destroy" do
|
4
|
+
before do
|
5
|
+
@c = Class.new(Sequel::Model(:items)) do
|
6
|
+
@@destroyed = []
|
7
|
+
def destroy
|
8
|
+
@@destroyed << self
|
9
|
+
end
|
10
|
+
def self.destroyed
|
11
|
+
@@destroyed
|
12
|
+
end
|
13
|
+
end
|
14
|
+
@d = @c.dataset
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should instantiate objects in the dataset and call destroy on each" do
|
18
|
+
def @d.fetch_rows(sql)
|
19
|
+
yield({:id=>1})
|
20
|
+
yield({:id=>2})
|
21
|
+
end
|
22
|
+
@d.destroy
|
23
|
+
@c.destroyed.collect{|x| x.values}.should == [{:id=>1}, {:id=>2}]
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should return the number of records destroyed" do
|
27
|
+
def @d.fetch_rows(sql)
|
28
|
+
yield({:id=>1})
|
29
|
+
yield({:id=>2})
|
30
|
+
end
|
31
|
+
@d.destroy.should == 2
|
32
|
+
def @d.fetch_rows(sql)
|
33
|
+
yield({:id=>1})
|
34
|
+
end
|
35
|
+
@d.destroy.should == 1
|
36
|
+
def @d.fetch_rows(sql)
|
37
|
+
end
|
38
|
+
@d.destroy.should == 0
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe Sequel::Model::DatasetMethods, "#to_hash" do
|
43
|
+
before do
|
44
|
+
@c = Class.new(Sequel::Model(:items)) do
|
45
|
+
set_primary_key :name
|
46
|
+
end
|
47
|
+
@d = @c.dataset
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should result in a hash with primary key value keys and model object values" do
|
51
|
+
def @d.fetch_rows(sql)
|
52
|
+
yield({:name=>1})
|
53
|
+
yield({:name=>2})
|
54
|
+
end
|
55
|
+
h = @d.to_hash
|
56
|
+
h.should be_a_kind_of(Hash)
|
57
|
+
a = h.to_a
|
58
|
+
a.collect{|x| x[1].class}.should == [@c, @c]
|
59
|
+
a.sort_by{|x| x[0]}.collect{|x| [x[0], x[1].values]}.should == [[1, {:name=>1}], [2, {:name=>2}]]
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should result in a hash with given value keys and model object values" do
|
63
|
+
def @d.fetch_rows(sql)
|
64
|
+
yield({:name=>1, :number=>3})
|
65
|
+
yield({:name=>2, :number=>4})
|
66
|
+
end
|
67
|
+
h = @d.to_hash(:number)
|
68
|
+
h.should be_a_kind_of(Hash)
|
69
|
+
a = h.to_a
|
70
|
+
a.collect{|x| x[1].class}.should == [@c, @c]
|
71
|
+
a.sort_by{|x| x[0]}.collect{|x| [x[0], x[1].values]}.should == [[3, {:name=>1, :number=>3}], [4, {:name=>2, :number=>4}]]
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should raise an error if the class doesn't have a primary key" do
|
75
|
+
@c.no_primary_key
|
76
|
+
proc{@d.to_hash}.should raise_error(Sequel::Error)
|
77
|
+
end
|
78
|
+
end
|