Skip to content
Browse files

Added integration tests for update/insert/import with CTE

  • Loading branch information...
1 parent bc59602 commit 71dc1b5d6f3e1c9847ff42e48030a408332a1bf2 David Lee committed Aug 27, 2009
Showing with 46 additions and 0 deletions.
  1. +46 −0 spec/integration/dataset_test.rb
View
46 spec/integration/dataset_test.rb
@@ -342,7 +342,9 @@
before do
@db = INTEGRATION_DB
@db.create_table!(:i1){Integer :id; Integer :parent_id}
+ @db.create_table!(:i2){Integer :id; Integer :parent_id}
@ds = @db[:i1]
+ @ds2 = @db[:i2]
@ds.insert(:id=>1)
@ds.insert(:id=>2)
@ds.insert(:id=>3, :parent_id=>1)
@@ -352,6 +354,7 @@
end
after do
@db.drop_table(:i1)
+ @db.drop_table(:i2)
end
specify "should give correct results for WITH" do
@@ -366,6 +369,49 @@
ps.call(:n=>3).should == [{:id=>5, :parent_id=>3}, {:id=>6, :parent_id=>5}]
ps.call(:n=>5).should == [{:id=>6, :parent_id=>5}]
end
+
+ specify "using #with should be able to update" do
+ @ds.insert(:id=>1)
+ @ds2.insert(:id=>2, :parent_id=>1)
+ @ds2.insert(:id=>3, :parent_id=>2)
+ @ds.with(:t, @ds2).filter(:id => @db[:t].select(:id)).update(:parent_id => :t__parent_id)
+ @ds[:id => 1].should == {:id => 1, :parent_id => nil}
+ @ds[:id => 2].should == {:id => 2, :parent_id => 1}
+ @ds[:id => 3].should == {:id => 3, :parent_id => 2}
+ @ds[:id => 4].should == {:id => 4, :parent_id => 1}
+ end
+
+ specify "using #with_recursive should be able to update" do
+ ds = @ds.with_recursive(:t, @ds.filter(:parent_id=>1), @ds.join(:t, :i=>:parent_id).select(:i1__id, :i1__parent_id), :args=>[:i, :pi])
+ ds.filter(~{:id => @ds[:t]}).update(:parent_id => 1)
+ @ds[:id => 1].should == {:id => 1, :parent_id => nil}
+ @ds[:id => 2].should == {:id => 2, :parent_id => 1}
+ @ds[:id => 5].should == {:id => 5, :parent_id => 3}
+ end
+
+ specify "using #with should be able to insert" do
+ @ds2.insert(:id=>7)
+ @ds.with(:t, @ds2).insert(@db[:t])
+ @ds[:id => 7].should == {:id => 7, :parent_id => nil}
+ end
+
+ specify "using #with_recursive should be able to insert" do
+ ds = @ds2.with_recursive(:t, @ds.filter(:parent_id=>1), @ds.join(:t, :i=>:parent_id).select(:i1__id, :i1__parent_id), :args=>[:i, :pi])
+ ds.insert @db[:t]
+ @ds2.all.should == [{:id => 3, :parent_id => 1}, {:id => 4, :parent_id => 1}, {:id => 5, :parent_id => 3}, {:id => 6, :parent_id => 5}]
+ end
+
+ specify "using #with should be able to import" do
+ @ds2.insert(:id=>7)
+ @ds.with(:t, @ds2).import [:id, :parent_id], @db[:t].select(:id, :parent_id)
+ @ds[:id => 7].should == {:id => 7, :parent_id => nil}
+ end
+
+ specify "using #with_recursive should be able to insert" do
+ ds = @ds2.with_recursive(:t, @ds.filter(:parent_id=>1), @ds.join(:t, :i=>:parent_id).select(:i1__id, :i1__parent_id), :args=>[:i, :pi])
+ ds.import [:id, :parent_id], @db[:t].select(:i, :pi)
+ @ds2.all.should == [{:id => 3, :parent_id => 1}, {:id => 4, :parent_id => 1}, {:id => 5, :parent_id => 3}, {:id => 6, :parent_id => 5}]
+ end
end
end

0 comments on commit 71dc1b5

Please sign in to comment.
Something went wrong with that request. Please try again.