sequel 0.3.2 → 0.3.3
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +16 -0
- data/Rakefile +1 -1
- data/lib/sequel.rb +1 -1
- data/lib/sequel/array_keys.rb +2 -2
- data/lib/sequel/connection_pool.rb +1 -1
- data/lib/sequel/core_ext.rb +53 -12
- data/lib/sequel/database.rb +1 -1
- data/lib/sequel/dataset/sql.rb +11 -11
- data/lib/sequel/mysql.rb +9 -9
- data/lib/sequel/postgres.rb +2 -2
- data/lib/sequel/schema/schema_sql.rb +4 -4
- data/lib/sequel/sqlite.rb +3 -3
- data/lib/sequel/worker.rb +44 -0
- data/spec/adapters/mysql_spec.rb +1 -1
- data/spec/adapters/oracle_spec.rb +130 -0
- data/spec/array_keys_spec.rb +6 -6
- data/spec/connection_pool_spec.rb +1 -1
- data/spec/core_ext_spec.rb +59 -1
- data/spec/database_spec.rb +17 -13
- data/spec/dataset_spec.rb +32 -32
- data/spec/model_spec.rb +14 -14
- data/spec/schema_spec.rb +26 -26
- data/spec/worker_spec.rb +63 -0
- metadata +5 -2
data/spec/array_keys_spec.rb
CHANGED
@@ -53,9 +53,9 @@ context "An array with symbol keys" do
|
|
53
53
|
@a.to_h.should == {:a => 1, :b => 2, :c => 3}
|
54
54
|
end
|
55
55
|
|
56
|
-
specify "should provide #
|
57
|
-
@a.
|
58
|
-
@a.
|
56
|
+
specify "should provide #columns as alias to #keys" do
|
57
|
+
@a.columns.should == [:a, :b, :c]
|
58
|
+
@a.columns = [:x, :y, :z]
|
59
59
|
|
60
60
|
@a[:x].should == 1
|
61
61
|
end
|
@@ -305,9 +305,9 @@ context "An array with string keys" do
|
|
305
305
|
@a.to_h.should == {:a => 1, :b => 2, :c => 3}
|
306
306
|
end
|
307
307
|
|
308
|
-
specify "should provide #
|
309
|
-
@a.
|
310
|
-
@a.
|
308
|
+
specify "should provide #columns as alias to #keys" do
|
309
|
+
@a.columns.should == ['a', 'b', 'c']
|
310
|
+
@a.columns = [:x, :y, :z]
|
311
311
|
|
312
312
|
@a[:x].should == 1
|
313
313
|
end
|
@@ -126,7 +126,7 @@ end
|
|
126
126
|
context "A connection pool with a max size of 1" do
|
127
127
|
setup do
|
128
128
|
@invoked_count = 0
|
129
|
-
@pool = Sequel::ConnectionPool.new(1) {@invoked_count += 1;'herro'}
|
129
|
+
@pool = Sequel::ConnectionPool.new(1) {@invoked_count += 1; 'herro'}
|
130
130
|
end
|
131
131
|
|
132
132
|
specify "should let only one thread access the connection at any time" do
|
data/spec/core_ext_spec.rb
CHANGED
@@ -152,7 +152,7 @@ context "Symbol#to_column_name" do
|
|
152
152
|
end
|
153
153
|
end
|
154
154
|
|
155
|
-
context "
|
155
|
+
context "ColumnCompositionMethods#column_title" do
|
156
156
|
specify "should return the column name for non aliased columns" do
|
157
157
|
:xyz.column_title.should == 'xyz'
|
158
158
|
:abc__xyz.column_title.should == 'xyz'
|
@@ -205,4 +205,62 @@ context "Symbol" do
|
|
205
205
|
ds = db[:t]
|
206
206
|
ds.select(:COUNT['1']).sql.should == "SELECT COUNT(1) FROM t"
|
207
207
|
end
|
208
|
+
end
|
209
|
+
|
210
|
+
context "Range#interval" do
|
211
|
+
specify "should return the interval between the beginning and end of the range" do
|
212
|
+
(1..10).interval.should == 9
|
213
|
+
|
214
|
+
r = rand(100000) + 10
|
215
|
+
t1 = Time.now; t2 = t1 + r
|
216
|
+
(t1..t2).interval.should == r
|
217
|
+
end
|
218
|
+
end
|
219
|
+
|
220
|
+
context "Numeric extensions" do
|
221
|
+
setup do
|
222
|
+
Sequel::NumericExtensions.enable
|
223
|
+
end
|
224
|
+
|
225
|
+
specify "should support conversion of minutes to seconds" do
|
226
|
+
1.minute.should == 60
|
227
|
+
3.minutes.should == 180
|
228
|
+
end
|
229
|
+
|
230
|
+
specify "should support conversion of hours to seconds" do
|
231
|
+
1.hour.should == 3600
|
232
|
+
3.hours.should == 3600 * 3
|
233
|
+
end
|
234
|
+
|
235
|
+
specify "should support conversion of days to seconds" do
|
236
|
+
1.day.should == 86400
|
237
|
+
3.days.should == 86400 * 3
|
238
|
+
end
|
239
|
+
|
240
|
+
specify "should support conversion of weeks to seconds" do
|
241
|
+
1.week.should == 86400 * 7
|
242
|
+
3.weeks.should == 86400 * 7 * 3
|
243
|
+
end
|
244
|
+
|
245
|
+
specify "should provide #ago functionality" do
|
246
|
+
t1 = Time.now
|
247
|
+
t2 = 1.day.ago
|
248
|
+
t1.should > t2
|
249
|
+
((t1 - t2).to_i - 86400).abs.should < 2
|
250
|
+
|
251
|
+
t1 = Time.now
|
252
|
+
t2 = 1.day.before(t1)
|
253
|
+
t2.should == t1 - 1.day
|
254
|
+
end
|
255
|
+
|
256
|
+
specify "should provide #from_now functionality" do
|
257
|
+
t1 = Time.now
|
258
|
+
t2 = 1.day.from_now
|
259
|
+
t1.should < t2
|
260
|
+
((t2 - t1).to_i - 86400).abs.should < 2
|
261
|
+
|
262
|
+
t1 = Time.now
|
263
|
+
t2 = 1.day.since(t1)
|
264
|
+
t2.should == t1 + 1.day
|
265
|
+
end
|
208
266
|
end
|
data/spec/database_spec.rb
CHANGED
@@ -197,11 +197,11 @@ class DummyDataset < Sequel::Dataset
|
|
197
197
|
end
|
198
198
|
|
199
199
|
class DummyDatabase < Sequel::Database
|
200
|
-
attr_reader :
|
200
|
+
attr_reader :sqls
|
201
201
|
|
202
202
|
def execute(sql)
|
203
|
-
@
|
204
|
-
@
|
203
|
+
@sqls ||= []
|
204
|
+
@sqls << sql
|
205
205
|
end
|
206
206
|
|
207
207
|
def transaction; yield; end
|
@@ -222,8 +222,10 @@ context "Database#create_table" do
|
|
222
222
|
column :name, :text
|
223
223
|
index :name, :unique => true
|
224
224
|
end
|
225
|
-
@db.
|
226
|
-
'CREATE TABLE test (id integer NOT NULL PRIMARY KEY AUTOINCREMENT, name text)
|
225
|
+
@db.sqls.should == [
|
226
|
+
'CREATE TABLE test (id integer NOT NULL PRIMARY KEY AUTOINCREMENT, name text)',
|
227
|
+
'CREATE UNIQUE INDEX test_name_index ON test (name)'
|
228
|
+
]
|
227
229
|
end
|
228
230
|
end
|
229
231
|
|
@@ -235,19 +237,21 @@ end
|
|
235
237
|
|
236
238
|
context "Database#drop_table" do
|
237
239
|
setup do
|
238
|
-
@db =
|
240
|
+
@db = DummyDatabase.new
|
239
241
|
end
|
240
242
|
|
241
243
|
specify "should construct proper SQL" do
|
242
244
|
@db.drop_table :test
|
243
|
-
@db.
|
244
|
-
'DROP TABLE test;'
|
245
|
+
@db.sqls.should == ['DROP TABLE test']
|
245
246
|
end
|
246
247
|
|
247
248
|
specify "should accept multiple table names" do
|
248
249
|
@db.drop_table :a, :bb, :ccc
|
249
|
-
@db.
|
250
|
-
'DROP TABLE a
|
250
|
+
@db.sqls.should == [
|
251
|
+
'DROP TABLE a',
|
252
|
+
'DROP TABLE bb',
|
253
|
+
'DROP TABLE ccc'
|
254
|
+
]
|
251
255
|
end
|
252
256
|
end
|
253
257
|
|
@@ -294,8 +298,8 @@ context "Database#transaction" do
|
|
294
298
|
end
|
295
299
|
|
296
300
|
specify "should issue ROLLBACK if an exception is raised, and re-raise" do
|
297
|
-
@db.transaction {@db.execute 'DROP TABLE test
|
298
|
-
@db.sql.should == ['BEGIN', 'DROP TABLE test
|
301
|
+
@db.transaction {@db.execute 'DROP TABLE test'; raise RuntimeError} rescue nil
|
302
|
+
@db.sql.should == ['BEGIN', 'DROP TABLE test', 'ROLLBACK']
|
299
303
|
|
300
304
|
proc {@db.transaction {raise RuntimeError}}.should raise_error(RuntimeError)
|
301
305
|
end
|
@@ -307,7 +311,7 @@ context "Database#transaction" do
|
|
307
311
|
@db.drop_table(:b)
|
308
312
|
end
|
309
313
|
|
310
|
-
@db.sql.should == ['BEGIN', 'DROP TABLE a
|
314
|
+
@db.sql.should == ['BEGIN', 'DROP TABLE a', 'ROLLBACK']
|
311
315
|
end
|
312
316
|
|
313
317
|
specify "should be re-entrant" do
|
data/spec/dataset_spec.rb
CHANGED
@@ -99,24 +99,24 @@ context "A simple dataset" do
|
|
99
99
|
end
|
100
100
|
|
101
101
|
specify "should format an insert statement with default values" do
|
102
|
-
@dataset.insert_sql.should == 'INSERT INTO test DEFAULT VALUES
|
102
|
+
@dataset.insert_sql.should == 'INSERT INTO test DEFAULT VALUES'
|
103
103
|
end
|
104
104
|
|
105
105
|
specify "should format an insert statement with hash" do
|
106
106
|
@dataset.insert_sql(:name => 'wxyz', :price => 342).
|
107
107
|
should match(/INSERT INTO test \(name, price\) VALUES \('wxyz', 342\)|INSERT INTO test \(price, name\) VALUES \(342, 'wxyz'\)/)
|
108
108
|
|
109
|
-
@dataset.insert_sql({}).should == "INSERT INTO test DEFAULT VALUES
|
109
|
+
@dataset.insert_sql({}).should == "INSERT INTO test DEFAULT VALUES"
|
110
110
|
end
|
111
111
|
|
112
112
|
specify "should format an insert statement with array with keys" do
|
113
113
|
v = [1, 2, 3]
|
114
114
|
v.keys = [:a, :b, :c]
|
115
|
-
@dataset.insert_sql(v).should == "INSERT INTO test (a, b, c) VALUES (1, 2, 3)
|
115
|
+
@dataset.insert_sql(v).should == "INSERT INTO test (a, b, c) VALUES (1, 2, 3)"
|
116
116
|
|
117
117
|
v = []
|
118
118
|
v.keys = [:a, :b]
|
119
|
-
@dataset.insert_sql(v).should == "INSERT INTO test DEFAULT VALUES
|
119
|
+
@dataset.insert_sql(v).should == "INSERT INTO test DEFAULT VALUES"
|
120
120
|
end
|
121
121
|
|
122
122
|
specify "should format an insert statement with a model instance" do
|
@@ -129,25 +129,25 @@ context "A simple dataset" do
|
|
129
129
|
|
130
130
|
v = @c.new(:a => 1)
|
131
131
|
|
132
|
-
@dataset.insert_sql(v).should == "INSERT INTO test (a) VALUES (1)
|
132
|
+
@dataset.insert_sql(v).should == "INSERT INTO test (a) VALUES (1)"
|
133
133
|
|
134
134
|
v = @c.new({})
|
135
|
-
@dataset.insert_sql(v).should == "INSERT INTO test DEFAULT VALUES
|
135
|
+
@dataset.insert_sql(v).should == "INSERT INTO test DEFAULT VALUES"
|
136
136
|
end
|
137
137
|
|
138
138
|
specify "should format an insert statement with an arbitrary value" do
|
139
|
-
@dataset.insert_sql(123).should == "INSERT INTO test VALUES (123)
|
139
|
+
@dataset.insert_sql(123).should == "INSERT INTO test VALUES (123)"
|
140
140
|
end
|
141
141
|
|
142
142
|
specify "should format an insert statement with sub-query" do
|
143
143
|
@sub = Sequel::Dataset.new(nil).from(:something).filter(:x => 2)
|
144
144
|
@dataset.insert_sql(@sub).should == \
|
145
|
-
"INSERT INTO test (SELECT * FROM something WHERE (x = 2))
|
145
|
+
"INSERT INTO test (SELECT * FROM something WHERE (x = 2))"
|
146
146
|
end
|
147
147
|
|
148
148
|
specify "should format an insert statement with array" do
|
149
149
|
@dataset.insert_sql('a', 2, 6.5).should ==
|
150
|
-
"INSERT INTO test VALUES ('a', 2, 6.5)
|
150
|
+
"INSERT INTO test VALUES ('a', 2, 6.5)"
|
151
151
|
end
|
152
152
|
|
153
153
|
specify "should format an update statement" do
|
@@ -1818,9 +1818,9 @@ context "Dataset#multi_insert" do
|
|
1818
1818
|
|
1819
1819
|
def transaction
|
1820
1820
|
@sqls ||= []
|
1821
|
-
@sqls << 'BEGIN
|
1821
|
+
@sqls << 'BEGIN'
|
1822
1822
|
yield
|
1823
|
-
@sqls << 'COMMIT
|
1823
|
+
@sqls << 'COMMIT'
|
1824
1824
|
end
|
1825
1825
|
end
|
1826
1826
|
@db = @dbc.new
|
@@ -1833,24 +1833,24 @@ context "Dataset#multi_insert" do
|
|
1833
1833
|
specify "should join all inserts into a single SQL string" do
|
1834
1834
|
@ds.multi_insert(@list)
|
1835
1835
|
@db.sqls.should == [
|
1836
|
-
'BEGIN
|
1837
|
-
"INSERT INTO items (name) VALUES ('abc')
|
1838
|
-
"INSERT INTO items (name) VALUES ('def')
|
1839
|
-
"INSERT INTO items (name) VALUES ('ghi')
|
1840
|
-
'COMMIT
|
1836
|
+
'BEGIN',
|
1837
|
+
"INSERT INTO items (name) VALUES ('abc')",
|
1838
|
+
"INSERT INTO items (name) VALUES ('def')",
|
1839
|
+
"INSERT INTO items (name) VALUES ('ghi')",
|
1840
|
+
'COMMIT'
|
1841
1841
|
]
|
1842
1842
|
end
|
1843
1843
|
|
1844
1844
|
specify "should accept the commit_every option for committing every x records" do
|
1845
1845
|
@ds.multi_insert(@list, :commit_every => 2)
|
1846
1846
|
@db.sqls.should == [
|
1847
|
-
'BEGIN
|
1848
|
-
"INSERT INTO items (name) VALUES ('abc')
|
1849
|
-
"INSERT INTO items (name) VALUES ('def')
|
1850
|
-
'COMMIT
|
1851
|
-
'BEGIN
|
1852
|
-
"INSERT INTO items (name) VALUES ('ghi')
|
1853
|
-
'COMMIT
|
1847
|
+
'BEGIN',
|
1848
|
+
"INSERT INTO items (name) VALUES ('abc')",
|
1849
|
+
"INSERT INTO items (name) VALUES ('def')",
|
1850
|
+
'COMMIT',
|
1851
|
+
'BEGIN',
|
1852
|
+
"INSERT INTO items (name) VALUES ('ghi')",
|
1853
|
+
'COMMIT'
|
1854
1854
|
]
|
1855
1855
|
end
|
1856
1856
|
end
|
@@ -2007,10 +2007,10 @@ context "Dataset#transform" do
|
|
2007
2007
|
|
2008
2008
|
specify "should change the dataset to transform values saved to the database" do
|
2009
2009
|
@ds.insert(:x => :toast)
|
2010
|
-
@ds.sql.should == "INSERT INTO items (x) VALUES ('#{Marshal.dump(:toast)}')
|
2010
|
+
@ds.sql.should == "INSERT INTO items (x) VALUES ('#{Marshal.dump(:toast)}')"
|
2011
2011
|
|
2012
2012
|
@ds.insert(:y => 'butter')
|
2013
|
-
@ds.sql.should == "INSERT INTO items (y) VALUES ('butter')
|
2013
|
+
@ds.sql.should == "INSERT INTO items (y) VALUES ('butter')"
|
2014
2014
|
|
2015
2015
|
@ds.update(:x => ['dream'])
|
2016
2016
|
@ds.sql.should == "UPDATE items SET x = '#{Marshal.dump(['dream'])}'"
|
@@ -2023,7 +2023,7 @@ context "Dataset#transform" do
|
|
2023
2023
|
@ds2.first.should == {:x => [1, 2, 3], :y => 'hello'}
|
2024
2024
|
|
2025
2025
|
@ds2.insert(:x => :toast)
|
2026
|
-
@ds2.sql.should == "INSERT INTO items (x) VALUES ('#{Marshal.dump(:toast)}')
|
2026
|
+
@ds2.sql.should == "INSERT INTO items (x) VALUES ('#{Marshal.dump(:toast)}')"
|
2027
2027
|
end
|
2028
2028
|
|
2029
2029
|
specify "should work correctly together with set_row_proc" do
|
@@ -2075,9 +2075,9 @@ context "Dataset#transform" do
|
|
2075
2075
|
@ds.first.should == {:x => [1, 2, 3], :y => 'hello'}
|
2076
2076
|
|
2077
2077
|
@ds.insert(:x => :toast)
|
2078
|
-
@ds.sql.should == "INSERT INTO items (x) VALUES ('#{:toast.to_yaml}')
|
2078
|
+
@ds.sql.should == "INSERT INTO items (x) VALUES ('#{:toast.to_yaml}')"
|
2079
2079
|
@ds.insert(:y => 'butter')
|
2080
|
-
@ds.sql.should == "INSERT INTO items (y) VALUES ('butter')
|
2080
|
+
@ds.sql.should == "INSERT INTO items (y) VALUES ('butter')"
|
2081
2081
|
@ds.update(:x => ['dream'])
|
2082
2082
|
@ds.sql.should == "UPDATE items SET x = '#{['dream'].to_yaml}'"
|
2083
2083
|
|
@@ -2085,7 +2085,7 @@ context "Dataset#transform" do
|
|
2085
2085
|
@ds2.raw = {:x => [1, 2, 3].to_yaml, :y => 'hello'}
|
2086
2086
|
@ds2.first.should == {:x => [1, 2, 3], :y => 'hello'}
|
2087
2087
|
@ds2.insert(:x => :toast)
|
2088
|
-
@ds2.sql.should == "INSERT INTO items (x) VALUES ('#{:toast.to_yaml}')
|
2088
|
+
@ds2.sql.should == "INSERT INTO items (x) VALUES ('#{:toast.to_yaml}')"
|
2089
2089
|
|
2090
2090
|
@ds.set_row_proc {|r| r[:z] = r[:x] * 2; r}
|
2091
2091
|
@ds.raw = {:x => "wow".to_yaml, :y => 'hello'}
|
@@ -2103,9 +2103,9 @@ context "Dataset#transform" do
|
|
2103
2103
|
@ds.first.should == {:x => [1, 2, 3], :y => 'hello'}
|
2104
2104
|
|
2105
2105
|
@ds.insert(:x => :toast)
|
2106
|
-
@ds.sql.should == "INSERT INTO items (x) VALUES ('#{Marshal.dump(:toast)}')
|
2106
|
+
@ds.sql.should == "INSERT INTO items (x) VALUES ('#{Marshal.dump(:toast)}')"
|
2107
2107
|
@ds.insert(:y => 'butter')
|
2108
|
-
@ds.sql.should == "INSERT INTO items (y) VALUES ('butter')
|
2108
|
+
@ds.sql.should == "INSERT INTO items (y) VALUES ('butter')"
|
2109
2109
|
@ds.update(:x => ['dream'])
|
2110
2110
|
@ds.sql.should == "UPDATE items SET x = '#{Marshal.dump(['dream'])}'"
|
2111
2111
|
|
@@ -2113,7 +2113,7 @@ context "Dataset#transform" do
|
|
2113
2113
|
@ds2.raw = {:x => Marshal.dump([1, 2, 3]), :y => 'hello'}
|
2114
2114
|
@ds2.first.should == {:x => [1, 2, 3], :y => 'hello'}
|
2115
2115
|
@ds2.insert(:x => :toast)
|
2116
|
-
@ds2.sql.should == "INSERT INTO items (x) VALUES ('#{Marshal.dump(:toast)}')
|
2116
|
+
@ds2.sql.should == "INSERT INTO items (x) VALUES ('#{Marshal.dump(:toast)}')"
|
2117
2117
|
|
2118
2118
|
@ds.set_row_proc {|r| r[:z] = r[:x] * 2; r}
|
2119
2119
|
@ds.raw = {:x => Marshal.dump("wow"), :y => 'hello'}
|
data/spec/model_spec.rb
CHANGED
@@ -252,13 +252,13 @@ context "A model class" do
|
|
252
252
|
specify "should be able to create rows in the associated table" do
|
253
253
|
o = @c.create(:x => 1)
|
254
254
|
o.class.should == @c
|
255
|
-
MODEL_DB.sqls.should == ['INSERT INTO items (x) VALUES (1)
|
255
|
+
MODEL_DB.sqls.should == ['INSERT INTO items (x) VALUES (1)', "SELECT * FROM items WHERE (id IN ('INSERT INTO items (x) VALUES (1)')) LIMIT 1"]
|
256
256
|
end
|
257
257
|
|
258
258
|
specify "should be able to create rows without any values specified" do
|
259
259
|
o = @c.create
|
260
260
|
o.class.should == @c
|
261
|
-
MODEL_DB.sqls.should == ["INSERT INTO items DEFAULT VALUES
|
261
|
+
MODEL_DB.sqls.should == ["INSERT INTO items DEFAULT VALUES", "SELECT * FROM items WHERE (id IN ('INSERT INTO items DEFAULT VALUES')) LIMIT 1"]
|
262
262
|
end
|
263
263
|
end
|
264
264
|
|
@@ -276,7 +276,7 @@ context "A model class without a primary key" do
|
|
276
276
|
i.class.should be(@c)
|
277
277
|
i.values.to_hash.should == {:x => 1}
|
278
278
|
|
279
|
-
MODEL_DB.sqls.should == ['INSERT INTO items (x) VALUES (1)
|
279
|
+
MODEL_DB.sqls.should == ['INSERT INTO items (x) VALUES (1)']
|
280
280
|
end
|
281
281
|
|
282
282
|
specify "should raise when deleting" do
|
@@ -288,7 +288,7 @@ context "A model class without a primary key" do
|
|
288
288
|
o = @c.new(:x => 2)
|
289
289
|
o.should be_new
|
290
290
|
o.save
|
291
|
-
MODEL_DB.sqls.should == ['INSERT INTO items (x) VALUES (2)
|
291
|
+
MODEL_DB.sqls.should == ['INSERT INTO items (x) VALUES (2)']
|
292
292
|
end
|
293
293
|
end
|
294
294
|
|
@@ -307,8 +307,8 @@ context "Model#serialize" do
|
|
307
307
|
@c.create(:abc => "hello")
|
308
308
|
|
309
309
|
MODEL_DB.sqls.should == [ \
|
310
|
-
"INSERT INTO items (abc) VALUES ('--- 1\n')
|
311
|
-
"INSERT INTO items (abc) VALUES ('--- hello\n')
|
310
|
+
"INSERT INTO items (abc) VALUES ('--- 1\n')", \
|
311
|
+
"INSERT INTO items (abc) VALUES ('--- hello\n')", \
|
312
312
|
]
|
313
313
|
end
|
314
314
|
|
@@ -324,8 +324,8 @@ context "Model#serialize" do
|
|
324
324
|
@c.create(:def => "hello")
|
325
325
|
|
326
326
|
MODEL_DB.sqls.should == [ \
|
327
|
-
"INSERT INTO items (def) VALUES ('--- 1\n')
|
328
|
-
"INSERT INTO items (def) VALUES ('--- hello\n')
|
327
|
+
"INSERT INTO items (def) VALUES ('--- 1\n')", \
|
328
|
+
"INSERT INTO items (def) VALUES ('--- hello\n')", \
|
329
329
|
]
|
330
330
|
end
|
331
331
|
|
@@ -339,8 +339,8 @@ context "Model#serialize" do
|
|
339
339
|
@c.create(:abc => "hello")
|
340
340
|
|
341
341
|
MODEL_DB.sqls.should == [ \
|
342
|
-
"INSERT INTO items (abc) VALUES ('\004\bi\006')
|
343
|
-
"INSERT INTO items (abc) VALUES ('\004\b\"\nhello')
|
342
|
+
"INSERT INTO items (abc) VALUES ('\004\bi\006')", \
|
343
|
+
"INSERT INTO items (abc) VALUES ('\004\b\"\nhello')", \
|
344
344
|
]
|
345
345
|
end
|
346
346
|
|
@@ -387,7 +387,7 @@ context "Model#serialize" do
|
|
387
387
|
|
388
388
|
ds.raw = {:id => 1, :abc => "--- 1\n", :def => "--- hello\n"}
|
389
389
|
o = @c.create(:abc => [1, 2, 3])
|
390
|
-
ds.sqls.should == "INSERT INTO items (abc) VALUES ('#{[1, 2, 3].to_yaml}')
|
390
|
+
ds.sqls.should == "INSERT INTO items (abc) VALUES ('#{[1, 2, 3].to_yaml}')"
|
391
391
|
end
|
392
392
|
end
|
393
393
|
|
@@ -490,8 +490,8 @@ context "Model.after_create" do
|
|
490
490
|
end
|
491
491
|
|
492
492
|
n = @c.create(:x => 1)
|
493
|
-
MODEL_DB.sqls.should == ["INSERT INTO items (x) VALUES (1)
|
494
|
-
s.should == ["INSERT INTO items (x) VALUES (1)
|
493
|
+
MODEL_DB.sqls.should == ["INSERT INTO items (x) VALUES (1)", "SELECT * FROM items WHERE (id = 1) LIMIT 1"]
|
494
|
+
s.should == ["INSERT INTO items (x) VALUES (1)", "SELECT * FROM items WHERE (id = 1) LIMIT 1"]
|
495
495
|
end
|
496
496
|
|
497
497
|
specify "should allow calling save in the hook" do
|
@@ -502,7 +502,7 @@ context "Model.after_create" do
|
|
502
502
|
end
|
503
503
|
|
504
504
|
n = @c.create(:id => 1)
|
505
|
-
MODEL_DB.sqls.should == ["INSERT INTO items (id) VALUES (1)
|
505
|
+
MODEL_DB.sqls.should == ["INSERT INTO items (id) VALUES (1)", "SELECT * FROM items WHERE (id = 1) LIMIT 1", "UPDATE items SET id = 2 WHERE (id = 1)"]
|
506
506
|
end
|
507
507
|
end
|
508
508
|
|
data/spec/schema_spec.rb
CHANGED
@@ -16,7 +16,7 @@ context "DB#create_table" do
|
|
16
16
|
|
17
17
|
specify "should accept the table name" do
|
18
18
|
@db.create_table(:cats) {}
|
19
|
-
@db.sqls.should == ['CREATE TABLE cats ()
|
19
|
+
@db.sqls.should == ['CREATE TABLE cats ()']
|
20
20
|
end
|
21
21
|
|
22
22
|
specify "should accept multiple columns" do
|
@@ -24,7 +24,7 @@ context "DB#create_table" do
|
|
24
24
|
column :id, :integer
|
25
25
|
column :name, :text
|
26
26
|
end
|
27
|
-
@db.sqls.should == ['CREATE TABLE cats (id integer, name text)
|
27
|
+
@db.sqls.should == ['CREATE TABLE cats (id integer, name text)']
|
28
28
|
end
|
29
29
|
|
30
30
|
specify "should accept method calls as data types" do
|
@@ -32,26 +32,26 @@ context "DB#create_table" do
|
|
32
32
|
integer :id
|
33
33
|
text :name
|
34
34
|
end
|
35
|
-
@db.sqls.should == ['CREATE TABLE cats (id integer, name text)
|
35
|
+
@db.sqls.should == ['CREATE TABLE cats (id integer, name text)']
|
36
36
|
end
|
37
37
|
|
38
38
|
specify "should accept primary key definition" do
|
39
39
|
@db.create_table(:cats) do
|
40
40
|
primary_key :id
|
41
41
|
end
|
42
|
-
@db.sqls.should == ['CREATE TABLE cats (id integer PRIMARY KEY AUTOINCREMENT)
|
42
|
+
@db.sqls.should == ['CREATE TABLE cats (id integer PRIMARY KEY AUTOINCREMENT)']
|
43
43
|
|
44
44
|
@db.sqls.clear
|
45
45
|
@db.create_table(:cats) do
|
46
46
|
primary_key :id, :serial, :auto_increment => false
|
47
47
|
end
|
48
|
-
@db.sqls.should == ['CREATE TABLE cats (id serial PRIMARY KEY)
|
48
|
+
@db.sqls.should == ['CREATE TABLE cats (id serial PRIMARY KEY)']
|
49
49
|
|
50
50
|
@db.sqls.clear
|
51
51
|
@db.create_table(:cats) do
|
52
52
|
primary_key :id, :type => :serial, :auto_increment => false
|
53
53
|
end
|
54
|
-
@db.sqls.should == ['CREATE TABLE cats (id serial PRIMARY KEY)
|
54
|
+
@db.sqls.should == ['CREATE TABLE cats (id serial PRIMARY KEY)']
|
55
55
|
end
|
56
56
|
|
57
57
|
specify "should accept and literalize default values" do
|
@@ -59,7 +59,7 @@ context "DB#create_table" do
|
|
59
59
|
integer :id, :default => 123
|
60
60
|
text :name, :default => "abc'def"
|
61
61
|
end
|
62
|
-
@db.sqls.should == ["CREATE TABLE cats (id integer DEFAULT 123, name text DEFAULT 'abc''def')
|
62
|
+
@db.sqls.should == ["CREATE TABLE cats (id integer DEFAULT 123, name text DEFAULT 'abc''def')"]
|
63
63
|
end
|
64
64
|
|
65
65
|
specify "should accept not null definition" do
|
@@ -67,7 +67,7 @@ context "DB#create_table" do
|
|
67
67
|
integer :id
|
68
68
|
text :name, :null => false
|
69
69
|
end
|
70
|
-
@db.sqls.should == ["CREATE TABLE cats (id integer, name text NOT NULL)
|
70
|
+
@db.sqls.should == ["CREATE TABLE cats (id integer, name text NOT NULL)"]
|
71
71
|
end
|
72
72
|
|
73
73
|
specify "should accept unique definition" do
|
@@ -75,79 +75,79 @@ context "DB#create_table" do
|
|
75
75
|
integer :id
|
76
76
|
text :name, :unique => true
|
77
77
|
end
|
78
|
-
@db.sqls.should == ["CREATE TABLE cats (id integer, name text UNIQUE)
|
78
|
+
@db.sqls.should == ["CREATE TABLE cats (id integer, name text UNIQUE)"]
|
79
79
|
end
|
80
80
|
|
81
81
|
specify "should accept [SET|ENUM](...) types" do
|
82
82
|
@db.create_table(:cats) do
|
83
83
|
set :color, :elements => ['black', 'tricolor', 'grey']
|
84
84
|
end
|
85
|
-
@db.sqls.should == ["CREATE TABLE cats (color set('black', 'tricolor', 'grey'))
|
85
|
+
@db.sqls.should == ["CREATE TABLE cats (color set('black', 'tricolor', 'grey'))"]
|
86
86
|
end
|
87
87
|
|
88
88
|
specify "should accept varchar size" do
|
89
89
|
@db.create_table(:cats) do
|
90
90
|
varchar :name
|
91
91
|
end
|
92
|
-
@db.sqls.should == ["CREATE TABLE cats (name varchar(255))
|
92
|
+
@db.sqls.should == ["CREATE TABLE cats (name varchar(255))"]
|
93
93
|
@db.sqls.clear
|
94
94
|
@db.create_table(:cats) do
|
95
95
|
varchar :name, :size => 51
|
96
96
|
end
|
97
|
-
@db.sqls.should == ["CREATE TABLE cats (name varchar(51))
|
97
|
+
@db.sqls.should == ["CREATE TABLE cats (name varchar(51))"]
|
98
98
|
end
|
99
99
|
|
100
100
|
specify "should accept foreign keys" do
|
101
101
|
@db.create_table(:cats) do
|
102
102
|
foreign_key :project_id, :table => :projects
|
103
103
|
end
|
104
|
-
@db.sqls.should == ["CREATE TABLE cats (project_id integer REFERENCES projects)
|
104
|
+
@db.sqls.should == ["CREATE TABLE cats (project_id integer REFERENCES projects)"]
|
105
105
|
end
|
106
106
|
|
107
107
|
specify "should accept foreign keys with arbitrary keys" do
|
108
108
|
@db.create_table(:cats) do
|
109
109
|
foreign_key :project_id, :table => :projects, :key => :id
|
110
110
|
end
|
111
|
-
@db.sqls.should == ["CREATE TABLE cats (project_id integer REFERENCES projects(id))
|
111
|
+
@db.sqls.should == ["CREATE TABLE cats (project_id integer REFERENCES projects(id))"]
|
112
112
|
|
113
113
|
@db.sqls.clear
|
114
114
|
@db.create_table(:cats) do
|
115
115
|
foreign_key :project_id, :table => :projects, :key => :zzz
|
116
116
|
end
|
117
|
-
@db.sqls.should == ["CREATE TABLE cats (project_id integer REFERENCES projects(zzz))
|
117
|
+
@db.sqls.should == ["CREATE TABLE cats (project_id integer REFERENCES projects(zzz))"]
|
118
118
|
end
|
119
119
|
|
120
120
|
specify "should accept foreign keys with ON DELETE clause" do
|
121
121
|
@db.create_table(:cats) do
|
122
122
|
foreign_key :project_id, :table => :projects, :on_delete => :restrict
|
123
123
|
end
|
124
|
-
@db.sqls.should == ["CREATE TABLE cats (project_id integer REFERENCES projects ON DELETE RESTRICT)
|
124
|
+
@db.sqls.should == ["CREATE TABLE cats (project_id integer REFERENCES projects ON DELETE RESTRICT)"]
|
125
125
|
|
126
126
|
@db.sqls.clear
|
127
127
|
@db.create_table(:cats) do
|
128
128
|
foreign_key :project_id, :table => :projects, :on_delete => :cascade
|
129
129
|
end
|
130
|
-
@db.sqls.should == ["CREATE TABLE cats (project_id integer REFERENCES projects ON DELETE CASCADE)
|
130
|
+
@db.sqls.should == ["CREATE TABLE cats (project_id integer REFERENCES projects ON DELETE CASCADE)"]
|
131
131
|
|
132
132
|
@db.sqls.clear
|
133
133
|
@db.create_table(:cats) do
|
134
134
|
foreign_key :project_id, :table => :projects, :on_delete => :no_action
|
135
135
|
end
|
136
|
-
@db.sqls.should == ["CREATE TABLE cats (project_id integer REFERENCES projects ON DELETE NO ACTION)
|
136
|
+
@db.sqls.should == ["CREATE TABLE cats (project_id integer REFERENCES projects ON DELETE NO ACTION)"]
|
137
137
|
@db.sqls.clear
|
138
138
|
|
139
139
|
@db.sqls.clear
|
140
140
|
@db.create_table(:cats) do
|
141
141
|
foreign_key :project_id, :table => :projects, :on_delete => :set_null
|
142
142
|
end
|
143
|
-
@db.sqls.should == ["CREATE TABLE cats (project_id integer REFERENCES projects ON DELETE SET NULL)
|
143
|
+
@db.sqls.should == ["CREATE TABLE cats (project_id integer REFERENCES projects ON DELETE SET NULL)"]
|
144
144
|
@db.sqls.clear
|
145
145
|
|
146
146
|
@db.sqls.clear
|
147
147
|
@db.create_table(:cats) do
|
148
148
|
foreign_key :project_id, :table => :projects, :on_delete => :set_default
|
149
149
|
end
|
150
|
-
@db.sqls.should == ["CREATE TABLE cats (project_id integer REFERENCES projects ON DELETE SET DEFAULT)
|
150
|
+
@db.sqls.should == ["CREATE TABLE cats (project_id integer REFERENCES projects ON DELETE SET DEFAULT)"]
|
151
151
|
@db.sqls.clear
|
152
152
|
end
|
153
153
|
|
@@ -156,7 +156,7 @@ context "DB#create_table" do
|
|
156
156
|
integer :id
|
157
157
|
index :id
|
158
158
|
end
|
159
|
-
@db.sqls.should == ["CREATE TABLE cats (id integer)
|
159
|
+
@db.sqls.should == ["CREATE TABLE cats (id integer)", "CREATE INDEX cats_id_index ON cats (id)"]
|
160
160
|
end
|
161
161
|
|
162
162
|
specify "should accept multiple index definitions" do
|
@@ -165,7 +165,7 @@ context "DB#create_table" do
|
|
165
165
|
index :id
|
166
166
|
index :name
|
167
167
|
end
|
168
|
-
@db.sqls.should == ["CREATE TABLE cats (id integer)
|
168
|
+
@db.sqls.should == ["CREATE TABLE cats (id integer)", "CREATE INDEX cats_id_index ON cats (id)", "CREATE INDEX cats_name_index ON cats (name)"]
|
169
169
|
end
|
170
170
|
|
171
171
|
specify "should accept custom index names" do
|
@@ -173,7 +173,7 @@ context "DB#create_table" do
|
|
173
173
|
integer :id
|
174
174
|
index :id, :name => 'abc'
|
175
175
|
end
|
176
|
-
@db.sqls.should == ["CREATE TABLE cats (id integer)
|
176
|
+
@db.sqls.should == ["CREATE TABLE cats (id integer)", "CREATE INDEX abc ON cats (id)"]
|
177
177
|
end
|
178
178
|
|
179
179
|
specify "should accept unique index definitions" do
|
@@ -181,7 +181,7 @@ context "DB#create_table" do
|
|
181
181
|
integer :id
|
182
182
|
index :id, :unique => true
|
183
183
|
end
|
184
|
-
@db.sqls.should == ["CREATE TABLE cats (id integer)
|
184
|
+
@db.sqls.should == ["CREATE TABLE cats (id integer)", "CREATE UNIQUE INDEX cats_id_index ON cats (id)"]
|
185
185
|
end
|
186
186
|
|
187
187
|
specify "should accept compound index definitions" do
|
@@ -189,7 +189,7 @@ context "DB#create_table" do
|
|
189
189
|
integer :id
|
190
190
|
index [:id, :name], :unique => true
|
191
191
|
end
|
192
|
-
@db.sqls.should == ["CREATE TABLE cats (id integer)
|
192
|
+
@db.sqls.should == ["CREATE TABLE cats (id integer)", "CREATE UNIQUE INDEX cats_id_name_index ON cats (id, name)"]
|
193
193
|
end
|
194
194
|
end
|
195
195
|
|
@@ -200,6 +200,6 @@ context "DB#drop_table" do
|
|
200
200
|
|
201
201
|
specify "should generate a DROP TABLE statement" do
|
202
202
|
@db.drop_table :cats
|
203
|
-
@db.sqls.should == ['DROP TABLE cats
|
203
|
+
@db.sqls.should == ['DROP TABLE cats']
|
204
204
|
end
|
205
205
|
end
|