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.
Files changed (137) hide show
  1. data/CHANGELOG +7 -1
  2. data/doc/advanced_associations.rdoc +614 -0
  3. data/doc/cheat_sheet.rdoc +223 -0
  4. data/doc/dataset_filtering.rdoc +158 -0
  5. data/doc/prepared_statements.rdoc +104 -0
  6. data/doc/release_notes/1.0.txt +38 -0
  7. data/doc/release_notes/1.1.txt +143 -0
  8. data/doc/release_notes/1.3.txt +101 -0
  9. data/doc/release_notes/1.4.0.txt +53 -0
  10. data/doc/release_notes/1.5.0.txt +155 -0
  11. data/doc/release_notes/2.0.0.txt +298 -0
  12. data/doc/release_notes/2.1.0.txt +271 -0
  13. data/doc/release_notes/2.10.0.txt +328 -0
  14. data/doc/release_notes/2.2.0.txt +253 -0
  15. data/doc/release_notes/2.3.0.txt +88 -0
  16. data/doc/release_notes/2.4.0.txt +106 -0
  17. data/doc/release_notes/2.5.0.txt +137 -0
  18. data/doc/release_notes/2.6.0.txt +157 -0
  19. data/doc/release_notes/2.7.0.txt +166 -0
  20. data/doc/release_notes/2.8.0.txt +171 -0
  21. data/doc/release_notes/2.9.0.txt +97 -0
  22. data/doc/schema.rdoc +29 -0
  23. data/doc/sharding.rdoc +113 -0
  24. data/lib/sequel.rb +1 -0
  25. data/lib/sequel_core/adapters/ado.rb +89 -0
  26. data/lib/sequel_core/adapters/db2.rb +143 -0
  27. data/lib/sequel_core/adapters/dbi.rb +112 -0
  28. data/lib/sequel_core/adapters/do/mysql.rb +38 -0
  29. data/lib/sequel_core/adapters/do/postgres.rb +92 -0
  30. data/lib/sequel_core/adapters/do/sqlite.rb +31 -0
  31. data/lib/sequel_core/adapters/do.rb +205 -0
  32. data/lib/sequel_core/adapters/firebird.rb +298 -0
  33. data/lib/sequel_core/adapters/informix.rb +85 -0
  34. data/lib/sequel_core/adapters/jdbc/h2.rb +69 -0
  35. data/lib/sequel_core/adapters/jdbc/mysql.rb +66 -0
  36. data/lib/sequel_core/adapters/jdbc/oracle.rb +23 -0
  37. data/lib/sequel_core/adapters/jdbc/postgresql.rb +113 -0
  38. data/lib/sequel_core/adapters/jdbc/sqlite.rb +43 -0
  39. data/lib/sequel_core/adapters/jdbc.rb +491 -0
  40. data/lib/sequel_core/adapters/mysql.rb +369 -0
  41. data/lib/sequel_core/adapters/odbc.rb +174 -0
  42. data/lib/sequel_core/adapters/openbase.rb +68 -0
  43. data/lib/sequel_core/adapters/oracle.rb +107 -0
  44. data/lib/sequel_core/adapters/postgres.rb +456 -0
  45. data/lib/sequel_core/adapters/shared/ms_access.rb +110 -0
  46. data/lib/sequel_core/adapters/shared/mssql.rb +102 -0
  47. data/lib/sequel_core/adapters/shared/mysql.rb +325 -0
  48. data/lib/sequel_core/adapters/shared/oracle.rb +61 -0
  49. data/lib/sequel_core/adapters/shared/postgres.rb +715 -0
  50. data/lib/sequel_core/adapters/shared/progress.rb +31 -0
  51. data/lib/sequel_core/adapters/shared/sqlite.rb +265 -0
  52. data/lib/sequel_core/adapters/sqlite.rb +248 -0
  53. data/lib/sequel_core/connection_pool.rb +258 -0
  54. data/lib/sequel_core/core_ext.rb +217 -0
  55. data/lib/sequel_core/core_sql.rb +202 -0
  56. data/lib/sequel_core/database/schema.rb +164 -0
  57. data/lib/sequel_core/database.rb +691 -0
  58. data/lib/sequel_core/dataset/callback.rb +13 -0
  59. data/lib/sequel_core/dataset/convenience.rb +237 -0
  60. data/lib/sequel_core/dataset/pagination.rb +96 -0
  61. data/lib/sequel_core/dataset/prepared_statements.rb +220 -0
  62. data/lib/sequel_core/dataset/query.rb +41 -0
  63. data/lib/sequel_core/dataset/schema.rb +15 -0
  64. data/lib/sequel_core/dataset/sql.rb +1010 -0
  65. data/lib/sequel_core/dataset/stored_procedures.rb +75 -0
  66. data/lib/sequel_core/dataset/unsupported.rb +43 -0
  67. data/lib/sequel_core/dataset.rb +511 -0
  68. data/lib/sequel_core/deprecated.rb +26 -0
  69. data/lib/sequel_core/exceptions.rb +44 -0
  70. data/lib/sequel_core/migration.rb +212 -0
  71. data/lib/sequel_core/object_graph.rb +230 -0
  72. data/lib/sequel_core/pretty_table.rb +71 -0
  73. data/lib/sequel_core/schema/generator.rb +320 -0
  74. data/lib/sequel_core/schema/sql.rb +325 -0
  75. data/lib/sequel_core/schema.rb +2 -0
  76. data/lib/sequel_core/sql.rb +887 -0
  77. data/lib/sequel_core/version.rb +11 -0
  78. data/lib/sequel_core.rb +172 -0
  79. data/lib/sequel_model/association_reflection.rb +267 -0
  80. data/lib/sequel_model/associations.rb +499 -0
  81. data/lib/sequel_model/base.rb +523 -0
  82. data/lib/sequel_model/caching.rb +82 -0
  83. data/lib/sequel_model/dataset_methods.rb +26 -0
  84. data/lib/sequel_model/eager_loading.rb +370 -0
  85. data/lib/sequel_model/exceptions.rb +7 -0
  86. data/lib/sequel_model/hooks.rb +101 -0
  87. data/lib/sequel_model/inflector.rb +281 -0
  88. data/lib/sequel_model/plugins.rb +62 -0
  89. data/lib/sequel_model/record.rb +568 -0
  90. data/lib/sequel_model/schema.rb +49 -0
  91. data/lib/sequel_model/validations.rb +429 -0
  92. data/lib/sequel_model.rb +91 -0
  93. data/spec/adapters/ado_spec.rb +46 -0
  94. data/spec/adapters/firebird_spec.rb +376 -0
  95. data/spec/adapters/informix_spec.rb +96 -0
  96. data/spec/adapters/mysql_spec.rb +881 -0
  97. data/spec/adapters/oracle_spec.rb +244 -0
  98. data/spec/adapters/postgres_spec.rb +687 -0
  99. data/spec/adapters/spec_helper.rb +10 -0
  100. data/spec/adapters/sqlite_spec.rb +555 -0
  101. data/spec/integration/dataset_test.rb +134 -0
  102. data/spec/integration/eager_loader_test.rb +696 -0
  103. data/spec/integration/prepared_statement_test.rb +130 -0
  104. data/spec/integration/schema_test.rb +180 -0
  105. data/spec/integration/spec_helper.rb +58 -0
  106. data/spec/integration/type_test.rb +96 -0
  107. data/spec/rcov.opts +6 -0
  108. data/spec/sequel_core/connection_pool_spec.rb +526 -0
  109. data/spec/sequel_core/core_ext_spec.rb +156 -0
  110. data/spec/sequel_core/core_sql_spec.rb +522 -0
  111. data/spec/sequel_core/database_spec.rb +1188 -0
  112. data/spec/sequel_core/dataset_spec.rb +3481 -0
  113. data/spec/sequel_core/expression_filters_spec.rb +363 -0
  114. data/spec/sequel_core/migration_spec.rb +261 -0
  115. data/spec/sequel_core/object_graph_spec.rb +272 -0
  116. data/spec/sequel_core/pretty_table_spec.rb +58 -0
  117. data/spec/sequel_core/schema_generator_spec.rb +167 -0
  118. data/spec/sequel_core/schema_spec.rb +780 -0
  119. data/spec/sequel_core/spec_helper.rb +55 -0
  120. data/spec/sequel_core/version_spec.rb +7 -0
  121. data/spec/sequel_model/association_reflection_spec.rb +93 -0
  122. data/spec/sequel_model/associations_spec.rb +1767 -0
  123. data/spec/sequel_model/base_spec.rb +419 -0
  124. data/spec/sequel_model/caching_spec.rb +215 -0
  125. data/spec/sequel_model/dataset_methods_spec.rb +78 -0
  126. data/spec/sequel_model/eager_loading_spec.rb +1165 -0
  127. data/spec/sequel_model/hooks_spec.rb +485 -0
  128. data/spec/sequel_model/inflector_spec.rb +119 -0
  129. data/spec/sequel_model/model_spec.rb +588 -0
  130. data/spec/sequel_model/plugins_spec.rb +80 -0
  131. data/spec/sequel_model/record_spec.rb +1184 -0
  132. data/spec/sequel_model/schema_spec.rb +90 -0
  133. data/spec/sequel_model/spec_helper.rb +78 -0
  134. data/spec/sequel_model/validations_spec.rb +1067 -0
  135. data/spec/spec.opts +0 -0
  136. data/spec/spec_config.rb.example +10 -0
  137. metadata +177 -3
