Skip to content

Commit

Permalink
Tests and mocks
Browse files Browse the repository at this point in the history
Signed-off-by: Rafael Raposo <[email protected]>
  • Loading branch information
RRap0so committed Oct 27, 2023
1 parent 5ec2a3d commit 1f2d4bb
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
13 changes: 13 additions & 0 deletions flyteadmin/pkg/manager/mocks/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import (
type CreateProjectFunc func(ctx context.Context, request admin.ProjectRegisterRequest) (*admin.ProjectRegisterResponse, error)
type ListProjectFunc func(ctx context.Context, request admin.ProjectListRequest) (*admin.Projects, error)
type UpdateProjectFunc func(ctx context.Context, request admin.Project) (*admin.ProjectUpdateResponse, error)
type GetProjectFunc func(ctx context.Context, request admin.ProjectRequest) (*admin.Project, error)

type MockProjectManager struct {
listProjectFunc ListProjectFunc
createProjectFunc CreateProjectFunc
updateProjectFunc UpdateProjectFunc
getProjectFunc GetProjectFunc
}

func (m *MockProjectManager) SetCreateProject(createProjectFunc CreateProjectFunc) {
Expand Down Expand Up @@ -45,3 +47,14 @@ func (m *MockProjectManager) ListProjects(
}
return nil, nil
}

func (m *MockProjectManager) SetGetCallBack(getProjectFunc GetProjectFunc) {
m.getProjectFunc = getProjectFunc
}

func (m *MockProjectManager) GetProject(ctx context.Context, request admin.ProjectRequest) (*admin.Project, error) {
if m.getProjectFunc != nil {
return m.getProjectFunc(ctx, request)
}
return nil, nil
}
18 changes: 18 additions & 0 deletions flyteadmin/pkg/rpc/adminservice/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,21 @@ func (m *AdminService) UpdateProject(ctx context.Context, request *admin.Project

return response, nil
}

func (m *AdminService) GetProject(ctx context.Context, request *admin.ProjectRequest) (*admin.Project, error) {
defer m.interceptPanic(ctx, request)
if request == nil {
return nil, status.Errorf(codes.InvalidArgument, "Incorrect request, nil requests not allowed")
}
var response *admin.Project
var err error
m.Metrics.projectEndpointMetrics.list.Time(func() {
response, err = m.ProjectManager.GetProject(ctx, *request)
})
if err != nil {
return nil, util.TransformAndRecordError(err, &m.Metrics.projectEndpointMetrics.list)
}

m.Metrics.projectEndpointMetrics.list.Success()
return response, nil
}
12 changes: 12 additions & 0 deletions flyteadmin/pkg/rpc/adminservice/tests/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,15 @@ func TestListProjects(t *testing.T) {
assert.Nil(t, err)
assert.True(t, proto.Equal(projects, resp))
}

func TestGetProject(t *testing.T) {
mockProjectManager := mocks.MockProjectManager{}
project := &admin.Project{Id: "project id", Name: "project"}
mockProjectManager.SetGetCallBack(func(ctx context.Context, request admin.ProjectRequest) (*admin.Project, error) {
assert.NotNil(t, request)
return project, nil
})
resp, err := mockProjectManager.GetProject(context.Background(), admin.ProjectRequest{})
assert.Nil(t, err)
assert.True(t, proto.Equal(project, resp))
}

0 comments on commit 1f2d4bb

Please sign in to comment.