spec/adapters/oracle_spec.rb in sequel-3.28.0 vs spec/adapters/oracle_spec.rb in sequel-3.29.0

- old
+ new

@@ -1,79 +1,70 @@ require File.join(File.dirname(File.expand_path(__FILE__)), 'spec_helper.rb') -require "timeout" unless defined?(ORACLE_DB) ORACLE_DB = Sequel.connect('oracle://hr:hr@localhost/XE') end INTEGRATION_DB = ORACLE_DB unless defined?(INTEGRATION_DB) -if ORACLE_DB.table_exists?(:items) - ORACLE_DB.drop_table :items -end -ORACLE_DB.create_table :items do - varchar2 :name, :size => 50 - number :value, :size => 38 - date :date_created +ORACLE_DB.create_table!(:items) do + String :name, :size => 50 + Integer :value + Date :date_created index :value end -if ORACLE_DB.table_exists?(:books) - ORACLE_DB.drop_table :books +ORACLE_DB.create_table!(:books) do + Integer :id + String :title, :size => 50 + Integer :category_id end -ORACLE_DB.create_table :books do - number :id, :size => 38 - varchar2 :title, :size => 50 - number :category_id, :size => 38 -end -if ORACLE_DB.table_exists?(:categories) - ORACLE_DB.drop_table :categories +ORACLE_DB.create_table!(:categories) do + Integer :id + String :cat_name, :size => 50 end -ORACLE_DB.create_table :categories do - number :id, :size => 38 - varchar2 :cat_name, :size => 50 -end describe "An Oracle database" do specify "should provide disconnect functionality" do ORACLE_DB.execute("select user from dual") ORACLE_DB.pool.size.should == 1 ORACLE_DB.disconnect ORACLE_DB.pool.size.should == 0 end + + specify "should be able to get current sequence value with SQL" do + begin + ORACLE_DB.create_table!(:foo){primary_key :id} + ORACLE_DB.fetch('SELECT seq_foo_id.nextval FROM DUAL').single_value.should == 1 + ensure + ORACLE_DB.drop_table(:foo) + end + end specify "should provide schema information" do - books_schema = [ - [:id, {:char_size=>0, :type=>:number, :allow_null=>true, :type_string=>"NUMBER(38)", :data_size=>22, :precision=>38, :char_used=>false, :scale=>0, :charset_form=>nil, :fsprecision=>38, :lfprecision=>0, :db_type=>"NUMBER(38)"}], - [:title, {:char_size=>50, :type=>:varchar2, :allow_null=>true, :type_string=>"VARCHAR2(50)", :data_size=>50, :precision=>0, :char_used=>false, :scale=>0, :charset_form=>:implicit, :fsprecision=>0, :lfprecision=>0, :db_type=>"VARCHAR2(50)"}], - [:category_id, {:char_size=>0, :type=>:number, :allow_null=>true, :type_string=>"NUMBER(38)", :data_size=>22, :precision=>38, :char_used=>false, :scale=>0, :charset_form=>nil, :fsprecision=>38, :lfprecision=>0, :db_type=>"NUMBER(38)"}]] - categories_schema = [ - [:id, {:char_size=>0, :type=>:number, :allow_null=>true, :type_string=>"NUMBER(38)", :data_size=>22, :precision=>38, :char_used=>false, :scale=>0, :charset_form=>nil, :fsprecision=>38, :lfprecision=>0, :db_type=>"NUMBER(38)"}], - [:cat_name, {:char_size=>50, :type=>:varchar2, :allow_null=>true, :type_string=>"VARCHAR2(50)", :data_size=>50, :precision=>0, :char_used=>false, :scale=>0, :charset_form=>:implicit, :fsprecision=>0, :lfprecision=>0, :db_type=>"VARCHAR2(50)"}]] - items_schema = [ - [:name, {:char_size=>50, :type=>:varchar2, :allow_null=>true, :type_string=>"VARCHAR2(50)", :data_size=>50, :precision=>0, :char_used=>false, :scale=>0, :charset_form=>:implicit, :fsprecision=>0, :lfprecision=>0, :db_type=>"VARCHAR2(50)"}], - [:value, {:char_size=>0, :type=>:number, :allow_null=>true, :type_string=>"NUMBER(38)", :data_size=>22, :precision=>38, :char_used=>false, :scale=>0, :charset_form=>nil, :fsprecision=>38, :lfprecision=>0, :db_type=>"NUMBER(38)"}], - [:date_created, {:charset_form=>nil, :type=>:date, :type_string=>"DATE", :fsprecision=>0, :data_size=>7, :lfprecision=>0, :precision=>0, :db_type=>"DATE", :char_used=>false, :char_size=>0, :scale=>0, :allow_null=>true}]] + books_schema = [[:id, [:integer, false, true, nil]], + [:title, [:string, false, true, nil]], + [:category_id, [:integer, false, true, nil]]] + categories_schema = [[:id, [:integer, false, true, nil]], + [:cat_name, [:string, false, true, nil]]] + items_schema = [[:name, [:string, false, true, nil]], + [:value, [:integer, false, true, nil]], + [:date_created, [:datetime, false, true, nil]]] {:books => books_schema, :categories => categories_schema, :items => items_schema}.each_pair do |table, expected_schema| schema = ORACLE_DB.schema(table) schema.should_not be_nil - expected_schema.should == schema + schema.map{|c, s| [c, s.values_at(:type, :primary_key, :allow_null, :ruby_default)]}.should == expected_schema end end specify "should create a temporary table" do - ORACLE_DB.create_table :test_tmp, :temporary => true do + ORACLE_DB.create_table! :test_tmp, :temp => true do + varchar2 :name, :size => 50 primary_key :id, :integer, :null => false - column :name, :text index :name, :unique => true end - - ORACLE_DB.sqls.should == [ - 'CREATE GLOBAL TEMPORARY TABLE test_tmp (id integer NOT NULL PRIMARY KEY AUTOINCREMENT, name text)', - 'CREATE UNIQUE INDEX test_tmp_name_index ON test_tmp (name)' - ] end end describe "An Oracle dataset" do before do @@ -146,31 +137,31 @@ @d.avg(:value).to_i.should == (789+123+456)/3 @d.max(:value).to_i.should == 789 - @d.select(:name, :AVG.sql_function(:value)).filter(:name => 'abc').group(:name).to_a.should == [ - {:name => 'abc', :"avg(value)" => (456+123)/2.0} + @d.select(:name, :AVG.sql_function(:value).as(:avg)).filter(:name => 'abc').group(:name).to_a.should == [ + {:name => 'abc', :avg => (456+123)/2.0} ] - @d.select(:AVG.sql_function(:value)).group(:name).order(:name).limit(1).to_a.should == [ - {:"avg(value)" => (456+123)/2.0} + @d.select(:AVG.sql_function(:value).as(:avg)).group(:name).order(:name).limit(1).to_a.should == [ + {:avg => (456+123)/2.0} ] - @d.select(:name, :AVG.sql_function(:value)).group(:name).order(:name).to_a.should == [ - {:name => 'abc', :"avg(value)" => (456+123)/2.0}, - {:name => 'def', :"avg(value)" => 789*1.0} + @d.select(:name, :AVG.sql_function(:value).as(:avg)).group(:name).order(:name).to_a.should == [ + {:name => 'abc', :avg => (456+123)/2.0}, + {:name => 'def', :avg => 789*1.0} ] - @d.select(:name, :AVG.sql_function(:value)).group(:name).order(:name).to_a.should == [ - {:name => 'abc', :"avg(value)" => (456+123)/2.0}, - {:name => 'def', :"avg(value)" => 789*1.0} + @d.select(:name, :AVG.sql_function(:value).as(:avg)).group(:name).order(:name).to_a.should == [ + {:name => 'abc', :avg => (456+123)/2.0}, + {:name => 'def', :avg => 789*1.0} ] - @d.select(:name, :AVG.sql_function(:value)).group(:name).having(:name => ['abc', 'def']).order(:name).to_a.should == [ - {:name => 'abc', :"avg(value)" => (456+123)/2.0}, - {:name => 'def', :"avg(value)" => 789*1.0} + @d.select(:name, :AVG.sql_function(:value).as(:avg)).group(:name).having(:name => ['abc', 'def']).order(:name).to_a.should == [ + {:name => 'abc', :avg => (456+123)/2.0}, + {:name => 'def', :avg => 789*1.0} ] @d.select(:name, :value).filter(:name => 'abc').union(@d.select(:name, :value).filter(:name => 'def')).order(:value).to_a.should == [ {:name => 'abc', :value => 123}, {:name => 'abc', :value => 456}, @@ -192,11 +183,11 @@ end specify "should translate values correctly" do @d << {:name => 'abc', :value => 456} @d << {:name => 'def', :value => 789} - @d.filter(:value > 500).update(:date_created => "to_timestamp('2009-09-09', 'YYYY-MM-DD')".lit) + @d.filter('value > 500').update(:date_created => "to_timestamp('2009-09-09', 'YYYY-MM-DD')".lit) @d[:name => 'def'][:date_created].should == Time.parse('2009-09-09') end specify "should delete records correctly" do @@ -287,36 +278,17 @@ end describe "Row locks in Oracle" do before do @d1 = ORACLE_DB[:books] - @d1.delete # remove all records + @d1.delete @d1 << {:id => 1, :title => 'aaa'} end specify "#for_update should use FOR UPDATE" do - @d1.for_update.sql.should == "SELECT * FROM BOOKS FOR UPDATE" + @d1.for_update.sql.should == 'SELECT * FROM "BOOKS" FOR UPDATE' end specify "#lock_style should accept symbols" do - @d1.lock_style(:update).sql.should == "SELECT * FROM BOOKS FOR UPDATE" - end - - specify "should not update during row lock" do - ORACLE_DB.transaction do - @d1.filter(:id => 1).for_update.to_a - proc do - t1 = Thread.start do - # wait for unlock - Timeout::timeout(0.02) do - ORACLE_DB[:books].filter(:id => 1).update(:title => "bbb") - end - end - t1.join - end.should raise_error - @d1.filter(:id => 1).first[:title].should == "aaa" - end - t2 = Thread.start { ORACLE_DB[:books].filter(:id => 1).update(:title => "bbb") } - t2.join - @d1.filter(:id => 1).first[:title].should == "bbb" + @d1.lock_style(:update).sql.should == 'SELECT * FROM "BOOKS" FOR UPDATE' end end