Skip to content

Commit

Permalink
test: improve test cases and use a better struct comparator
Browse files Browse the repository at this point in the history
  • Loading branch information
christiangda committed Oct 1, 2023
1 parent 9a40b4c commit f78a3da
Show file tree
Hide file tree
Showing 8 changed files with 413 additions and 200 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"Babs",
"bjensen",
"christiangda",
"cmpopts",
"codecov",
"codeql",
"Debugf",
Expand Down
4 changes: 2 additions & 2 deletions internal/core/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestWithIdentityProviderGroupsFilter(t *testing.T) {
}

if !reflect.DeepEqual(got, want) {
t.Errorf("got = %s, want %s", convert.ToJSONString(got, true), convert.ToJSONString(want, true))
t.Errorf("NewSyncService() got = %s, want %s", convert.ToJSONString(got, true), convert.ToJSONString(want, true))
}
})
}
Expand Down Expand Up @@ -102,7 +102,7 @@ func TestWithIdentityProviderUsersFilter(t *testing.T) {
}

if !reflect.DeepEqual(got, want) {
t.Errorf("got = %s, want %s", convert.ToJSONString(got, true), convert.ToJSONString(want, true))
t.Errorf("NewSyncService() got = %s, want %s", convert.ToJSONString(got, true), convert.ToJSONString(want, true))
}
})
}
4 changes: 0 additions & 4 deletions internal/idp/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,6 @@ func Test_buildUser(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
got := buildUser(tt.given)

// if !reflect.DeepEqual(got, tt.want) {
// t.Errorf("buildUser() got = %v, want %v", string(utils.ToJSON(got)), string(utils.ToJSON(tt.want)))
// }

sort := func(x, y string) bool { return x > y }
if diff := cmp.Diff(tt.want, got, cmpopts.SortSlices(sort)); diff != "" {
t.Errorf("mismatch (-want +got):\n%s", diff)
Expand Down
33 changes: 21 additions & 12 deletions internal/idp/idp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package idp
import (
"context"
"errors"
"reflect"
"testing"

gomock "github.com/golang/mock/gomock"
"github.com/slashdevops/idp-scim-sync/internal/convert"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/slashdevops/idp-scim-sync/internal/model"

mocks "github.com/slashdevops/idp-scim-sync/mocks/idp"
Expand Down Expand Up @@ -146,8 +146,10 @@ func TestGetGroups(t *testing.T) {
t.Errorf("GoogleProvider.GetGroups() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GoogleProvider.GetGroups() = %s, want %s", convert.ToJSONString(got, true), convert.ToJSONString(tt.want, true))

sort := func(x, y string) bool { return x > y }
if diff := cmp.Diff(tt.want, got, cmpopts.SortSlices(sort)); diff != "" {
t.Errorf("GetGroups() (-want +got):\n%s", diff)
}
})
}
Expand Down Expand Up @@ -320,8 +322,10 @@ func TestGetUsers(t *testing.T) {
t.Errorf("GoogleProvider.GetUsers() got error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GoogleProvider.GetUsers() got = %s, want %s", convert.ToJSONString(got, true), convert.ToJSONString(tt.want, true))

sort := func(x, y string) bool { return x > y }
if diff := cmp.Diff(tt.want, got, cmpopts.SortSlices(sort)); diff != "" {
t.Errorf("GetUsers() (-want +got):\n%s", diff)
}
})
}
Expand Down Expand Up @@ -455,8 +459,10 @@ func TestGetGroupMembers(t *testing.T) {
t.Errorf("GoogleProvider.GetGroupMembers() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GoogleProvider.GetGroupMembers() = %s, want %s", convert.ToJSONString(got, true), convert.ToJSONString(tt.want, true))

sort := func(x, y string) bool { return x > y }
if diff := cmp.Diff(tt.want, got, cmpopts.SortSlices(sort)); diff != "" {
t.Errorf("GetGroupMembers() (-want +got):\n%s", diff)
}
})
}
Expand Down Expand Up @@ -705,8 +711,10 @@ func TestGetUsersByGroupsMembers(t *testing.T) {
t.Errorf("GoogleProvider.GetUsersFromGroupMembers() got error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GoogleProvider.GetUsersFromGroupMembers() got = %s, want %s", convert.ToJSONString(got, true), convert.ToJSONString(tt.want, true))

sort := func(x, y string) bool { return x > y }
if diff := cmp.Diff(tt.want, got, cmpopts.SortSlices(sort)); diff != "" {
t.Errorf("GetUsersByGroupsMembers() (-want +got):\n%s", diff)
}
})
}
Expand Down Expand Up @@ -872,8 +880,9 @@ func TestGetGroupsMembers(t *testing.T) {
return
}

if !tt.wantErr && !reflect.DeepEqual(got, tt.want) {
t.Errorf("GoogleProvider.GetGroupsMembers() = %s, want %s", convert.ToJSONString(got, true), convert.ToJSONString(tt.want, true))
sort := func(x, y string) bool { return x > y }
if diff := cmp.Diff(tt.want, got, cmpopts.SortSlices(sort)); diff != "" {
t.Errorf("GetGroupsMembers() (-want +got):\n%s", diff)
}
})
}
Expand Down
8 changes: 6 additions & 2 deletions internal/model/member_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"reflect"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/slashdevops/idp-scim-sync/internal/convert"
)

Expand Down Expand Up @@ -310,8 +312,10 @@ func TestGroupsMembersResult_MarshalJSON(t *testing.T) {
t.Errorf("UsersResult.MarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("UsersResult.MarshalJSON() = %s, want %s", string(got), string(tt.want))

sort := func(x, y string) bool { return x > y }
if diff := cmp.Diff(tt.want, got, cmpopts.SortSlices(sort)); diff != "" {
t.Errorf("MarshalJSON() (-want +got):\n%s", diff)
}
})
}
Expand Down
Loading

0 comments on commit f78a3da

Please sign in to comment.