diff options
author | Joe Chen <jc@unknwon.io> | 2022-06-11 12:43:56 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-11 12:43:56 +0800 |
commit | 1df62fafb99c03d4d3d4b26c99839d165d5425da (patch) | |
tree | da9308befb456e888518ef397bbe49ae6e510d53 /internal/route | |
parent | 5e32058c13f34b46c69b7cdee6ccc0b7fe3b6df3 (diff) |
db: use `context` and go-mockgen for `ReposStore` (#7046)
Diffstat (limited to 'internal/route')
-rw-r--r-- | internal/route/api/v1/api.go | 2 | ||||
-rw-r--r-- | internal/route/lfs/route.go | 2 | ||||
-rw-r--r-- | internal/route/lfs/route_test.go | 30 | ||||
-rw-r--r-- | internal/route/repo/http.go | 2 | ||||
-rw-r--r-- | internal/route/repo/tasks.go | 2 |
5 files changed, 22 insertions, 16 deletions
diff --git a/internal/route/api/v1/api.go b/internal/route/api/v1/api.go index 9d256e3e..a517c6df 100644 --- a/internal/route/api/v1/api.go +++ b/internal/route/api/v1/api.go @@ -45,7 +45,7 @@ func repoAssignment() macaron.Handler { } c.Repo.Owner = owner - repo, err := db.Repos.GetByName(owner.ID, reponame) + repo, err := db.Repos.GetByName(c.Req.Context(), owner.ID, reponame) if err != nil { c.NotFoundOrError(err, "get repository by name") return diff --git a/internal/route/lfs/route.go b/internal/route/lfs/route.go index 94c42fea..c00f7374 100644 --- a/internal/route/lfs/route.go +++ b/internal/route/lfs/route.go @@ -119,7 +119,7 @@ func authorize(mode db.AccessMode) macaron.Handler { return } - repo, err := db.Repos.GetByName(owner.ID, reponame) + repo, err := db.Repos.GetByName(c.Req.Context(), owner.ID, reponame) if err != nil { if db.IsErrRepoNotExist(err) { c.Status(http.StatusNotFound) diff --git a/internal/route/lfs/route_test.go b/internal/route/lfs/route_test.go index ee668fc6..cc6fb1be 100644 --- a/internal/route/lfs/route_test.go +++ b/internal/route/lfs/route_test.go @@ -167,7 +167,7 @@ func Test_authorize(t *testing.T) { name string authroize macaron.Handler mockUsersStore func() db.UsersStore - mockReposStore *db.MockReposStore + mockReposStore func() db.ReposStore mockPermsStore func() db.PermsStore expStatusCode int expBody string @@ -192,10 +192,10 @@ func Test_authorize(t *testing.T) { }) return mock }, - mockReposStore: &db.MockReposStore{ - MockGetByName: func(ownerID int64, name string) (*db.Repository, error) { - return nil, db.ErrRepoNotExist{} - }, + mockReposStore: func() db.ReposStore { + mock := db.NewMockReposStore() + mock.GetByNameFunc.SetDefaultReturn(nil, db.ErrRepoNotExist{}) + return mock }, expStatusCode: http.StatusNotFound, }, @@ -209,10 +209,12 @@ func Test_authorize(t *testing.T) { }) return mock }, - mockReposStore: &db.MockReposStore{ - MockGetByName: func(ownerID int64, name string) (*db.Repository, error) { + mockReposStore: func() db.ReposStore { + mock := db.NewMockReposStore() + mock.GetByNameFunc.SetDefaultHook(func(ctx context.Context, ownerID int64, name string) (*db.Repository, error) { return &db.Repository{Name: name}, nil - }, + }) + return mock }, mockPermsStore: func() db.PermsStore { mock := db.NewMockPermsStore() @@ -234,10 +236,12 @@ func Test_authorize(t *testing.T) { }) return mock }, - mockReposStore: &db.MockReposStore{ - MockGetByName: func(ownerID int64, name string) (*db.Repository, error) { + mockReposStore: func() db.ReposStore { + mock := db.NewMockReposStore() + mock.GetByNameFunc.SetDefaultHook(func(ctx context.Context, ownerID int64, name string) (*db.Repository, error) { return &db.Repository{Name: name}, nil - }, + }) + return mock }, mockPermsStore: func() db.PermsStore { mock := db.NewMockPermsStore() @@ -255,7 +259,9 @@ func Test_authorize(t *testing.T) { if test.mockUsersStore != nil { db.SetMockUsersStore(t, test.mockUsersStore()) } - db.SetMockReposStore(t, test.mockReposStore) + if test.mockReposStore != nil { + db.SetMockReposStore(t, test.mockReposStore()) + } if test.mockPermsStore != nil { db.SetMockPermsStore(t, test.mockPermsStore()) } diff --git a/internal/route/repo/http.go b/internal/route/repo/http.go index 888ad4d8..af51a076 100644 --- a/internal/route/repo/http.go +++ b/internal/route/repo/http.go @@ -76,7 +76,7 @@ func HTTPContexter() macaron.Handler { return } - repo, err := db.Repos.GetByName(owner.ID, repoName) + repo, err := db.Repos.GetByName(c.Req.Context(), owner.ID, repoName) if err != nil { if db.IsErrRepoNotExist(err) { c.Status(http.StatusNotFound) diff --git a/internal/route/repo/tasks.go b/internal/route/repo/tasks.go index c5b555b9..d9215885 100644 --- a/internal/route/repo/tasks.go +++ b/internal/route/repo/tasks.go @@ -44,7 +44,7 @@ func TriggerTask(c *macaron.Context) { return } - repo, err := db.Repos.GetByName(owner.ID, reponame) + repo, err := db.Repos.GetByName(c.Req.Context(), owner.ID, reponame) if err != nil { if db.IsErrRepoNotExist(err) { c.Error(http.StatusBadRequest, "Repository does not exist") |