sequel 1.3 → 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +127 -0
- data/COPYING +1 -0
- data/README +5 -4
- data/Rakefile +78 -25
- data/lib/sequel.rb +1 -2
- data/lib/sequel_model.rb +324 -0
- data/lib/sequel_model/associations.rb +351 -0
- data/lib/sequel_model/base.rb +120 -0
- data/lib/sequel_model/caching.rb +42 -0
- data/lib/sequel_model/eager_loading.rb +169 -0
- data/lib/sequel_model/hooks.rb +55 -0
- data/lib/sequel_model/plugins.rb +47 -0
- data/lib/sequel_model/pretty_table.rb +73 -0
- data/lib/sequel_model/record.rb +336 -0
- data/lib/sequel_model/schema.rb +48 -0
- data/lib/sequel_model/validations.rb +15 -0
- data/spec/associations_spec.rb +712 -0
- data/spec/base_spec.rb +239 -0
- data/spec/caching_spec.rb +150 -0
- data/spec/deprecated_relations_spec.rb +153 -0
- data/spec/eager_loading_spec.rb +260 -0
- data/spec/hooks_spec.rb +269 -0
- data/spec/model_spec.rb +543 -0
- data/spec/plugins_spec.rb +74 -0
- data/spec/rcov.opts +4 -0
- data/spec/record_spec.rb +593 -0
- data/spec/schema_spec.rb +69 -0
- data/spec/spec.opts +5 -0
- data/spec/spec_helper.rb +43 -0
- data/spec/validations_spec.rb +246 -0
- metadata +90 -56
data/spec/schema_spec.rb
ADDED
@@ -0,0 +1,69 @@
|
|
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
|
data/spec/spec.opts
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,43 @@
|
|
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
|
@@ -0,0 +1,246 @@
|
|
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
|
metadata
CHANGED
@@ -1,74 +1,108 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.9.4
|
3
|
+
specification_version: 1
|
2
4
|
name: sequel
|
3
5
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
5
|
-
|
6
|
-
|
7
|
-
|
6
|
+
version: 1.4.0
|
7
|
+
date: 2008-04-08 00:00:00 -07:00
|
8
|
+
summary: "The Database Toolkit for Ruby: Model Classes"
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: code@jeremyevans.net
|
12
|
+
homepage: http://sequel.rubyforge.org
|
13
|
+
rubyforge_project: sequel
|
14
|
+
description: "The Database Toolkit for Ruby: Model Classes"
|
8
15
|
autorequire:
|
16
|
+
default_executable:
|
9
17
|
bindir: bin
|
10
|
-
|
18
|
+
has_rdoc: true
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.8.4
|
24
|
+
version:
|
25
|
+
platform: ruby
|
26
|
+
signing_key:
|
27
|
+
cert_chain:
|
28
|
+
post_install_message:
|
29
|
+
authors:
|
30
|
+
- Jeremy Evans
|
31
|
+
files:
|
32
|
+
- COPYING
|
33
|
+
- README
|
34
|
+
- Rakefile
|
35
|
+
- doc/rdoc
|
36
|
+
- spec/associations_spec.rb
|
37
|
+
- spec/base_spec.rb
|
38
|
+
- spec/caching_spec.rb
|
39
|
+
- spec/deprecated_relations_spec.rb
|
40
|
+
- spec/eager_loading_spec.rb
|
41
|
+
- spec/hooks_spec.rb
|
42
|
+
- spec/model_spec.rb
|
43
|
+
- spec/plugins_spec.rb
|
44
|
+
- spec/rcov.opts
|
45
|
+
- spec/record_spec.rb
|
46
|
+
- spec/schema_spec.rb
|
47
|
+
- spec/spec.opts
|
48
|
+
- spec/spec_helper.rb
|
49
|
+
- spec/validations_spec.rb
|
50
|
+
- lib/sequel.rb
|
51
|
+
- lib/sequel_model.rb
|
52
|
+
- lib/sequel_model
|
53
|
+
- lib/sequel_model/associations.rb
|
54
|
+
- lib/sequel_model/base.rb
|
55
|
+
- lib/sequel_model/caching.rb
|
56
|
+
- lib/sequel_model/eager_loading.rb
|
57
|
+
- lib/sequel_model/hooks.rb
|
58
|
+
- lib/sequel_model/plugins.rb
|
59
|
+
- lib/sequel_model/pretty_table.rb
|
60
|
+
- lib/sequel_model/record.rb
|
61
|
+
- lib/sequel_model/schema.rb
|
62
|
+
- lib/sequel_model/validations.rb
|
63
|
+
- CHANGELOG
|
64
|
+
test_files: []
|
65
|
+
|
66
|
+
rdoc_options:
|
67
|
+
- --quiet
|
68
|
+
- --title
|
69
|
+
- "Sequel: The Database Toolkit for Ruby"
|
70
|
+
- --opname
|
71
|
+
- index.html
|
72
|
+
- --line-numbers
|
73
|
+
- --main
|
74
|
+
- README
|
75
|
+
- --inline-source
|
76
|
+
- --exclude
|
77
|
+
- ^(examples|extras)/
|
78
|
+
- --exclude
|
79
|
+
- lib/sequel_model.rb
|
80
|
+
extra_rdoc_files:
|
81
|
+
- README
|
82
|
+
- CHANGELOG
|
83
|
+
- COPYING
|
84
|
+
executables: []
|
85
|
+
|
86
|
+
extensions: []
|
87
|
+
|
88
|
+
requirements: []
|
11
89
|
|
12
|
-
date: 2008-03-08 00:00:00 +02:00
|
13
|
-
default_executable:
|
14
90
|
dependencies:
|
15
91
|
- !ruby/object:Gem::Dependency
|
16
|
-
name:
|
92
|
+
name: assistance
|
17
93
|
version_requirement:
|
18
|
-
version_requirements: !ruby/object:Gem::Requirement
|
94
|
+
version_requirements: !ruby/object:Gem::Version::Requirement
|
19
95
|
requirements:
|
20
96
|
- - ">="
|
21
97
|
- !ruby/object:Gem::Version
|
22
|
-
version:
|
98
|
+
version: 0.1.2
|
23
99
|
version:
|
24
100
|
- !ruby/object:Gem::Dependency
|
25
|
-
name:
|
101
|
+
name: sequel_core
|
26
102
|
version_requirement:
|
27
|
-
version_requirements: !ruby/object:Gem::Requirement
|
103
|
+
version_requirements: !ruby/object:Gem::Version::Requirement
|
28
104
|
requirements:
|
29
|
-
- - "
|
105
|
+
- - "="
|
30
106
|
- !ruby/object:Gem::Version
|
31
|
-
version:
|
107
|
+
version: 1.4.0
|
32
108
|
version:
|
33
|
-
description: Database access for Ruby
|
34
|
-
email: ciconia@gmail.com
|
35
|
-
executables: []
|
36
|
-
|
37
|
-
extensions: []
|
38
|
-
|
39
|
-
extra_rdoc_files:
|
40
|
-
- README
|
41
|
-
- COPYING
|
42
|
-
files:
|
43
|
-
- Rakefile
|
44
|
-
- lib/sequel.rb
|
45
|
-
- README
|
46
|
-
- COPYING
|
47
|
-
has_rdoc: true
|
48
|
-
homepage: http://sequel.rubyforge.org
|
49
|
-
post_install_message:
|
50
|
-
rdoc_options: []
|
51
|
-
|
52
|
-
require_paths:
|
53
|
-
- lib
|
54
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
-
requirements:
|
56
|
-
- - ">="
|
57
|
-
- !ruby/object:Gem::Version
|
58
|
-
version: 1.8.4
|
59
|
-
version:
|
60
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
-
requirements:
|
62
|
-
- - ">="
|
63
|
-
- !ruby/object:Gem::Version
|
64
|
-
version: "0"
|
65
|
-
version:
|
66
|
-
requirements: []
|
67
|
-
|
68
|
-
rubyforge_project: sequel
|
69
|
-
rubygems_version: 1.0.1
|
70
|
-
signing_key:
|
71
|
-
specification_version: 2
|
72
|
-
summary: Database access for Ruby
|
73
|
-
test_files: []
|
74
|
-
|