sequel 3.24.1 → 3.25.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -60,3 +60,32 @@ class SchemaDummyDatabase < Sequel::Database
60
60
  @sqls << sql
61
61
  end
62
62
  end
63
+
64
+ class DummyDataset < Sequel::Dataset
65
+ def first
66
+ raise if @opts[:from] == [:a]
67
+ true
68
+ end
69
+ end
70
+
71
+ class DummyDatabase < Sequel::Database
72
+ attr_reader :sqls
73
+
74
+ def execute(sql, opts={})
75
+ @sqls ||= []
76
+ @sqls << sql
77
+ end
78
+
79
+ def transaction; yield; end
80
+
81
+ def dataset
82
+ DummyDataset.new(self)
83
+ end
84
+ end
85
+
86
+ class Dummy2Database < Sequel::Database
87
+ attr_reader :sql
88
+ def execute(sql); @sql = sql; end
89
+ def transaction; yield; end
90
+ end
91
+
@@ -89,4 +89,35 @@ describe "Sequel::Plugins::AssociationPks" do
89
89
  @db.sqls[3].should == "COMMIT"
90
90
  end
91
91
 
92
+ specify "should automatically convert keys to numbers if the primary key is an integer for one_to_many associations" do
93
+ @Album.db_schema[:id][:type] = :integer
94
+ @Artist.load(:id=>1).album_pks = %w'1 2'
95
+ @db.sqls.should == ["UPDATE albums SET artist_id = 1 WHERE (id IN (1, 2))",
96
+ "UPDATE albums SET artist_id = NULL WHERE ((albums.artist_id = 1) AND (id NOT IN (1, 2)))"]
97
+ end
98
+
99
+ specify "should not automatically convert keys if the primary key is not an integer for many_to_many associations" do
100
+ @Album.db_schema[:id][:type] = :string
101
+ @Artist.load(:id=>1).album_pks = %w'1 2'
102
+ @db.sqls.should == ["UPDATE albums SET artist_id = 1 WHERE (id IN ('1', '2'))",
103
+ "UPDATE albums SET artist_id = NULL WHERE ((albums.artist_id = 1) AND (id NOT IN ('1', '2')))"]
104
+ end
105
+
106
+ specify "should automatically convert keys to numbers if the primary key is an integer for one_to_many associations" do
107
+ @Tag.db_schema[:id][:type] = :integer
108
+ @Album.load(:id=>2).tag_pks = %w'1 3'
109
+ @db.sqls[0].should == "DELETE FROM albums_tags WHERE ((album_id = 2) AND (tag_id NOT IN (1, 3)))"
110
+ @db.sqls[1].should =~ /INSERT INTO albums_tags \((album_id, tag_id|tag_id, album_id)\) VALUES \((2, 1|1, 2)\)/
111
+ @db.sqls.length.should == 2
112
+ end
113
+
114
+ specify "should not automatically convert keys to numbers if the primary key is an integer for many_to_many associations" do
115
+ @Tag.db_schema[:id][:type] = :string
116
+ @Album.load(:id=>2).tag_pks = %w'1 3'
117
+ @db.sqls[0].should == "DELETE FROM albums_tags WHERE ((album_id = 2) AND (tag_id NOT IN ('1', '3')))"
118
+ @db.sqls[1].should =~ /INSERT INTO albums_tags \((album_id, tag_id|tag_id, album_id)\) VALUES \((2, '1'|'1', 2)\)/
119
+ @db.sqls[2].should =~ /INSERT INTO albums_tags \((album_id, tag_id|tag_id, album_id)\) VALUES \((2, '3'|'3', 2)\)/
120
+ @db.sqls.length.should == 3
121
+ end
122
+
92
123
  end
@@ -23,22 +23,19 @@ describe Sequel::Model, "dataset & schema" do
23
23
  end
24
24
 
25
25
  describe Sequel::Model, "table_exists?" do
26
-
27
- before(:each) do
26
+ before do
28
27
  MODEL_DB.reset
29
28
  @model = Class.new(Sequel::Model(:items))
