Skip to content
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

Merged
merged 19 commits into from
Nov 15, 2023

Conversation

Freedisch
Copy link
Contributor

Proposed changes:

Added testcases for environments handler https://github.com/litmuschaos/litmus/blob/master/chaoscenter/graphql/server/pkg/environment/handler/handler.go

this follows the previous PR #4159

Types of changes

What types of changes does your code introduce to Litmus? Put an x in the boxes that apply

  • New feature (non-breaking change which adds functionality)
  • Bugfix (non-breaking change which fixes an issue)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation Update (if none of the other choices applies)

Checklist

Put an x in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your code.

  • I have read the CONTRIBUTING doc
  • I have signed the commit for DCO to be passed.
  • Lint and unit tests pass locally with my changes
  • I have added tests that prove my fix is effective or that my feature works (if appropriate)
  • I have added necessary documentation (if appropriate)

Dependency

  • Please add the links to the dependent PR need to be merged before this (if any).

Special notes for your reviewer:

Signed-off-by: Freedisch <freeproduc@gmail.com>
Signed-off-by: Freedisch <freeproduc@gmail.com>
Copy link
Member

@S-ayanide S-ayanide left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Signed-off-by: freedisch <freeproduc@gmail.com>
Copy link
Member

@namkyu1999 namkyu1999 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @Freedisch , proposed changes looks good to me. Just a small request,

You defined a struct called MockEnvironmentOperator, but you didn't use it. In all your test code, instead of using operator := &environments.Operator{} Please use mock operator.

Signed-off-by: freedisch <freeproduc@gmail.com>
Signed-off-by: freedisch <freeproduc@gmail.com>
@Freedisch
Copy link
Contributor Author

@namkyu1999, I have completed the necessary changes

Copy link
Member

@namkyu1999 namkyu1999 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Take a look at my comments, please!

"go.mongodb.org/mongo-driver/mongo"
)

type MockEnvironmentOperator struct {
Copy link
Member

@namkyu1999 namkyu1999 Oct 25, 2023

Choose a reason for hiding this comment

The 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)

reference: https://github.com/litmuschaos/litmus/blob/master/chaoscenter/graphql/server/pkg/chaos_experiment_run/service_test.go

Copy link
Contributor Author

@Freedisch Freedisch Oct 31, 2023

Choose a reason for hiding this comment

The 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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay, thanks

…t.go

Co-authored-by: Namkyu Park <53862866+namkyu1999@users.noreply.github.com>
Signed-off-by: Magnim BATALE <82499435+Freedisch@users.noreply.github.com>
Signed-off-by: freedisch <freeproduc@gmail.com>
Signed-off-by: freedisch <freeproduc@gmail.com>
Signed-off-by: freedisch <freeproduc@gmail.com>
@Saranya-jena
Copy link
Contributor

@namkyu1999 Could review this PR again?

Comment on lines 28 to 40
// Inside the dbOperationsEnvironment package

type EnvironmentOperatorInterface interface {
InsertEnvironment(ctx context.Context, env Environment) error
GetEnvironments(ctx context.Context, query bson.D) ([]Environment, error)
UpdateEnvironment(ctx context.Context, query bson.D, update bson.D) error
GetEnvironment(query bson.D) (Environment, error)
GetAggregateEnvironments(pipeline mongo.Pipeline) (*mongo.Cursor, error)
GetEnvironmentDetails(ctx context.Context, environmentID string, projectID string) (Environment, error)
GetEnvironmentWithProjectID(projectID string) ([]*Environment, error)
}

// InsertEnvironment takes details of a chaos_environment and inserts into the database collection
Copy link
Member

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

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)

we don't also need MockEnvironmentOperator struct and relevant functions in chaoscenter/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 ?

Copy link
Contributor Author

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

Signed-off-by: freedisch <freeproduc@gmail.com>
Copy link
Member

@namkyu1999 namkyu1999 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🚀 @Freedisch

Freedisch and others added 6 commits November 11, 2023 17:22
…t.go

Co-authored-by: Namkyu Park <53862866+namkyu1999@users.noreply.github.com>
Signed-off-by: Magnim BATALE <82499435+Freedisch@users.noreply.github.com>
…t.go

Co-authored-by: Namkyu Park <53862866+namkyu1999@users.noreply.github.com>
Signed-off-by: Magnim BATALE <82499435+Freedisch@users.noreply.github.com>
…t.go

Co-authored-by: Namkyu Park <53862866+namkyu1999@users.noreply.github.com>
Signed-off-by: Magnim BATALE <82499435+Freedisch@users.noreply.github.com>
Signed-off-by: freedisch <freeproduc@gmail.com>
@Saranya-jena Saranya-jena merged commit ee5a046 into litmuschaos:master Nov 15, 2023
15 of 16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/unit-test LFX-MENTORSHIP Linux Foundation Mentor ship Issue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants