Skip to content

Commit

Permalink
Initial Asessment API test
Browse files Browse the repository at this point in the history
Signed-off-by: Marek Aufart <[email protected]>
  • Loading branch information
aufi committed Oct 5, 2023
1 parent 81422c1 commit 31e3f51
Show file tree
Hide file tree
Showing 5 changed files with 208 additions and 0 deletions.
50 changes: 50 additions & 0 deletions binding/assessment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package binding

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

//
// Assessment API.
type Assessment struct {
client *Client
}

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

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

//
// List Assessments.
func (h *Assessment) List() (list []api.Assessment, err error) {
list = []api.Assessment{}
err = h.client.Get(api.AssessmentsRoot, &list)
return
}

//
// Update a Assessment.
func (h *Assessment) Update(r *api.Assessment) (err error) {
path := Path(api.AssessmentRoot).Inject(Params{api.ID: r.ID})
err = h.client.Put(path, r)
return
}

//
// Delete a Assessment.
func (h *Assessment) Delete(id uint) (err error) {
err = h.client.Delete(Path(api.AssessmentRoot).Inject(Params{api.ID: id}))
return
}
4 changes: 4 additions & 0 deletions binding/richclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func init() {
type RichClient struct {
// Resources APIs.
Application Application
Assessment Assessment
Bucket Bucket
BusinessService BusinessService
Dependency Dependency
Expand Down Expand Up @@ -58,6 +59,9 @@ func New(baseUrl string) (r *RichClient) {
Application: Application{
client: client,
},
Assessment: Assessment{
client: client,
},
Bucket: Bucket{
client: client,
},
Expand Down
81 changes: 81 additions & 0 deletions test/api/assessment/api_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package assessment

import (
"fmt"
"testing"

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

func TestAssessmentCRUD(t *testing.T) {
for _, r := range Samples {
t.Run(fmt.Sprintf("%s for application %s", r.Questionnaire.Name, r.Application.Name), func(t *testing.T) {
// Create.
err := Assessment.Create(&r)
if err != nil {
t.Errorf(err.Error())
}

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

// Update.
r.Name = "Updated " + r.Name

Check failure on line 29 in test/api/assessment/api_test.go

View workflow job for this annotation

GitHub Actions / test-api

r.Name undefined (type api.Assessment has no field or method Name)
r.Required = false

Check failure on line 30 in test/api/assessment/api_test.go

View workflow job for this annotation

GitHub Actions / test-api

r.Required undefined (type api.Assessment has no field or method Required)
err = Assessment.Update(&r)
if err != nil {
t.Errorf(err.Error())
}

got, err = Assessment.Get(r.ID)
if err != nil {
t.Errorf(err.Error())
}
if got.Name != r.Name {

Check failure on line 40 in test/api/assessment/api_test.go

View workflow job for this annotation

GitHub Actions / test-api

got.Name undefined (type *api.Assessment has no field or method Name)

Check failure on line 40 in test/api/assessment/api_test.go

View workflow job for this annotation

GitHub Actions / test-api

r.Name undefined (type api.Assessment has no field or method Name)
t.Errorf("Different response error. Got %s, expected %s", got.Name, r.Name)

Check failure on line 41 in test/api/assessment/api_test.go

View workflow job for this annotation

GitHub Actions / test-api

got.Name undefined (type *api.Assessment has no field or method Name)

Check failure on line 41 in test/api/assessment/api_test.go

View workflow job for this annotation

GitHub Actions / test-api

r.Name undefined (type api.Assessment has no field or method Name)
}
if got.Required != false {

Check failure on line 43 in test/api/assessment/api_test.go

View workflow job for this annotation

GitHub Actions / test-api

got.Required undefined (type *api.Assessment has no field or method Required)
t.Errorf("Required should be false after update. Got %+v, expected %+v", got, r)
}

// Delete.
err = Assessment.Delete(r.ID)
if err != nil {
t.Errorf(err.Error())
}

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

func TestAssessmentList(t *testing.T) {
samples := Samples

for name := range samples {
sample := samples[name]
assert.Must(t, Assessment.Create(&sample))
samples[name] = sample
}

got, err := Assessment.List()
if err != nil {
t.Errorf(err.Error())
}
if assert.FlatEqual(got, &samples) {
t.Errorf("Different response error. Got %v, expected %v", got, samples)
}

for _, r := range samples {
assert.Must(t, Assessment.Delete(r.ID))
}
}
20 changes: 20 additions & 0 deletions test/api/assessment/pkg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package assessment

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

var (
RichClient *binding.RichClient
Assessment binding.Assessment
)


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

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

import (
"github.com/konveyor/tackle2-hub/api"
"github.com/konveyor/tackle2-hub/assessment"
"github.com/konveyor/tackle2-hub/test/api/application"
"github.com/konveyor/tackle2-hub/test/api/questionnaire"
)

// Set of valid resources for tests and reuse.
var (
Assessment1 = api.Assessment{
// Ref resource are created by the test.
Application: &api.Ref{
Name: application.Minimal.Name,
},
Questionnaire: api.Ref{
Name: questionnaire.Questionnaire1.Name,
},
Sections: []assessment.Section{
{
Order: 1,
Name: "Section 1",
Questions: []assessment.Question{
{
Order: 1,
Text: "What is your favorite color?",
Explanation: "Please tell us your favorite color.",
Answers: []assessment.Answer{
{
Order: 1,
Text: "Red",
Risk: "red",
},
{
Order: 2,
Text: "Green",
Risk: "green",
},
{
Order: 3,
Text: "Blue",
Risk: "yellow",
Selected: true,
},
},
},
},
},
},
}
Samples = []api.Assessment{Assessment1}
)

0 comments on commit 31e3f51

Please sign in to comment.