-
Notifications
You must be signed in to change notification settings - Fork 86
Restrict iterating over every workitem field for reorder #2261
Restrict iterating over every workitem field for reorder #2261
Conversation
Ike Plugins (test-keeper)Thank you @DhritiShikhar for this contribution! It seems that this PR already contains some added or changed tests. Good job! For more information please head over to official documentation. You can find there how to configure the plugin. |
Codecov Report
@@ Coverage Diff @@
## master #2261 +/- ##
==========================================
+ Coverage 69.53% 71.37% +1.84%
==========================================
Files 176 176
Lines 16670 17818 +1148
==========================================
+ Hits 11591 12718 +1127
+ Misses 3956 3940 -16
- Partials 1123 1160 +37
Continue to review full report at Codecov.
|
[test] |
originalTitle := "Original Title" | ||
payload.Data.Attributes[workitem.SystemTitle] = originalTitle | ||
payload.Data.Attributes[workitem.SystemState] = workitem.SystemStateClosed | ||
_, workitem1 := test.CreateWorkitemsCreated(s.T(), s.svc.Context, s.svc, s.workitemsCtrl, *payload.Data.Relationships.Space.Data.ID, &payload) |
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.
You can use test fixture to create the initial work item. That would remove all the code from line 566 to 576
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.
@jarifibrahim The test fixture gives me a workitem of workitem.Workitem
type but I need app.WorkItem
to create reorder payload.
workitem/workitem_repository.go
Outdated
} | ||
} | ||
tx = tx.Where("Version = ?", wi.Version).Save(&res) | ||
tx = tx.Model(&res).Where("Version = ?", wi.Version).UpdateColumns(WorkItemStorage{ExecutionOrder: order, Version: res.Version + 1}) |
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.
Can't we use update()
instead of updateColumns
?
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.
When reading the gorm docs I found this:
Above updating operations will perform the model's
BeforeUpdate
,AfterUpdate
method, update itsUpdatedAt
timestamp, save its Associations when updaing, if you don't want to call them, you could useUpdateColumn
,UpdateColumns
.
(source: http://doc.gorm.io/crud.html#update)
We certainly want to call all those functions so UpdateColumn
and UpdateColumns
are not an option for us to use.
Probably the safest way to update the columns is using this syntax:
tx = tx.Model(WorkItemStorage{Version:wi.Version}).Select("version").Select("execution_order").Updates(map[string]interface{}{
"execution_order": order,
"version": res.Version + 1,
})
Notice that by using Model()
as I did we don't need the Where
function anymore.
What do you think?
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.
@kwk I am not completely sure how this works
tx = tx.Model(WorkItemStorage{Version:wi.Version}).Select("version").Select("execution_order").Updates(map[string]interface{}{
"execution_order": order,
"version": res.Version + 1,
})
I couldn't find examples like tx.Model(WorkItemStorage{Version:wi.Version})
. Is it equivalent to select .. where version = wi.Version?
dataArray = append(dataArray, workitem1.Data) | ||
payload2.Data = dataArray | ||
payload2.Position.ID = workitem2.Data.ID // Position.ID specifies the workitem ID above or below which the workitem(s) should be placed | ||
payload2.Position.Direction = string(workitem.DirectionAbove) |
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.
workitem.DirectionAbove
is already a string, you don't need the string(...)
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.
workitem.DirectionAbove
is workitem.DirectionType
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.
Yes, but workitem.DirectionType
is of type string
See https://play.golang.org/p/Nov7b0810R-
package main
import (
"fmt"
)
func main() {
type direction string
const foo direction = "above"
bar := foo
fmt.Println(foo, bar)
}
|
||
payload2 := minimumRequiredReorderPayload() | ||
|
||
var dataArray []*app.WorkItem // dataArray contains the workitem(s) that have to be reordered |
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.
This can also be removed when fxt.Workitems[]
is used.
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.
@jarifibrahim The test fixture gives me a workitem of workitem.Workitem type but I need app.WorkItem to create reorder payload.
controller/workitem_blackbox_test.go
Outdated
assert.Equal(s.T(), workitem1.Data.Attributes["version"].(int)+1, reorderedList.Data[0].Attributes["version"]) | ||
assert.Equal(s.T(), *workitem1.Data.ID, *reorderedList.Data[0].ID) | ||
assert.NotEqual(s.T(), titleNotUpdated, reorderedList.Data[0].Attributes[workitem.SystemTitle]) | ||
assert.Equal(s.T(), originalTitle, reorderedList.Data[0].Attributes[workitem.SystemTitle]) |
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.
We don't need both, this and https://github.com/fabric8-services/fabric8-wit/pull/2261/files#diff-98914f859911696f3cdd98dd289d00faR589 . One of them should be sufficient.
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.
fixed here adccee4
workitem/workitem_repository.go
Outdated
} | ||
} | ||
tx = tx.Where("Version = ?", wi.Version).Save(&res) | ||
tx = tx.Model(&res).Where("Version = ?", wi.Version).UpdateColumns(WorkItemStorage{ExecutionOrder: order, Version: res.Version + 1}) |
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.
When reading the gorm docs I found this:
Above updating operations will perform the model's
BeforeUpdate
,AfterUpdate
method, update itsUpdatedAt
timestamp, save its Associations when updaing, if you don't want to call them, you could useUpdateColumn
,UpdateColumns
.
(source: http://doc.gorm.io/crud.html#update)
We certainly want to call all those functions so UpdateColumn
and UpdateColumns
are not an option for us to use.
Probably the safest way to update the columns is using this syntax:
tx = tx.Model(WorkItemStorage{Version:wi.Version}).Select("version").Select("execution_order").Updates(map[string]interface{}{
"execution_order": order,
"version": res.Version + 1,
})
Notice that by using Model()
as I did we don't need the Where
function anymore.
What do you think?
I use
|
[test] |
[test] |
} | ||
} | ||
tx = tx.Where("Version = ?", wi.Version).Save(&res) | ||
tx = tx.Model(&res).Select("execution_order", "version").Updates(WorkItemStorage{ |
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.
Why doesn't this have a where
clause?
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.
because I pass .Model
instead of .Where
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.
@DhritiShikhar can you please show the .Debug().
output of this query? I think Gorm only checks for the primary key if you do .Model(&res).
. And the version
is not the primary field.
fixes openshiftio/openshift.io#4259