-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🌱 Add Questionnaire to binding and API test (#492)
Adding Questionnaire endpoint to binding and basic API test. Started looking on Pathfinder export/Assessment import (via CLI), adding basic API test as a side-effect. Related to #492 --------- Signed-off-by: Marek Aufart <[email protected]>
- Loading branch information
Showing
6 changed files
with
208 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package binding | ||
|
||
import ( | ||
"github.com/konveyor/tackle2-hub/api" | ||
) | ||
|
||
// | ||
// Questionnaire API. | ||
type Questionnaire struct { | ||
client *Client | ||
} | ||
|
||
// | ||
// Create a Questionnaire. | ||
func (h *Questionnaire) Create(r *api.Questionnaire) (err error) { | ||
err = h.client.Post(api.QuestionnairesRoot, &r) | ||
return | ||
} | ||
|
||
// | ||
// Get a Questionnaire by ID. | ||
func (h *Questionnaire) Get(id uint) (r *api.Questionnaire, err error) { | ||
r = &api.Questionnaire{} | ||
path := Path(api.QuestionnaireRoot).Inject(Params{api.ID: id}) | ||
err = h.client.Get(path, r) | ||
return | ||
} | ||
|
||
// | ||
// List Questionnaires. | ||
func (h *Questionnaire) List() (list []api.Questionnaire, err error) { | ||
list = []api.Questionnaire{} | ||
err = h.client.Get(api.QuestionnairesRoot, &list) | ||
return | ||
} | ||
|
||
// | ||
// Update a Questionnaire. | ||
func (h *Questionnaire) Update(r *api.Questionnaire) (err error) { | ||
path := Path(api.QuestionnaireRoot).Inject(Params{api.ID: r.ID}) | ||
err = h.client.Put(path, r) | ||
return | ||
} | ||
|
||
// | ||
// Delete a Questionnaire. | ||
func (h *Questionnaire) Delete(id uint) (err error) { | ||
err = h.client.Delete(Path(api.QuestionnaireRoot).Inject(Params{api.ID: id})) | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package questionnaire | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/konveyor/tackle2-hub/test/assert" | ||
) | ||
|
||
func TestQuestionnaireCRUD(t *testing.T) { | ||
for _, r := range Samples { | ||
t.Run(r.Name, func(t *testing.T) { | ||
// Create. | ||
err := Questionnaire.Create(&r) | ||
if err != nil { | ||
t.Errorf(err.Error()) | ||
} | ||
|
||
// Get. | ||
got, err := Questionnaire.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 | ||
r.Required = false | ||
err = Questionnaire.Update(&r) | ||
if err != nil { | ||
t.Errorf(err.Error()) | ||
} | ||
|
||
got, err = Questionnaire.Get(r.ID) | ||
if err != nil { | ||
t.Errorf(err.Error()) | ||
} | ||
if got.Name != r.Name { | ||
t.Errorf("Different response error. Got %s, expected %s", got.Name, r.Name) | ||
} | ||
if got.Required != false { | ||
t.Errorf("Required should be false after update. Got %+v, expected %+v", got, r) | ||
} | ||
|
||
// Delete. | ||
err = Questionnaire.Delete(r.ID) | ||
if err != nil { | ||
t.Errorf(err.Error()) | ||
} | ||
|
||
_, err = Questionnaire.Get(r.ID) | ||
if err == nil { | ||
t.Errorf("Resource exits, but should be deleted: %v", r) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestQuestionnaireList(t *testing.T) { | ||
samples := Samples | ||
|
||
for name := range samples { | ||
sample := samples[name] | ||
assert.Must(t, Questionnaire.Create(&sample)) | ||
samples[name] = sample | ||
} | ||
|
||
got, err := Questionnaire.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, Questionnaire.Delete(r.ID)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package questionnaire | ||
|
||
import ( | ||
"github.com/konveyor/tackle2-hub/binding" | ||
"github.com/konveyor/tackle2-hub/test/api/client" | ||
) | ||
|
||
var ( | ||
RichClient *binding.RichClient | ||
Questionnaire binding.Questionnaire | ||
) | ||
|
||
|
||
func init() { | ||
// Prepare RichClient and login to Hub API (configured from env variables). | ||
RichClient = client.PrepareRichClient() | ||
|
||
// Shortcut for Questionnaire-related RichClient methods. | ||
Questionnaire = RichClient.Questionnaire | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package questionnaire | ||
|
||
import ( | ||
"github.com/konveyor/tackle2-hub/api" | ||
"github.com/konveyor/tackle2-hub/assessment" | ||
) | ||
|
||
// Set of valid resources for tests and reuse. | ||
var ( | ||
Questionnaire1 = api.Questionnaire{ | ||
Name: "Questionnaire1", | ||
Description: "Questionnaire minimal sample 1", | ||
Required: true, | ||
Thresholds: assessment.Thresholds{}, | ||
RiskMessages: assessment.RiskMessages{}, | ||
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.Questionnaire{Questionnaire1} | ||
) |