Class: Oinky::Test::DBCompare

Inherits:
Object
  • Object
show all
Defined in:
lib/oinky/testsup.rb

Instance Method Summary collapse

Constructor Details

#initializeDBCompare

Returns a new instance of DBCompare.



97
98
99
# File 'lib/oinky/testsup.rb', line 97

def initialize
  @context = []
end

Instance Method Details

#check_eq(l, r) ⇒ Object

Raises:

  • (RuntimeError)


7
8
9
# File 'lib/oinky/testsup.rb', line 7

def check_eq(l,r)
  raise RuntimeError.new("Inequality: [#{l.inspect}] != [#{r.inspect}]  Context:[#{@context.to_s}]") unless l == r
end

#compare(l, r) ⇒ Object



88
89
90
91
92
93
94
95
96
# File 'lib/oinky/testsup.rb', line 88

def compare(l, r)
  begin
    kv_compare(l.tables, r.tables, self.method(:table_compare))
  rescue StandardError => e
    puts "Exception comparing database instances: [#{@context}]"
    raise e
  end
  self
end

#index_compare(l, r) ⇒ Object



78
79
80
81
82
83
84
85
86
# File 'lib/oinky/testsup.rb', line 78

def index_compare(l, r)
  kv_compare(l.definition, r.definition, self.method(:check_eq))
  check_eq(l.row_count, r.row_count)
  # We will only compare the indexed columns.  Building the selectors
  # outside of the loop saves work.
  ls = l.selector
  rs = r.selector
  seq_compare(l.each(ls), r.each(rs), self.method(:check_eq), l.definition[:unique])
end

#kv_compare(ls, rs, fn) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/oinky/testsup.rb', line 11

def kv_compare(ls, rs, fn)
  check_eq(ls.size,rs.size)
  ls.each{|k,v|
    @context.push k
    fn.call(v, rs[k])
    @context.pop
  }
end

#seq_compare(ls, rs, fn, neq_prev = false) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/oinky/testsup.rb', line 19

def seq_compare(ls, rs, fn, neq_prev = false)
  l = ls.each
  r = rs.each
  count = 0
  last = self
  begin
    while true
      left = true
      lv = l.next
      left = false
      rv = r.next
      @context.push count
      fn.call(lv,rv)

      # If we are verifying that each list is also unique, then
      # check against the previous value
      if neq_prev and (last != self)
        # We EXPECT this comparison to raise NEQ
        peq = false
        begin
          fn.call(last, lv)
          peq = true
        rescue
        end
        if peq
          check_eq("Sequence is not unique",nil)
        end
      end
      last = lv

      @context.pop
      count += 1
    end
  rescue StopIteration
    # Left should have raised.
    check_eq(left, true)
  end
  # verify that the right enumerator is also done
  begin
    r.next
    # These are obviously not equal if the above didn't raise.
    check_eq("nonempty enumerator",nil)
  rescue StopIteration
  end
end

#table_compare(l, r) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/oinky/testsup.rb', line 65

def table_compare(l, r)
  kv_compare(l.columns, r.columns, self.method(:check_eq))
  check_eq(l.row_count, r.row_count)
  # Table ordering is not guaranteed to match.  We have to sort.
  # However, we may have to sort over variant types, so convert
  # everything to a string before ordering.
  seq_compare(
              l.cursor_each.map {|c| c.select_all_values.map{|v| v.to_s} }.sort, 
              r.cursor_each.map {|c| c.select_all_values.map{|v| v.to_s} }.sort, 
              self.method(:check_eq))
  kv_compare(l.indices, r.indices, self.method(:index_compare))
end