You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by GitBox <gi...@apache.org> on 2021/07/07 17:06:56 UTC

[GitHub] [beam] lostluck commented on a change in pull request #15131: [BEAM-12548] Add unit tests for sum function to passert package

lostluck commented on a change in pull request #15131:
URL: https://github.com/apache/beam/pull/15131#discussion_r665538667



##########
File path: sdks/go/pkg/beam/testing/passert/sum_test.go
##########
@@ -0,0 +1,111 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package passert
+
+import (
+        "strings"
+	"testing"

Review comment:
       You'll want to add "go fmt" to your save hooks. Go is big on automatic formatting of the code, and changing things to tabs rather than spaces.

##########
File path: sdks/go/pkg/beam/testing/passert/sum_test.go
##########
@@ -0,0 +1,111 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package passert
+
+import (
+        "strings"
+	"testing"
+
+        "github.com/apache/beam/sdks/go/pkg/beam"
+        "github.com/apache/beam/sdks/go/pkg/beam/testing/ptest"
+)
+
+func TestSum_good(t *testing.T) {
+	tests := []struct {
+		name   string
+		values []int
+		size   int
+		total  int
+	}{
+		{
+			"all positive",
+			[]int{1, 2, 3, 4, 5},
+			5,
+			15,
+		},
+		{
+			"all negative",
+			[]int{-1, -2, -3, -4, -5},
+			5,
+			-15,
+		},
+		{
+			"mixed",
+			[]int{1, -2, 3, -4, 5},
+			5,
+			3,
+		},
+		{
+			"empty",
+			[]int{},
+			0,
+			0,
+		},
+	}
+	for _, tc := range tests {
+		p, s := beam.NewPipelineWithRoot()
+		col := beam.CreateList(s, tc.values)
+		Sum(s, col, tc.name, tc.size, tc.total)
+		if err := ptest.Run(p); err != nil {
+			t.Errorf("Pipeline failed: %v", err)
+		}
+	}
+}
+
+func TestSum_bad(t *testing.T) {
+	tests := []struct {
+		name       string
+		col        []int
+		size       int
+		total      int
+		errorParts []string
+	}{
+		{
+			"bad size",
+			[]int{1, 2, 3, 4, 5},
+			4,
+			15,
+			[]string{"{15, size: 5}", "want {15, size:4}"},
+		},
+		{
+			"bad total",
+			[]int{1, 2, 3, 4, 5},
+			5,
+			16,
+			[]string{"{15, size: 5}", "want {16, size:5}"},
+		},
+	}
+	for _, tc := range tests {
+		p, s := beam.NewPipelineWithRoot()
+		col := beam.CreateList(s, tc.col)
+		Sum(s, col, tc.name, tc.size, tc.total)
+		if err := ptest.Run(p); err == nil {
+			t.Errorf("Pipeline succeeded but should have failed: %v", tc.name)

Review comment:
       Go Style wise, the idiomatic way of writing this is to use a t.Fatalf instead of an t.Errorf, and avoid the indentation that an else block ads. t.Fatalf aborts the test, so there's no need to put things into an else block.
   
   Note that this will abort any subsequent test cases, since the body of the test is not using a sub test, using t.Run. See https://blog.golang.org/subtests for more details.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org