sequel 0.4.5 → 0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,148 +0,0 @@
1
- describe Sequel::Model, "one_to_one" do
2
-
3
- before(:each) do
4
- MODEL_DB.reset
5
-
6
- @c1 = Class.new(Sequel::Model(:attributes)) do
7
- end
8
-
9
- @c2 = Class.new(Sequel::Model(:nodes)) do
10
- end
11
-
12
- @dataset = @c2.dataset
13
-
14
- $sqls = []
15
- @dataset.extend(Module.new {
16
- def fetch_rows(sql)
17
- $sqls << sql
18
- yield({:hey => 1})
19
- end
20
-
21
- def update(values)
22
- $sqls << update_sql(values)
23
- end
24
- }
25
- )
26
- end
27
-
28
- it "should use implicit key if omitted" do
29
- @c2.one_to_one :parent, :from => @c2
30
-
31
- d = @c2.new(:id => 1, :parent_id => 234)
32
- p = d.parent
33
- p.class.should == @c2
34
- p.values.should == {:hey => 1}
35
-
36
- $sqls.should == ["SELECT * FROM nodes WHERE (id = 234) LIMIT 1"]
37
- end
38
-
39
- it "should use explicit key if given" do
40
- @c2.one_to_one :parent, :from => @c2, :key => :blah
41
-
42
- d = @c2.new(:id => 1, :blah => 567)
43
- p = d.parent
44
- p.class.should == @c2
45
- p.values.should == {:hey => 1}
46
-
47
- $sqls.should == ["SELECT * FROM nodes WHERE (id = 567) LIMIT 1"]
48
- end
49
-
50
- it "should support plain dataset in the from option" do
51
- @c2.one_to_one :parent, :from => MODEL_DB[:xyz]
52
-
53
- d = @c2.new(:id => 1, :parent_id => 789)
54
- p = d.parent
55
- p.class.should == Hash
56
-
57
- MODEL_DB.sqls.should == ["SELECT * FROM xyz WHERE (id = 789) LIMIT 1"]
58
- end
59
-
60
- it "should support table name in the from option" do
61
- @c2.one_to_one :parent, :from => :abc
62
-
63
- d = @c2.new(:id => 1, :parent_id => 789)
64
- p = d.parent
65
- p.class.should == Hash
66
-
67
- MODEL_DB.sqls.should == ["SELECT * FROM abc WHERE (id = 789) LIMIT 1"]
68
- end
69
-
70
- it "should return nil if key value is nil" do
71
- @c2.one_to_one :parent, :from => @c2
72
-
73
- d = @c2.new(:id => 1)
74
- d.parent.should == nil
75
- end
76
-
77
- it "should define a setter method" do
78
- @c2.one_to_one :parent, :from => @c2
79
-
80
- d = @c2.new(:id => 1)
81
- d.parent = {:id => 4321}
82
- d.values.should == {:id => 1, :parent_id => 4321}
83
- $sqls.last.should == "UPDATE nodes SET parent_id = 4321 WHERE (id = 1)"
84
-
85
- d.parent = nil
86
- d.values.should == {:id => 1, :parent_id => nil}
87
- $sqls.last.should == "UPDATE nodes SET parent_id = NULL WHERE (id = 1)"
88
-
89
- e = @c2.new(:id => 6677)
90
- d.parent = e
91
- d.values.should == {:id => 1, :parent_id => 6677}
92
- $sqls.last.should == "UPDATE nodes SET parent_id = 6677 WHERE (id = 1)"
93
- end
94
-
95
- it "should warn with a depreciation notice if :class option was used" do
96
- pending("write this spec")
97
- end
98
-
99
- end
100
-
101
- describe Sequel::Model, "one_to_many" do
102
-
103
- before(:each) do
104
- MODEL_DB.reset
105
-
106
- @c1 = Class.new(Sequel::Model(:attributes)) do
107
- end
108
-
109
- @c2 = Class.new(Sequel::Model(:nodes)) do
110
- end
111
- end
112
-
113
- it "should define a getter method" do
114
- @c2.one_to_many :attributes, :from => @c1, :key => :node_id
115
-
116
- n = @c2.new(:id => 1234)
117
- a = n.attributes
118
- a.should be_a_kind_of(Sequel::Dataset)
119
- a.sql.should == 'SELECT * FROM attributes WHERE (node_id = 1234)'
120
- end
121
-
122
- it "should support plain dataset in the from option" do
123
- @c2.one_to_many :attributes, :from => MODEL_DB[:xyz], :key => :node_id
124
-
125
- n = @c2.new(:id => 1234)
126
- a = n.attributes
127
- a.should be_a_kind_of(Sequel::Dataset)
128
- a.sql.should == 'SELECT * FROM xyz WHERE (node_id = 1234)'
129
- end
130
-
131
- it "should support table name in the from option" do
132
- @c2.one_to_many :attributes, :from => :abc, :key => :node_id
133
-
134
- n = @c2.new(:id => 1234)
135
- a = n.attributes
136
- a.should be_a_kind_of(Sequel::Dataset)
137
- a.sql.should == 'SELECT * FROM abc WHERE (node_id = 1234)'
138
- end
139
-
140
- it "should warn with a depreciation notice if :class option was used" do
141
- pending("write this spec")
142
- end
143
-
144
- it "should warn with a depreciation notice if :on option was used" do
145
- pending("write this spec")
146
- end
147
-
148
- end
@@ -1,80 +0,0 @@
1
- describe Sequel::Model, "table_exists?" do
2
-
3
- before(:each) do
4
- MODEL_DB.reset
5
- @model = Class.new(Sequel::Model(:items))
6
- end
7
-
8
- it "should get the table name and question the model's db if table_exists?" do
9
- @model.should_receive(:table_name).and_return(:items)
10
- @model.db.should_receive(:table_exists?)
11
- @model.table_exists?
12
- end
13
-
14
- end
15
-
16
- describe Sequel::Model, "create_table" do
17
-
18
- before(:each) do
19
- MODEL_DB.reset
20
- @model = Class.new(Sequel::Model(:items))
21
- end
22
-
23
- it "should get the create table SQL list from the db and execute it line by line" do
24
- #db.create_table_sql_list(table_name, *schema.create_info).each {|s| db << s}
25
- @model.should_receive(:table_name).and_return(:items)
26
- @model.schema.should_receive(:create_info)
27
- @model.db.should_receive(:create_table_sql_list)
28
- pending("Finish specing this")
29
- @model.create_table
30
- end
31
-
32
- end
33
-
34
- describe Sequel::Model, "drop_table" do
35
-
36
- before(:each) do
37
- MODEL_DB.reset
38
- @model = Class.new(Sequel::Model(:items))
39
- end
40
-
41
- it "should get the drop table SQL for the associated table and then execute the SQL." do
42
- @model.should_receive(:table_name).and_return(:items)
43
- @model.db.should_receive(:drop_table_sql).with(:items)
44
- @model.db.should_receive(:execute).and_return(:true)
45
- @model.drop_table
46
- end
47
-
48
- end
49
-
50
- describe Sequel::Model, "create_table!" do
51
-
52
- before(:each) do
53
- MODEL_DB.reset
54
- @model = Class.new(Sequel::Model(:items))
55
- end
56
-
57
- it "should drop table if it exists and then create the table" do
58
- @model.should_receive(:table_exists?).and_return(true)
59
- @model.should_receive(:drop_table).and_return(true)
60
- @model.should_receive(:create_table).and_return(true)
61
-
62
- @model.create_table!
63
- end
64
-
65
- end
66
-
67
- describe Sequel::Model, "recreate_table" do
68
-
69
- before(:each) do
70
- MODEL_DB.reset
71
- @model = Class.new(Sequel::Model(:items))
72
- end
73
-
74
- it "should raise a depreciation warning and then call create_table!" do
75
- @model.should_receive(:warn)
76
- @model.should_receive(:create_table!).and_return(true)
77
- @model.recreate_table
78
- end
79
-
80
- end
@@ -1,292 +0,0 @@
1
- describe Sequel::Model, "Validations" do
2
-
3
- before(:all) do
4
- class Person < Sequel::Model(:people)
5
- def columns
6
- [:id,:name,:first_name,:last_name,:middle_name,:initials,:age, :terms]
7
- end
8
- end
9
-
10
- class Smurf < Person
11
- end
12
-
13
- class Cow < Sequel::Model(:cows)
14
- def columns
15
- [:id, :name, :got_milk]
16
- end
17
- end
18
-
19
- class User < Sequel::Model(:users)
20
- def columns
21
- [:id, :username, :password]
22
- end
23
- end
24
-
25
- class Address < Sequel::Model(:addresses)
26
- def columns
27
- [:id, :zip_code]
28
- end
29
- end
30
- end
31
-
32
- it "should have a hook before validating" do
33
- class Person < Sequel::Model(:people)
34
- before_validation do
35
- self.name = "default name"
36
- end
37
- validations.clear
38
- validates_presence_of :name
39
- end
40
-
41
- @person = Person.new
42
- @person.valid?.should be_true
43
- end
44
-
45
- it "should include errors from other models" do
46
- pending("Waiting for Wayne's amazing associations!")
47
- end
48
-
49
- it "should validate the acceptance of a column" do
50
- class Cow < Sequel::Model(:cows)
51
- validations.clear
52
- validates_acceptance_of :got_milk
53
- end
54
-
55
- @cow = Cow.new
56
- @cow.valid?.should be_false
57
- @cow.errors.on(:got_milk).should == "must be accepted"
58
-
59
- @cow.got_milk = "true"
60
- @cow.valid?.should be_true
61
- end
62
-
63
- it "should validate the confirmation of a column" do
64
- class User < Sequel::Model(:users)
65
- def password_confirmation
66
- "test"
67
- end
68
-
69
- validations.clear
70
- validates_confirmation_of :password
71
- end
72
-
73
- @user = User.new
74
- @user.valid?.should be_false
75
- @user.errors.on(:password).should == "doesn't match confirmation"
76
-
77
- @user.password = "test"
78
- @user.valid?.should be_true
79
- end
80
-
81
- it "should validate each with logic" do
82
- class ZipCodeService; end
83
-
84
- class Address < Sequel::Model(:addresses)
85
- validations.clear
86
- validates_each :zip_code, :logic => lambda { errors.add(:zip_code, "is not valid") unless ZipCodeService.allows(zip_code) }
87
- end
88
-
89
- @address = Address.new :zip_code => "48108"
90
- ZipCodeService.should_receive(:allows).with("48108").and_return(false)
91
- @address.valid?.should be_false
92
- @address.errors.on(:zip_code).should == "is not valid"
93
-
94
- @address2 = Address.new :zip_code => "48104"
95
- ZipCodeService.should_receive(:allows).with("48104").and_return(true)
96
- @address2.valid?.should be_true
97
- end
98
-
99
- it "should validate format of column" do
100
- class Person < Sequel::Model(:people)
101
- validates_format_of :first_name, :with => /^[a-zA-Z]+$/
102
- end
103
-
104
- @person = Person.new :first_name => "Lancelot99"
105
- @person.valid?.should be_false
106
- @person = Person.new :first_name => "Anita"
107
- @person.valid?.should be_true
108
- end
109
-
110
- it "should allow for :with_exactly => /[a-zA-Z/, which wraps the supplied regex with ^<regex>$" do
111
- pending("TODO: Add this option to Validatable#validates_format_of")
112
- end
113
-
114
- it "should validate length of column" do
115
- class Person < Sequel::Model(:people)
116
- validations.clear
117
- validates_length_of :first_name, :maximum => 30
118
- validates_length_of :last_name, :minimum => 30
119
- validates_length_of :middle_name, :within => 1..5
120
- validates_length_of :initials, :is => 2
121
- end
122
-
123
- @person = Person.new(
124
- :first_name => "Anamethatiswaytofreakinglongandwayoverthirtycharacters",
125
- :last_name => "Alastnameunderthirtychars",
126
- :initials => "LGC",
127
- :middle_name => "danger"
128
- )
129
-
130
- @person.valid?.should be_false
131
- @person.errors.on(:first_name).should == "is invalid"
132
- @person.errors.on(:last_name).should == "is invalid"
133
- @person.errors.on(:initials).should == "is invalid"
134
- @person.errors.on(:middle_name).should == "is invalid"
135
-
136
- @person.first_name = "Lancelot"
137
- @person.last_name = "1234567890123456789012345678901"
138
- @person.initials = "LC"
139
- @person.middle_name = "Will"
140
- @person.valid?.should be_true
141
- end
142
-
143
- it "should validate numericality of column" do
144
- class Person < Sequel::Model(:people)
145
- validations.clear
146
- validates_numericality_of :age
147
- end
148
-
149
- @person = Person.new :age => "Twenty"
150
- @person.valid?.should be_false
151
- @person.errors.on(:age).should == "must be a number"
152
-
153
- @person.age = 20
154
- @person.valid?.should be_true
155
- end
156
-
157
- it "should validate the presence of a column" do
158
- class Cow < Sequel::Model(:cows)
159
- validations.clear
160
- validates_presence_of :name
161
- end
162
-
163
- @cow = Cow.new
164
- @cow.valid?.should be_false
165
- @cow.errors.on(:name).should == "can't be empty"
166
- @cow.errors.full_messages.first.should == "Name can't be empty"
167
-
168
- @cow.name = "Betsy"
169
- @cow.valid?.should be_true
170
- end
171
-
172
- it "should validate true for a column" do
173
- class Person < Sequel::Model(:people)
174
- validations.clear
175
- validates_true_for :first_name, :logic => lambda { first_name == "Alison" }
176
- end
177
-
178
- @person = Person.new :first_name => "Nina"
179
- @person.valid?.should be_false
180
- @person.errors.on(:first_name).should == "is invalid"
181
-
182
- @person.first_name = "Alison"
183
- @person.valid?.should be_true
184
- end
185
-
186
- it "should have a validates block that calls multple validations" do
187
- class Person < Sequel::Model(:people)
188
- validations.clear
189
- validates do
190
- format_of :first_name, :with => /^[a-zA-Z]+$/
191
- length_of :first_name, :maximum => 30
192
- end
193
- end
194
-
195
- Person.validations.length.should eql(2)
196
-
197
- @person = Person.new :first_name => "Lancelot99"
198
- @person.valid?.should be_false
199
-
200
- @person2 = Person.new :first_name => "Wayne"
201
- @person2.valid?.should be_true
202
- end
203
-
204
- it "should require and include the validatable gem" do
205
- Gem.loaded_specs["validatable"].should_not be_nil
206
- Sequel::Model.should respond_to(:validates_format_of) # validatable gem
207
- Sequel::Model.should respond_to(:validations) # Validations module
208
- end
209
-
210
- it "should description" do
211
- Sequel::Model.should_receive(:require).with("validatable").and_raise(LoadError)
212
- STDERR.should_receive(:puts)
213
- load File.join(File.dirname(__FILE__), "../../lib/sequel/model/validations.rb")
214
- end
215
-
216
- it "should allow 'longhand' validations direcly within the model." do
217
- lambda {
218
- class Person < Sequel::Model(:people)
219
- validations.clear
220
- validates_length_of :first_name, :maximum => 30
221
- end
222
- }.should_not raise_error
223
- Person.validations.length.should eql(1)
224
- end
225
-
226
- it "should validates do should allow shorthand method for every longhand validates_* method" do
227
- class Person
228
- validations.clear
229
- validates do
230
- format_of :first_name, :with => /^[a-zA-Z]+$/
231
- length_of :first_name, :maximum => 30
232
- presence_of :first_name
233
- numericality_of :age
234
- acceptance_of :terms
235
- confirmation_of :password
236
- true_for :first_name, :logic => lambda { first_name == "Alison" }
237
- #validates_each :last_name, :logic => lambda { errors.add(:zip_code, "is not valid") unless ZipCodeService.allows(zip_code) }
238
- #base
239
- end
240
-
241
- # Now check to make sure that each validation exists in the model's validations.
242
- end
243
- pending("finish this spec for each case")
244
- end
245
-
246
- it "should define a has_validations? method which returns true if the model has validations, false otherwise" do
247
- class Person < Sequel::Model(:people)
248
- validations.clear
249
- validates do
250
- format_of :first_name, :with => /\w+/
251
- length_of :first_name, :maximum => 30
252
- end
253
- end
254
-
255
- class Smurf < Person
256
- validations.clear
257
- end
258
-
259
- Person.should have_validations
260
- Smurf.should_not have_validations
261
- end
262
-
263
- end
264
-
265
- describe Sequel::Model, "validates" do
266
-
267
-
268
- before(:all) do
269
- class Person < Sequel::Model(:people)
270
- def columns
271
- [:id,:name,:first_name,:last_name,:middle_name,:initials,:age]
272
- end
273
- end
274
- end
275
-
276
- it "should runs the validations block on the model & store as :default when only a validations block is passed in" do
277
- pending
278
- end
279
-
280
- it "should store the block under the name passed in when both a name and a validations block are passed in" do
281
- pending
282
- end
283
-
284
- it "should return the stored validations block corresponding to the name given, if only a name is given (no block)" do
285
- pending
286
- end
287
-
288
- it "should return true or false based on if validations exist on the model if no arguments are given" do
289
- pending
290
- end
291
-
292
- end