Skip to content
This repository was archived by the owner on May 30, 2024. It is now read-only.

Commit 724a073

Browse files
author
Noah Lee
authored
Refactoring UserInterator (#343)
* Fix ListUsers method * Fix unit tests of the `interactor` pkg * Merge the sync file into the user file * Simplify the unit test
1 parent 7eb6227 commit 724a073

File tree

18 files changed

+319
-308
lines changed

18 files changed

+319
-308
lines changed

internal/interactor/_mock.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
mockgen \
2+
-aux_files \
3+
github.com/gitploy-io/gitploy/internal/interactor=user.go\
4+
,github.com/gitploy-io/gitploy/internal/interactor=sync.go\
5+
-source ./interface.go \
6+
-package mock \
7+
-destination ./mock/pkg.go

internal/interactor/deployment_test.go

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package interactor
1+
package interactor_test
22

33
import (
44
"context"
@@ -7,8 +7,8 @@ import (
77

88
"github.com/AlekSi/pointer"
99
"github.com/golang/mock/gomock"
10-
"go.uber.org/zap"
1110

11+
i "github.com/gitploy-io/gitploy/internal/interactor"
1212
"github.com/gitploy-io/gitploy/internal/interactor/mock"
1313
"github.com/gitploy-io/gitploy/model/ent"
1414
"github.com/gitploy-io/gitploy/model/ent/deployment"
@@ -17,14 +17,6 @@ import (
1717
"github.com/gitploy-io/gitploy/pkg/e"
1818
)
1919

20-
func newMockDeploymentInteractor(store Store, scm SCM) *DeploymentInteractor {
21-
return &DeploymentInteractor{
22-
store: store,
23-
scm: scm,
24-
log: zap.L(),
25-
}
26-
}
27-
2820
func TestInteractor_IsApproved(t *testing.T) {
2921
t.Run("Return false when a review is rejected.", func(t *testing.T) {
3022
ctrl := gomock.NewController(t)
@@ -44,10 +36,13 @@ func TestInteractor_IsApproved(t *testing.T) {
4436
},
4537
}, nil)
4638

47-
i := newMockDeploymentInteractor(store, scm)
39+
it := i.NewInteractor(&i.InteractorConfig{
40+
Store: store,
41+
SCM: scm,
42+
})
4843

4944
expected := false
50-
if ret := i.IsApproved(context.Background(), &ent.Deployment{}); ret != expected {
45+
if ret := it.IsApproved(context.Background(), &ent.Deployment{}); ret != expected {
5146
t.Fatalf("IsApproved = %v, wanted %v", ret, expected)
5247
}
5348
})
@@ -75,9 +70,12 @@ func TestInteractor_Deploy(t *testing.T) {
7570
store := mock.NewMockStore(ctrl)
7671
scm := mock.NewMockSCM(ctrl)
7772

78-
i := newMockDeploymentInteractor(store, scm)
73+
it := i.NewInteractor(&i.InteractorConfig{
74+
Store: store,
75+
SCM: scm,
76+
})
7977

80-
_, err := i.Deploy(context.Background(), &ent.User{}, &ent.Repo{}, input.d, input.e)
78+
_, err := it.Deploy(context.Background(), &ent.User{}, &ent.Repo{}, input.d, input.e)
8179
if !e.HasErrorCode(err, e.ErrorCodeEntityUnprocessable) {
8280
t.Fatalf("Deploy' error = %v, wanted ErrorCodeDeploymentLocked", err)
8381
}
@@ -93,9 +91,12 @@ func TestInteractor_Deploy(t *testing.T) {
9391
HasLockOfRepoForEnv(ctx, gomock.AssignableToTypeOf(&ent.Repo{}), "").
9492
Return(true, nil)
9593

96-
i := newMockDeploymentInteractor(store, scm)
94+
it := i.NewInteractor(&i.InteractorConfig{
95+
Store: store,
96+
SCM: scm,
97+
})
9798

98-
_, err := i.Deploy(context.Background(), &ent.User{}, &ent.Repo{}, &ent.Deployment{}, &extent.Env{})
99+
_, err := it.Deploy(context.Background(), &ent.User{}, &ent.Repo{}, &ent.Deployment{}, &extent.Env{})
99100
if !e.HasErrorCode(err, e.ErrorCodeDeploymentLocked) {
100101
t.Fatalf("Deploy' error = %v, wanted ErrorCodeDeploymentLocked", err)
101102
}
@@ -159,9 +160,12 @@ func TestInteractor_Deploy(t *testing.T) {
159160
EXPECT().
160161
CreateDeploymentStatus(ctx, gomock.AssignableToTypeOf(&ent.DeploymentStatus{}))
161162

162-
i := newMockDeploymentInteractor(store, scm)
163+
it := i.NewInteractor(&i.InteractorConfig{
164+
Store: store,
165+
SCM: scm,
166+
})
163167

164-
d, err := i.Deploy(context.Background(), &ent.User{}, &ent.Repo{}, input.d, input.e)
168+
d, err := it.Deploy(context.Background(), &ent.User{}, &ent.Repo{}, input.d, input.e)
165169
if err != nil {
166170
t.Fatalf("Deploy returns a error: %s", err)
167171
}
@@ -243,9 +247,12 @@ func TestInteractor_Deploy(t *testing.T) {
243247
CreateEvent(ctx, gomock.AssignableToTypeOf(&ent.Event{})).
244248
Return(&ent.Event{}, nil)
245249

246-
i := newMockDeploymentInteractor(store, scm)
250+
it := i.NewInteractor(&i.InteractorConfig{
251+
Store: store,
252+
SCM: scm,
253+
})
247254

248-
d, err := i.Deploy(context.Background(), &ent.User{}, &ent.Repo{}, input.d, input.e)
255+
d, err := it.Deploy(context.Background(), &ent.User{}, &ent.Repo{}, input.d, input.e)
249256
if err != nil {
250257
t.Errorf("Deploy returns a error: %s", err)
251258
t.FailNow()
@@ -281,9 +288,12 @@ func TestInteractor_DeployToRemote(t *testing.T) {
281288
store := mock.NewMockStore(ctrl)
282289
scm := mock.NewMockSCM(ctrl)
283290

284-
i := newMockDeploymentInteractor(store, scm)
291+
it := i.NewInteractor(&i.InteractorConfig{
292+
Store: store,
293+
SCM: scm,
294+
})
285295

286-
_, err := i.DeployToRemote(context.Background(), &ent.User{}, &ent.Repo{}, input.d, input.e)
296+
_, err := it.DeployToRemote(context.Background(), &ent.User{}, &ent.Repo{}, input.d, input.e)
287297
if !e.HasErrorCode(err, e.ErrorCodeDeploymentStatusInvalid) {
288298
t.Fatalf("CreateRemoteDeployment error = %v, wanted ErrorCodeDeploymentStatusInvalid", err)
289299
}
@@ -345,9 +355,12 @@ func TestInteractor_DeployToRemote(t *testing.T) {
345355
EXPECT().
346356
CreateDeploymentStatus(ctx, gomock.AssignableToTypeOf(&ent.DeploymentStatus{}))
347357

348-
i := newMockDeploymentInteractor(store, scm)
358+
it := i.NewInteractor(&i.InteractorConfig{
359+
Store: store,
360+
SCM: scm,
361+
})
349362

350-
d, err := i.DeployToRemote(context.Background(), &ent.User{}, &ent.Repo{}, input.d, input.e)
363+
d, err := it.DeployToRemote(context.Background(), &ent.User{}, &ent.Repo{}, input.d, input.e)
351364
if err != nil {
352365
t.Errorf("CreateRemoteDeployment returns a error: %s", err)
353366
t.FailNow()

internal/interactor/deploymentstatistics_test.go

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
package interactor
1+
package interactor_test
22

33
import (
44
"context"
55
"testing"
66

7+
i "github.com/gitploy-io/gitploy/internal/interactor"
78
"github.com/gitploy-io/gitploy/internal/interactor/mock"
89
"github.com/gitploy-io/gitploy/model/ent"
910
"github.com/gitploy-io/gitploy/model/extent"
1011
"github.com/golang/mock/gomock"
11-
"go.uber.org/zap"
1212
)
1313

1414
func TestInteractor_ProduceDeploymentStatisticsOfRepo(t *testing.T) {
@@ -56,13 +56,12 @@ func TestInteractor_ProduceDeploymentStatisticsOfRepo(t *testing.T) {
5656
return s, nil
5757
})
5858

59-
i := &DeploymentStatisticsInteractor{
60-
store: store,
61-
scm: scm,
62-
log: zap.L(),
63-
}
59+
it := i.NewInteractor(&i.InteractorConfig{
60+
Store: store,
61+
SCM: scm,
62+
})
6463

65-
_, err := i.ProduceDeploymentStatisticsOfRepo(context.Background(), input.repo, input.d)
64+
_, err := it.ProduceDeploymentStatisticsOfRepo(context.Background(), input.repo, input.d)
6665
if err != nil {
6766
t.Fatalf("ProduceDeploymentStatisticsOfRepo returns an error: %s", err)
6867
}
@@ -118,13 +117,12 @@ func TestInteractor_ProduceDeploymentStatisticsOfRepo(t *testing.T) {
118117
return s, nil
119118
})
120119

121-
i := &DeploymentStatisticsInteractor{
122-
store: store,
123-
scm: scm,
124-
log: zap.L(),
125-
}
120+
it := i.NewInteractor(&i.InteractorConfig{
121+
Store: store,
122+
SCM: scm,
123+
})
126124

127-
_, err := i.ProduceDeploymentStatisticsOfRepo(context.Background(), input.repo, input.d)
125+
_, err := it.ProduceDeploymentStatisticsOfRepo(context.Background(), input.repo, input.d)
128126
if err != nil {
129127
t.Fatalf("ProduceDeploymentStatisticsOfRepo returns an error: %s", err)
130128
}

internal/interactor/interactor.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ type (
2222
*LicenseInteractor
2323
*LockInteractor
2424
*RepoInteractor
25-
*SyncInteractor
2625
*UserInteractor
2726
}
2827

@@ -74,10 +73,6 @@ func NewInteractor(c *InteractorConfig) *Interactor {
7473
}
7574
i.LockInteractor = (*LockInteractor)(i.common)
7675
i.RepoInteractor = (*RepoInteractor)(i.common)
77-
i.SyncInteractor = &SyncInteractor{
78-
service: i.common,
79-
orgEntries: c.OrgEntries,
80-
}
8176
i.UserInteractor = &UserInteractor{
8277
service: i.common,
8378
admins: c.AdminUsers,

internal/interactor/interface.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:generate mockgen -source ./interface.go -destination ./mock/pkg.go -package mock
1+
//go:generate sh _mock.sh
22

33
package interactor
44

@@ -13,14 +13,7 @@ import (
1313

1414
type (
1515
Store interface {
16-
CountUsers(context.Context) (int, error)
17-
ListUsers(ctx context.Context, login string, page, perPage int) ([]*ent.User, error)
18-
FindUserByID(ctx context.Context, id int64) (*ent.User, error)
19-
FindUserByHash(ctx context.Context, hash string) (*ent.User, error)
20-
FindUserByLogin(ctx context.Context, login string) (*ent.User, error)
21-
CreateUser(ctx context.Context, u *ent.User) (*ent.User, error)
22-
UpdateUser(ctx context.Context, u *ent.User) (*ent.User, error)
23-
DeleteUser(ctx context.Context, u *ent.User) error
16+
UserStore
2417

2518
FindChatUserByID(ctx context.Context, id string) (*ent.ChatUser, error)
2619
CreateChatUser(ctx context.Context, cu *ent.ChatUser) (*ent.ChatUser, error)
@@ -89,8 +82,7 @@ type (
8982
}
9083

9184
SCM interface {
92-
GetRemoteUserByToken(ctx context.Context, token string) (*extent.RemoteUser, error)
93-
ListRemoteOrgsByToken(ctx context.Context, token string) ([]string, error)
85+
UserSCM
9486

9587
ListRemoteRepos(ctx context.Context, u *ent.User) ([]*extent.RemoteRepo, error)
9688

internal/interactor/license_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
package interactor
1+
package interactor_test
22

33
import (
44
"context"
55
"testing"
66

7+
i "github.com/gitploy-io/gitploy/internal/interactor"
78
"github.com/gitploy-io/gitploy/internal/interactor/mock"
89
"github.com/gitploy-io/gitploy/model/extent"
910
"github.com/golang/mock/gomock"
1011
)
1112

12-
func TestStore_GetLicense(t *testing.T) {
13+
func TestInteractor_GetLicense(t *testing.T) {
1314
t.Run("Return the trial license when the signing data is nil.", func(t *testing.T) {
1415
ctrl := gomock.NewController(t)
1516
store := mock.NewMockStore(ctrl)
@@ -25,9 +26,11 @@ func TestStore_GetLicense(t *testing.T) {
2526
CountDeployments(gomock.AssignableToTypeOf(context.Background())).
2627
Return(extent.TrialDeploymentLimit, nil)
2728

28-
i := &LicenseInteractor{service: &service{store: store}}
29+
it := i.NewInteractor(&i.InteractorConfig{
30+
Store: store,
31+
})
2932

30-
lic, err := i.GetLicense(context.Background())
33+
lic, err := it.GetLicense(context.Background())
3134
if err != nil {
3235
t.Fatalf("GetLicense returns an error: %s", err)
3336
}

internal/interactor/mock/pkg.go

Lines changed: 16 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/interactor/repo_test.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
package interactor
1+
package interactor_test
22

33
import (
44
"context"
55
"testing"
66

77
"github.com/golang/mock/gomock"
8-
"go.uber.org/zap"
98

9+
i "github.com/gitploy-io/gitploy/internal/interactor"
1010
"github.com/gitploy-io/gitploy/internal/interactor/mock"
1111
"github.com/gitploy-io/gitploy/model/ent"
1212
"github.com/gitploy-io/gitploy/pkg/e"
@@ -29,13 +29,12 @@ func TestInteractor_DeactivateRepo(t *testing.T) {
2929
Deactivate(gomock.Any(), gomock.AssignableToTypeOf(&ent.Repo{})).
3030
Return(&ent.Repo{}, nil)
3131

32-
i := &RepoInteractor{
33-
store: store,
34-
scm: scm,
35-
log: zap.L(),
36-
}
32+
it := i.NewInteractor(&i.InteractorConfig{
33+
Store: store,
34+
SCM: scm,
35+
})
3736

38-
_, err := i.DeactivateRepo(context.Background(), &ent.User{}, &ent.Repo{})
37+
_, err := it.DeactivateRepo(context.Background(), &ent.User{}, &ent.Repo{})
3938
if err != nil {
4039
t.Fatalf("DeactivateRepo returns an error: %v", err)
4140
}

internal/interactor/shared.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package interactor
2+
3+
type (
4+
// ListCursorOptions specifies the optional parameters that
5+
// support cursor pagination.
6+
ListOptions struct {
7+
Page int
8+
PerPage int
9+
}
10+
)

0 commit comments

Comments
 (0)