Skip to content

Commit

Permalink
🌱 Add Setting, Schema basic API tests (#580)
Browse files Browse the repository at this point in the history
Adding basic API test for following resources:
- Setting - basic CRUD
- Schema - Get

---------

Signed-off-by: Marek Aufart <[email protected]>
  • Loading branch information
aufi authored Dec 19, 2023
1 parent 829b523 commit d31e05e
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 0 deletions.
31 changes: 31 additions & 0 deletions binding/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,34 @@ func (h *Setting) Int(key string) (n int, err error) {
err = h.Get(key, &n)
return
}

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

//
// List Settings.
func (h *Setting) List() (list []api.Setting, err error) {
list = []api.Setting{}
err = h.client.Get(api.SettingsRoot, &list)
return
}

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

//
// Delete a Setting.
func (h *Setting) Delete(key string) (err error) {
path := Path(api.SettingRoot).Inject(Params{api.Key: key})
err = h.client.Delete(path)
return
}
19 changes: 19 additions & 0 deletions test/api/schema/api_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package schema

import (
"testing"

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

func TestGetSchema(t *testing.T) {
// Get.
schema := api.Schema{}
err := RichClient.Client.Get("/schema", &schema)
if err != nil {
t.Errorf(err.Error())
}
if len(schema.Paths) < 1 {
t.Errorf("Got empty Paths from /schema.")
}
}
16 changes: 16 additions & 0 deletions test/api/schema/pkg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package schema

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

var (
RichClient *binding.RichClient
)


func init() {
// Prepare RichClient and login to Hub API (configured from env variables).
RichClient = client.PrepareRichClient()
}
64 changes: 64 additions & 0 deletions test/api/setting/api_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package setting

import (
"testing"
)

func TestSettingCRUD(t *testing.T) {
for _, r := range Samples {
t.Run(r.Key, func(t *testing.T) {
// Create.
err := Setting.Create(&r)
if err != nil {
t.Errorf(err.Error())
}

// Get.
gotValue := ""
err = Setting.Get(r.Key, &gotValue)
if err != nil {
t.Errorf(err.Error())
}
if gotValue != r.Value {
t.Errorf("Different response error. Got %v, expected %v", gotValue, r)
}

// Update.
updateValue := "data-updated"
r.Value = updateValue
err = Setting.Update(&r)
if err != nil {
t.Errorf(err.Error())
}

err = Setting.Get(r.Key, &r)
if err != nil {
t.Errorf(err.Error())
}
if r.Value != updateValue {
t.Errorf("Different Setting Value error. Got %s, expected %s", gotValue, updateValue)
}

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

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

func TestSettingList(t *testing.T) {
got, err := Setting.List()
if err != nil {
t.Errorf(err.Error())
}
if len(got) < 1 {
t.Errorf("Got empty Settings list.")
}
}
20 changes: 20 additions & 0 deletions test/api/setting/pkg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package setting

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

var (
RichClient *binding.RichClient
Setting binding.Setting
)


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

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

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

// Set of valid resources for tests and reuse.
var (
SampleSetting = api.Setting{
Key: "sample.setting.1",
Value: "data-123",
}

Samples = []api.Setting{SampleSetting}
)

0 comments on commit d31e05e

Please sign in to comment.