30
29
  end
31
30
 
32
31
  it "should get the table name and question the model's db if table_exists?" do
33
- @model.should_receive(:table_name).and_return(:items)
34
- @model.db.should_receive(:table_exists?)
35
- @model.table_exists?
32
+ @model.db.should_receive(:table_exists?).and_return(false)
33
+ @model.table_exists?.should == false
36
34
  end
37
35
  end
38
36
 
39
37
  describe Sequel::Model, "create_table and schema" do
40
-
41
- before(:each) do
38
+ before do
42
39
  MODEL_DB.reset
43
40
  @model = Class.new(Sequel::Model) do
44
41
  set_schema(:items) do
@@ -84,19 +81,15 @@ describe Sequel::Model, "create_table and schema" do
84
81
  end
85
82
 
86
83
  describe Sequel::Model, "drop_table" do
87
-
88
- before(:each) do
89
- MODEL_DB.reset
84
+ before do
90
85
  @model = Class.new(Sequel::Model(:items))
86
+ MODEL_DB.reset
91
87
  end
92
88
 
93
89
  it "should get the drop table SQL for the associated table and then execute the SQL." do
94
- @model.should_receive(:table_name).and_return(:items)
95
- @model.db.should_receive(:drop_table_sql).with(:items)
96
- @model.db.should_receive(:execute).and_return(:true)
97
90
  @model.drop_table
91
+ MODEL_DB.sqls.should == ['DROP TABLE items']
98
92
  end
99
-
100
93
  end
101
94
 
102
95
  describe Sequel::Model, "create_table!" do
@@ -106,9 +99,8 @@ describe Sequel::Model, "create_table!" do
106
99
  end
107
100
 
108
101
  it "should drop table if it exists and then create the table" do
109
- @model.should_receive(:drop_table).and_return(true)
110
- @model.should_receive(:create_table).and_return(true)
111
102
  @model.create_table!
103
+ MODEL_DB.sqls.should == ['DROP TABLE items', 'CREATE TABLE items ()']
112
104
  end
113
105
  end
114
106
 
@@ -120,13 +112,13 @@ describe Sequel::Model, "create_table?" do
120
112
 
121
113
  it "should not create the table if it already exists" do
122
114
  @model.should_receive(:table_exists?).and_return(true)
123
- @model.should_not_receive(:create_table)
124
- @model.create_table?.should == nil
115
+ @model.create_table?
116
+ MODEL_DB.sqls.should == []
125
117
  end
126
118
 
127
119
  it "should create the table if it doesn't exist" do
128
120
  @model.should_receive(:table_exists?).and_return(false)
129
- @model.should_receive(:create_table).and_return(3)
130
- @model.create_table?.should == 3
121
+ @model.create_table?
122
+ MODEL_DB.sqls.should == ['CREATE TABLE items ()']
131
123
  end
132
124
  end
@@ -632,6 +632,23 @@ describe "Sequel::Dataset main SQL methods" do
632
632
  @ds.filter(:a=>20).or(:b=>15).all.should == [{:a=>20, :b=>30}]
633
633
  @ds.filter(:a=>10).or(:b=>15).all.should == []
634
634
  end
635
+
636
+ it "#select_group should work correctly" do
637
+ @ds.unordered!
638
+ @ds.select_group(:a).all.should == []
639
+ @ds.insert(20, 30)
640
+ @ds.select_group(:a).all.should == [{:a=>20}]
641
+ @ds.select_group(:b).all.should == [{:b=>30}]
642
+ @ds.insert(20, 40)
643
+ @ds.select_group(:a).all.should == [{:a=>20}]
644
+ @ds.order(:b).select_group(:b).all.should == [{:b=>30}, {:b=>40}]
645
+ end
646
+
647
+ it "#select_group should work correctly when aliasing" do
648
+ @ds.unordered!
649
+ @ds.insert(20, 30)
650
+ @ds.select_group(:b___c).all.should == [{:c=>30}]
651
+ end
635
652
 
636
653
  it "#having should work correctly" do
637
654
  @ds.unordered!
