test/test_rhubarb.rb in rhubarb-0.2.6 vs test/test_rhubarb.rb in rhubarb-0.2.7

- old
+ new

@@ -76,21 +76,33 @@ class ToRef include Rhubarb::Persisting declare_column :foo, :string end -class FromRef +class FromRef include Rhubarb::Persisting declare_column :t, :integer, references(ToRef, :on_delete=>:cascade) + declare_query :to_is, "t = ?" + declare_query :to_is_hash, "t = :t" end class FreshTestTable include Rhubarb::Persisting declare_column :fee, :integer declare_column :fie, :integer declare_column :foe, :integer declare_column :fum, :integer + declare_column :foo, :integer + declare_column :bar, :integer + + def <=>(other) + [:fie,:foe,:fum,:foo,:bar].each do |msg| + tmpresult = self.send(msg) <=> other.send(msg) + return tmpresult unless tmpresult == 0 + end + return 0 + end end class BlobTestTable include Rhubarb::Persisting declare_column :info, :blob @@ -639,10 +651,14 @@ tc1p = TestClass.find(tc1.row_id) assert_equal(tc1, tc1) # equality is reflexive assert_equal(tc1p, tc1) # even after find operations assert_equal(tc1, tc1p) # ... and it should be symmetric + + assert_equal(tc1.hash, tc1p.hash) + assert_not_equal(tc2.hash, tc1p.hash) + assert_not_equal(tc1, tc2) # these are not identical assert_not_equal(tc1p, tc2) # even after find operations assert_not_equal(tc2, tc1p) # ... and it should be symmetric assert_not_same(tc1, tc2) # but these are not identical assert_not_equal(tc1, tc3) # these aren't even equal! @@ -651,24 +667,43 @@ assert_not_equal(tc3, tc2) # under symmetry end def freshness_query_fixture @flist = [] + @fresh_fields = [:fee,:fie,:foe,:fum,:foo,:bar,:created,:updated,:row_id] + @ffcounts = {:fie=>2, :foe=>3, :fum=>4, :foo=>5, :bar=>6} - 0.upto(99) do |x| - @flist << FreshTestTable.create(:fee=>x, :fie=>(x%7), :foe=>(x%11), :fum=>(x%13)) + x = 0 + + 2.times do + @ffcounts[:fie].times do |fie| + @ffcounts[:foe].times do |foe| + @ffcounts[:fum].times do |fum| + @ffcounts[:foo].times do |foo| + @ffcounts[:bar].times do |bar| + row = FreshTestTable.create(:fee=>@flist.size, :fie=>fie, :foe=>foe, :fum=>fum, :foo=>foo, :bar=>bar) + assert(row != nil) + @flist << row + end + end + end + end + end end + + @fcount = @flist.size + @ffcounts[:fee] = @fcount end def test_freshness_query_basic freshness_query_fixture # basic test basic = FreshTestTable.find_freshest(:group_by=>[:fee]) assert_equal(@flist.size, basic.size) - 0.upto(99) do |x| - [:fee,:fie,:foe,:fum,:created,:updated,:row_id].each do |msg| + 0.upto(@fcount - 1) do |x| + @fresh_fields.each do |msg| assert_equal(@flist[x].send(msg), basic[x].send(msg)) end end end @@ -678,56 +713,89 @@ basic = FreshTestTable.find_freshest(:group_by=>[:fee], :version=>@flist[30].created, :debug=>true) assert_equal(31, basic.size) 0.upto(30) do |x| - [:fee,:fie,:foe,:fum,:created,:updated,:row_id].each do |msg| + @fresh_fields.each do |msg| assert_equal(@flist[x].send(msg), basic[x].send(msg)) end end end def test_freshness_query_basic_select freshness_query_fixture # basic test - basic = FreshTestTable.find_freshest(:group_by=>[:fee], :select_by=>{:fie=>0}, :debug=>true) + ffc = @ffcounts[:fie] - expected_ct = 99/7 + 1; + basic = FreshTestTable.find_freshest(:group_by=>[:fee], :select_by=>{:fie=>0}, :debug=>true).sort_by {|x| x.fee} + expected_ct = @fcount/ffc + expected_rows = @flist.select{|tup| tup.fie == 0}.sort_by{|tup| tup.fee} + assert_equal(expected_ct, basic.size) 0.upto(expected_ct - 1) do |x| - [:fee,:fie,:foe,:fum,:created,:updated,:row_id].each do |msg| - assert_equal(@flist[x*7].send(msg), basic[x].send(msg)) + @fresh_fields.each do |msg| + assert_equal(expected_rows[x].send(msg), basic[x].send(msg)) end end end def test_freshness_query_group_single freshness_query_fixture # more basic tests - pairs = {:fie=>7,:foe=>11,:fum=>13} + pairs = @ffcounts.dup + pairs.delete(:fee) + pairs.each do |col,ct| basic = FreshTestTable.find_freshest(:group_by=>[col]) assert_equal(ct,basic.size) expected_objs = {} - 99.downto(99-ct+1) do |x| - expected_objs[x%ct] = @flist[x] + needed_vals = Set[*(0..ct).to_a] + + @flist.reverse_each do |row| + break if needed_vals.empty? + + colval = row.send(col) + if needed_vals.include? colval + expected_objs[colval] = row + needed_vals.delete(colval) + end end basic.each do |row| res = expected_objs[row.send(col)] - [:fee,:fie,:foe,:fum,:created,:updated,:row_id].each do |msg| + puts expected_objs.keys.inspect if res.nil? + @fresh_fields.each do |msg| assert_equal(res.send(msg), row.send(msg)) end end end end + def test_freshness_query_group_powerset + freshness_query_fixture + # more basic tests + + pairs = @ffcounts.dup + pairs.delete(:fee) + pairs_powerset = pairs.to_a.inject([[]]){|c,y|r=[];c.each{|i|r<<i;r<<i+[y]};r}.map {|h| h.transpose} + pairs_powerset.shift + + pairs_powerset.each do |cols,cts| + expected_ct = cts.inject(1){|x,acc| acc * x} + basic = FreshTestTable.find_freshest(:group_by=>cols) + + assert_equal(expected_ct,basic.size) + + # XXX: test identities, too + end + end + def test_blob_data words = %w{the quick brown fox jumps over a lazy dog now is time for all good men to come aid party jackdaws love my big sphinx of quartz} text = "" (0..300).each do text << words[rand(words.length)] << " " @@ -795,9 +863,28 @@ assert_equal otts.size, things.size things.zip(otts) do |thing, ott| assert_equal thing.class.name.to_s, ott.otype assert_equal thing, ott.obj + end + end + + [true, false].each do |create_uses_rowid| + [true, false].each do |lookup_uses_rowid| + [true, false].each do |to_is_hash| + + define_method("test_reference_params_in_#{to_is_hash ? "hashed_" : ""}custom_queries_#{create_uses_rowid ? "createUsesRowID" : "createUsesObject"}_#{lookup_uses_rowid ? "lookupUsesRowID" : "lookupUsesObject"}".to_sym) do + to_refs = %w{foo bar blah}.map {|s| ToRef.create(:foo=>s)} + to_refs.each {} + to_refs.each {|tr| FromRef.create(:t=>create_uses_rowid ? tr.row_id : tr)} + + to_refs.each do |tr| + frs = to_is_hash ? FromRef.to_is_hash(:t=>lookup_uses_rowid ? tr.row_id : tr) : FromRef.to_is(lookup_uses_rowid ? tr.row_id : tr) + assert_equal frs.size, 1 + assert_equal frs[0].t, tr + end + end + end end end end class NoPreparedStmtBackendTests < PreparedStmtBackendTests