Skip to content

Commit

Permalink
chore: refactor tests
Browse files Browse the repository at this point in the history
align error message with expected format
refactor testing params for TestListLogEntriesRequest
  • Loading branch information
minherz committed Feb 14, 2022
1 parent 2f5524b commit 2f54167
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions logging/logadmin/logadmin_test.go
Expand Up @@ -30,7 +30,6 @@ import (
"cloud.google.com/go/internal/testutil"
"cloud.google.com/go/logging"
ltesting "cloud.google.com/go/logging/internal/testing"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
durpb "github.com/golang/protobuf/ptypes/duration"
structpb "github.com/golang/protobuf/ptypes/struct"
Expand Down Expand Up @@ -269,11 +268,11 @@ func TestListLogEntriesRequestDefaults(t *testing.T) {

// parse time from filter
if len(got.Filter) < len(timeFilterPrefix) {
t.Errorf("expect default filter to start with %v; got %v", timeFilterPrefix, got.Filter)
t.Errorf("got %v; want len(%v) start with '%v'", got, got.Filter, timeFilterPrefix)
}
filterTime, e := time.Parse(time.RFC3339, strings.Trim(got.Filter[len(timeFilterPrefix):], "\""))
if e != nil {
t.Errorf("expect default filter to have time in RFC3339; got %v", got.Filter)
filterTime, err := time.Parse(time.RFC3339, strings.Trim(got.Filter[len(timeFilterPrefix):], "\""))
if err != nil {
t.Errorf("%v: expect default filter to have time in RFC3339; got %v", err, got.Filter)
}
timeDiff := time.Now().UTC().Sub(filterTime)

Expand All @@ -287,35 +286,34 @@ func TestListLogEntriesRequest(t *testing.T) {
for _, test := range []struct {
opts []EntriesOption
resourceNames []string
filter string
filterPrefix string
orderBy string
}{
// Timestamp default does not override user's filter
{[]EntriesOption{NewestFirst(), Filter(`timestamp > "2020-10-30T15:39:09Z"`)},
[]string{"projects/PROJECT_ID"}, `timestamp > "2020-10-30T15:39:09Z"`, "", "timestamp desc"},
[]string{"projects/PROJECT_ID"}, `timestamp > "2020-10-30T15:39:09Z"`, "timestamp desc"},
{[]EntriesOption{NewestFirst(), Filter("f")},
[]string{"projects/PROJECT_ID"}, "", "f AND timestamp >= \"", "timestamp desc"},
[]string{"projects/PROJECT_ID"}, "f AND timestamp >= \"", "timestamp desc"},
{[]EntriesOption{ProjectIDs([]string{"foo"})},
[]string{"projects/foo"}, "", "timestamp >= \"", ""},
[]string{"projects/foo"}, "timestamp >= \"", ""},
{[]EntriesOption{ResourceNames([]string{"folders/F", "organizations/O"})},
[]string{"folders/F", "organizations/O"}, "", "timestamp >= \"", ""},
[]string{"folders/F", "organizations/O"}, "timestamp >= \"", ""},
{[]EntriesOption{NewestFirst(), Filter("f"), ProjectIDs([]string{"foo"})},
[]string{"projects/foo"}, "", "f AND timestamp >= \"", "timestamp desc"},
[]string{"projects/foo"}, "f AND timestamp >= \"", "timestamp desc"},
{[]EntriesOption{NewestFirst(), Filter("f"), ProjectIDs([]string{"foo"})},
[]string{"projects/foo"}, "", "f AND timestamp >= \"", "timestamp desc"},
[]string{"projects/foo"}, "f AND timestamp >= \"", "timestamp desc"},
// If there are repeats, last one wins.
{[]EntriesOption{NewestFirst(), Filter("no"), ProjectIDs([]string{"foo"}), Filter("f")},
[]string{"projects/foo"}, "", "f AND timestamp >= \"", "timestamp desc"},
[]string{"projects/foo"}, "f AND timestamp >= \"", "timestamp desc"},
} {
got := listLogEntriesRequest("projects/PROJECT_ID", test.opts)
want := &logpb.ListLogEntriesRequest{
ResourceNames: test.resourceNames,
Filter: test.filter,
Filter: test.filterPrefix,
OrderBy: test.orderBy,
}
if (test.filterPrefix == "" && !proto.Equal(got, want)) || !strings.HasPrefix(got.Filter, test.filterPrefix) || got.OrderBy != test.orderBy || !testutil.Equal(got.ResourceNames, test.resourceNames) {
t.Errorf("%v:\ngot %v\nwant %v", test.opts, got, want)
if !testutil.Equal(got.ResourceNames, want.ResourceNames) || !strings.HasPrefix(got.Filter, want.Filter) || got.OrderBy != want.OrderBy {
t.Errorf("got: %v; want %v (mind wanted Filter is prefix)", got, want)
}
}
}

0 comments on commit 2f54167

Please sign in to comment.