@@ -95,6 +95,13 @@ describe "Prepared Statements and Bound Arguments" do
95
95
  @ds.order(:id).map(:number).should == [10, 20]
96
96
  end
97
97
 
98
+ specify "should support bound variables with NULL values" do
99
+ @ds.delete
100
+ @ds.call(:insert, {:n=>nil}, :number=>@ds.ba(:$n))
101
+ @ds.count.should == 1
102
+ @ds.map(:number).should == [nil]
103
+ end
104
+
98
105
  specify "should have insert return primary key value when using bound arguments" do
99
106
  @ds.call(:insert, {:n=>20}, :number=>@ds.ba(:$n)).should == 2
100
107
  @ds.filter(:id=>2).first[:number].should == 20
@@ -172,6 +179,14 @@ describe "Prepared Statements and Bound Arguments" do
172
179
  @ds.order(:id).map(:number).should == [10, 20]
173
180
  end
174
181
 
182
+ specify "should support prepared statements with NULL values" do
183
+ @ds.delete
184
+ @ds.prepare(:insert, :insert_n, :number=>@ds.ba(:$n))
185
+ INTEGRATION_DB.call(:insert_n, :n=>nil)
186
+ @ds.count.should == 1
187
+ @ds.map(:number).should == [nil]
188
+ end
189
+
175
190
  specify "should have insert return primary key value when using prepared statements" do
176
191
  @ds.prepare(:insert, :insert_n, :number=>@ds.ba(:$n))
177
192
  INTEGRATION_DB.call(:insert_n, :n=>20).should == 2
@@ -19,6 +19,27 @@ describe "Sequel::Model()" do
19
19
  c.table_name.should == :blah
20
20
  end
21
21
 
22
+ it "should return a model subclass with a dataset with the default database and given table name if given an SQL::Identifier" do
23
+ c = Sequel::Model(:blah.identifier)
24
+ c.superclass.should == Sequel::Model
25
+ c.db.should == @db
26
+ c.table_name.should == :blah.identifier
27
+ end
28
+
29
+ it "should return a model subclass with a dataset with the default database and given table name if given an SQL::QualifiedIdentifier" do
30
+ c = Sequel::Model(:blah.qualify(:boo))
31
+ c.superclass.should == Sequel::Model
32
+ c.db.should == @db
33
+ c.table_name.should == :blah.qualify(:boo)
34
+ end
35
+
36
+ it "should return a model subclass with a dataset with the default database and given table name if given an SQL::AliasedExpression" do
37
+ c = Sequel::Model(:blah.as(:boo))
38
+ c.superclass.should == Sequel::Model
39
+ c.db.should == @db
40
+ c.table_name.should == :boo
41
+ end
42
+
22
43
  it "should return a model subclass associated to the given database if given a database" do
23
44
  db = Sequel::Database.new
24
45
  c = Sequel::Model(db)
@@ -43,6 +64,27 @@ describe "Sequel::Model()" do
43
64
  end.should_not raise_error
44
65
  end
45
66
 
67
+ it "should work without raising an exception with an SQL::Identifier " do
68
+ proc do
69
+ class ::Album < Sequel::Model(:table.identifier); end
70
+ class ::Album < Sequel::Model(:table.identifier); end
71
+ end.should_not raise_error
72
+ end
73
+
74
+ it "should work without raising an exception with an SQL::QualifiedIdentifier " do
75
+ proc do
76
+ class ::Album < Sequel::Model(:table.qualify(:schema)); end
77
+ class ::Album < Sequel::Model(:table.qualify(:schema)); end
78
+ end.should_not raise_error
79
+ end
80
+
81
+ it "should work without raising an exception with an SQL::AliasedExpression" do
82
+ proc do
83
+ class ::Album < Sequel::Model(:table.as(:alias)); end
84
+ class ::Album < Sequel::Model(:table.as(:alias)); end
85
+ end.should_not raise_error
86
+ end
87
+
46
88
  it "should work without raising an exception with a database" do
47
89
  proc do
48
90
  class ::Album < Sequel::Model(@db); end
