Skip to content

Commit

Permalink
Add error checking to endpoint state tests
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 13, 2020
1 parent b7fe428 commit 9c2b9eb
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 20 deletions.
25 changes: 16 additions & 9 deletions twins/api/http/endpoint_states_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ const (
attrSubtopic1 = "engine"
attrName2 = "humidity"
attrSubtopic2 = "chassis"

publisher = "twins"
)

type stateRes struct {
Expand Down Expand Up @@ -60,14 +62,16 @@ func TestListStates(t *testing.T) {
attr2,
},
}
tw, _ := svc.AddTwin(context.Background(), token, twin, def)
tw, err := svc.AddTwin(context.Background(), token, twin, def)
require.Nil(t, err, fmt.Sprintf("unexpected error: %s", err))

recs := createSenML(100, attrName1)
message := createMessage(attr1, recs)
err := svc.SaveStates(message)
message, err := createMessage(attr1, recs)
require.Nil(t, err, fmt.Sprintf("unexpected error: %s", err))
err = svc.SaveStates(message)
require.Nil(t, err, fmt.Sprintf("unexpected error: %s", err))

data := []stateRes{}
var data []stateRes
for i := 0; i < len(recs); i++ {
res := createStateResponse(i, tw, recs[i])
data = append(data, res)
Expand Down Expand Up @@ -111,7 +115,7 @@ func TestListStates(t *testing.T) {
res: nil,
},
{
desc: "get a list of states with + limit > total",
desc: "get a list of states with + limit > total",
auth: token,
status: http.StatusOK,
url: fmt.Sprintf(queryFmt, baseURL, 91, 20),
Expand Down Expand Up @@ -230,14 +234,17 @@ func createSenML(n int, bn string) []senml.Record {
return recs
}

func createMessage(attr twins.Attribute, recs []senml.Record) *messaging.Message {
mRecs, _ := json.Marshal(recs)
func createMessage(attr twins.Attribute, recs []senml.Record) (*messaging.Message, error) {
mRecs, err := json.Marshal(recs)
if err != nil {
return nil, err
}
return &messaging.Message{
Channel: attr.Channel,
Subtopic: attr.Subtopic,
Payload: mRecs,
Publisher: "twins",
}
Publisher: publisher,
}, nil
}

func createStateResponse(id int, tw twins.Twin, rec senml.Record) stateRes {
Expand Down
8 changes: 5 additions & 3 deletions twins/api/http/endpoint_twins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ func TestUpdateTwin(t *testing.T) {

twin := twins.Twin{}
def := twins.Definition{}
stw, _ := svc.AddTwin(context.Background(), token, twin, def)
stw, err := svc.AddTwin(context.Background(), token, twin, def)
require.Nil(t, err, fmt.Sprintf("unexpected error: %s", err))

twin.Name = twinName
data := toJSON(twin)
Expand Down Expand Up @@ -373,7 +374,7 @@ func TestListTwins(t *testing.T) {
ts := newServer(svc)
defer ts.Close()

data := []twinRes{}
var data []twinRes
for i := 0; i < 100; i++ {
name := fmt.Sprintf("%s-%d", twinName, i)
twin := twins.Twin{
Expand Down Expand Up @@ -548,7 +549,8 @@ func TestRemoveTwin(t *testing.T) {

def := twins.Definition{}
twin := twins.Twin{}
stw, _ := svc.AddTwin(context.Background(), token, twin, def)
stw, err := svc.AddTwin(context.Background(), token, twin, def)
require.Nil(t, err, fmt.Sprintf("unexpected error: %s", err))

cases := []struct {
desc string
Expand Down
2 changes: 1 addition & 1 deletion twins/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

// Package twins contains the domain concept definitions needed to support
// Mainflux twins service functionality. Twin is a semantic representation of a
// Mainflux twins service functionality. Twin is a digital representation of a
// real world data system consisting of data producers and consumers. It stores
// the sequence of attribute based definitions of a data system and refers to a
// time series of definition based states that store the system historical data.
Expand Down
2 changes: 1 addition & 1 deletion twins/mocks/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type mockBroker struct {
}

// New returns mock message publisher.
func New(sub map[string]string) messaging.Publisher {
func NewBroker(sub map[string]string) messaging.Publisher {
return &mockBroker{
subscriptions: sub,
}
Expand Down
2 changes: 1 addition & 1 deletion twins/mocks/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ func New(tokens map[string]string) twins.Service {
statesRepo := NewStateRepository()
idp := NewIdentityProvider()
subs := map[string]string{"chanID": "chanID"}
broker := New(subs)
broker := NewBroker(subs)
return twins.New(broker, auth, twinsRepo, statesRepo, idp, "chanID", nil)
}
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.NewService(map[string]string{token: email})
svc := mocks.New(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.NewService(map[string]string{token: email})
svc := mocks.New(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.NewService(map[string]string{token: email})
svc := mocks.New(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.NewService(map[string]string{token: email})
svc := mocks.New(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.NewService(map[string]string{token: email})
svc := mocks.New(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 9c2b9eb

Please sign in to comment.