spec/integration/plugin_test.rb in sequel-3.23.0 vs spec/integration/plugin_test.rb in sequel-3.24.0

- old
+ new

@@ -194,10 +194,16 @@ Artist[1].albums.map{|x| x.name}.sort.should == %w'A D' Artist[2].albums.map{|x| x.name}.sort.should == %w'A C' Artist[3].albums.map{|x| x.name}.sort.should == %w'B C' Artist[4].albums.map{|x| x.name}.sort.should == %w'B D' + Artist.plugin :prepared_statements_associations + Artist[1].albums.map{|x| x.name}.sort.should == %w'A D' + Artist[2].albums.map{|x| x.name}.sort.should == %w'A C' + Artist[3].albums.map{|x| x.name}.sort.should == %w'B C' + Artist[4].albums.map{|x| x.name}.sort.should == %w'B D' + Artist.filter(:id=>1).eager(:albums).all.map{|x| x.albums.map{|a| a.name}}.flatten.sort.should == %w'A D' Artist.filter(:id=>2).eager(:albums).all.map{|x| x.albums.map{|a| a.name}}.flatten.sort.should == %w'A C' Artist.filter(:id=>3).eager(:albums).all.map{|x| x.albums.map{|a| a.name}}.flatten.sort.should == %w'B C' Artist.filter(:id=>4).eager(:albums).all.map{|x| x.albums.map{|a| a.name}}.flatten.sort.should == %w'B D' @@ -208,19 +214,39 @@ Artist.filter(:albums=>@album1).all.map{|a| a.name}.sort.should == %w'1 2' Artist.filter(:albums=>@album2).all.map{|a| a.name}.sort.should == %w'3 4' Artist.filter(:albums=>@album3).all.map{|a| a.name}.sort.should == %w'2 3' Artist.filter(:albums=>@album4).all.map{|a| a.name}.sort.should == %w'1 4' + + Artist.exclude(:albums=>@album1).all.map{|a| a.name}.sort.should == %w'3 4' + Artist.exclude(:albums=>@album2).all.map{|a| a.name}.sort.should == %w'1 2' + Artist.exclude(:albums=>@album3).all.map{|a| a.name}.sort.should == %w'1 4' + Artist.exclude(:albums=>@album4).all.map{|a| a.name}.sort.should == %w'2 3' + + Artist.filter(:albums=>[@album1, @album3]).all.map{|a| a.name}.sort.should == %w'1 2 3' + Artist.filter(:albums=>[@album2, @album4]).all.map{|a| a.name}.sort.should == %w'1 3 4' + + Artist.exclude(:albums=>[@album1, @album3]).all.map{|a| a.name}.sort.should == %w'4' + Artist.exclude(:albums=>[@album2, @album4]).all.map{|a| a.name}.sort.should == %w'2' + + Artist.filter(:albums=>Album.filter(:id=>[@album1.id, @album3.id])).all.map{|a| a.name}.sort.should == %w'1 2 3' + Artist.exclude(:albums=>Album.filter(:id=>[@album1.id, @album3.id])).all.map{|a| a.name}.sort.should == %w'4' end specify "should handle typical case with 3 join tables" do Artist.many_through_many :related_artists, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_artists, :album_id, :artist_id]], :class=>Artist, :distinct=>true Artist[1].related_artists.map{|x| x.name}.sort.should == %w'1 2 4' Artist[2].related_artists.map{|x| x.name}.sort.should == %w'1 2 3' Artist[3].related_artists.map{|x| x.name}.sort.should == %w'2 3 4' Artist[4].related_artists.map{|x| x.name}.sort.should == %w'1 3 4' + Artist.plugin :prepared_statements_associations + Artist[1].related_artists.map{|x| x.name}.sort.should == %w'1 2 4' + Artist[2].related_artists.map{|x| x.name}.sort.should == %w'1 2 3' + Artist[3].related_artists.map{|x| x.name}.sort.should == %w'2 3 4' + Artist[4].related_artists.map{|x| x.name}.sort.should == %w'1 3 4' + Artist.filter(:id=>1).eager(:related_artists).all.map{|x| x.related_artists.map{|a| a.name}}.flatten.sort.should == %w'1 2 4' Artist.filter(:id=>2).eager(:related_artists).all.map{|x| x.related_artists.map{|a| a.name}}.flatten.sort.should == %w'1 2 3' Artist.filter(:id=>3).eager(:related_artists).all.map{|x| x.related_artists.map{|a| a.name}}.flatten.sort.should == %w'2 3 4' Artist.filter(:id=>4).eager(:related_artists).all.map{|x| x.related_artists.map{|a| a.name}}.flatten.sort.should == %w'1 3 4' @@ -231,10 +257,21 @@ Artist.filter(:related_artists=>@artist1).all.map{|a| a.name}.sort.should == %w'1 2 4' Artist.filter(:related_artists=>@artist2).all.map{|a| a.name}.sort.should == %w'1 2 3' Artist.filter(:related_artists=>@artist3).all.map{|a| a.name}.sort.should == %w'2 3 4' Artist.filter(:related_artists=>@artist4).all.map{|a| a.name}.sort.should == %w'1 3 4' + + Artist.exclude(:related_artists=>@artist1).all.map{|a| a.name}.sort.should == %w'3' + Artist.exclude(:related_artists=>@artist2).all.map{|a| a.name}.sort.should == %w'4' + Artist.exclude(:related_artists=>@artist3).all.map{|a| a.name}.sort.should == %w'1' + Artist.exclude(:related_artists=>@artist4).all.map{|a| a.name}.sort.should == %w'2' + + Artist.filter(:related_artists=>[@artist1, @artist4]).all.map{|a| a.name}.sort.should == %w'1 2 3 4' + Artist.exclude(:related_artists=>[@artist1, @artist4]).all.map{|a| a.name}.sort.should == %w'' + + Artist.filter(:related_artists=>Artist.filter(:id=>@artist1.id)).all.map{|a| a.name}.sort.should == %w'1 2 4' + Artist.exclude(:related_artists=>Artist.filter(:id=>@artist1.id)).all.map{|a| a.name}.sort.should == %w'3' end specify "should handle extreme case with 5 join tables" do Artist.many_through_many :related_albums, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_artists, :album_id, :artist_id], [:artists, :id, :id], [:albums_artists, :artist_id, :album_id]], :class=>Album, :distinct=>true @db[:albums_artists].delete @@ -249,10 +286,16 @@ Artist[1].related_albums.map{|x| x.name}.sort.should == %w'A B C' Artist[2].related_albums.map{|x| x.name}.sort.should == %w'A B C D' Artist[3].related_albums.map{|x| x.name}.sort.should == %w'A B D' Artist[4].related_albums.map{|x| x.name}.sort.should == %w'B D' + Artist.plugin :prepared_statements_associations + Artist[1].related_albums.map{|x| x.name}.sort.should == %w'A B C' + Artist[2].related_albums.map{|x| x.name}.sort.should == %w'A B C D' + Artist[3].related_albums.map{|x| x.name}.sort.should == %w'A B D' + Artist[4].related_albums.map{|x| x.name}.sort.should == %w'B D' + Artist.filter(:id=>1).eager(:related_albums).all.map{|x| x.related_albums.map{|a| a.name}}.flatten.sort.should == %w'A B C' Artist.filter(:id=>2).eager(:related_albums).all.map{|x| x.related_albums.map{|a| a.name}}.flatten.sort.should == %w'A B C D' Artist.filter(:id=>3).eager(:related_albums).all.map{|x| x.related_albums.map{|a| a.name}}.flatten.sort.should == %w'A B D' Artist.filter(:id=>4).eager(:related_albums).all.map{|x| x.related_albums.map{|a| a.name}}.flatten.sort.should == %w'B D' @@ -263,10 +306,24 @@ Artist.filter(:related_albums=>@album1).all.map{|a| a.name}.sort.should == %w'1 2 3' Artist.filter(:related_albums=>@album2).all.map{|a| a.name}.sort.should == %w'1 2 3 4' Artist.filter(:related_albums=>@album3).all.map{|a| a.name}.sort.should == %w'1 2' Artist.filter(:related_albums=>@album4).all.map{|a| a.name}.sort.should == %w'2 3 4' + + Artist.exclude(:related_albums=>@album1).all.map{|a| a.name}.sort.should == %w'4' + Artist.exclude(:related_albums=>@album2).all.map{|a| a.name}.sort.should == %w'' + Artist.exclude(:related_albums=>@album3).all.map{|a| a.name}.sort.should == %w'3 4' + Artist.exclude(:related_albums=>@album4).all.map{|a| a.name}.sort.should == %w'1' + + Artist.filter(:related_albums=>[@album1, @album3]).all.map{|a| a.name}.sort.should == %w'1 2 3' + Artist.filter(:related_albums=>[@album3, @album4]).all.map{|a| a.name}.sort.should == %w'1 2 3 4' + + Artist.exclude(:related_albums=>[@album1, @album3]).all.map{|a| a.name}.sort.should == %w'4' + Artist.exclude(:related_albums=>[@album2, @album4]).all.map{|a| a.name}.sort.should == %w'' + + Artist.filter(:related_albums=>Album.filter(:id=>[@album1.id, @album3.id])).all.map{|a| a.name}.sort.should == %w'1 2 3' + Artist.exclude(:related_albums=>Album.filter(:id=>[@album1.id, @album3.id])).all.map{|a| a.name}.sort.should == %w'4' end end describe "Lazy Attributes plugin" do before do @@ -1317,7 +1374,69 @@ it "children should be specified order" do one = Lorem.find(:id => 2) one.children.map{|m| m[:neque]}.should == [1, 2] end + end +end + +describe "Sequel::Plugins::PreparedStatements" do + before do + @db = INTEGRATION_DB + @db.create_table!(:ps_test) do + primary_key :id + String :name + Integer :i + end + @c = Class.new(Sequel::Model(@db[:ps_test])) + @foo = @c.create(:name=>'foo', :i=>10) + @bar = @c.create(:name=>'bar', :i=>20) + @c.plugin :prepared_statements_with_pk + end + after do + @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 + @c[0].should == nil + end + + it "should work with looking up using Dataset#with_pk" do + @c.dataset.with_pk(@foo.id).should == @foo + @c.dataset.with_pk(@bar.id).should == @bar + @c.dataset.with_pk(0).should == nil + + @c.dataset.filter(:i=>0).with_pk(@foo.id).should == nil + @c.dataset.filter(:i=>10).with_pk(@foo.id).should == @foo + @c.dataset.filter(:i=>20).with_pk(@bar.id).should == @bar + @c.dataset.filter(:name=>'foo').with_pk(@foo.id).should == @foo + @c.dataset.filter(:name=>'bar').with_pk(@bar.id).should == @bar + @c.dataset.filter(:name=>'baz').with_pk(@bar.id).should == nil + end + + it "should work with Model#destroy" do + @foo.destroy + @bar.destroy + @c[@foo.id].should == nil + @c[@bar.id].should == nil + end + + it "should work with Model#update" do + @foo.update(:name=>'foo2', :i=>30) + @c[@foo.id].should == @c.load(:id=>@foo.id, :name=>'foo2', :i=>30) + @foo.update(:name=>'foo3') + @c[@foo.id].should == @c.load(:id=>@foo.id, :name=>'foo3', :i=>30) + @foo.update(:i=>40) + @c[@foo.id].should == @c.load(:id=>@foo.id, :name=>'foo3', :i=>40) + end + + it "should work with Model#create" do + o = @c.create(:name=>'foo2', :i=>30) + @c[o.id].should == @c.load(:id=>o.id, :name=>'foo2', :i=>30) + o = @c.create(:name=>'foo2') + @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) end end