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 2389017
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 57 deletions.
21 changes: 7 additions & 14 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 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},
}
}
78 changes: 35 additions & 43 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 @@ -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,10 +386,6 @@ 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))
}
}

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 @@ -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"`
}

0 comments on commit 2389017

Please sign in to comment.