-
-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #304 from rusq/list-channels-fix
Fix list channels logic
- Loading branch information
Showing
8 changed files
with
329 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package list |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package list | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"io/fs" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/rusq/slack" | ||
"go.uber.org/mock/gomock" | ||
) | ||
|
||
func Test_getCachedUsers(t *testing.T) { | ||
var ( | ||
testUsers = []slack.User{ | ||
{ID: "U1"}, | ||
{ID: "U2"}, | ||
{ID: "U3"}, | ||
} | ||
) | ||
type args struct { | ||
ctx context.Context | ||
teamID string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
expect func(c *MockuserCacher, g *MockuserGetter) | ||
want []slack.User | ||
wantErr bool | ||
}{ | ||
/* oh happy days */ | ||
{ | ||
"users loaded from cache", | ||
args{context.Background(), "TEAM1"}, | ||
func(c *MockuserCacher, g *MockuserGetter) { | ||
c.EXPECT().LoadUsers("TEAM1", gomock.Any()).Return(testUsers, nil) | ||
}, | ||
testUsers, | ||
false, | ||
}, | ||
{ | ||
"getting users from API ok (recoverable cache error)", | ||
args{context.Background(), "TEAM1"}, | ||
func(c *MockuserCacher, g *MockuserGetter) { | ||
c.EXPECT().LoadUsers("TEAM1", gomock.Any()).Return(nil, &fs.PathError{}) | ||
g.EXPECT().GetUsers(gomock.Any()).Return(testUsers, nil) | ||
c.EXPECT().CacheUsers("TEAM1", testUsers).Return(nil) | ||
}, | ||
testUsers, | ||
false, | ||
}, | ||
{ | ||
"saving cache fails, but we continue", | ||
args{context.Background(), "TEAM1"}, | ||
func(c *MockuserCacher, g *MockuserGetter) { | ||
c.EXPECT().LoadUsers("TEAM1", gomock.Any()).Return(nil, &fs.PathError{}) | ||
g.EXPECT().GetUsers(gomock.Any()).Return(testUsers, nil) | ||
c.EXPECT().CacheUsers("TEAM1", testUsers).Return(errors.New("disk mulching detected")) | ||
}, | ||
testUsers, | ||
false, | ||
}, | ||
/* unhappy days */ | ||
{ | ||
"unrecoverable error", | ||
args{context.Background(), "TEAM1"}, | ||
func(c *MockuserCacher, g *MockuserGetter) { | ||
c.EXPECT().LoadUsers("TEAM1", gomock.Any()).Return(nil, errors.New("frobnication error")) | ||
}, | ||
nil, | ||
true, | ||
}, | ||
{ | ||
"getting users from API fails", | ||
args{context.Background(), "TEAM1"}, | ||
func(c *MockuserCacher, g *MockuserGetter) { | ||
c.EXPECT().LoadUsers("TEAM1", gomock.Any()).Return(nil, &fs.PathError{}) | ||
g.EXPECT().GetUsers(gomock.Any()).Return(nil, errors.New("blip")) | ||
}, | ||
nil, | ||
true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
muc := NewMockuserCacher(ctrl) | ||
mug := NewMockuserGetter(ctrl) | ||
|
||
tt.expect(muc, mug) | ||
|
||
got, err := getCachedUsers(tt.args.ctx, mug, muc, tt.args.teamID) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("getCachedUsers() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("getCachedUsers() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package osext | ||
|
||
import ( | ||
"errors" | ||
"io/fs" | ||
) | ||
|
||
// IsPathError reports whether the error is an fs.PathError. | ||
func IsPathError(err error) bool { | ||
var pathError *fs.PathError | ||
return errors.As(err, &pathError) | ||
} |
Oops, something went wrong.