Skip to content

Commit

Permalink
[BEAM-12548] Add EqualsList functionality to PAssert (#15110)
Browse files Browse the repository at this point in the history
* [BEAM-12548] Add EqualsList functionality, corresponding unit tests. Also refactors equals_test.go to use better naming conventions
* [BEAM-12548] Add clarifying comment to EqualsList
  • Loading branch information
jrmccluskey authored Jun 30, 2021
1 parent 07bf18a commit 7b30339
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 45 deletions.
14 changes: 14 additions & 0 deletions sdks/go/pkg/beam/testing/passert/equals.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ func Equals(s beam.Scope, col beam.PCollection, values ...interface{}) beam.PCol
return equals(subScope, col, other)
}

// EqualsList verifies that the given collection has the same values as a
// given list, under coder equality. The values must be provided as an
// array or slice. This is equivalent to passing a beam.CreateList PCollection
// to Equals.
func EqualsList(s beam.Scope, col beam.PCollection, list interface{}) beam.PCollection {
subScope := s.Scope("passert.EqualsList")
if list == nil {
return Empty(subScope, col)
}
listCollection := beam.CreateList(subScope, list)
return equals(subScope, col, listCollection)
}

// equals verifies that the actual values match the expected ones.
func equals(s beam.Scope, actual, expected beam.PCollection) beam.PCollection {
unexpected, correct, missing := Diff(s, actual, expected)
Expand Down Expand Up @@ -100,3 +113,4 @@ func readToStrings(iter func(*beam.T) bool) []string {
sort.Strings(out)
return out
}

123 changes: 78 additions & 45 deletions sdks/go/pkg/beam/testing/passert/equals_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"github.com/apache/beam/sdks/go/pkg/beam/testing/ptest"
)

func TestGood(t *testing.T) {
func TestEquals_Good(t *testing.T) {
p, s := beam.NewPipelineWithRoot()
wantC := beam.Create(s, "c", "b", "a")
gotC := beam.Create(s, "a", "b", "c")
Expand All @@ -34,51 +34,63 @@ func TestGood(t *testing.T) {
}
}

func TestBad(t *testing.T) {
tests := []struct {
name string
actual []string
expected []string
errorParts []string
}{
{
"missing entry",
[]string{"a", "b"},
[]string{"a", "b", "MISSING"},
[]string{"2 correct entries", "0 unexpected entries", "1 missing entries", "MISSING"},
},
{
"unexpected entry",
[]string{"a", "b", "UNEXPECTED"},
[]string{"a", "b"},
[]string{"2 correct entries", "1 unexpected entries", "0 missing entries", "UNEXPECTED"},
},
{
"both kinds of problem",
[]string{"a", "b", "UNEXPECTED"},
[]string{"a", "b", "MISSING"},
[]string{"2 correct entries", "1 unexpected entries", "1 missing entries", "UNEXPECTED", "MISSING"},
},
{
"not enough",
[]string{"not enough"},
[]string{"not enough", "not enough"},
[]string{"1 correct entries", "0 unexpected entries", "1 missing entries", "not enough"},
},
{
"too many",
[]string{"too many", "too many"},
[]string{"too many"},
[]string{"1 correct entries", "1 unexpected entries", "0 missing entries", "too many"},
},
{
"both kinds of wrong count",
[]string{"too many", "too many", "not enough"},
[]string{"not enough", "too many", "not enough"},
[]string{"2 correct entries", "1 unexpected entries", "1 missing entries", "too many", "not enough"},
},
func TestEqualsList_Good(t *testing.T) {
p, s := beam.NewPipelineWithRoot()
wantL := [3]string{"c", "b", "a"}
gotC := beam.Create(s, "a", "b", "c")

EqualsList(s, gotC, wantL)
if err := ptest.Run(p); err != nil {
t.Errorf("Pipeline failed: %v", err)
}
for _, tc := range tests {
}

var badEqualsTests = []struct {
name string
actual []string
expected []string
errorParts []string
}{
{
"missing entry",
[]string{"a", "b"},
[]string{"a", "b", "MISSING"},
[]string{"2 correct entries", "0 unexpected entries", "1 missing entries", "MISSING"},
},
{
"unexpected entry",
[]string{"a", "b", "UNEXPECTED"},
[]string{"a", "b"},
[]string{"2 correct entries", "1 unexpected entries", "0 missing entries", "UNEXPECTED"},
},
{
"both kinds of problem",
[]string{"a", "b", "UNEXPECTED"},
[]string{"a", "b", "MISSING"},
[]string{"2 correct entries", "1 unexpected entries", "1 missing entries", "UNEXPECTED", "MISSING"},
},
{
"not enough",
[]string{"not enough"},
[]string{"not enough", "not enough"},
[]string{"1 correct entries", "0 unexpected entries", "1 missing entries", "not enough"},
},
{
"too many",
[]string{"too many", "too many"},
[]string{"too many"},
[]string{"1 correct entries", "1 unexpected entries", "0 missing entries", "too many"},
},
{
"both kinds of wrong count",
[]string{"too many", "too many", "not enough"},
[]string{"not enough", "too many", "not enough"},
[]string{"2 correct entries", "1 unexpected entries", "1 missing entries", "too many", "not enough"},
},
}

func TestEquals_Bad(t *testing.T) {
for _, tc := range badEqualsTests {
p, s := beam.NewPipelineWithRoot()
out := Equals(s, beam.CreateList(s, tc.actual), beam.CreateList(s, tc.expected))
if err := ptest.Run(p); err == nil {
Expand All @@ -97,3 +109,24 @@ func TestBad(t *testing.T) {
}
}
}

func TestEqualsList_Bad(t *testing.T) {
for _, tc := range badEqualsTests {
p, s := beam.NewPipelineWithRoot()
out := EqualsList(s, beam.CreateList(s, tc.actual), tc.expected)
if err := ptest.Run(p); err == nil {
t.Errorf("%v: pipeline SUCCEEDED but should have failed; got %v", tc.name, out)
} else {
str := err.Error()
missing := []string{}
for _, part := range tc.errorParts {
if !strings.Contains(str, part) {
missing = append(missing, part)
}
}
if len(missing) != 0 {
t.Errorf("%v: pipeline failed correctly, but substrings %#v are not present in message:\n%v", tc.name, missing, str)
}
}
}
}

0 comments on commit 7b30339

Please sign in to comment.