Skip to content

Commit

Permalink
Remove definition fields from twin req and res
Browse files Browse the repository at this point in the history
Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com>
  • Loading branch information
darkodraskovic committed May 18, 2020
1 parent 8e2c601 commit 41ca3e4
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 69 deletions.
23 changes: 8 additions & 15 deletions twins/api/http/endpoint_states_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,10 @@ const (
)

type stateRes struct {
TwinID string `json:"twin_id"`
ID int64 `json:"id"`
Definition int `json:"definition"`
// Created time.Time `json:"created"`
Payload map[string]interface{} `json:"payload"`
TwinID string `json:"twin_id"`
ID int64 `json:"id"`
Definition int `json:"definition"`
Payload map[string]interface{} `json:"payload"`
}

type statesPageRes struct {
Expand All @@ -46,7 +45,7 @@ type statesPageRes struct {
}

func TestListStates(t *testing.T) {
svc := mocks.New(map[string]string{token: email})
svc := mocks.NewService(map[string]string{token: email})
ts := newServer(svc)
defer ts.Close()

Expand Down Expand Up @@ -229,9 +228,8 @@ func createSenML(n int, bn string) []senml.Record {
rec := senml.Record{
BaseName: bn,
BaseTime: float64(time.Now().Unix()),
// BaseTime: float64(time.Now().UnixNano()) / float64(1e9),
Time: float64(i),
Value: nil,
Time: float64(i),
Value: nil,
}
recs = append(recs, rec)
}
Expand All @@ -252,15 +250,10 @@ func createMessage(attr twins.Attribute, recs []senml.Record) (*messaging.Messag
}

func createStateResponse(id int, tw twins.Twin, rec senml.Record) stateRes {
// recSec := rec.BaseTime + rec.Time
// sec, dec := math.Modf(recSec)
// recTime := time.Unix(int64(sec), int64(dec*nanosec))

return stateRes{
TwinID: tw.ID,
ID: int64(id),
Definition: tw.Definitions[len(tw.Definitions)-1].ID,
// Created: recTime,
Payload: map[string]interface{}{rec.BaseName: nil},
Payload: map[string]interface{}{rec.BaseName: nil},
}
}
88 changes: 40 additions & 48 deletions twins/api/http/endpoint_twins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,31 @@ const (

var invalidName = strings.Repeat("m", maxNameSize+1)

type twinReq struct {
token string
Name string `json:"name,omitempty"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
}

type twinRes struct {
Owner string `json:"owner"`
ID string `json:"id"`
Name string `json:"name,omitempty"`
Revision int `json:"revision"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
}

type pageRes struct {
Total uint64 `json:"total"`
Offset uint64 `json:"offset"`
Limit uint64 `json:"limit"`
}

type twinsPageRes struct {
pageRes
Twins []twinRes `json:"twins"`
}

type testRequest struct {
client *http.Client
method string
Expand Down Expand Up @@ -70,7 +95,7 @@ func toJSON(data interface{}) string {
}

func TestAddTwin(t *testing.T) {
svc := mocks.New(map[string]string{token: email})
svc := mocks.NewService(map[string]string{token: email})
ts := newServer(svc)
defer ts.Close()

Expand Down Expand Up @@ -173,7 +198,7 @@ func TestAddTwin(t *testing.T) {
}

func TestUpdateTwin(t *testing.T) {
svc := mocks.New(map[string]string{token: email})
svc := mocks.NewService(map[string]string{token: email})
ts := newServer(svc)
defer ts.Close()

Expand Down Expand Up @@ -286,7 +311,7 @@ func TestUpdateTwin(t *testing.T) {
}

func TestViewTwin(t *testing.T) {
svc := mocks.New(map[string]string{token: email})
svc := mocks.NewService(map[string]string{token: email})
ts := newServer(svc)
defer ts.Close()

Expand All @@ -296,12 +321,11 @@ func TestViewTwin(t *testing.T) {
require.Nil(t, err, fmt.Sprintf("unexpected error: %s", err))

twres := twinRes{
Owner: stw.Owner,
Name: stw.Name,
ID: stw.ID,
Revision: stw.Revision,
Definitions: stw.Definitions,
Metadata: stw.Metadata,
Owner: stw.Owner,
Name: stw.Name,
ID: stw.ID,
Revision: stw.Revision,
Metadata: stw.Metadata,
}

cases := []struct {
Expand Down Expand Up @@ -362,15 +386,11 @@ func TestViewTwin(t *testing.T) {
var resData twinRes
err = json.NewDecoder(res.Body).Decode(&resData)
assert.Equal(t, tc.res, resData, fmt.Sprintf("%s: expected body %v got %v", tc.desc, tc.res, resData))

// body, err := ioutil.ReadAll(res.Body)
// data := strings.Trim(string(body), "\n")
// assert.Equal(t, tc.res, data, fmt.Sprintf("%s: expected body %s got %s", tc.desc, tc.res, data))
}
}

func TestListTwins(t *testing.T) {
svc := mocks.New(map[string]string{token: email})
svc := mocks.NewService(map[string]string{token: email})
ts := newServer(svc)
defer ts.Close()

Expand All @@ -384,12 +404,11 @@ func TestListTwins(t *testing.T) {
tw, err := svc.AddTwin(context.Background(), token, twin, twins.Definition{})
require.Nil(t, err, fmt.Sprintf("unexpected error: %s", err))
twres := twinRes{
Owner: tw.Owner,
ID: tw.ID,
Name: tw.Name,
Revision: tw.Revision,
Definitions: tw.Definitions,
Metadata: tw.Metadata,
Owner: tw.Owner,
ID: tw.ID,
Name: tw.Name,
Revision: tw.Revision,
Metadata: tw.Metadata,
}
data = append(data, twres)
}
Expand Down Expand Up @@ -546,7 +565,7 @@ func TestListTwins(t *testing.T) {
}

func TestRemoveTwin(t *testing.T) {
svc := mocks.New(map[string]string{token: email})
svc := mocks.NewService(map[string]string{token: email})
ts := newServer(svc)
defer ts.Close()

Expand Down Expand Up @@ -605,30 +624,3 @@ func TestRemoveTwin(t *testing.T) {
assert.Equal(t, tc.status, res.StatusCode, fmt.Sprintf("%s: expected status code %d got %d", tc.desc, tc.status, res.StatusCode))
}
}

type twinReq struct {
token string
Name string `json:"name,omitempty"`
Definition twins.Definition `json:"definition,omitempty"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
}

type twinRes struct {
Owner string `json:"owner"`
ID string `json:"id"`
Name string `json:"name,omitempty"`
Revision int `json:"revision"`
Definitions []twins.Definition `json:"definitions"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
}

type pageRes struct {
Total uint64 `json:"total"`
Offset uint64 `json:"offset"`
Limit uint64 `json:"limit"`
}

type twinsPageRes struct {
pageRes
Twins []twinRes `json:"twins"`
}
3 changes: 2 additions & 1 deletion twins/mocks/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import (
"github.com/mainflux/mainflux/twins"
)

func New(tokens map[string]string) twins.Service {
// NewService use mock dependencies to create real twins service
func NewService(tokens map[string]string) twins.Service {
auth := NewAuthNServiceClient(tokens)
twinsRepo := NewTwinRepository()
statesRepo := NewStateRepository()
Expand Down
10 changes: 5 additions & 5 deletions twins/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const (
)

func TestAddTwin(t *testing.T) {
svc := mocks.New(map[string]string{token: email})
svc := mocks.NewService(map[string]string{token: email})
twin := twins.Twin{}
def := twins.Definition{}

Expand Down Expand Up @@ -55,7 +55,7 @@ func TestAddTwin(t *testing.T) {
}

func TestUpdateTwin(t *testing.T) {
svc := mocks.New(map[string]string{token: email})
svc := mocks.NewService(map[string]string{token: email})
twin := twins.Twin{}
other := twins.Twin{}
def := twins.Definition{}
Expand Down Expand Up @@ -99,7 +99,7 @@ func TestUpdateTwin(t *testing.T) {
}

func TestViewTwin(t *testing.T) {
svc := mocks.New(map[string]string{token: email})
svc := mocks.NewService(map[string]string{token: email})
twin := twins.Twin{}
def := twins.Definition{}
saved, err := svc.AddTwin(context.Background(), token, twin, def)
Expand Down Expand Up @@ -134,7 +134,7 @@ func TestViewTwin(t *testing.T) {
}

func TestListTwins(t *testing.T) {
svc := mocks.New(map[string]string{token: email})
svc := mocks.NewService(map[string]string{token: email})
twin := twins.Twin{Name: twinName, Owner: email}
def := twins.Definition{}
m := make(map[string]interface{})
Expand Down Expand Up @@ -192,7 +192,7 @@ func TestListTwins(t *testing.T) {
}

func TestRemoveTwin(t *testing.T) {
svc := mocks.New(map[string]string{token: email})
svc := mocks.NewService(map[string]string{token: email})
twin := twins.Twin{}
def := twins.Definition{}
saved, err := svc.AddTwin(context.Background(), token, twin, def)
Expand Down

0 comments on commit 41ca3e4

Please sign in to comment.