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

Fetch Workflows #56

Closed
wants to merge 11 commits into from
Closed

Fetch Workflows #56

wants to merge 11 commits into from

Conversation

kartikay101
Copy link
Member

Handle get workflow requests with proper pagination
Added sort by id, name and createdAt, these can be passed as queryParam (as filters)
Example:
{
"id": "asc",
"created": "desc",
"name": "desc"
}
Filters can be applied in any order, depending on how they are received

@kartikay101 kartikay101 changed the title Get Requests Fetch Workflows Oct 12, 2020
@kartikay101 kartikay101 linked an issue Oct 12, 2020 that may be closed by this pull request
Copy link
Member

@priyaaank priyaaank left a comment

Choose a reason for hiding this comment

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

Have left a bunch of review comments. I feel overall a bit of renaming and restructuring of tests is required here.

utils/parser.go Outdated
}

func cleanFilters(filters map[string]string) (map[string]string, error) {
supportedKeys := []string{"id", "createdDate", "name"}
Copy link
Member

Choose a reason for hiding this comment

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

Magic strings should be in constants. When a string is being used in multiple places, pull them out into constants so that change in string value is isolated to a variable

Copy link
Member Author

Choose a reason for hiding this comment

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

If you could suggest a proper place for it. That would be great

query := p.getDb().Model(&pgWorkflows)
for key, value := range filters {
if value != "" {
query = query.Order(key + " " + value)
Copy link
Member

Choose a reason for hiding this comment

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

Read about sql injection and see how you can prevent it in go lang? Are we susceptible to SQL injection here? Can we validate?

Copy link
Member

Choose a reason for hiding this comment

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

you are using filters for ordering? Shouldn't filters be used for filtering and then sortBy query params for ordering? This doesn't feel right.

Copy link
Member Author

Choose a reason for hiding this comment

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

I was thinking about not calling it filters. I will rename it to something else.

As far as SQL injection is concerned, the parser ensures no illegal filters passed

utils/parser.go Outdated
@@ -0,0 +1,46 @@
package utils
Copy link
Member

Choose a reason for hiding this comment

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

I don't like utils as a package name. I would rather do filtering or filter as package and then parsing and other functions will be in that package. utils means that I could not identify a domain object for this responsibility so lets use utils.

Copy link
Member Author

Choose a reason for hiding this comment

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

This can be changed I think filters is fine

Copy link
Member

Choose a reason for hiding this comment

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

Filters is not the right name if this is sorting criteria. It should be order or ordering package instead.

Copy link
Member

Choose a reason for hiding this comment

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

Ok I didn't realize that utils is outside repository. This is a bad design. The db or repository related stuff should be inside repository package. And any filtering or sorting should be within postgres file since if we decide to have multiple repositories the implementation of repository will differ. Lets talk about this one.

@@ -250,3 +251,26 @@ func TestShouldThrowErrorIfQueryParamsAreNotValidValuesInGetAllWorkflows(t *test
assert.NotNil(t, jsonResp)
assert.Equal(t, "page number or page size is not in proper format", jsonResp.Message)
}

func TestShouldThrowErrorIfFiltersAreNotInTheRightFormat(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

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

This is an integration test. These are expensive to run and manage. Here you have only covered negative path. If anything you should have a happy path at least.

Copy link
Member

Choose a reason for hiding this comment

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

Alternatively, extracting error handling code into a common method and writing tests only once for that is better. All positive and negative tests can be covered in actual implementations' unit test

Copy link
Member

@priyaaank priyaaank left a comment

Choose a reason for hiding this comment

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

More feedback around cleanup method

utils/parser.go Outdated
cleanedFilters := make(map[string]string)
for _, key := range supportedKeys {
value := filters[key]
if value != "asc" && value != "desc" && value != "" {
Copy link
Member

Choose a reason for hiding this comment

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

Should it be case insensitive?

Copy link
Member

Choose a reason for hiding this comment

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

What happens down the method if the string is empty? Are you setting a default? It doesn't look like

Copy link
Member Author

Choose a reason for hiding this comment

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

yes it can be case insensitive ( do we want it to be ?)

Go by default assigns "" to keys that are missing so just to verify validity we need to check empty too

Copy link
Member

Choose a reason for hiding this comment

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

but for keys that are valid and value is empty what happens? Also, yes if DB support case insensitivity, which I think it does, it should be. Or we should force lowercase conversion before parsing the string.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Create API to get list of workflows created with pagination
2 participants