1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
|
require File.join(File.dirname(__FILE__), '../../lib/sequel_core')
require File.join(File.dirname(__FILE__), '../spec_helper.rb')
unless defined?(SQLITE_DB)
SQLITE_DB = Sequel.connect('sqlite:/')
end
SQLITE_DB.create_table :items do
integer :id, :primary_key => true, :auto_increment => true
text :name
float :value
end
SQLITE_DB.create_table :test2 do
text :name
integer :value
end
SQLITE_DB.create_table(:time) {timestamp :t}
context "An SQLite database" do
setup do
@db = Sequel.connect('sqlite:/')
end
specify "should provide a list of existing tables" do
@db.tables.should == []
@db.create_table :testing do
text :name
end
@db.tables.should include(:testing)
end
specify "should support getting pragma values" do
@db.pragma_get(:auto_vacuum).should == '0'
end
specify "should support setting pragma values" do
@db.pragma_set(:auto_vacuum, '1')
@db.pragma_get(:auto_vacuum).should == '1'
end
specify "should support getting and setting the auto_vacuum pragma" do
@db.auto_vacuum = :full
@db.auto_vacuum.should == :full
@db.auto_vacuum = :none
@db.auto_vacuum.should == :none
proc {@db.auto_vacuum = :invalid}.should raise_error(Sequel::Error)
end
specify "should support getting and setting the synchronous pragma" do
@db.synchronous = :off
@db.synchronous.should == :off
@db.synchronous = :normal
@db.synchronous.should == :normal
@db.synchronous = :full
@db.synchronous.should == :full
proc {@db.synchronous = :invalid}.should raise_error(Sequel::Error)
end
specify "should support getting and setting the temp_store pragma" do
@db.temp_store = :default
@db.temp_store.should == :default
@db.temp_store = :file
@db.temp_store.should == :file
@db.temp_store = :memory
@db.temp_store.should == :memory
proc {@db.temp_store = :invalid}.should raise_error(Sequel::Error)
end
specify "should be able to execute multiple statements at once" do
@db.create_table :t do
text :name
end
@db << "insert into t (name) values ('abc');insert into t (name) values ('def')"
@db[:t].count.should == 2
@db[:t].order(:name).map(:name).should == ['abc', 'def']
end
specify "should be able to execute transactions" do
@db.transaction do
@db.create_table(:t) {text :name}
end
@db.tables.should == [:t]
proc {@db.transaction do
@db.create_table(:u) {text :name}
raise ArgumentError
end}.should raise_error(ArgumentError)
# no commit
@db.tables.should == [:t]
proc {@db.transaction do
@db.create_table(:v) {text :name}
raise Sequel::Error::Rollback
end}.should_not raise_error
# no commit
@db.tables.should == [:t]
end
specify "should support nested transactions" do
@db.transaction do
@db.transaction do
@db.create_table(:t) {text :name}
end
end
@db.tables.should == [:t]
proc {@db.transaction do
@db.create_table(:v) {text :name}
@db.transaction do
raise Sequel::Error::Rollback # should roll back the top-level transaction
end
end}.should_not raise_error
# no commit
@db.tables.should == [:t]
end
specify "should provide disconnect functionality" do
@db.tables
@db.pool.size.should == 1
@db.disconnect
@db.pool.size.should == 0
end
specify "should support timestamps" do
t1 = Time.at(Time.now.to_i) #normalize time
SQLITE_DB[:time] << {:t => t1}
SQLITE_DB[:time].first[:t].should == t1
end
specify "should support sequential primary keys" do
@db.create_table!(:with_pk) {primary_key :id; text :name}
@db[:with_pk] << {:name => 'abc'}
@db[:with_pk] << {:name => 'def'}
@db[:with_pk] << {:name => 'ghi'}
@db[:with_pk].order(:name).all.should == [
{:id => 1, :name => 'abc'},
{:id => 2, :name => 'def'},
{:id => 3, :name => 'ghi'}
]
end
specify "should catch invalid SQL errors and raise them as Error::InvalidStatement" do
proc {@db.execute 'blah blah'}.should raise_error(
Sequel::Error::InvalidStatement, "blah blah\r\nnear \"blah\": syntax error")
proc {@db.execute_insert 'blah blah'}.should raise_error(
Sequel::Error::InvalidStatement, "blah blah\r\nnear \"blah\": syntax error")
proc {@db.execute_select 'blah blah'}.should raise_error(
Sequel::Error::InvalidStatement, "blah blah\r\nnear \"blah\": syntax error")
proc {@db.single_value 'blah blah'}.should raise_error(
Sequel::Error::InvalidStatement, "blah blah\r\nnear \"blah\": syntax error")
end
end
context "An SQLite dataset" do
setup do
@d = SQLITE_DB[:items]
@d.delete # remove all records
end
specify "should return the correct records" do
@d.to_a.should == []
@d << {:name => 'abc', :value => 1.23}
@d << {:name => 'abc', :value => 4.56}
@d << {:name => 'def', :value => 7.89}
@d.select(:name, :value).to_a.sort_by {|h| h[:value]}.should == [
{:name => 'abc', :value => 1.23},
{:name => 'abc', :value => 4.56},
{:name => 'def', :value => 7.89}
]
end
specify "should return the correct record count" do
@d.count.should == 0
@d << {:name => 'abc', :value => 1.23}
@d << {:name => 'abc', :value => 4.56}
@d << {:name => 'def', :value => 7.89}
@d.count.should == 3
end
specify "should return the last inserted id when inserting records" do
id = @d << {:name => 'abc', :value => 1.23}
id.should == @d.first[:id]
end
specify "should update records correctly" do
@d << {:name => 'abc', :value => 1.23}
@d << {:name => 'abc', :value => 4.56}
@d << {:name => 'def', :value => 7.89}
@d.filter(:name => 'abc').update(:value => 5.3)
# the third record should stay the same
@d[:name => 'def'][:value].should == 7.89
@d.filter(:value => 5.3).count.should == 2
end
specify "should delete records correctly" do
@d << {:name => 'abc', :value => 1.23}
@d << {:name => 'abc', :value => 4.56}
@d << {:name => 'def', :value => 7.89}
@d.filter(:name => 'abc').delete
@d.count.should == 1
@d.first[:name].should == 'def'
end
end
context "An SQLite dataset" do
setup do
@d = SQLITE_DB[:items]
@d.delete # remove all records
@d << {:name => 'abc', :value => 1.23}
@d << {:name => 'def', :value => 4.56}
@d << {:name => 'ghi', :value => 7.89}
end
specify "should correctly return avg" do
@d.avg(:value).should == ((1.23 + 4.56 + 7.89) / 3).to_s
end
specify "should correctly return sum" do
@d.sum(:value).should == (1.23 + 4.56 + 7.89).to_s
end
specify "should correctly return max" do
@d.max(:value).should == 7.89.to_s
end
specify "should correctly return min" do
@d.min(:value).should == 1.23.to_s
end
end
context "SQLite::Dataset#delete" do
setup do
@d = SQLITE_DB[:items]
@d.delete # remove all records
@d << {:name => 'abc', :value => 1.23}
@d << {:name => 'def', :value => 4.56}
@d << {:name => 'ghi', :value => 7.89}
end
specify "should return the number of records affected when filtered" do
@d.count.should == 3
@d.filter {:value < 3}.delete.should == 1
@d.count.should == 2
@d.filter {:value < 3}.delete.should == 0
@d.count.should == 2
end
specify "should return the number of records affected when unfiltered" do
@d.count.should == 3
@d.delete.should == 3
@d.count.should == 0
@d.delete.should == 0
end
end
context "SQLite::Dataset#update" do
setup do
@d = SQLITE_DB[:items]
@d.delete # remove all records
@d << {:name => 'abc', :value => 1.23}
@d << {:name => 'def', :value => 4.56}
@d << {:name => 'ghi', :value => 7.89}
end
specify "should return the number of records affected" do
@d.filter(:name => 'abc').update(:value => 2).should == 1
@d.update(:value => 10).should == 3
@d.filter(:name => 'xxx').update(:value => 23).should == 0
end
end
context "SQLite dataset" do
setup do
SQLITE_DB.create_table :test do
integer :id, :primary_key => true, :auto_increment => true
text :name
float :value
end
@d = SQLITE_DB[:items]
@d.delete # remove all records
@d << {:name => 'abc', :value => 1.23}
@d << {:name => 'def', :value => 4.56}
@d << {:name => 'ghi', :value => 7.89}
end
teardown do
SQLITE_DB.drop_table :test
end
specify "should be able to insert from a subquery" do
SQLITE_DB[:test] << @d
SQLITE_DB[:test].count.should == 3
SQLITE_DB[:test].select(:name, :value).order(:value).to_a.should == \
@d.select(:name, :value).order(:value).to_a
end
end
__END__
context "A SQLite database" do
setup do
@db = SQLITE_DB
end
specify "should support add_column operations" do
@db.add_column :test2, :xyz, :text
@db[:test2].columns.should == [:name, :value, :xyz]
@db[:test2] << {:name => 'mmm', :value => 111}
@db[:test2].first[:xyz].should == '000'
end
specify "should not support drop_column operations" do
proc {@db.drop_column :test2, :xyz}.should raise_error(Sequel::Error)
end
specify "should not support rename_column operations" do
@db[:test2].delete
@db.add_column :test2, :xyz, :text, :default => '000'
@db[:test2] << {:name => 'mmm', :value => 111, :xyz => 'qqqq'}
@db[:test2].columns.should == [:name, :value, :xyz]
proc {@db.rename_column :test2, :xyz, :zyx}.should raise_error(Sequel::Error)
end
specify "should not support set_column_type operations" do
@db.add_column :test2, :xyz, :float
@db[:test2].delete
@db[:test2] << {:name => 'mmm', :value => 111, :xyz => 56.78}
proc {@db.set_column_type :test2, :xyz, :integer}.should raise_error(Sequel::Error)
end
end
|