@@ -0,0 +1,588 @@
1
+ require File.join(File.dirname(__FILE__), "spec_helper")
2
+
3
+ describe Sequel::Model do
4
+ it "should have class method aliased as model" do
5
+ Sequel::Model.instance_methods.collect{|x| x.to_s}.should include("model")
6
+
7
+ model_a = Class.new(Sequel::Model(:items))
8
+ model_a.new.model.should be(model_a)
9
+ end
10
+
11
+ it "should be associated with a dataset" do
12
+ model_a = Class.new(Sequel::Model) { set_dataset MODEL_DB[:as] }
13
+
14
+ model_a.dataset.should be_a_kind_of(MockDataset)
15
+ model_a.dataset.opts[:from].should == [:as]
16
+
17
+ model_b = Class.new(Sequel::Model) { set_dataset MODEL_DB[:bs] }
18
+
19
+ model_b.dataset.should be_a_kind_of(MockDataset)
20
+ model_b.dataset.opts[:from].should == [:bs]
21
+
22
+ model_a.dataset.opts[:from].should == [:as]
23
+ end
24
+
25
+ end
26
+
27
+ describe Sequel::Model, "dataset & schema" do
28
+ before do
29
+ @model = Class.new(Sequel::Model(:items))
30
+ end
31
+
32
+ it "creates dynamic model subclass with set table name" do
33
+ @model.table_name.should == :items
34
+ end
35
+
36
+ it "defaults to primary key of id" do
37
+ @model.primary_key.should == :id
38
+ end
39
+
40
+ it "allow primary key change" do
41
+ @model.set_primary_key :ssn
42
+ @model.primary_key.should == :ssn
43
+ end
44
+
45
+ it "allows dataset change" do
46
+ @model.set_dataset(MODEL_DB[:foo])
47
+ @model.table_name.should == :foo
48
+ end
49
+
50
+ it "set_dataset should take a symbol" do
51
+ @model.db = MODEL_DB
52
+ @model.set_dataset(:foo)
53
+ @model.table_name.should == :foo
54
+ end
55
+
56
+ it "set_dataset should raise an error unless given a Symbol or Dataset" do
57
+ proc{@model.set_dataset(Object.new)}.should raise_error(Sequel::Error)
58
+ end
59
+
60
+ it "set_dataset should add the destroy method to the dataset" do
61
+ ds = MODEL_DB[:foo]
62
+ ds.should_not respond_to(:destroy)
63
+ @model.set_dataset(ds)
64
+ ds.should respond_to(:destroy)
65
+ end
66
+
67
+ it "sets schema with implicit table name" do
68
+ @model.set_schema do
69
+ primary_key :ssn, :string
70
+ end
71
+ @model.primary_key.should == :ssn
72
+ @model.table_name.should == :items
73
+ end
74
+
75
+ it "sets schema with explicit table name" do
76
+ @model.set_schema :foo do
77
+ primary_key :id
78
+ end
79
+ @model.primary_key.should == :id
80
+ @model.table_name.should == :foo
81
+ end
82
+
83
+ it "doesn't raise an error on set_dataset if there is an error raised getting the schema" do
84
+ @model.meta_def(:get_db_schema){raise Sequel::Error}
85
+ proc{@model.set_dataset(MODEL_DB[:foo])}.should_not raise_error
86
+ end
87
+
88
+ it "doesn't raise an error on inherited if there is an error setting the dataset" do
89
+ @model.meta_def(:set_dataset){raise Sequel::Error}
90
+ proc{Class.new(@model)}.should_not raise_error
91
+ end
92
+ end
93
+
94
+ describe Sequel::Model, "#sti_key" do
95
+ before do
96
+ class ::StiTest < Sequel::Model
97
+ def kind=(x); self[:kind] = x; end
98
+ def refresh; end
99
+ set_sti_key :kind
100
+ end
101
+ class ::StiTestSub1 < StiTest
102
+ end
103
+ class ::StiTestSub2 < StiTest
104
+ end
105
+ @ds = StiTest.dataset
106
+ MODEL_DB.reset
107
+ end
108
+
109
+ it "should return rows with the correct class based on the polymorphic_key value" do
110
+ def @ds.fetch_rows(sql)
111
+ yield({:kind=>'StiTest'})
112
+ yield({:kind=>'StiTestSub1'})
113
+ yield({:kind=>'StiTestSub2'})
114
+ end
115
+ StiTest.all.collect{|x| x.class}.should == [StiTest, StiTestSub1, StiTestSub2]
116
+ end
117
+
118
+ it "should fallback to the main class if polymophic_key value is NULL" do
119
+ def @ds.fetch_rows(sql)
120
+ yield({:kind=>nil})
121
+ end
122
+ StiTest.all.collect{|x| x.class}.should == [StiTest]
123
+ end
124
+
125
+ it "should fallback to the main class if the given class does not exist" do
126
+ def @ds.fetch_rows(sql)
127
+ yield({:kind=>'StiTestSub3'})
128
+ end
129
+ StiTest.all.collect{|x| x.class}.should == [StiTest]
130
+ end
131
+
132
+ it "should add a before_create hook that sets the model class name for the key" do
133
+ StiTest.new.save
134
+ StiTestSub1.new.save
135
+ StiTestSub2.new.save
136
+ MODEL_DB.sqls.should == ["INSERT INTO sti_tests (kind) VALUES ('StiTest')", "INSERT INTO sti_tests (kind) VALUES ('StiTestSub1')", "INSERT INTO sti_tests (kind) VALUES ('StiTestSub2')"]
137
+ end
138
+
139
+ it "should add a filter to model datasets inside subclasses hook to only retreive objects with the matching key" do
140
+ StiTest.dataset.sql.should == "SELECT * FROM sti_tests"
141
+ StiTestSub1.dataset.sql.should == "SELECT * FROM sti_tests WHERE (kind = 'StiTestSub1')"
142
+ StiTestSub2.dataset.sql.should == "SELECT * FROM sti_tests WHERE (kind = 'StiTestSub2')"
143
+ end
144
+ end
145
+
146
+ describe Sequel::Model, "constructor" do
147
+
148
+ before(:each) do
149
+ @m = Class.new(Sequel::Model)
150
+ @m.columns :a, :b
151
+ end
152
+
153
+ it "should accept a hash" do
154
+ m = @m.new(:a => 1, :b => 2)
155
+ m.values.should == {:a => 1, :b => 2}
156
+ m.should be_new
157
+ end
158
+
159
+ it "should accept a block and yield itself to the block" do
160
+ block_called = false
161
+ m = @m.new {|i| block_called = true; i.should be_a_kind_of(@m); i.values[:a] = 1}
162
+
163
+ block_called.should be_true
164
+ m.values[:a].should == 1
165
+ end
166
+
167
+ end
168
+
169
+ describe Sequel::Model, "new" do
170
+
171
+ before(:each) do
172
+ @m = Class.new(Sequel::Model) do
173
+ set_dataset MODEL_DB[:items]
174
+ columns :x, :id
175
+ end
176
+ end
177
+
178
+ it "should be marked as new?" do
179
+ o = @m.new
180
+ o.should be_new
181
+ end
182
+
183
+ it "should not be marked as new? once it is saved" do
184
+ o = @m.new(:x => 1)
185
+ o.should be_new
186
+ o.save
187
+ o.should_not be_new
188
+ end
189
+
190
+ it "should use the last inserted id as primary key if not in values" do
191
+ d = @m.dataset
192
+ def d.insert(*args)
193
+ super
194
+ 1234
195
+ end
196
+
197
+ def d.first
198
+ {:x => 1, :id => 1234}
199
+ end
200
+
201
+ o = @m.new(:x => 1)
202
+ o.save
203
+ o.id.should == 1234
204
+
205
+ o = @m.load(:x => 1, :id => 333)
206
+ o.save
207
+ o.id.should == 333
208
+ end
209
+
210
+ end
211
+
212
+ describe Sequel::Model, ".subset" do
213
+ before do
214
+ MODEL_DB.reset
215
+
216
+ @c = Class.new(Sequel::Model(:items))
217
+ end
218
+
219
+ specify "should create a filter on the underlying dataset" do
220
+ proc {@c.new_only}.should raise_error(NoMethodError)
221
+
222
+ @c.subset(:new_only) {:age.sql_number < 'new'}
223
+
224
+ @c.new_only.sql.should == "SELECT * FROM items WHERE (age < 'new')"
225
+ @c.dataset.new_only.sql.should == "SELECT * FROM items WHERE (age < 'new')"
226
+
227
+ @c.subset(:pricey) {:price.sql_number > 100}
228
+
229
+ @c.pricey.sql.should == "SELECT * FROM items WHERE (price > 100)"
230
+ @c.dataset.pricey.sql.should == "SELECT * FROM items WHERE (price > 100)"
231
+
232
+ @c.pricey.new_only.sql.should == "SELECT * FROM items WHERE ((price > 100) AND (age < 'new'))"
233
+ @c.new_only.pricey.sql.should == "SELECT * FROM items WHERE ((age < 'new') AND (price > 100))"
234
+ end
235
+
236
+ end
237
+
238
+ describe Sequel::Model, ".find" do
239
+
240
+ before(:each) do
241
+ MODEL_DB.reset
242
+
243
+ @c = Class.new(Sequel::Model(:items))
244
+
245
+ $cache_dataset_row = {:name => 'sharon', :id => 1}
246
+ @dataset = @c.dataset
247
+ $sqls = []
248
+ @dataset.extend(Module.new {
249
+ def fetch_rows(sql)
250
+ $sqls << sql
251
+ yield $cache_dataset_row
252
+ end
253
+ })
254
+ end
255
+
256
+ it "should return the first record matching the given filter" do
257
+ @c.find(:name => 'sharon').should be_a_kind_of(@c)
258
+ $sqls.last.should == "SELECT * FROM items WHERE (name = 'sharon') LIMIT 1"
259
+
260
+ @c.find(:name.like('abc%')).should be_a_kind_of(@c)
261
+ $sqls.last.should == "SELECT * FROM items WHERE (name LIKE 'abc%') LIMIT 1"
262
+ end
263
+
264
+ specify "should accept filter blocks" do
265
+ @c.find{:id.sql_number > 1}.should be_a_kind_of(@c)
266
+ $sqls.last.should == "SELECT * FROM items WHERE (id > 1) LIMIT 1"
267
+
268
+ @c.find {(:x.sql_number > 1) & (:y.sql_number < 2)}.should be_a_kind_of(@c)
269
+ $sqls.last.should == "SELECT * FROM items WHERE ((x > 1) AND (y < 2)) LIMIT 1"
270
+ end
271
+
272
+ end
273
+
274
+ describe Sequel::Model, ".fetch" do
275
+
276
+ before(:each) do
277
+ MODEL_DB.reset
278
+ @c = Class.new(Sequel::Model(:items))
279
+ end
280
+
281
+ it "should return instances of Model" do
282
+ @c.fetch("SELECT * FROM items").first.should be_a_kind_of(@c)
283
+ end
284
+
285
+ it "should return true for .empty? and not raise an error on empty selection" do
286
+ rows = @c.fetch("SELECT * FROM items WHERE FALSE")
287
+ @c.class_def(:fetch_rows) {|sql| yield({:count => 0})}
288
+ proc {rows.empty?}.should_not raise_error
289
+ end
290
+
291
+ end
292
+
293
+ describe Sequel::Model, ".find_or_create" do
294
+
295
+ before(:each) do
296
+ MODEL_DB.reset
297
+ @c = Class.new(Sequel::Model(:items)) do
298
+ no_primary_key
299
+ columns :x
300
+ end
301
+ end
302
+
303
+ it "should find the record" do
304
+ @c.find_or_create(:x => 1)
305
+ MODEL_DB.sqls.should == ["SELECT * FROM items WHERE (x = 1) LIMIT 1"]
306
+
307
+ MODEL_DB.reset
308
+ end
309
+
310
+ it "should create the record if not found" do
311
+ @c.meta_def(:find) do |*args|
312
+ dataset.filter(*args).first
313
+ nil
314
+ end
315
+
316
+ @c.find_or_create(:x => 1)
317
+ MODEL_DB.sqls.should == [
318
+ "SELECT * FROM items WHERE (x = 1) LIMIT 1",
319
+ "INSERT INTO items (x) VALUES (1)"
320
+ ]
321
+ end
322
+ end
323
+
324
+ describe Sequel::Model, ".delete_all" do
325
+
326
+ before(:each) do
327
+ MODEL_DB.reset
328
+ @c = Class.new(Sequel::Model(:items)) do
329
+ no_primary_key
330
+ end
331
+
332
+ @c.dataset.meta_def(:delete) {MODEL_DB << delete_sql}
333
+ end
334
+
335
+ it "should delete all records in the dataset" do
336
+ @c.delete_all
337
+ MODEL_DB.sqls.should == ["DELETE FROM items"]
338
+ end
339
+
340
+ end
341
+
342
+ describe Sequel::Model, ".destroy_all" do
343
+
344
+ before(:each) do
345
+ MODEL_DB.reset
346
+ @c = Class.new(Sequel::Model(:items)) do
347
+ no_primary_key
348
+ end
349
+
350
+ @c.dataset.meta_def(:delete) {MODEL_DB << delete_sql}
351
+ end
352
+
353
+ it "should delete all records in the dataset" do
354
+ @c.dataset.meta_def(:destroy) {MODEL_DB << "DESTROY this stuff"}
355
+ @c.destroy_all
356
+ MODEL_DB.sqls.should == ["DESTROY this stuff"]
357
+ end
358
+
359
+ it "should call dataset.destroy" do
360
+ @c.dataset.should_receive(:destroy).and_return(true)
361
+ @c.destroy_all
362
+ end
363
+ end
364
+
365
+ describe Sequel::Model, ".all" do
366
+
367
+ before(:each) do
368
+ MODEL_DB.reset
369
+ @c = Class.new(Sequel::Model(:items)) do
370
+ no_primary_key
371
+ end
372
+
373
+ @c.dataset.meta_def(:all) {1234}
374
+ end
375
+
376
+ it "should return all records in the dataset" do
377
+ @c.all.should == 1234
378
+ end
379
+
380
+ end
381
+
382
+ class DummyModelBased < Sequel::Model(:blog)
383
+ end
384
+
385
+ describe Sequel::Model, "(:tablename)" do
386
+
387
+ it "should allow reopening of descendant classes" do
388
+ proc do
389
+ eval "class DummyModelBased < Sequel::Model(:blog); end"
390
+ end.should_not raise_error
391
+ end
392
+
393
+ end
394
+
395
+ describe Sequel::Model, "A model class without a primary key" do
396
+
397
+ before(:each) do
398
+ MODEL_DB.reset
399
+ @c = Class.new(Sequel::Model(:items)) do
400
+ columns :x
401
+ no_primary_key
402
+ end
403
+ end
404
+
405
+ it "should be able to insert records without selecting them back" do
406
+ i = nil
407
+ proc {i = @c.create(:x => 1)}.should_not raise_error
408
+ i.class.should be(@c)
409
+ i.values.to_hash.should == {:x => 1}
410
+
411
+ MODEL_DB.sqls.should == ['INSERT INTO items (x) VALUES (1)']
412
+ end
413
+
414
+ it "should raise when deleting" do
415
+ o = @c.new
416
+ proc {o.delete}.should raise_error
417
+ end
418
+
419
+ it "should insert a record when saving" do
420
+ o = @c.new(:x => 2)
421
+ o.should be_new
422
+ o.save
423
+ MODEL_DB.sqls.should == ['INSERT INTO items (x) VALUES (2)']
424
+ end
425
+
426
+ end
427
+
428
+ describe Sequel::Model, "attribute accessors" do
429
+ before do
430
+ MODEL_DB.reset
431
+ @dataset = Sequel::Dataset.new(MODEL_DB)
432
+ def @dataset.columns; [:x, :y]; end
433
+ @c = Class.new(Sequel::Model) do
434
+ def self.db_schema
435
+ set_columns(Array(@columns))
436
+ @db_schema = {:x=>{}, :y=>{}}
437
+ end
438
+ def self.set_dataset(ds, opts={})
439
+ @columns = ds.columns
440
+ db_schema
441
+ end
442
+ end
443
+ end
444
+
445
+ it "should be created on set_dataset" do
446
+ %w'x y x= y='.each do |x|
447
+ @c.instance_methods.collect{|y| y.to_s}.should_not include(x)
448
+ end
449
+ @c.set_dataset(@dataset)
450
+ %w'x y x= y='.each do |x|
451
+ @c.instance_methods.collect{|y| y.to_s}.should include(x)
452
+ end
453
+ o = @c.new
454
+ %w'x y x= y='.each do |x|
455
+ o.methods.collect{|y| y.to_s}.should include(x)
456
+ end
457
+
458
+ o.x.should be_nil
459
+ o.x = 34
460
+ o.x.should == 34
461
+ end
462
+
463
+ it "should be only accept one argument for the write accessor" do
464
+ @c.set_dataset(@dataset)
465
+ o = @c.new
466
+
467
+ o.x = 34
468
+ o.x.should == 34
469
+ proc{o.send(:x=)}.should raise_error
470
+ proc{o.send(:x=, 3, 4)}.should raise_error
471
+ end
472
+ end
473
+
474
+ describe Sequel::Model, ".[]" do
475
+
476
+ before(:each) do
477
+ MODEL_DB.reset
478
+
479
+ @c = Class.new(Sequel::Model(:items))
480
+
481
+ $cache_dataset_row = {:name => 'sharon', :id => 1}
482
+ @dataset = @c.dataset
483
+ $sqls = []
484
+ @dataset.extend(Module.new {
485
+ def fetch_rows(sql)
486
+ $sqls << sql
487
+ yield $cache_dataset_row
488
+ end
489
+ })
490
+ end
491
+
492
+ it "should return the first record for the given pk" do
493
+ @c[1].should be_a_kind_of(@c)
494
+ $sqls.last.should == "SELECT * FROM items WHERE (id = 1) LIMIT 1"
495
+ @c[9999].should be_a_kind_of(@c)
496
+ $sqls.last.should == "SELECT * FROM items WHERE (id = 9999) LIMIT 1"
497
+ end
498
+
499
+ it "should work correctly for custom primary key" do
500
+ @c.set_primary_key :name
501
+ @c['sharon'].should be_a_kind_of(@c)
502
+ $sqls.last.should == "SELECT * FROM items WHERE (name = 'sharon') LIMIT 1"
503
+ end
504
+
505
+ it "should work correctly for composite primary key" do
506
+ @c.set_primary_key [:node_id, :kind]
507
+ @c[3921, 201].should be_a_kind_of(@c)
508
+ $sqls.last.should =~ \
509
+ /^SELECT \* FROM items WHERE \((\(node_id = 3921\) AND \(kind = 201\))|(\(kind = 201\) AND \(node_id = 3921\))\) LIMIT 1$/
510
+ end
511
+ end
512
+
513
+ context "Model#inspect" do
514
+ setup do
515
+ @o = Sequel::Model.load(:x => 333)
516
+ end
517
+
518
+ specify "should include the class name and the values" do
519
+ @o.inspect.should == '#<Sequel::Model @values={:x=>333}>'
520
+ end
521
+ end
522
+
523
+ context "Model.db_schema" do
524
+ setup do
525
+ @c = Class.new(Sequel::Model(:items)) do
526
+ def self.columns; orig_columns; end
527
+ end
528
+ @dataset = Sequel::Dataset.new(nil).from(:items)
529
+ @dataset.meta_def(:db){@db ||= Sequel::Database.new}
530
+ def @dataset.set_model(blah); end
531
+ def @dataset.naked; self; end
532
+ def @dataset.columns; []; end
533
+ def @dataset.def_mutation_method(*names); end
534
+ end
535
+
536
+ specify "should use the database's schema_for_table and set the columns and dataset columns" do
537
+ d = @dataset.db
538
+ def d.schema(table, opts = {})
539
+ [[:x, {:type=>:integer}], [:y, {:type=>:string}]]
540
+ end
541
+ @c.dataset = @dataset
542
+ @c.db_schema.should == {:x=>{:type=>:integer}, :y=>{:type=>:string}}
543
+ @c.columns.should == [:x, :y]
544
+ @c.dataset.instance_variable_get(:@columns).should == [:x, :y]
545
+ end
546
+
547
+ specify "should restrict the schema and columns for datasets with a :select option" do
548
+ ds = @dataset.select(:x, :y___z)
549
+ d = ds.db
550
+ def d.schema(table, opts = {})
551
+ [[:x, {:type=>:integer}], [:y, {:type=>:string}]]
552
+ end
553
+ def @c.columns; [:x, :z]; end
554
+ @c.dataset = ds
555
+ @c.db_schema.should == {:x=>{:type=>:integer}, :z=>{}}
556
+ end
557
+
558
+ specify "should not use schema_for_table if the dataset uses multiple tables or custom sql" do
559
+ ds = @dataset.join(:x, :id)
560
+ d = ds.db
561
+ e = false
562
+ d.meta_def(:schema){|table| e = true}
563
+ def @c.columns; [:x]; end
564
+ @c.dataset = ds
565
+ @c.db_schema.should == {:x=>{}}
566
+ e.should == false
567
+ end
568
+
569
+ specify "should fallback to fetching records if schema_for_table raises an error" do
570
+ ds = @dataset.join(:x, :id)
571
+ d = ds.db
572
+ def d.schema(table)
573
+ raise StandardError
574
+ end
575
+ def @c.columns; [:x]; end
576
+ @c.dataset = ds
577
+ @c.db_schema.should == {:x=>{}}
578
+ end
579
+ end
580
+
581
+ context "Model.str_columns" do
582
+ specify "should return the columns as frozen strings" do
583
+ c = Class.new(Sequel::Model)
584
+ c.meta_def(:columns){[:a, :b]}
585
+ c.orig_str_columns.should == %w'a b'
586
+ proc{c.orig_str_columns.first << 'a'}.should raise_error
587
+ end
588
+ end
@@ -0,0 +1,80 @@
1
+ require File.join(File.dirname(__FILE__), "spec_helper")
2
+
3
+ module Sequel::Plugins
4
+
5
+ module Timestamped
6
+ def self.apply(m, opts)
7
+ m.class_def(:get_stamp) {@values[:stamp]}
8
+ m.meta_def(:stamp_opts) {opts}
9
+ m.before_save {@values[:stamp] = Time.now}
10
+ end
11
+
12
+ module InstanceMethods
13
+ def abc; timestamped_opts; end
14
+ end
15
+
16
+ module ClassMethods
17
+ def deff; timestamped_opts; end
18
+ end
19
+
20
+ module DatasetMethods
21
+ def ghi; timestamped_opts; end
22
+ end
23
+ end
24
+
25
+ end
26
+
27
+ describe Sequel::Model, "using a plugin" do
28
+
29
+ it "should fail if the plugin is not found" do
30
+ proc do
31
+ c = Class.new(Sequel::Model)
32
+ c.class_eval do
33
+ is :something_or_other
34
+ end
35
+ end.should raise_error(LoadError)
36
+ end
37
+
38
+ it "should apply the plugin to the class" do
39
+ c = nil
40
+ proc do
41
+ c = Class.new(Sequel::Model)
42
+ c.class_eval do
43
+ set_dataset MODEL_DB[:items]
44
+ is :timestamped, :a => 1, :b => 2
45
+ end
46
+ end.should_not raise_error(LoadError)
47
+
48
+ c.should respond_to(:stamp_opts)
49
+ c.stamp_opts.should == {:a => 1, :b => 2}
50
+
51
+ # instance methods
52
+ m = c.new
53
+ m.should respond_to(:get_stamp)
54
+ m.should respond_to(:abc)
55
+ m.abc.should == {:a => 1, :b => 2}
56
+ t = Time.now
57
+ m[:stamp] = t
58
+ m.get_stamp.should == t
59
+
60
+ # class methods
61
+ c.should respond_to(:deff)
62
+ c.deff.should == {:a => 1, :b => 2}
63
+
64
+ # dataset methods
65
+ c.dataset.should respond_to(:ghi)
66
+ c.dataset.ghi.should == {:a => 1, :b => 2}
67
+
68
+ # dataset methods called on the class
69
+ c.should respond_to(:ghi)
70
+ c.ghi.should == {:a => 1, :b => 2}
71
+ end
72
+
73
+ it "should fail to apply if the plugin has DatasetMethod and the model has no datset" do
74
+ proc do
75
+ Class.new(Sequel::Model) do
76
+ is :timestamped, :a => 1, :b => 2
77
+ end
78
+ end.should raise_error(Sequel::Error)
79
+ end
80
+ end