-
Notifications
You must be signed in to change notification settings - Fork 705
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added tests for environments handler #4175
Changes from 7 commits
a1c5fae
f4c32e2
b2ec764
70b429b
fb0c252
bc423c7
0b66a2c
9ec72b8
65e64e3
3df2561
2f1ff8a
c527246
b202815
d770979
69dfb53
e9c1b84
dbe48bf
f217bbc
45ca0a1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
package handler_test | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
|
||
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model" | ||
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/authorization" | ||
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/environments" | ||
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/environment/handler" | ||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
) | ||
|
||
type MockEnvironmentOperator struct { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about mocking the Mongo Operator instead of mocking the Environment Operator? This allows you to write test cases that are flexible to future changes in the Environment Operator. How do you think? import (
dbMocks "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/mocks"
dbOperationsEnvironment "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/environments"
)
mongodbMockOperator := new(dbMocks.MongoOperator)
environmentOperator := dbOperationsEnvironment.NewEnvironmentOperator(mongodbMockOperator) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I could stick with Environment Operator since I already updated the handler to consider an environment Mockoperator There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. okay, thanks |
||
InsertEnvironmentFunc func(ctx context.Context, env environments.Environment) error | ||
GetEnvironmentsFunc func(ctx context.Context, query bson.D) ([]environments.Environment, error) | ||
UpdateEnvironmentFunc func(ctx context.Context, query bson.D, update bson.D) error | ||
GetEnvironmentFunc func(query bson.D) (environments.Environment, error) | ||
GetAggregateEnvironmentsFunc func(pipeline mongo.Pipeline) (*mongo.Cursor, error) | ||
GetEnvironmentWithProjectIDFunc func(projectID string) ([]*environments.Environment, error) | ||
GetEnvironmentDetailFunc func(ctx context.Context, environmentID string, projectID string) (environments.Environment, error) | ||
} | ||
|
||
func (m *MockEnvironmentOperator) InsertEnvironment(ctx context.Context, env environments.Environment) error { | ||
if m.InsertEnvironmentFunc != nil { | ||
return m.InsertEnvironmentFunc(ctx, env) | ||
} | ||
return nil | ||
} | ||
|
||
func (m *MockEnvironmentOperator) GetEnvironment(query bson.D) (environments.Environment, error) { | ||
if m.GetEnvironmentFunc != nil { | ||
return m.GetEnvironmentFunc(query) | ||
} | ||
return environments.Environment{}, nil | ||
} | ||
|
||
func (m *MockEnvironmentOperator) GetEnvironments(ctx context.Context, query bson.D) ([]environments.Environment, error) { | ||
if m.GetEnvironmentsFunc != nil { | ||
return m.GetEnvironmentsFunc(ctx, query) | ||
} | ||
return nil, nil | ||
} | ||
|
||
func (m *MockEnvironmentOperator) UpdateEnvironment(ctx context.Context, query bson.D, update bson.D) error { | ||
if m.UpdateEnvironmentFunc != nil { | ||
return m.UpdateEnvironmentFunc(ctx, query, update) | ||
} | ||
return nil | ||
} | ||
|
||
func (m *MockEnvironmentOperator) GetAggregateEnvironments(pipeline mongo.Pipeline) (*mongo.Cursor, error) { | ||
if m.GetAggregateEnvironmentsFunc != nil { | ||
return m.GetAggregateEnvironmentsFunc(pipeline) | ||
} | ||
return nil, nil | ||
} | ||
|
||
func (m *MockEnvironmentOperator) GetEnvironmentWithProjectID(projectID string) ([]*environments.Environment, error) { | ||
if m.GetEnvironmentWithProjectIDFunc != nil { | ||
return m.GetEnvironmentWithProjectIDFunc(projectID) | ||
} | ||
return nil, nil | ||
} | ||
|
||
func (m *MockEnvironmentOperator) GetEnvironmentDetails(ctx context.Context, environmentID string, projectID string) (environments.Environment, error) { | ||
if m.GetEnvironmentDetailFunc != nil { | ||
return m.GetEnvironmentDetailFunc(ctx, environmentID, projectID) | ||
} | ||
return environments.Environment{}, nil | ||
} | ||
|
||
func TestCreateEnvironment(t *testing.T) { | ||
testCases := []struct { | ||
Freedisch marked this conversation as resolved.
Show resolved
Hide resolved
|
||
name string | ||
projectID string | ||
input *model.CreateEnvironmentRequest | ||
token string | ||
mockInsertFunc func(ctx context.Context, env environments.Environment) error | ||
expectedEnv *model.Environment | ||
expectedErr error | ||
}{ | ||
{ | ||
name: "Failed environment creation due to invalid token", | ||
projectID: "testProject", | ||
input: &model.CreateEnvironmentRequest{ | ||
EnvironmentID: "testEnvID", | ||
Name: "testName", | ||
}, | ||
token: "invalidToken", | ||
mockInsertFunc: func(ctx context.Context, env environments.Environment) error { | ||
return nil | ||
}, | ||
expectedEnv: nil, | ||
expectedErr: errors.New("invalid Token"), | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
ctx := context.WithValue(context.Background(), authorization.AuthKey, tc.token) | ||
mockOperator := &MockEnvironmentOperator{ | ||
InsertEnvironmentFunc: tc.mockInsertFunc, | ||
} | ||
service := handler.NewEnvironmentService(mockOperator) | ||
//service := handler.NewEnvironmentService(operator) | ||
Freedisch marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
env, err := service.CreateEnvironment(ctx, tc.projectID, tc.input) | ||
if (err != nil && tc.expectedErr == nil) || | ||
(err == nil && tc.expectedErr != nil) || | ||
(err != nil && tc.expectedErr != nil && err.Error() != tc.expectedErr.Error()) { | ||
t.Fatalf("Expected error %v, but got %v", tc.expectedErr, err) | ||
} | ||
|
||
if tc.expectedEnv != nil && (env.EnvironmentID != tc.expectedEnv.EnvironmentID || | ||
env.Name != tc.expectedEnv.Name) { | ||
t.Fatalf("Expected environment %+v, but got %+v", tc.expectedEnv, env) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestDeleteEnvironment(t *testing.T) { | ||
testCases := []struct { | ||
Freedisch marked this conversation as resolved.
Show resolved
Hide resolved
|
||
name string | ||
projectID string | ||
environmentID string | ||
token string | ||
mockGetEnvironmentFunc func(query bson.D) (environments.Environment, error) | ||
mockUpdateFunc func(ctx context.Context, query bson.D, update bson.D) error | ||
expectedResult string | ||
expectedErr error | ||
}{ | ||
{ | ||
name: "Failed environment deletion due to invalid token", | ||
projectID: "testProject", | ||
environmentID: "testEnvID", | ||
token: "invalidToken", | ||
mockGetEnvironmentFunc: func(query bson.D) (environments.Environment, error) { | ||
return environments.Environment{ | ||
EnvironmentID: "testEnvID", | ||
}, nil | ||
}, | ||
mockUpdateFunc: func(ctx context.Context, query bson.D, update bson.D) error { | ||
return nil | ||
}, | ||
expectedErr: errors.New("invalid Token"), | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
ctx := context.WithValue(context.Background(), authorization.AuthKey, tc.token) | ||
|
||
mockOperator := &MockEnvironmentOperator{ | ||
UpdateEnvironmentFunc: tc.mockUpdateFunc, | ||
} | ||
service := handler.NewEnvironmentService(mockOperator) | ||
|
||
_, err := service.DeleteEnvironment(ctx, tc.projectID, tc.environmentID) | ||
if (err != nil && tc.expectedErr == nil) || | ||
(err == nil && tc.expectedErr != nil) || | ||
(err != nil && tc.expectedErr != nil && err.Error() != tc.expectedErr.Error()) { | ||
t.Fatalf("Expected error %v, but got %v", tc.expectedErr, err) | ||
} | ||
}) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO, we don't need this code if we adopt below code
we don't also need
MockEnvironmentOperator
struct and relevant functions inchaoscenter/graphql/server/pkg/environment/handler/handler_test.go
if we adopt the above.I think reducing unnecessary code is a great way to make open source understandable to many people.
How do you think @Saranya-jena @gdsoumya @Freedisch ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make sense. I updated the code to reflect it