Skip to content

Commit

Permalink
gh-1106 extend get single action with api links
Browse files Browse the repository at this point in the history
  • Loading branch information
etiennedi committed Mar 27, 2020
1 parent 90bad8b commit b70055e
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 3 deletions.
5 changes: 5 additions & 0 deletions adapters/handlers/rest/handlers_kinds.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,11 @@ func (h *kindHandlers) getAction(params actions.ActionsGetParams,
}
}

schemaMap, ok := action.Schema.(map[string]interface{})
if ok {
action.Schema = h.extendSchemaWithAPILinks(schemaMap)
}

h.telemetryLogAsync(telemetry.TypeREST, telemetry.LocalQuery)
return actions.NewActionsGetOK().WithPayload(action)
}
Expand Down
70 changes: 67 additions & 3 deletions adapters/handlers/rest/handlers_kinds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"github.com/go-openapi/strfmt"
"github.com/semi-technologies/weaviate/adapters/handlers/rest/operations/actions"
"github.com/semi-technologies/weaviate/adapters/handlers/rest/operations/things"
"github.com/semi-technologies/weaviate/entities/models"
"github.com/stretchr/testify/assert"
Expand All @@ -14,7 +15,6 @@ import (

func TestEnrichObjectsWithLinks(t *testing.T) {
t.Run("get thing", func(t *testing.T) {

type test struct {
name string
thing *models.Thing
Expand Down Expand Up @@ -76,13 +76,77 @@ func TestEnrichObjectsWithLinks(t *testing.T) {
assert.Equal(t, test.expectedResult, parsed.Payload)
})
}
})

t.Run("get action", func(t *testing.T) {
type test struct {
name string
action *models.Action
expectedResult *models.Action
}

tests := []test{
test{
name: "without props - noaction changes",
action: &models.Action{Class: "Foo", Schema: nil},
expectedResult: &models.Action{Class: "Foo", Schema: nil},
},
test{
name: "without ref props - noaction changes",
action: &models.Action{Class: "Foo", Schema: map[string]interface{}{
"name": "hello world",
"numericalField": 134,
}},
expectedResult: &models.Action{Class: "Foo", Schema: map[string]interface{}{
"name": "hello world",
"numericalField": 134,
}},
},
test{
name: "with a ref prop - no origin configured",
action: &models.Action{Class: "Foo", Schema: map[string]interface{}{
"name": "hello world",
"numericalField": 134,
"someRef": models.MultipleRef{
&models.SingleRef{
Beacon: "weaviate://localhost/actions/85f78e29-5937-4390-a121-5379f262b4e5",
},
},
}},
expectedResult: &models.Action{Class: "Foo", Schema: map[string]interface{}{
"name": "hello world",
"numericalField": 134,
"someRef": models.MultipleRef{
&models.SingleRef{
Beacon: "weaviate://localhost/actions/85f78e29-5937-4390-a121-5379f262b4e5",
Href: "/v1/actions/85f78e29-5937-4390-a121-5379f262b4e5",
},
},
}},
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {

fakeManager := &fakeManager{
getActionReturn: test.action,
}
fakeRequestLog := &fakeRequestLog{}
h := &kindHandlers{manager: fakeManager, requestsLog: fakeRequestLog}
res := h.getAction(actions.ActionsGetParams{HTTPRequest: httptest.NewRequest("GET", "/v1/actions", nil)}, nil)
parsed, ok := res.(*actions.ActionsGetOK)
require.True(t, ok)
assert.Equal(t, test.expectedResult, parsed.Payload)
})
}
})

}

type fakeManager struct {
getThingReturn *models.Thing
getThingReturn *models.Thing
getActionReturn *models.Action
}

func (f *fakeManager) AddThing(_ context.Context, _ *models.Principal, _ *models.Thing) (*models.Thing, error) {
Expand All @@ -106,7 +170,7 @@ func (f *fakeManager) GetThing(_ context.Context, _ *models.Principal, _ strfmt.
}

func (f *fakeManager) GetAction(_ context.Context, _ *models.Principal, _ strfmt.UUID, _ bool) (*models.Action, error) {
panic("not implemented") // TODO: Implement
return f.getActionReturn, nil
}

func (f *fakeManager) GetThings(_ context.Context, _ *models.Principal, _ *int64, _ bool) ([]*models.Thing, error) {
Expand Down

0 comments on commit b70055e

Please sign in to comment.