spec/integration/plugin_test.rb in sequel-3.33.0 vs spec/integration/plugin_test.rb in sequel-3.34.0

- old
+ new

@@ -6,11 +6,11 @@ unless Sequel.guarded?(:h2, :mssql, :db2) describe "Class Table Inheritance Plugin" do before(:all) do @db = INTEGRATION_DB @db.instance_variable_set(:@schemas, {}) - [:staff, :executives, :managers, :employees].each{|t| @db.drop_table(t) if @db.table_exists?(t)} + @db.drop_table?(:staff, :executives, :managers, :employees) @db.create_table(:employees) do primary_key :id String :name String :kind end @@ -52,11 +52,11 @@ end after do [:Executive, :Manager, :Staff, :Employee].each{|s| Object.send(:remove_const, s)} end after(:all) do - @db.drop_table :staff, :executives, :managers, :employees + @db.drop_table? :staff, :executives, :managers, :employees end specify "should return rows as subclass instances" do Employee.order(:id).all.should == [ Employee.load(:id=>@i1, :name=>'E', :kind=>'Employee'), @@ -149,11 +149,11 @@ describe "Many Through Many Plugin" do before(:all) do @db = INTEGRATION_DB @db.instance_variable_set(:@schemas, {}) - [:albums_artists, :albums, :artists].each{|t| @db.drop_table(t) if @db.table_exists?(t)} + @db.drop_table?(:albums_artists, :albums, :artists) @db.create_table(:albums) do primary_key :id String :name end @db.create_table(:artists) do @@ -193,11 +193,11 @@ end after do [:Album, :Artist].each{|s| Object.send(:remove_const, s)} end after(:all) do - @db.drop_table :albums_artists, :albums, :artists + @db.drop_table? :albums_artists, :albums, :artists end def self_join(c) c.join(c.table_name.as(:b), Array(c.primary_key).zip(Array(c.primary_key))).select_all(c.table_name) end @@ -408,11 +408,11 @@ plugin :lazy_attributes, :num end Item.create(:name=>'J', :num=>1) end after(:all) do - @db.drop_table(:items) + @db.drop_table?(:items) Object.send(:remove_const, :Item) end specify "should not include lazy attribute columns by default" do Item.first.should == Item.load(:id=>1, :name=>'J') @@ -478,11 +478,11 @@ end after do [:Album, :Artist].each{|s| Object.send(:remove_const, s)} end after(:all) do - @db.drop_table :albums, :artists + @db.drop_table? :albums, :artists end specify "should eagerly load associations for all items when accessing any item" do a = Artist.order(:name).all a.map{|x| x.associations}.should == [{}, {}, {}, {}] @@ -508,11 +508,11 @@ plugin :identity_map end Item.create(:name=>'J', :num=>3) end after do - @db.drop_table(:items) + @db.drop_table?(:items) Object.send(:remove_const, :Item) end specify "should return the same instance if retrieved more than once" do Item.with_identity_map{Item.first.object_id.should == Item.first.object_id} @@ -559,20 +559,20 @@ end after do [:Album, :Artist].each{|s| Object.send(:remove_const, s)} end after(:all) do - @db.drop_table :albums, :artists + @db.drop_table? :albums, :artists end specify "should update the timestamp column when touching the record" do @album.updated_at.should == nil @album.touch @album.updated_at.to_i.should be_within(2).of(Time.now.to_i) end - cspecify "should update the timestamp column for associated records when the record is updated or destroyed", [:do, :sqlite], [:jdbc, :sqlite] do + cspecify "should update the timestamp column for associated records when the record is updated or destroyed", [:do, :sqlite], [:jdbc, :sqlite], [:swift] do @artist.updated_at.should == nil @album.update(:name=>'B') ua = @artist.reload.updated_at if ua.is_a?(Time) ua.to_i.should be_within(2).of(Time.now.to_i) @@ -599,11 +599,11 @@ class ::Item < Sequel::Model(@db) plugin :serialization, :marshal, :stuff end end after do - @db.drop_table(:items) + @db.drop_table?(:items) Object.send(:remove_const, :Item) end specify "should serialize and deserialize items as needed" do i = Item.create(:stuff=>{:a=>1}) @@ -631,11 +631,11 @@ before do @db[:people].delete @p = Person.create(:name=>'John') end after(:all) do - @db.drop_table(:people) + @db.drop_table?(:people) Object.send(:remove_const, :Person) end specify "should raise an error when updating a stale record" do p1 = Person[@p.id] @@ -682,11 +682,11 @@ end @e1 = Event.create(:year=>2010, :month=>2, :day=>15) @e2 = Event.create(:year=>nil) end after do - @db.drop_table(:events) + @db.drop_table?(:events) Object.send(:remove_const, :Event) end specify "should return a composed object if the underlying columns have a value" do @e1.date.should == Date.civil(2010, 2, 15) @@ -742,11 +742,11 @@ end before do @nodes.each{|n| n.associations.clear} end after(:all) do - @db.drop_table :nodes + @db.drop_table? :nodes Object.send(:remove_const, :Node) end specify "should load all standard (not-CTE) methods correctly" do @a.children.should == [@aa, @ab] @@ -963,11 +963,11 @@ @i = Item.create(:name=>'J', :number=>1, :cost=>2) @i.instance_filter(:number=>1) @i.set(:name=>'K') end after(:all) do - @db.drop_table(:items) + @db.drop_table?(:items) Object.send(:remove_const, :Item) end specify "should not raise an error if saving only updates one row" do @i.save @@ -1024,11 +1024,11 @@ before do @ds.delete @ds.insert(:a=>1, :b=>3) end after(:all) do - @db.drop_table(:t) + @db.drop_table?(:t) end specify "should handle regular updates" do @c.first.update(:b=>4) @db[:t].all.should == [{:a=>1, :b=>4}] @@ -1067,11 +1067,11 @@ end describe "AssociationPks plugin" do before(:all) do @db = INTEGRATION_DB - [:albums_tags, :tags, :albums, :artists].each{|t| @db.drop_table(t) if @db.table_exists?(t)} + @db.drop_table?(:albums_tags, :tags, :albums, :artists) @db.create_table(:artists) do primary_key :id String :name end @db.create_table(:albums) do @@ -1111,11 +1111,11 @@ {@al1=>[@t1, @t2, @t3], @al2=>[@t2]}.each do |aid, tids| tids.each{|tid| @db[:albums_tags].insert([aid, tid])} end end after(:all) do - @db.drop_table :albums_tags, :tags, :albums, :artists + @db.drop_table? :albums_tags, :tags, :albums, :artists [:Artist, :Album, :Tag].each{|s| Object.send(:remove_const, s)} end specify "should return correct associated pks for one_to_many associations" do Artist.order(:id).all.map{|a| a.album_pks}.should == [[@al1, @al2, @al3], []] @@ -1182,11 +1182,11 @@ @c.create :name => "abc" @c.create :name => "def" @c.create :name => "hig" end after(:all) do - @db.drop_table(:sites) + @db.drop_table?(:sites) end it "should return rows in order of position" do @c.map(:position).should == [1,2,3] @c.map(:name).should == %w[ abc def hig ] @@ -1260,11 +1260,11 @@ @c.create :name => "P2", :parent_id => p2.id @c.create :name => "P3", :parent_id => p2.id @c.create :name => "Au", :parent_id => p1.id end after(:all) do - @db.drop_table(:pages) + @db.drop_table?(:pages) end it "should return rows in order of position" do @c.map(:name).should == %w[ Hm Ps Au P1 P2 P3 ] end @@ -1348,11 +1348,11 @@ class ::Node < Sequel::Model plugin :tree end end after(:all) do - @db.drop_table(:nodes) + @db.drop_table?(:nodes) Object.send(:remove_const, :Node) end it "should instantiate" do Node.all.size.should == 12 @@ -1464,11 +1464,11 @@ class ::Lorem < Sequel::Model plugin :tree, :key => :ipsum_id, :order => :neque end end after(:all) do - @db.drop_table(:lorems) + @db.drop_table?(:lorems) Object.send(:remove_const, :Lorem) end it "iterate top-level nodes in order by position" do Lorem.roots_dataset.count.should == 2 @@ -1497,11 +1497,11 @@ @c.delete @foo = @c.create(:name=>'foo', :i=>10) @bar = @c.create(:name=>'bar', :i=>20) end after(:all) do - @db.drop_table(:ps_test) + @db.drop_table?(:ps_test) end it "should work with looking up using Model.[]" do @c[@foo.id].should == @foo @c[@bar.id].should == @bar @@ -1550,7 +1550,70 @@ @c[o.id].should == @c.load(:id=>o.id, :name=>'foo2', :i=>nil) o = @c.create(:i=>30) @c[o.id].should == @c.load(:id=>o.id, :name=>nil, :i=>30) o = @c.create(:name=>nil, :i=>40) @c[o.id].should == @c.load(:id=>o.id, :name=>nil, :i=>40) + end +end + +describe "Caching plugins" do + before(:all) do + @db = INTEGRATION_DB + @db.drop_table?(:albums, :artists) + @db.create_table(:artists) do + primary_key :id + end + @db.create_table(:albums) do + primary_key :id + foreign_key :artist_id, :artists + end + @db[:artists].insert + @db[:albums].insert(:artist_id=>1) + end + before do + @Album = Class.new(Sequel::Model(@db[:albums])) + @Album.plugin :many_to_one_pk_lookup + end + after(:all) do + @db.drop_table?(:albums, :artists) + end + + shared_examples_for "a caching plugin" do + it "should work with looking up using Model.[]" do + @Artist[1].should equal(@Artist[1]) + @Artist[:id=>1].should == @Artist[1] + @Artist[0].should == nil + @Artist[nil].should == nil + end + + it "should work with lookup up many_to_one associated objects" do + a = @Artist[1] + @Album.first.artist.should equal(a) + end + end + + describe "caching plugin" do + before do + @cache_class = Class.new(Hash) do + def set(k, v, ttl) self[k] = v end + alias get [] + end + @cache = @cache_class.new + + @Artist = Class.new(Sequel::Model(@db[:artists])) + @Artist.plugin :caching, @cache + @Album.many_to_one :artist, :class=>@Artist + end + + it_should_behave_like "a caching plugin" + end + + describe "static_cache plugin" do + before do + @Artist = Class.new(Sequel::Model(@db[:artists])) + @Artist.plugin :static_cache + @Album.many_to_one :artist, :class=>@Artist + end + + it_should_behave_like "a caching plugin" end end