@@ -112,6 +154,24 @@ describe Sequel::Model, "dataset & schema" do
112
154
  @model.table_name.should == :foo
113
155
  end
114
156
 
157
+ it "set_dataset should take an SQL::Identifier" do
158
+ @model.db = MODEL_DB
159
+ @model.set_dataset(:foo.identifier)
160
+ @model.table_name.should == :foo.identifier
161
+ end
162
+
163
+ it "set_dataset should take an SQL::QualifiedIdentifier" do
164
+ @model.db = MODEL_DB
165
+ @model.set_dataset(:foo.qualify(:bar))
166
+ @model.table_name.should == :foo.qualify(:bar)
167
+ end
168
+
169
+ it "set_dataset should take an SQL::AliasedExpression" do
170
+ @model.db = MODEL_DB
171
+ @model.set_dataset(:foo.as(:bar))
172
+ @model.table_name.should == :bar
173
+ end
174
+
115
175
  it "table_name should respect table aliases" do
116
176
  @model.set_dataset(:foo___x)
117
177
  @model.table_name.should == :x
@@ -233,4 +233,21 @@ describe Sequel::Model, ".plugin" do
233
233
  end
234
234
  lambda{@c.plugin m}.should_not raise_error
235
235
  end
236
+
237
+ it "should not raise an error if plugin submodule names exist higher up in the namespace hierarchy" do
238
+ class ::ClassMethods; end
239
+ @c.plugin(m = Module.new)
240
+ Object.send(:remove_const, :ClassMethods)
241
+ @c.plugins.should include(m)
242
+
243
+ class ::InstanceMethods; end
244
+ @c.plugin(m = Module.new)
245
+ Object.send(:remove_const, :InstanceMethods)
246
+ @c.plugins.should include(m)
247
+
248
+ class ::DatasetMethods; end
249
+ @c.plugin(m = Module.new)
250
+ Object.send(:remove_const, :DatasetMethods)
251
+ @c.plugins.should include(m)
252
+ end
236
253
  end
@@ -61,6 +61,17 @@ describe Sequel::Model::Errors do
61
61
  msgs.should include('blow blieuh', 'blow blich', 'blay bliu')
62
62
  end
63
63
 
64
+ specify "should not add column names for LiteralStrings" do
65
+ @errors.full_messages.should == []
66
+
67
+ @errors[:blow] << 'blieuh'
68
+ @errors[:blow] << 'blich'.lit
69
+ @errors[:blay] << 'bliu'
70
+ msgs = @errors.full_messages
71
+ msgs.size.should == 3
72
+ msgs.should include('blow blieuh', 'blich', 'blay bliu')
73
+ end
74
+
64
75
  specify "should return the number of error messages using #count" do
65
76
  @errors.count.should == 0
66
77
  @errors.add(:a, 'b')
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sequel
3
3
  version: !ruby/object:Gem::Version
4
- hash: 101
4
+ hash: 99
5
5
  prerelease: false
6
6
  segments:
7
7
  - 3
8
- - 24
9
- - 1
10
- version: 3.24.1
8
+ - 25
9
+ - 0
10
+ version: 3.25.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jeremy Evans
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-06-03 00:00:00 -07:00
18
+ date: 2011-07-01 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -88,6 +88,7 @@ extra_rdoc_files:
88
88
  - doc/release_notes/3.22.0.txt
89
89
  - doc/release_notes/3.23.0.txt
90
90
  - doc/release_notes/3.24.0.txt
91
+ - doc/release_notes/3.25.0.txt
91
92
  files:
92
93
  - MIT-LICENSE
93
94
  - CHANGELOG
@@ -143,6 +144,7 @@ files:
143
144
  - doc/release_notes/3.22.0.txt
144
145
  - doc/release_notes/3.23.0.txt
145
146
  - doc/release_notes/3.24.0.txt
147
+ - doc/release_notes/3.25.0.txt
146
148
  - doc/sharding.rdoc
147
149
  - doc/sql.rdoc
148
150
  - doc/virtual_rows.rdoc