Skip to content

Commit

Permalink
refactor: Combine almost identical tests
Browse files Browse the repository at this point in the history
Combine TestUserByID and TestUserByName, TestGroupByID and
TestGroupByName, because the test bodies were identical (except for the
called function).
  • Loading branch information
adombeck committed Jan 6, 2025
1 parent 86d7dc6 commit ea0d887
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 86 deletions.
115 changes: 29 additions & 86 deletions internal/users/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,55 +291,22 @@ func TestUpdateBrokerForUser(t *testing.T) {
}
}

//nolint:dupl // This is not a duplicate test
func TestUserByName(t *testing.T) {
func TestUserByIDAndName(t *testing.T) {
tests := map[string]struct {
uid uint32
username string
dbFile string

wantErr bool
wantErrType error
}{
"Successfully get user by ID": {uid: 1111, dbFile: "multiple_users_and_groups"},
"Successfully get user by name": {username: "user1", dbFile: "multiple_users_and_groups"},

"Error if user does not exist": {username: "doesnotexist", dbFile: "multiple_users_and_groups", wantErrType: cache.NoDataFoundError{}},
"Error if db has invalid entry": {username: "user1", dbFile: "invalid_entry_in_userByName", wantErr: true},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// We don't care about the output of gpasswd in this test, but we still need to mock it.
_ = localgroupstestutils.SetupGPasswdMock(t, "empty.group")

cacheDir := t.TempDir()
cache.Z_ForTests_CreateDBFromYAML(t, filepath.Join("testdata", "db", tc.dbFile+".db.yaml"), cacheDir)
m := newManagerForTests(t, cacheDir)

got, err := m.UserByName(tc.username)

requireErrorAssertions(t, err, tc.wantErrType, tc.wantErr)
if tc.wantErrType != nil || tc.wantErr {
return
}

want := golden.LoadWithUpdateYAML(t, got)
require.Equal(t, want, got, "UserByName should return the expected user, but did not")
})
}
}

//nolint:dupl // This is not a duplicate test
func TestUserByID(t *testing.T) {
tests := map[string]struct {
uid uint32
dbFile string

wantErr bool
wantErrType error
}{
"Successfully get user by ID": {uid: 1111, dbFile: "multiple_users_and_groups"},

"Error if user does not exist": {uid: 0, dbFile: "multiple_users_and_groups", wantErrType: cache.NoDataFoundError{}},
"Error if db has invalid entry": {uid: 1111, dbFile: "invalid_entry_in_userByID", wantErr: true},
"Error if user does not exist - by ID": {uid: 0, dbFile: "multiple_users_and_groups", wantErrType: cache.NoDataFoundError{}},
"Error if user does not exist - by name": {username: "doesnotexist", dbFile: "multiple_users_and_groups", wantErrType: cache.NoDataFoundError{}},
"Error if db has invalid entry - by ID": {uid: 1111, dbFile: "invalid_entry_in_userByID", wantErr: true},
"Error if db has invalid entry - by name": {username: "user1", dbFile: "invalid_entry_in_userByName", wantErr: true},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
Expand All @@ -351,15 +318,20 @@ func TestUserByID(t *testing.T) {

m := newManagerForTests(t, cacheDir)

got, err := m.UserByID(tc.uid)
var user users.UserEntry
var err error
if tc.username != "" {
user, err = m.UserByName(tc.username)
} else {
user, err = m.UserByID(tc.uid)
}

requireErrorAssertions(t, err, tc.wantErrType, tc.wantErr)
if tc.wantErrType != nil || tc.wantErr {
return
}

want := golden.LoadWithUpdateYAML(t, got)
require.Equal(t, want, got, "UserByID should return the expected user, but did not")
golden.CheckOrUpdateYAML(t, user)
})
}
}
Expand Down Expand Up @@ -397,19 +369,22 @@ func TestAllUsers(t *testing.T) {
}
}

//nolint:dupl // This is not a duplicate test
func TestGroupByName(t *testing.T) {
func TestGroupByIDAndName(t *testing.T) {
tests := map[string]struct {
gid uint32
groupname string
dbFile string

wantErr bool
wantErrType error
}{
"Successfully get group by ID": {gid: 11111, dbFile: "multiple_users_and_groups"},
"Successfully get group by name": {groupname: "group1", dbFile: "multiple_users_and_groups"},

"Error if group does not exist": {groupname: "doesnotexist", dbFile: "multiple_users_and_groups", wantErrType: cache.NoDataFoundError{}},
"Error if db has invalid entry": {groupname: "group1", dbFile: "invalid_entry_in_groupByName", wantErr: true},
"Error if group does not exist - by ID": {gid: 0, dbFile: "multiple_users_and_groups", wantErrType: cache.NoDataFoundError{}},
"Error if group does not exist - by name": {groupname: "doesnotexist", dbFile: "multiple_users_and_groups", wantErrType: cache.NoDataFoundError{}},
"Error if db has invalid entry - by ID": {gid: 11111, dbFile: "invalid_entry_in_groupByID", wantErr: true},
"Error if db has invalid entry - by name": {groupname: "group1", dbFile: "invalid_entry_in_groupByName", wantErr: true},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
Expand All @@ -420,51 +395,20 @@ func TestGroupByName(t *testing.T) {
cache.Z_ForTests_CreateDBFromYAML(t, filepath.Join("testdata", "db", tc.dbFile+".db.yaml"), cacheDir)
m := newManagerForTests(t, cacheDir)

got, err := m.GroupByName(tc.groupname)

requireErrorAssertions(t, err, tc.wantErrType, tc.wantErr)
if tc.wantErrType != nil || tc.wantErr {
return
var group users.GroupEntry
var err error
if tc.groupname != "" {
group, err = m.GroupByName(tc.groupname)
} else {
group, err = m.GroupByID(tc.gid)
}

want := golden.LoadWithUpdateYAML(t, got)
require.Equal(t, want, got, "GroupByName should return the expected group, but did not")
})
}
}

//nolint:dupl // This is not a duplicate test
func TestGroupByID(t *testing.T) {
tests := map[string]struct {
gid uint32
dbFile string

wantErr bool
wantErrType error
}{
"Successfully get group by ID": {gid: 11111, dbFile: "multiple_users_and_groups"},

"Error if group does not exist": {gid: 0, dbFile: "multiple_users_and_groups", wantErrType: cache.NoDataFoundError{}},
"Error if db has invalid entry": {gid: 11111, dbFile: "invalid_entry_in_groupByID", wantErr: true},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// We don't care about the output of gpasswd in this test, but we still need to mock it.
_ = localgroupstestutils.SetupGPasswdMock(t, "empty.group")

cacheDir := t.TempDir()
cache.Z_ForTests_CreateDBFromYAML(t, filepath.Join("testdata", "db", tc.dbFile+".db.yaml"), cacheDir)
m := newManagerForTests(t, cacheDir)

got, err := m.GroupByID(tc.gid)

requireErrorAssertions(t, err, tc.wantErrType, tc.wantErr)
if tc.wantErrType != nil || tc.wantErr {
return
}

want := golden.LoadWithUpdateYAML(t, got)
require.Equal(t, want, got, "GroupByID should return the expected group, but did not")
golden.CheckOrUpdateYAML(t, group)
})
}
}
Expand Down Expand Up @@ -503,7 +447,6 @@ func TestAllGroups(t *testing.T) {
}
}

//nolint:dupl // This is not a duplicate test
func TestShadowByName(t *testing.T) {
tests := map[string]struct {
username string
Expand Down

0 comments on commit ea0d887

Please sign in to comment.