sequel_model 0.5.0.2 → 3.8.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,69 +0,0 @@
1
- require File.join(File.dirname(__FILE__), "spec_helper")
2
-
3
- describe Sequel::Model, "table_exists?" do
4
-
5
- before(:each) do
6
- MODEL_DB.reset
7
- @model = Class.new(Sequel::Model(:items))
8
- end
9
-
10
- it "should get the table name and question the model's db if table_exists?" do
11
- @model.should_receive(:table_name).and_return(:items)
12
- @model.db.should_receive(:table_exists?)
13
- @model.table_exists?
14
- end
15
-
16
- end
17
-
18
- describe Sequel::Model, "create_table" do
19
-
20
- before(:each) do
21
- MODEL_DB.reset
22
- @model = Class.new(Sequel::Model) do
23
- set_dataset MODEL_DB[:items]
24
- set_schema do
25
- text :name
26
- float :price, :null => false
27
- end
28
- end
29
- end
30
-
31
- it "should get the create table SQL list from the db and execute it line by line" do
32
- @model.create_table
33
- MODEL_DB.sqls.should == ['CREATE TABLE items (name text, price float NOT NULL)']
34
- end
35
-
36
- end
37
-
38
- describe Sequel::Model, "drop_table" do
39
-
40
- before(:each) do
41
- MODEL_DB.reset
42
- @model = Class.new(Sequel::Model(:items))
43
- end
44
-
45
- it "should get the drop table SQL for the associated table and then execute the SQL." do
46
- @model.should_receive(:table_name).and_return(:items)
47
- @model.db.should_receive(:drop_table_sql).with(:items)
48
- @model.db.should_receive(:execute).and_return(:true)
49
- @model.drop_table
50
- end
51
-
52
- end
53
-
54
- describe Sequel::Model, "create_table!" do
55
-
56
- before(:each) do
57
- MODEL_DB.reset
58
- @model = Class.new(Sequel::Model(:items))
59
- end
60
-
61
- it "should drop table if it exists and then create the table" do
62
- @model.should_receive(:table_exists?).and_return(true)
63
- @model.should_receive(:drop_table).and_return(true)
64
- @model.should_receive(:create_table).and_return(true)
65
-
66
- @model.create_table!
67
- end
68
-
69
- end
@@ -1,5 +0,0 @@
1
- --colour
2
- --backtrace
3
- --format
4
- specdoc
5
- --diff
@@ -1,43 +0,0 @@
1
- require 'rubygems'
2
- unless Object.const_defined?('Sequel')
3
- require 'sequel_core'
4
- end
5
- require File.join(File.dirname(__FILE__), "../lib/sequel_model")
6
-
7
- class MockDataset < Sequel::Dataset
8
- def insert(*args)
9
- @db.execute insert_sql(*args)
10
- end
11
-
12
- def update(*args)
13
- @db.execute update_sql(*args)
14
- end
15
-
16
- def delete(*args)
17
- @db.execute delete_sql(*args)
18
- end
19
-
20
- def fetch_rows(sql)
21
- @db.execute(sql)
22
- yield({:id => 1, :x => 1})
23
- end
24
- end
25
-
26
- class MockDatabase < Sequel::Database
27
- attr_reader :sqls
28
-
29
- def execute(sql)
30
- @sqls ||= []
31
- @sqls << sql
32
- end
33
-
34
- def reset
35
- @sqls = []
36
- end
37
-
38
- def transaction; yield; end
39
-
40
- def dataset; MockDataset.new(self); end
41
- end
42
-
43
- Sequel::Model.db = MODEL_DB = MockDatabase.new
@@ -1,246 +0,0 @@
1
- require File.join(File.dirname(__FILE__), "spec_helper")
2
-
3
- describe Sequel::Model, "Validations" do
4
-
5
- before(:all) do
6
- class Person < Sequel::Model
7
- def columns
8
- [:id,:name,:first_name,:last_name,:middle_name,:initials,:age, :terms]
9
- end
10
- end
11
-
12
- class Smurf < Person
13
- end
14
-
15
- class Cow < Sequel::Model
16
- def columns
17
- [:id, :name, :got_milk]
18
- end
19
- end
20
-
21
- class User < Sequel::Model
22
- def columns
23
- [:id, :username, :password]
24
- end
25
- end
26
-
27
- class Address < Sequel::Model
28
- def columns
29
- [:id, :zip_code]
30
- end
31
- end
32
- end
33
-
34
- it "should validate the acceptance of a column" do
35
- class Cow < Sequel::Model
36
- validations.clear
37
- validates_acceptance_of :got_milk, :accept => 'blah', :allow_nil => false
38
- end
39
-
40
- @cow = Cow.new
41
- @cow.should_not be_valid
42
- @cow.errors.full_messages.should == ["got_milk is not accepted"]
43
-
44
- @cow.got_milk = "blah"
45
- @cow.should be_valid
46
- end
47
-
48
- it "should validate the confirmation of a column" do
49
- class User < Sequel::Model
50
- def password_confirmation
51
- "test"
52
- end
53
-
54
- validations.clear
55
- validates_confirmation_of :password
56
- end
57
-
58
- @user = User.new
59
- @user.should_not be_valid
60
- @user.errors.full_messages.should == ["password is not confirmed"]
61
-
62
- @user.password = "test"
63
- @user.should be_valid
64
- end
65
-
66
- it "should validate format of column" do
67
- class Person < Sequel::Model
68
- validates_format_of :first_name, :with => /^[a-zA-Z]+$/
69
- end
70
-
71
- @person = Person.new :first_name => "Lancelot99"
72
- @person.valid?.should be_false
73
- @person = Person.new :first_name => "Anita"
74
- @person.valid?.should be_true
75
- end
76
-
77
- # it "should allow for :with_exactly => /[a-zA-Z]/, which wraps the supplied regex with ^<regex>$" do
78
- # pending("TODO: Add this option to Validatable#validates_format_of")
79
- # end
80
-
81
- it "should validate length of column" do
82
- class Person < Sequel::Model
83
- validations.clear
84
- validates_length_of :first_name, :maximum => 30
85
- validates_length_of :last_name, :minimum => 30
86
- validates_length_of :middle_name, :within => 1..5
87
- validates_length_of :initials, :is => 2
88
- end
89
-
90
- @person = Person.new(
91
- :first_name => "Anamethatiswaytofreakinglongandwayoverthirtycharacters",
92
- :last_name => "Alastnameunderthirtychars",
93
- :initials => "LGC",
94
- :middle_name => "danger"
95
- )
96
-
97
- @person.should_not be_valid
98
- @person.errors.full_messages.size.should == 4
99
- @person.errors.full_messages.should include(
100
- 'first_name is too long',
101
- 'last_name is too short',
102
- 'middle_name is the wrong length',
103
- 'initials is the wrong length'
104
- )
105
-
106
- @person.first_name = "Lancelot"
107
- @person.last_name = "1234567890123456789012345678901"
108
- @person.initials = "LC"
109
- @person.middle_name = "Will"
110
- @person.should be_valid
111
- end
112
-
113
- it "should validate numericality of column" do
114
- class Person < Sequel::Model
115
- validations.clear
116
- validates_numericality_of :age
117
- end
118
-
119
- @person = Person.new :age => "Twenty"
120
- @person.should_not be_valid
121
- @person.errors.full_messages.should == ['age is not a number']
122
-
123
- @person.age = 20
124
- @person.should be_valid
125
- end
126
-
127
- it "should validate the presence of a column" do
128
- class Cow < Sequel::Model
129
- validations.clear
130
- validates_presence_of :name
131
- end
132
-
133
- @cow = Cow.new
134
- @cow.should_not be_valid
135
- @cow.errors.full_messages.should == ['name is not present']
136
-
137
- @cow.name = "Betsy"
138
- @cow.should be_valid
139
- end
140
-
141
- it "should have a validates block that contains multiple validations" do
142
- class Person < Sequel::Model
143
- validations.clear
144
- validates do
145
- format_of :first_name, :with => /^[a-zA-Z]+$/
146
- length_of :first_name, :maximum => 30
147
- end
148
- end
149
-
150
- Person.validations[:first_name].size.should == 2
151
-
152
- @person = Person.new :first_name => "Lancelot99"
153
- @person.valid?.should be_false
154
-
155
- @person2 = Person.new :first_name => "Wayne"
156
- @person2.valid?.should be_true
157
- end
158
-
159
- it "should allow 'longhand' validations direcly within the model." do
160
- lambda {
161
- class Person < Sequel::Model
162
- validations.clear
163
- validates_length_of :first_name, :maximum => 30
164
- end
165
- }.should_not raise_error
166
- Person.validations.length.should eql(1)
167
- end
168
-
169
- it "should define a has_validations? method which returns true if the model has validations, false otherwise" do
170
- class Person < Sequel::Model
171
- validations.clear
172
- validates do
173
- format_of :first_name, :with => /\w+/
174
- length_of :first_name, :maximum => 30
175
- end
176
- end
177
-
178
- class Smurf < Person
179
- validations.clear
180
- end
181
-
182
- Person.should have_validations
183
- Smurf.should_not have_validations
184
- end
185
-
186
- it "should validate correctly instances initialized with string keys" do
187
- class Can < Sequel::Model
188
- def columns; [:id, :name]; end
189
-
190
- validates_length_of :name, :minimum => 4
191
- end
192
-
193
- Can.new('name' => 'ab').should_not be_valid
194
- Can.new('name' => 'abcd').should be_valid
195
- end
196
-
197
- end
198
-
199
- describe "Model#save!" do
200
- setup do
201
- @c = Class.new(Sequel::Model(:people)) do
202
- def columns; [:id]; end
203
-
204
- validates_each :id do |o, a, v|
205
- o.errors[a] << 'blah' unless v == 5
206
- end
207
- end
208
- @m = @c.load(:id => 4)
209
- MODEL_DB.reset
210
- end
211
-
212
- specify "should save regardless of validations" do
213
- @m.should_not be_valid
214
- @m.save!
215
- MODEL_DB.sqls.should == ['UPDATE people SET id = 4 WHERE (id = 4)']
216
- end
217
- end
218
-
219
- describe "Model#save" do
220
- setup do
221
- @c = Class.new(Sequel::Model(:people)) do
222
- def columns; [:id]; end
223
-
224
- validates_each :id do |o, a, v|
225
- o.errors[a] << 'blah' unless v == 5
226
- end
227
- end
228
- @m = @c.load(:id => 4)
229
- MODEL_DB.reset
230
- end
231
-
232
- specify "should save only if validations pass" do
233
- @m.should_not be_valid
234
- @m.save
235
- MODEL_DB.sqls.should be_empty
236
-
237
- @m.id = 5
238
- @m.should be_valid
239
- @m.save.should_not be_false
240
- MODEL_DB.sqls.should == ['UPDATE people SET id = 5 WHERE (id = 5)']
241
- end
242
-
243
- specify "should return false if validations fail" do
244
- @m.save.should == false
245
- end
246
- end