-
Notifications
You must be signed in to change notification settings - Fork 35
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
🌱 add dependency package in test/api #375
Conversation
Signed-off-by: Yash Khare <yash2010118@akgec.ac.in>
03b021e
to
6ed6cd0
Compare
:seedling:
add dependency directory necessary files
:seedling:
add dependency directory necessary filesSigned-off-by: Yash Khare <yash2010118@akgec.ac.in>
Working on handling the Samples array to take |
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.
Hey Yash, thanks for your PR 👍 There are few things we need to work on (comments in the source code), but overall looks good!
test/api/dependency/api_test.go
Outdated
for _, r := range Samples { | ||
t.Run("Dependency_CRUD", func(t *testing.T) { | ||
// Create. | ||
err := Dependency.Create(&r) |
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.
The API Dependency links two Applications and keeps their IDs in From/To fields. These fields are Refs (https://github.com/konveyor/tackle2-hub/blob/main/api/base.go#L379) so contain just IDs (and optionally Name, not that not important), but the Refs are not actual Applications. The From and To Applications need to be created before creating Dependency.
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.
As I understood from the review that dependency need To/From fields which will be used to refer to the applications?
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.
and that To/From will be generated by creating an instance of application in api_test.go
. Am i in the right direction?
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
test/api/dependency/api_test.go
Outdated
|
||
for name := range samples { | ||
sample := samples[name] | ||
assert.Must(t, Dependency.Create(&sample)) |
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.
Same with creating Dependencies - 1st Applications, 2nd Dependency between them.
test/api/dependency/samples.go
Outdated
} | ||
tc = []TestCase{testCase} | ||
Samples = []api.Dependency{tc} | ||
) |
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.
Definition of Dependencies (and likely also TestCases) will need to be updated. There is some limitation caused by api.Dependency keeping just Ref of Aplication, so we need store application elsewhere. I can see few options:
- Use TestCases struct that might even not contain the Dependency itself, but something like just ApplicationFrom and ApplicationTo, so the test will go through testcases and create ApplicationTo, ApplicationFrom and then dependency between these applications (the dependency doesn't have any special fields than the Application refs, so it should work well).
- Use model.Dependency model, that stores links to Application (not just Refs), see https://github.com/konveyor/tackle2-hub/blob/main/migration/v2/model/dependency.go#L12-L18 (yes, we do have slightly different models for database and API). The Samples array would be array of these model.Dependency and test could create both Applications and "convert" the model.Dependency to api.Dependency using method With() https://github.com/konveyor/tackle2-hub/blob/main/api/dependency.go#L157-L161
- It would great if you can try both ways (or maybe find something else if you'd like it more than suggestions above) and we can discuss how it works if needed.
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.
Definition of Dependencies (and likely also TestCases) will need to be updated. There is some limitation caused by api.Dependency keeping just Ref of Aplication, so we need store application elsewhere. I can see few options:
- Use TestCases struct that might even not contain the Dependency itself, but something like just ApplicationFrom and ApplicationTo, so the test will go through testcases and create ApplicationTo, ApplicationFrom and then dependency between these applications (the dependency doesn't have any special fields than the Application refs, so it should work well).
- Use model.Dependency model, that stores links to Application (not just Refs), see https://github.com/konveyor/tackle2-hub/blob/main/migration/v2/model/dependency.go#L12-L18 (yes, we do have slightly different models for database and API). The Samples array would be array of these model.Dependency and test could create both Applications and "convert" the model.Dependency to api.Dependency using method With() https://github.com/konveyor/tackle2-hub/blob/main/api/dependency.go#L157-L161
- It would great if you can try both ways (or maybe find something else if you'd like it more than suggestions above) and we can discuss how it works if needed.
I think 1st option is easier to implement because it involves less complexities and 2nd option is is less intuitive which can be easily dealt by the 1st. What's your say on that?
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.
I have a thought process of injecting dependency withdependecy-less TestCase in Samples[]. and then use From/To in the api_test.go
to access them in the dependency
to be used by various CRUD functions.
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.
I'm not sure I fully understand, but sounds good.
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.
A Followup to this discussion and the 1st option, the dependency variable (and Samples containing only the dependency when api_test.go creates new applicationFrom/To instances) here seems to me to be overcomplicated.
What about drop the dependency variable, considering update/rename TestCase and putting to Samples (or renamed array for tests) and doing all this in since var( ... ) block?
Signed-off-by: Yash Khare <yash2010118@akgec.ac.in>
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.
Thanks for update! Left few comments, let's sync tomorrow.
test/api/dependency/samples.go
Outdated
} | ||
|
||
var ( | ||
dependency = api.Dependency{ |
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.
Some of these lines seems to be redundant to me (the dependency here and init() below). But I'd suggest get it to (partially) working state (passing tests against locally running Hub https://github.com/konveyor/tackle2-hub/tree/main/test#rest-api) and we can refactor it then.
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.
Refactored the code Kindly review
Signed-off-by: Yash Khare <yash2010118@akgec.ac.in>
Signed-off-by: Yash Khare <yash2010118@akgec.ac.in>
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.
Thanks for update @khareyash05, I'm happy that there is already a working/test passing code, great job!
I can see two things to be done before mering this PR:
- remove unnecessary parts from the code (comments inline)
- add test to ensure there are no cyclic dependencies
-
- direct: when Gateway->Inventory dependency exists, API shoud not allow Inventory->Gateway dependency to be created
-
- indirect: when Gateway->Inventory and Inventory->Database dependencies exist, API shoud not allow Database->Gateway dependency to be created
(just for info, related code inside the Hub is https://github.com/konveyor/tackle2-hub/blob/main/migration/v2/model/dependency.go#L30)
- indirect: when Gateway->Inventory and Inventory->Database dependencies exist, API shoud not allow Database->Gateway dependency to be created
test/api/dependency/samples.go
Outdated
} | ||
tc = []TestCase{testCase} | ||
Samples = []api.Dependency{tc} | ||
) |
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.
A Followup to this discussion and the 1st option, the dependency variable (and Samples containing only the dependency when api_test.go creates new applicationFrom/To instances) here seems to me to be overcomplicated.
What about drop the dependency variable, considering update/rename TestCase and putting to Samples (or renamed array for tests) and doing all this in since var( ... ) block?
test/api/dependency/api_test.go
Outdated
for _, r := range Samples { | ||
t.Run(fmt.Sprintf("Dependency from %s -> %s", r.From.Name, r.To.Name), func(t *testing.T) { | ||
|
||
applicationFrom := api.Application{ |
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.
I'm not sure about creating new instances of applicationFrom and To, but that depends on samples.go, see comment there.
Signed-off-by: Yash Khare <yash2010118@akgec.ac.in>
TODO: Cyclic Dependency. |
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.
Looks good! Added few minor comments, once resolved those, I'm OK merging this PR. The cyclic dependency test can be in a follow-up PR.
Signed-off-by: Yash Khare <yash2010118@akgec.ac.in>
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.
👍 Needed some clarification on Must/Should and fix List test - recommend run the test locally more than once to be sure, that one test run doesn't leave data for another test run.
test/api/dependency/api_test.go
Outdated
|
||
// Create applications. | ||
assert.Should(t, Application.Create(&sample.ApplicationFrom)) | ||
assert.Should(t, Application.Create(&sample.ApplicationTo)) |
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.
Lines assigning IDs to sample to allow its deletion in the end of the test should have stay here (sample.ApplicationFrom.ID = applicationFrom.ID).
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.
but we are deleting applicationFrom.ID as it is not needed. I am failing to understand as to why will we need this line
assert.Should(t, Application.Create(&sample.ApplicationFrom)) | ||
assert.Should(t, Application.Create(&sample.ApplicationTo)) | ||
} | ||
|
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.
There should be a Dependency creation and condition below should check if got
is an array with the created dependency. Sorry, I missed this part before.
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.
Shall I just compare the ID's of dependency created and the ApplicationFrom/To ID's will it suffice?
Signed-off-by: Yash Khare <yash2010118@akgec.ac.in>
Signed-off-by: Yash Khare <yash2010118@akgec.ac.in>
@aufi Kindly review |
test/api/dependency/api_test.go
Outdated
} | ||
if assert.FlatEqual(got, Samples) { | ||
t.Errorf("Different response error. Got %v, expected %v", got, Samples) | ||
} |
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.
Found out we're using the assert.FlatEqual method in a wrong way (also in some other tests outside of this PR), the condition should look like if !assert.FlatEqual(got, Samples) { //error
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.
Was wondering too when working on the part before! This might be a breaking change in the code, as inverting the condition will make the test fail. Will dig deep on the same .
test/api/dependency/api_test.go
Outdated
} | ||
|
||
// Delete Dependencies. | ||
for _, dependency := range got { |
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.
Thihs would delete all dependencies in API, not only ones created by this test. Not saying it is blocker in this case, but better track created objects to be deleted.
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.
Ya that's something should be taken care of. A way to tackle this what I can think of is creating an array of ID's and store ID's of dependencies at their time of creation. The Delete() of the Dependency will work on the array of ID's and will only delete them.
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.
As we have (or we will) ensure that all the created Dependencies are present in the Dependency.List(), we will ensure that all the dependencies are used and thus they are allowed to be deleted.
Signed-off-by: Yash Khare <yash2010118@akgec.ac.in>
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.
There are some things (assert with FlatEqual and add cyclic test), that need to be updated in followup PR, but:
Looks good to me, nice work Yash!
Thank you and will do that in the followup PR |
Upstream issue konveyor/operator#220
Issue in the repo #370
added api_test.go,pkg.go,samples.go in
dependency
directory. Also addeddependency.go
in binding package and added dependency client inbinding/richclient.go