Skip to content

Commit

Permalink
gh-1106 setup unit test setup for kinds handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
etiennedi committed Mar 26, 2020
1 parent fe365dc commit 1aefdd0
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 2 deletions.
34 changes: 32 additions & 2 deletions adapters/handlers/rest/handlers_kinds.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
package rest

import (
"context"

middleware "github.com/go-openapi/runtime/middleware"
"github.com/go-openapi/strfmt"
"github.com/semi-technologies/weaviate/adapters/handlers/rest/operations"
"github.com/semi-technologies/weaviate/adapters/handlers/rest/operations/actions"
"github.com/semi-technologies/weaviate/adapters/handlers/rest/operations/things"
Expand All @@ -25,8 +28,35 @@ import (
)

type kindHandlers struct {
manager *kinds.Manager
requestsLog *telemetry.RequestsLog
manager kindsManager
requestsLog requestLog
}

type requestLog interface {
Register(string, string)
}

type kindsManager interface {
AddThing(context.Context, *models.Principal, *models.Thing) (*models.Thing, error)
AddAction(context.Context, *models.Principal, *models.Action) (*models.Action, error)
ValidateThing(context.Context, *models.Principal, *models.Thing) error
ValidateAction(context.Context, *models.Principal, *models.Action) error
GetThing(context.Context, *models.Principal, strfmt.UUID, bool) (*models.Thing, error)
GetAction(context.Context, *models.Principal, strfmt.UUID, bool) (*models.Action, error)
GetThings(context.Context, *models.Principal, *int64, bool) ([]*models.Thing, error)
GetActions(context.Context, *models.Principal, *int64, bool) ([]*models.Action, error)
UpdateThing(context.Context, *models.Principal, strfmt.UUID, *models.Thing) (*models.Thing, error)
UpdateAction(context.Context, *models.Principal, strfmt.UUID, *models.Action) (*models.Action, error)
MergeThing(context.Context, *models.Principal, strfmt.UUID, *models.Thing) error
MergeAction(context.Context, *models.Principal, strfmt.UUID, *models.Action) error
DeleteThing(context.Context, *models.Principal, strfmt.UUID) error
DeleteAction(context.Context, *models.Principal, strfmt.UUID) error
AddThingReference(context.Context, *models.Principal, strfmt.UUID, string, *models.SingleRef) error
AddActionReference(context.Context, *models.Principal, strfmt.UUID, string, *models.SingleRef) error
UpdateThingReferences(context.Context, *models.Principal, strfmt.UUID, string, models.MultipleRef) error
UpdateActionReferences(context.Context, *models.Principal, strfmt.UUID, string, models.MultipleRef) error
DeleteThingReference(context.Context, *models.Principal, strfmt.UUID, string, *models.SingleRef) error
DeleteActionReference(context.Context, *models.Principal, strfmt.UUID, string, *models.SingleRef) error
}

func (h *kindHandlers) addThing(params things.ThingsCreateParams,
Expand Down
137 changes: 137 additions & 0 deletions adapters/handlers/rest/handlers_kinds_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package rest

import (
"context"
"net/http/httptest"
"testing"

"github.com/go-openapi/strfmt"
"github.com/semi-technologies/weaviate/adapters/handlers/rest/operations/things"
"github.com/semi-technologies/weaviate/entities/models"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

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

type test struct {
name string
thing *models.Thing
expectedResult *models.Thing
}

tests := []test{
test{
name: "without props - nothing changes",
thing: &models.Thing{Class: "Foo", Schema: nil},
expectedResult: &models.Thing{Class: "Foo", Schema: nil},
},
}

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

fakeManager := &fakeManager{
getThingReturn: test.thing,
}
fakeRequestLog := &fakeRequestLog{}
h := &kindHandlers{manager: fakeManager, requestsLog: fakeRequestLog}
res := h.getThing(things.ThingsGetParams{HTTPRequest: httptest.NewRequest("GET", "/v1/things", nil)}, nil)
parsed, ok := res.(*things.ThingsGetOK)
require.True(t, ok)
assert.Equal(t, test.expectedResult, parsed.Payload)
})
}

})

}

type fakeManager struct {
getThingReturn *models.Thing
}

func (f *fakeManager) AddThing(_ context.Context, _ *models.Principal, _ *models.Thing) (*models.Thing, error) {
panic("not implemented") // TODO: Implement
}

func (f *fakeManager) AddAction(_ context.Context, _ *models.Principal, _ *models.Action) (*models.Action, error) {
panic("not implemented") // TODO: Implement
}

func (f *fakeManager) ValidateThing(_ context.Context, _ *models.Principal, _ *models.Thing) error {
panic("not implemented") // TODO: Implement
}

func (f *fakeManager) ValidateAction(_ context.Context, _ *models.Principal, _ *models.Action) error {
panic("not implemented") // TODO: Implement
}

func (f *fakeManager) GetThing(_ context.Context, _ *models.Principal, _ strfmt.UUID, _ bool) (*models.Thing, error) {
return f.getThingReturn, nil
}

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

func (f *fakeManager) GetThings(_ context.Context, _ *models.Principal, _ *int64, _ bool) ([]*models.Thing, error) {
panic("not implemented") // TODO: Implement
}

func (f *fakeManager) GetActions(_ context.Context, _ *models.Principal, _ *int64, _ bool) ([]*models.Action, error) {
panic("not implemented") // TODO: Implement
}

func (f *fakeManager) UpdateThing(_ context.Context, _ *models.Principal, _ strfmt.UUID, _ *models.Thing) (*models.Thing, error) {
panic("not implemented") // TODO: Implement
}

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

func (f *fakeManager) MergeThing(_ context.Context, _ *models.Principal, _ strfmt.UUID, _ *models.Thing) error {
panic("not implemented") // TODO: Implement
}

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

func (f *fakeManager) DeleteThing(_ context.Context, _ *models.Principal, _ strfmt.UUID) error {
panic("not implemented") // TODO: Implement
}

func (f *fakeManager) DeleteAction(_ context.Context, _ *models.Principal, _ strfmt.UUID) error {
panic("not implemented") // TODO: Implement
}

func (f *fakeManager) AddThingReference(_ context.Context, _ *models.Principal, _ strfmt.UUID, _ string, _ *models.SingleRef) error {
panic("not implemented") // TODO: Implement
}

func (f *fakeManager) AddActionReference(_ context.Context, _ *models.Principal, _ strfmt.UUID, _ string, _ *models.SingleRef) error {
panic("not implemented") // TODO: Implement
}

func (f *fakeManager) UpdateThingReferences(_ context.Context, _ *models.Principal, _ strfmt.UUID, _ string, _ models.MultipleRef) error {
panic("not implemented") // TODO: Implement
}

func (f *fakeManager) UpdateActionReferences(_ context.Context, _ *models.Principal, _ strfmt.UUID, _ string, _ models.MultipleRef) error {
panic("not implemented") // TODO: Implement
}

func (f *fakeManager) DeleteThingReference(_ context.Context, _ *models.Principal, _ strfmt.UUID, _ string, _ *models.SingleRef) error {
panic("not implemented") // TODO: Implement
}

func (f *fakeManager) DeleteActionReference(_ context.Context, _ *models.Principal, _ strfmt.UUID, _ string, _ *models.SingleRef) error {
panic("not implemented") // TODO: Implement
}

type fakeRequestLog struct{}

func (f *fakeRequestLog) Register(_ string, _ string) {}

0 comments on commit 1aefdd0

Please sign in to comment.