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

🌱 add dependency package in test/api #375

Merged
merged 10 commits into from
Jun 9, 2023
16 changes: 10 additions & 6 deletions api/dependency.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package api

import (
"net/http"

khareyash05 marked this conversation as resolved.
Show resolved Hide resolved
"github.com/gin-gonic/gin"
"github.com/konveyor/tackle2-hub/model"
"gorm.io/gorm/clause"
"net/http"
)

//
Expand Down Expand Up @@ -67,12 +68,15 @@ func (h DependencyHandler) List(ctx *gin.Context) {
var list []model.Dependency

db := h.Paginated(ctx)
to := ctx.Query("to.id")
from := ctx.Query("from.id")
if to != "" {
db = db.Where("toid = ?", to)
from := ctx.Query("from")
khareyash05 marked this conversation as resolved.
Show resolved Hide resolved
to := ctx.Query("to")

if from != "" && to != "" {
db = db.Where("fromid = ? AND toid = ?", from, to)
} else if from != "" {
db = db.Where("fromid = ?", from)
} else if to != "" {
db = db.Where("toid = ?", to)
}

db = h.preLoad(db, clause.Associations)
Expand Down Expand Up @@ -115,7 +119,7 @@ func (h DependencyHandler) Create(ctx *gin.Context) {
_ = ctx.Error(err)
return
}

r.With(m)
khareyash05 marked this conversation as resolved.
Show resolved Hide resolved
h.Respond(ctx, http.StatusCreated, r)
}

Expand Down
43 changes: 43 additions & 0 deletions binding/dependency.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package binding

import (
"github.com/konveyor/tackle2-hub/api"
)

//
// Dependency API.
type Dependency struct {
// hub API client.
client *Client
}

//
// Create a Dependency.
func (h *Dependency) Create(r *api.Dependency) (err error) {
err = h.client.Post(api.DependenciesRoot, &r)
return
}

//
// Get a Dependency by ID.
func (h *Dependency) Get(id uint) (r *api.Dependency, err error) {
r = &api.Dependency{}
path := Path(api.DependencyRoot).Inject(Params{api.ID: id})
err = h.client.Get(path, r)
return
}

//
// List Dependencies.
func (h *Dependency) List() (list []api.Dependency, err error) {
list = []api.Dependency{}
err = h.client.Get(api.DependenciesRoot, &list)
return
}

//
// Delete a Dependency.
func (h *Dependency) Delete(id uint) (err error) {
err = h.client.Delete(Path(api.DependencyRoot).Inject(Params{api.ID: id}))
return
}
6 changes: 5 additions & 1 deletion binding/richclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func init() {
type RichClient struct {
// Resources APIs.
Application Application
Bucket Bucket
Bucket Bucket
BusinessService BusinessService
Identity Identity
JobFunction JobFunction
Expand All @@ -32,6 +32,7 @@ type RichClient struct {
Tag Tag
TagCategory TagCategory
Task Task
Dependency Dependency

// A REST client.
client *Client
Expand Down Expand Up @@ -60,6 +61,9 @@ func New(baseUrl string) (r *RichClient) {
BusinessService: BusinessService{
client: client,
},
Dependency: Dependency{
client: client,
},
Identity: Identity{
client: client,
},
Expand Down
91 changes: 91 additions & 0 deletions test/api/dependency/api_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package dependency

import (
"fmt"
"testing"

"github.com/konveyor/tackle2-hub/api"
"github.com/konveyor/tackle2-hub/test/assert"
)

func TestDependencyCRUD(t *testing.T) {
for _, sample := range Samples {
t.Run(fmt.Sprintf("Dependency from %s -> %s", sample.ApplicationFrom.Name, sample.ApplicationTo.Name), func(t *testing.T) {

assert.Must(t, Application.Create(&sample.ApplicationFrom))
assert.Must(t, Application.Create(&sample.ApplicationTo))

// Create.
dependency := api.Dependency{
From: api.Ref{
ID: sample.ApplicationFrom.ID,
},
To: api.Ref{
ID: sample.ApplicationTo.ID,
},
}
assert.Should(t, Dependency.Create(&dependency))

// Get.
got, err := Dependency.Get(dependency.ID)
if err != nil {
t.Errorf(err.Error())
}
if assert.FlatEqual(got, dependency.ID) {
t.Errorf("Different response error. Got %v, expected %v", got, dependency.ID)
}

// Delete dependency.
assert.Should(t, Dependency.Delete(dependency.ID))
khareyash05 marked this conversation as resolved.
Show resolved Hide resolved

_, err = Dependency.Get(dependency.ID)
if err == nil {
t.Errorf("Resource exits, but should be deleted: %v", dependency)
}

//Delete Applications
assert.Must(t, Application.Delete(sample.ApplicationFrom.ID))
assert.Must(t, Application.Delete(sample.ApplicationTo.ID))
})
}
}

func TestDependencyList(t *testing.T) {
for _, sample := range Samples {

// Create applications.
assert.Must(t, Application.Create(&sample.ApplicationFrom))
assert.Must(t, Application.Create(&sample.ApplicationTo))

// Create dependencies.
dependency := api.Dependency{
From: api.Ref{
ID: sample.ApplicationFrom.ID,
},
To: api.Ref{
ID: sample.ApplicationTo.ID,
},
}
assert.Should(t, Dependency.Create(&dependency))
}

Copy link
Member

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.

Copy link
Contributor Author

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?

// List dependencies.
got, err := Dependency.List()
if err != nil {
t.Errorf(err.Error())
}
if assert.FlatEqual(got, Samples) {
t.Errorf("Different response error. Got %v, expected %v", got, Samples)
}
Copy link
Member

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

Copy link
Contributor Author

@khareyash05 khareyash05 Jun 8, 2023

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 .


// Delete Dependencies.
for _, dependency := range got {
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

assert.Should(t, Dependency.Delete(dependency.ID))
}

// Delete applications.
for _, sample := range Samples {
assert.Must(t, Application.Delete(sample.ApplicationFrom.ID))
assert.Must(t, Application.Delete(sample.ApplicationTo.ID))
khareyash05 marked this conversation as resolved.
Show resolved Hide resolved
}
}
23 changes: 23 additions & 0 deletions test/api/dependency/pkg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package dependency

import (
"github.com/konveyor/tackle2-hub/binding"
"github.com/konveyor/tackle2-hub/test/api/client"
)

var (
RichClient *binding.RichClient
Dependency binding.Dependency
Application binding.Application
)

func init() {
// Prepare RichClient and login to Hub API (configured from env variables).
RichClient = client.PrepareRichClient()

// Shortcut for Dependency related RichClient methods.
Dependency = RichClient.Dependency
khareyash05 marked this conversation as resolved.
Show resolved Hide resolved

//Shortcut for Application related RichClient methods.
Application = RichClient.Application
}
24 changes: 24 additions & 0 deletions test/api/dependency/samples.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package dependency

import (
"github.com/konveyor/tackle2-hub/api"
)

// Struct to hold dependency sample data.
type DependencySample struct {
ApplicationFrom api.Application
ApplicationTo api.Application
}

var Samples = []DependencySample{
{
ApplicationFrom: api.Application{
Name: "Gateway",
Description: "Gateway application",
},
ApplicationTo: api.Application{
Name: "Inventory",
Description: "Inventory application",
},
},
}