youngoli commented on a change in pull request #17003: URL: https://github.com/apache/beam/pull/17003#discussion_r821099752
########## File path: sdks/go/pkg/beam/options/jobopts/options_test.go ########## @@ -0,0 +1,122 @@ +// 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 jobopts + +import ( + "context" + "reflect" + "strings" + "testing" + + "github.com/apache/beam/sdks/v2/go/pkg/beam/core" +) + +func TestGetEndpoint(t *testing.T) { + address := "localhost:8099" + Endpoint = &address + v, err := GetEndpoint() + if err != nil { + t.Fatalf("unexpected error GetEndpoint(): %v", err) + } + if v != address { + t.Errorf("GetEndpoint() = %v, want %v", v, address) + } +} + +func TestGetEndpoint_Bad(t *testing.T) { + address := "" + Endpoint = &address + expectedErr := "no job service endpoint specified. Use --endpoint=<endpoint>" Review comment: If you're going to test the error, I would strongly recommend against string comparison. I recommend creating a custom error type and returning that, and then in the test do type assertion to see if it matches that type. (Example here: https://quii.gitbook.io/learn-go-with-tests/questions-and-answers/error-types) In this case, it doesn't even have to be a custom struct. You can just do something like `type missingFlagError error`. Then return the error as that new type and type assert here. (As an aside, I think it's strange that this one function specifically returns an error while all the others just return the empty values, but it's too late to change that now.) ########## File path: sdks/go/pkg/beam/options/jobopts/options_test.go ########## @@ -0,0 +1,122 @@ +// 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 jobopts + +import ( + "context" + "reflect" + "strings" + "testing" + + "github.com/apache/beam/sdks/v2/go/pkg/beam/core" +) + +func TestGetEndpoint(t *testing.T) { + address := "localhost:8099" + Endpoint = &address + v, err := GetEndpoint() + if err != nil { + t.Fatalf("unexpected error GetEndpoint(): %v", err) + } + if v != address { + t.Errorf("GetEndpoint() = %v, want %v", v, address) + } +} + +func TestGetEndpoint_Bad(t *testing.T) { + address := "" + Endpoint = &address + expectedErr := "no job service endpoint specified. Use --endpoint=<endpoint>" + if _, err := GetEndpoint(); err == nil { + t.Errorf("GetEndpoint() executed incorrectly, expected error: %v", expectedErr) + } +} + +func TestGetJobName(t *testing.T) { + tests := []struct { + jobname string + wantName string + }{ + { + "", + "go-job-", + }, + { + "go-job-example", + "go-job-example", + }, + } + for _, test := range tests { + JobName = &test.jobname + if got := GetJobName(); !strings.Contains(got, test.wantName) { + t.Errorf("GetJobName() = %v, want %v", got, test.wantName) + } + } +} + +func TestGetEnvironamentUrn(t *testing.T) { + tests := []struct { + env string + urn string + }{ Review comment: Since this is for coverage I think you should test each type of possible env to use as input. Or at the very least, add "DOCKER" there (since I see only "docker" and "loopback" are actually documented in the flag description). -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
