epugh-sequel 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +652 -0
- data/VERSION.yml +4 -0
- data/bin/sequel +104 -0
- data/lib/sequel.rb +1 -0
- data/lib/sequel/adapters/ado.rb +85 -0
- data/lib/sequel/adapters/db2.rb +132 -0
- data/lib/sequel/adapters/dbi.rb +101 -0
- data/lib/sequel/adapters/do.rb +197 -0
- data/lib/sequel/adapters/do/mysql.rb +38 -0
- data/lib/sequel/adapters/do/postgres.rb +92 -0
- data/lib/sequel/adapters/do/sqlite.rb +31 -0
- data/lib/sequel/adapters/firebird.rb +307 -0
- data/lib/sequel/adapters/informix.rb +75 -0
- data/lib/sequel/adapters/jdbc.rb +485 -0
- data/lib/sequel/adapters/jdbc/h2.rb +62 -0
- data/lib/sequel/adapters/jdbc/mysql.rb +56 -0
- data/lib/sequel/adapters/jdbc/oracle.rb +23 -0
- data/lib/sequel/adapters/jdbc/postgresql.rb +101 -0
- data/lib/sequel/adapters/jdbc/sqlite.rb +43 -0
- data/lib/sequel/adapters/mysql.rb +370 -0
- data/lib/sequel/adapters/odbc.rb +184 -0
- data/lib/sequel/adapters/openbase.rb +57 -0
- data/lib/sequel/adapters/oracle.rb +140 -0
- data/lib/sequel/adapters/postgres.rb +453 -0
- data/lib/sequel/adapters/shared/mssql.rb +93 -0
- data/lib/sequel/adapters/shared/mysql.rb +341 -0
- data/lib/sequel/adapters/shared/oracle.rb +62 -0
- data/lib/sequel/adapters/shared/postgres.rb +743 -0
- data/lib/sequel/adapters/shared/progress.rb +34 -0
- data/lib/sequel/adapters/shared/sqlite.rb +263 -0
- data/lib/sequel/adapters/sqlite.rb +243 -0
- data/lib/sequel/adapters/utils/date_format.rb +21 -0
- data/lib/sequel/adapters/utils/stored_procedures.rb +75 -0
- data/lib/sequel/adapters/utils/unsupported.rb +62 -0
- data/lib/sequel/connection_pool.rb +258 -0
- data/lib/sequel/core.rb +204 -0
- data/lib/sequel/core_sql.rb +185 -0
- data/lib/sequel/database.rb +687 -0
- data/lib/sequel/database/schema_generator.rb +324 -0
- data/lib/sequel/database/schema_methods.rb +164 -0
- data/lib/sequel/database/schema_sql.rb +324 -0
- data/lib/sequel/dataset.rb +422 -0
- data/lib/sequel/dataset/convenience.rb +237 -0
- data/lib/sequel/dataset/prepared_statements.rb +220 -0
- data/lib/sequel/dataset/sql.rb +1105 -0
- data/lib/sequel/deprecated.rb +529 -0
- data/lib/sequel/exceptions.rb +44 -0
- data/lib/sequel/extensions/blank.rb +42 -0
- data/lib/sequel/extensions/inflector.rb +288 -0
- data/lib/sequel/extensions/pagination.rb +96 -0
- data/lib/sequel/extensions/pretty_table.rb +78 -0
- data/lib/sequel/extensions/query.rb +48 -0
- data/lib/sequel/extensions/string_date_time.rb +47 -0
- data/lib/sequel/metaprogramming.rb +44 -0
- data/lib/sequel/migration.rb +212 -0
- data/lib/sequel/model.rb +142 -0
- data/lib/sequel/model/association_reflection.rb +263 -0
- data/lib/sequel/model/associations.rb +1024 -0
- data/lib/sequel/model/base.rb +911 -0
- data/lib/sequel/model/deprecated.rb +188 -0
- data/lib/sequel/model/deprecated_hooks.rb +103 -0
- data/lib/sequel/model/deprecated_inflector.rb +335 -0
- data/lib/sequel/model/deprecated_validations.rb +384 -0
- data/lib/sequel/model/errors.rb +37 -0
- data/lib/sequel/model/exceptions.rb +7 -0
- data/lib/sequel/model/inflections.rb +230 -0
- data/lib/sequel/model/plugins.rb +74 -0
- data/lib/sequel/object_graph.rb +230 -0
- data/lib/sequel/plugins/caching.rb +122 -0
- data/lib/sequel/plugins/hook_class_methods.rb +122 -0
- data/lib/sequel/plugins/schema.rb +53 -0
- data/lib/sequel/plugins/single_table_inheritance.rb +63 -0
- data/lib/sequel/plugins/validation_class_methods.rb +373 -0
- data/lib/sequel/sql.rb +854 -0
- data/lib/sequel/version.rb +11 -0
- data/lib/sequel_core.rb +1 -0
- data/lib/sequel_model.rb +1 -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 +875 -0
- data/spec/adapters/oracle_spec.rb +272 -0
- data/spec/adapters/postgres_spec.rb +692 -0
- data/spec/adapters/spec_helper.rb +10 -0
- data/spec/adapters/sqlite_spec.rb +550 -0
- data/spec/core/connection_pool_spec.rb +526 -0
- data/spec/core/core_ext_spec.rb +156 -0
- data/spec/core/core_sql_spec.rb +528 -0
- data/spec/core/database_spec.rb +1214 -0
- data/spec/core/dataset_spec.rb +3513 -0
- data/spec/core/expression_filters_spec.rb +363 -0
- data/spec/core/migration_spec.rb +261 -0
- data/spec/core/object_graph_spec.rb +280 -0
- data/spec/core/pretty_table_spec.rb +58 -0
- data/spec/core/schema_generator_spec.rb +167 -0
- data/spec/core/schema_spec.rb +778 -0
- data/spec/core/spec_helper.rb +82 -0
- data/spec/core/version_spec.rb +7 -0
- data/spec/extensions/blank_spec.rb +67 -0
- data/spec/extensions/caching_spec.rb +201 -0
- data/spec/extensions/hook_class_methods_spec.rb +470 -0
- data/spec/extensions/inflector_spec.rb +122 -0
- data/spec/extensions/pagination_spec.rb +99 -0
- data/spec/extensions/pretty_table_spec.rb +91 -0
- data/spec/extensions/query_spec.rb +85 -0
- data/spec/extensions/schema_spec.rb +111 -0
- data/spec/extensions/single_table_inheritance_spec.rb +53 -0
- data/spec/extensions/spec_helper.rb +90 -0
- data/spec/extensions/string_date_time_spec.rb +93 -0
- data/spec/extensions/validation_class_methods_spec.rb +1054 -0
- data/spec/integration/dataset_test.rb +160 -0
- data/spec/integration/eager_loader_test.rb +683 -0
- data/spec/integration/prepared_statement_test.rb +130 -0
- data/spec/integration/schema_test.rb +183 -0
- data/spec/integration/spec_helper.rb +75 -0
- data/spec/integration/type_test.rb +96 -0
- data/spec/model/association_reflection_spec.rb +93 -0
- data/spec/model/associations_spec.rb +1780 -0
- data/spec/model/base_spec.rb +494 -0
- data/spec/model/caching_spec.rb +217 -0
- data/spec/model/dataset_methods_spec.rb +78 -0
- data/spec/model/eager_loading_spec.rb +1165 -0
- data/spec/model/hooks_spec.rb +472 -0
- data/spec/model/inflector_spec.rb +126 -0
- data/spec/model/model_spec.rb +588 -0
- data/spec/model/plugins_spec.rb +142 -0
- data/spec/model/record_spec.rb +1243 -0
- data/spec/model/schema_spec.rb +92 -0
- data/spec/model/spec_helper.rb +124 -0
- data/spec/model/validations_spec.rb +1080 -0
- data/spec/rcov.opts +6 -0
- data/spec/spec.opts +0 -0
- data/spec/spec_config.rb.example +10 -0
- metadata +202 -0
@@ -0,0 +1,494 @@
|
|
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(: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
|
+
end
|
190
|
+
end
|
191
|
+
|
192
|
+
it "should add a method to the dataset and model if called with a block argument" do
|
193
|
+
@c.instance_eval do
|
194
|
+
def_dataset_method(:return_3){3}
|
195
|
+
end
|
196
|
+
@c.return_3.should == 3
|
197
|
+
@c.dataset.return_3.should == 3
|
198
|
+
end
|
199
|
+
|
200
|
+
it "should add all passed methods to the model if called without a block argument" do
|
201
|
+
@c.instance_eval do
|
202
|
+
def_dataset_method(:return_3, :return_4)
|
203
|
+
end
|
204
|
+
proc{@c.return_3}.should raise_error(NoMethodError)
|
205
|
+
proc{@c.return_4}.should raise_error(NoMethodError)
|
206
|
+
@c.dataset.instance_eval do
|
207
|
+
def return_3; 3; end
|
208
|
+
def return_4; 4; end
|
209
|
+
end
|
210
|
+
@c.return_3.should == 3
|
211
|
+
@c.return_4.should == 4
|
212
|
+
end
|
213
|
+
|
214
|
+
it "should cache calls and readd methods if set_dataset is used" do
|
215
|
+
@c.instance_eval do
|
216
|
+
def_dataset_method(:return_3){3}
|
217
|
+
end
|
218
|
+
@c.set_dataset :items
|
219
|
+
@c.return_3.should == 3
|
220
|
+
@c.dataset.return_3.should == 3
|
221
|
+
end
|
222
|
+
|
223
|
+
it "should readd methods to subclasses, if set_dataset is used in a subclass" do
|
224
|
+
@c.instance_eval do
|
225
|
+
def_dataset_method(:return_3){3}
|
226
|
+
end
|
227
|
+
c = Class.new(@c)
|
228
|
+
c.set_dataset :items
|
229
|
+
c.return_3.should == 3
|
230
|
+
c.dataset.return_3.should == 3
|
231
|
+
end
|
232
|
+
end
|
233
|
+
|
234
|
+
describe "A model class with implicit table name" do
|
235
|
+
setup do
|
236
|
+
class Donkey < Sequel::Model
|
237
|
+
end
|
238
|
+
end
|
239
|
+
|
240
|
+
specify "should have a dataset associated with the model class" do
|
241
|
+
Donkey.dataset.model.should == Donkey
|
242
|
+
end
|
243
|
+
end
|
244
|
+
|
245
|
+
describe "A model inheriting from a model" do
|
246
|
+
setup do
|
247
|
+
class Feline < Sequel::Model
|
248
|
+
end
|
249
|
+
|
250
|
+
class Leopard < Feline
|
251
|
+
end
|
252
|
+
end
|
253
|
+
|
254
|
+
specify "should have a dataset associated with itself" do
|
255
|
+
Feline.dataset.model.should == Feline
|
256
|
+
Leopard.dataset.model.should == Leopard
|
257
|
+
end
|
258
|
+
end
|
259
|
+
|
260
|
+
describe "Model.db=" do
|
261
|
+
setup do
|
262
|
+
$db1 = MockDatabase.new
|
263
|
+
$db2 = MockDatabase.new
|
264
|
+
|
265
|
+
class BlueBlue < Sequel::Model(:items)
|
266
|
+
set_dataset $db1[:blue].filter(:x=>1)
|
267
|
+
end
|
268
|
+
end
|
269
|
+
|
270
|
+
specify "should affect the underlying dataset" do
|
271
|
+
BlueBlue.db = $db2
|
272
|
+
|
273
|
+
BlueBlue.dataset.db.should === $db2
|
274
|
+
BlueBlue.dataset.db.should_not === $db1
|
275
|
+
end
|
276
|
+
|
277
|
+
specify "should keep the same dataset options" do
|
278
|
+
BlueBlue.db = $db2
|
279
|
+
BlueBlue.dataset.sql.should == 'SELECT * FROM blue WHERE (x = 1)'
|
280
|
+
end
|
281
|
+
|
282
|
+
specify "should use the database for subclasses" do
|
283
|
+
BlueBlue.db = $db2
|
284
|
+
Class.new(BlueBlue).db.should === $db2
|
285
|
+
end
|
286
|
+
end
|
287
|
+
|
288
|
+
describe Sequel::Model, ".(allowed|restricted)_columns " do
|
289
|
+
setup do
|
290
|
+
@c = Class.new(Sequel::Model(:blahblah)) do
|
291
|
+
columns :x, :y, :z
|
292
|
+
def refresh
|
293
|
+
self
|
294
|
+
end
|
295
|
+
end
|
296
|
+
@c.strict_param_setting = false
|
297
|
+
@c.instance_variable_set(:@columns, [:x, :y, :z])
|
298
|
+
end
|
299
|
+
|
300
|
+
it "should set the allowed columns correctly" do
|
301
|
+
@c.allowed_columns.should == nil
|
302
|
+
@c.set_allowed_columns :x
|
303
|
+
@c.allowed_columns.should == [:x]
|
304
|
+
@c.set_allowed_columns :x, :y
|
305
|
+
@c.allowed_columns.should == [:x, :y]
|
306
|
+
end
|
307
|
+
|
308
|
+
it "should set the restricted columns correctly" do
|
309
|
+
@c.restricted_columns.should == nil
|
310
|
+
@c.set_restricted_columns :x
|
311
|
+
@c.restricted_columns.should == [:x]
|
312
|
+
@c.set_restricted_columns :x, :y
|
313
|
+
@c.restricted_columns.should == [:x, :y]
|
314
|
+
end
|
315
|
+
|
316
|
+
it "should only set allowed columns by default" do
|
317
|
+
@c.set_allowed_columns :x, :y
|
318
|
+
i = @c.new(:x => 1, :y => 2, :z => 3)
|
319
|
+
i.values.should == {:x => 1, :y => 2}
|
320
|
+
i.set(:x => 4, :y => 5, :z => 6)
|
321
|
+
i.values.should == {:x => 4, :y => 5}
|
322
|
+
i.update(:x => 7, :y => 8, :z => 9)
|
323
|
+
i.values.delete(:id) # stupid specs
|
324
|
+
i.values.should == {:x => 7, :y => 8}
|
325
|
+
end
|
326
|
+
|
327
|
+
it "should not set restricted columns by default" do
|
328
|
+
@c.set_restricted_columns :z
|
329
|
+
i = @c.new(:x => 1, :y => 2, :z => 3)
|
330
|
+
i.values.should == {:x => 1, :y => 2}
|
331
|
+
i.set(:x => 4, :y => 5, :z => 6)
|
332
|
+
i.values.should == {:x => 4, :y => 5}
|
333
|
+
i.update(:x => 7, :y => 8, :z => 9)
|
334
|
+
i.values.delete(:id) # stupid specs
|
335
|
+
i.values.should == {:x => 7, :y => 8}
|
336
|
+
end
|
337
|
+
|
338
|
+
it "should have allowed take precedence over restricted" do
|
339
|
+
@c.set_allowed_columns :x, :y
|
340
|
+
@c.set_restricted_columns :y, :z
|
341
|
+
i = @c.new(:x => 1, :y => 2, :z => 3)
|
342
|
+
i.values.should == {:x => 1, :y => 2}
|
343
|
+
i.set(:x => 4, :y => 5, :z => 6)
|
344
|
+
i.values.should == {:x => 4, :y => 5}
|
345
|
+
i.update(:x => 7, :y => 8, :z => 9)
|
346
|
+
i.values.delete(:id) # stupid specs
|
347
|
+
i.values.should == {:x => 7, :y => 8}
|
348
|
+
end
|
349
|
+
end
|
350
|
+
|
351
|
+
describe Sequel::Model, ".(un)?restrict_primary_key\\??" do
|
352
|
+
setup do
|
353
|
+
@c = Class.new(Sequel::Model(:blahblah)) do
|
354
|
+
set_primary_key :id
|
355
|
+
columns :x, :y, :z, :id
|
356
|
+
def refresh
|
357
|
+
self
|
358
|
+
end
|
359
|
+
end
|
360
|
+
@c.strict_param_setting = false
|
361
|
+
@c.instance_variable_set(:@columns, [:x, :y, :z])
|
362
|
+
end
|
363
|
+
|
364
|
+
it "should restrict updates to primary key by default" do
|
365
|
+
i = @c.new(:x => 1, :y => 2, :id => 3)
|
366
|
+
i.values.should == {:x => 1, :y => 2}
|
367
|
+
i.set(:x => 4, :y => 5, :id => 6)
|
368
|
+
i.values.should == {:x => 4, :y => 5}
|
369
|
+
end
|
370
|
+
|
371
|
+
it "should allow updates to primary key if unrestrict_primary_key is used" do
|
372
|
+
@c.unrestrict_primary_key
|
373
|
+
i = @c.new(:x => 1, :y => 2, :id => 3)
|
374
|
+
i.values.should == {:x => 1, :y => 2, :id=>3}
|
375
|
+
i.set(:x => 4, :y => 5, :id => 6)
|
376
|
+
i.values.should == {:x => 4, :y => 5, :id=>6}
|
377
|
+
end
|
378
|
+
|
379
|
+
it "should have restrict_primary_key? return true or false depending" do
|
380
|
+
@c.restrict_primary_key?.should == true
|
381
|
+
@c.unrestrict_primary_key
|
382
|
+
@c.restrict_primary_key?.should == false
|
383
|
+
c1 = Class.new(@c)
|
384
|
+
c1.restrict_primary_key?.should == false
|
385
|
+
@c.restrict_primary_key
|
386
|
+
@c.restrict_primary_key?.should == true
|
387
|
+
c1.restrict_primary_key?.should == false
|
388
|
+
c2 = Class.new(@c)
|
389
|
+
c2.restrict_primary_key?.should == true
|
390
|
+
end
|
391
|
+
end
|
392
|
+
|
393
|
+
describe Sequel::Model, ".strict_param_setting" do
|
394
|
+
setup do
|
395
|
+
@c = Class.new(Sequel::Model(:blahblah)) do
|
396
|
+
columns :x, :y, :z, :id
|
397
|
+
set_restricted_columns :z
|
398
|
+
def refresh
|
399
|
+
self
|
400
|
+
end
|
401
|
+
end
|
402
|
+
@c.instance_variable_set(:@columns, [:x, :y, :z])
|
403
|
+
end
|
404
|
+
|
405
|
+
it "should be enabled by default" do
|
406
|
+
@c.strict_param_setting.should == true
|
407
|
+
end
|
408
|
+
|
409
|
+
it "should raise an error if a missing/restricted column/method is accessed" do
|
410
|
+
proc{@c.new(:z=>1)}.should raise_error(Sequel::Error)
|
411
|
+
proc{@c.create(:z=>1)}.should raise_error(Sequel::Error)
|
412
|
+
c = @c.new
|
413
|
+
proc{c.set(:z=>1)}.should raise_error(Sequel::Error)
|
414
|
+
proc{c.set_all(:id=>1)}.should raise_error(Sequel::Error)
|
415
|
+
proc{c.set_only({:x=>1}, :y)}.should raise_error(Sequel::Error)
|
416
|
+
proc{c.set_except({:x=>1}, :x)}.should raise_error(Sequel::Error)
|
417
|
+
proc{c.update(:z=>1)}.should raise_error(Sequel::Error)
|
418
|
+
proc{c.update_all(:id=>1)}.should raise_error(Sequel::Error)
|
419
|
+
proc{c.update_only({:x=>1}, :y)}.should raise_error(Sequel::Error)
|
420
|
+
proc{c.update_except({:x=>1}, :x)}.should raise_error(Sequel::Error)
|
421
|
+
end
|
422
|
+
|
423
|
+
it "should be disabled by strict_param_setting = false" do
|
424
|
+
@c.strict_param_setting = false
|
425
|
+
@c.strict_param_setting.should == false
|
426
|
+
proc{@c.new(:z=>1)}.should_not raise_error
|
427
|
+
end
|
428
|
+
end
|
429
|
+
|
430
|
+
describe Sequel::Model, ".[] optimization" do
|
431
|
+
setup do
|
432
|
+
@c = Class.new(Sequel::Model(:a))
|
433
|
+
@c.instance_eval do
|
434
|
+
def simple_table
|
435
|
+
@simple_table
|
436
|
+
end
|
437
|
+
end
|
438
|
+
end
|
439
|
+
|
440
|
+
it "should set simple_pk to the literalized primary key column name if a single primary key" do
|
441
|
+
@c.simple_pk.should == 'id'
|
442
|
+
@c.set_primary_key :b
|
443
|
+
@c.simple_pk.should == 'b'
|
444
|
+
@c.set_primary_key :b__a.identifier
|
445
|
+
@c.simple_pk.should == 'b__a'
|
446
|
+
end
|
447
|
+
|
448
|
+
it "should have simple_pk be blank if compound or no primary key" do
|
449
|
+
@c.no_primary_key
|
450
|
+
@c.simple_pk.should == nil
|
451
|
+
@c.set_primary_key :b, :a
|
452
|
+
@c.simple_pk.should == nil
|
453
|
+
end
|
454
|
+
|
455
|
+
it "should have simple table set if passed a Symbol to set_dataset" do
|
456
|
+
@c.set_dataset :a
|
457
|
+
@c.simple_table.should == 'a'
|
458
|
+
@c.set_dataset :b
|
459
|
+
@c.simple_table.should == 'b'
|
460
|
+
@c.set_dataset :b__a
|
461
|
+
@c.simple_table.should == 'b.a'
|
462
|
+
end
|
463
|
+
|
464
|
+
it "should have simple_table = nil if passed a dataset to set_dataset" do
|
465
|
+
@c.set_dataset @c.db[:a]
|
466
|
+
@c.simple_table.should == nil
|
467
|
+
end
|
468
|
+
|
469
|
+
it "should have simple_table superclasses setting if inheriting" do
|
470
|
+
@c.set_dataset :a
|
471
|
+
Class.new(@c).simple_table.should == 'a'
|
472
|
+
@c.instance_variable_set(:@simple_table, nil)
|
473
|
+
Class.new(@c).simple_table.should == nil
|
474
|
+
@c.instance_variable_set(:@simple_table, "'b'")
|
475
|
+
Class.new(@c).simple_table.should == "'b'"
|
476
|
+
end
|
477
|
+
|
478
|
+
specify "should have simple_table = nil if inheriting and sti_key is set" do
|
479
|
+
@c.plugin :single_table_inheritance, :x
|
480
|
+
Class.new(@c).simple_table.should == nil
|
481
|
+
end
|
482
|
+
|
483
|
+
it "should use Dataset#with_sql if simple_table and simple_pk are true" do
|
484
|
+
@c.set_dataset :a
|
485
|
+
@c.dataset.should_receive(:with_sql).and_return(@c.dataset)
|
486
|
+
@c[1]
|
487
|
+
end
|
488
|
+
|
489
|
+
it "should not use Dataset#with_sql if either simple_table or simple_pk is nil" do
|
490
|
+
@c.set_dataset @c.dataset
|
491
|
+
@c.dataset.should_not_receive(:with_sql)
|
492
|
+
@c[1]
|
493
|
+
end
|
494
|
+
end
|