Skip to content

Commit

Permalink
exec: verify test results based on set or ordered comparison
Browse files Browse the repository at this point in the history
Before my recent change (#34924), test results were compared in
order; after the change, test results are compared using a set
comparison (i.e. whether two sets contain the same tuples).
However, we should be able to use one or the other based on what
we are testing, so this commit adds this ability.

Release note: None
  • Loading branch information
yuzefovich committed Feb 20, 2019
1 parent 0cc63ad commit 2c64f97
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 14 deletions.
8 changes: 4 additions & 4 deletions pkg/sql/exec/aggregator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ func TestAggregatorOneFunc(t *testing.T) {
// Explicitly reinitialize the aggregator with the given output batch
// size.
a.(*orderedAggregator).initWithBatchSize(tc.batchSize, tc.outputBatchSize)
if err := out.Verify(); err != nil {
if err := out.VerifyAnyOrder(); err != nil {
t.Fatal(err)
}

Expand All @@ -255,7 +255,7 @@ func TestAggregatorOneFunc(t *testing.T) {
t.Fatal(err)
}
out := newOpTestOutput(a, []int{0}, tc.expected)
if err := out.Verify(); err != nil {
if err := out.VerifyAnyOrder(); err != nil {
t.Fatal(err)
}
})
Expand Down Expand Up @@ -341,7 +341,7 @@ func TestAggregatorMultiFunc(t *testing.T) {
t.Fatal(err)
}
out := newOpTestOutput(a, []int{0, 1}, tc.expected)
if err := out.Verify(); err != nil {
if err := out.VerifyAnyOrder(); err != nil {
t.Fatal(err)
}
})
Expand Down Expand Up @@ -392,7 +392,7 @@ func TestAggregatorKitchenSink(t *testing.T) {
t.Fatal(err)
}
out := newOpTestOutput(a, []int{0, 1, 2, 3}, tc.expected)
if err := out.Verify(); err != nil {
if err := out.VerifyAnyOrder(); err != nil {
t.Fatal(err)
}
})
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/exec/count_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestCount(t *testing.T) {
count := NewCountOp(input[0])
out := newOpTestOutput(count, []int{0}, tc.expected)

if err := out.Verify(); err != nil {
if err := out.VerifyAnyOrder(); err != nil {
t.Fatal(err)
}
})
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/exec/distinct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestSortedDistinct(t *testing.T) {
}
out := newOpTestOutput(distinct, []int{0, 1, 2, 3}, tc.expected)

if err := out.Verify(); err != nil {
if err := out.VerifyAnyOrder(); err != nil {
t.Fatal(err)
}
})
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/exec/hashjoiner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ func TestHashJoinerInt64(t *testing.T) {

out := newOpTestOutput(hj, cols, tc.expectedTuples)

if err := out.Verify(); err != nil {
if err := out.VerifyAnyOrder(); err != nil {
t.Fatal(err)
}
})
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/exec/limit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func TestLimit(t *testing.T) {
limit := NewLimitOp(input[0], tc.limit)
out := newOpTestOutput(limit, []int{0}, tc.expected)

if err := out.Verify(); err != nil {
if err := out.VerifyAnyOrder(); err != nil {
t.Fatal(err)
}
})
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/exec/offset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestOffset(t *testing.T) {
s := NewOffsetOp(input[0], tc.offset)
out := newOpTestOutput(s, []int{0}, tc.expected)

if err := out.Verify(); err != nil {
if err := out.VerifyAnyOrder(); err != nil {
t.Fatal(err)
}
})
Expand Down
41 changes: 36 additions & 5 deletions pkg/sql/exec/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,27 @@ func (r *opTestOutput) next() tuple {
}

// Verify ensures that the input to this opTestOutput produced the same results
// as the ones expected in the opTestOutput's expected tuples, using a slow,
// and in the same order as the ones expected in the opTestOutput's expected
// tuples, using a slow, reflection-based comparison method, returning an error
// if the input isn't equal to the expected.
func (r *opTestOutput) Verify() error {
var actual tuples
for {
tup := r.next()
if tup == nil {
break
}
actual = append(actual, tup)
}
return assertTuplesOrderedEqual(r.expected, actual)
}

// VerifyAnyOrder ensures that the input to this opTestOutput produced the same
// results but in any order (meaning set comparison behavior is used) as the
// ones expected in the opTestOutput's expected tuples, using a slow,
// reflection-based comparison method, returning an error if the input isn't
// equal to the expected.
func (r *opTestOutput) Verify() error {
func (r *opTestOutput) VerifyAnyOrder() error {
var actual tuples
for {
tup := r.next()
Expand All @@ -266,7 +283,7 @@ func (r *opTestOutput) Verify() error {
}
actual = append(actual, tup)
}
return assertTuplesEquals(r.expected, actual)
return assertTuplesSetsEqual(r.expected, actual)
}

// tupleEquals checks that two tuples are equal, using a slow,
Expand All @@ -290,8 +307,8 @@ func tupleEquals(expected tuple, actual tuple) bool {
return true
}

// assertTuplesEquals asserts that two sets of tuples are equal.
func assertTuplesEquals(expected tuples, actual tuples) error {
// assertTuplesSetsEqual asserts that two sets of tuples are equal.
func assertTuplesSetsEqual(expected tuples, actual tuples) error {
if len(expected) != len(actual) {
return errors.Errorf("expected %+v, actual %+v", expected, actual)
}
Expand All @@ -314,6 +331,20 @@ func assertTuplesEquals(expected tuples, actual tuples) error {
return nil
}

// assertTuplesOrderedEqual asserts that two permutations of tuples are equal
// in order.
func assertTuplesOrderedEqual(expected tuples, actual tuples) error {
if len(expected) != len(actual) {
return errors.Errorf("expected %+v, actual %+v", expected, actual)
}
for i := range expected {
if !tupleEquals(expected[i], actual[i]) {
return errors.Errorf("expected %+v, actual %+v\n", expected, actual)
}
}
return nil
}

// repeatableBatchSource is an Operator that returns the same batch forever.
type repeatableBatchSource struct {
internalBatch ColBatch
Expand Down

0 comments on commit 2c64f97

Please sign in to comment.