Fix twins update revision counter (#1011)

Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com>
This commit is contained in:
Darko Draskovic 2020-01-22 15:58:24 +01:00 committed by Nikola Marčetić
parent 5714a6dfcb
commit 0d361f3df1
4 changed files with 27 additions and 11 deletions

View File

@ -28,6 +28,7 @@ import (
)
const (
twinName = "name"
contentType = "application/json"
email = "user@example.com"
token = "token"
@ -201,9 +202,11 @@ func TestUpdateTwin(t *testing.T) {
twin := twins.Twin{ThingID: thingID}
def := twins.Definition{}
data := toJSON(twin)
stw, _ := svc.AddTwin(context.Background(), token, twin, def)
twin.Name = twinName
data := toJSON(twin)
tw := twin
tw.Name = invalidName
invalidData := toJSON(tw)
@ -230,7 +233,7 @@ func TestUpdateTwin(t *testing.T) {
id: stw.ID,
contentType: contentType,
auth: token,
status: http.StatusOK,
status: http.StatusBadRequest,
},
{
desc: "update non-existent twin",

View File

@ -154,27 +154,38 @@ func (ts *twinsService) UpdateTwin(ctx context.Context, token string, twin Twin,
if err != nil {
return err
}
tw.Updated = time.Now()
tw.Revision++
revision := false
if twin.Name != "" {
revision = true
tw.Name = twin.Name
}
if twin.ThingID != "" {
revision = true
tw.ThingID = twin.ThingID
}
if len(def.Attributes) > 0 {
revision = true
def.Created = time.Now()
def.ID = tw.Definitions[len(tw.Definitions)-1].ID + 1
tw.Definitions = append(tw.Definitions, def)
}
if len(twin.Metadata) == 0 {
if len(twin.Metadata) > 0 {
revision = true
tw.Metadata = twin.Metadata
}
if !revision {
return ErrMalformedEntity
}
tw.Updated = time.Now()
tw.Revision++
if err := ts.twins.Update(ctx, tw); err != nil {
return err
}

View File

@ -86,6 +86,8 @@ func TestUpdateTwin(t *testing.T) {
saved, err := svc.AddTwin(context.Background(), token, twin, def)
require.Nil(t, err, fmt.Sprintf("unexpected error: %s\n", err))
saved.Name = twinName
cases := []struct {
desc string
twin twins.Twin

View File

@ -13,16 +13,16 @@ type Metadata map[string]interface{}
// Attribute stores individual attribute data
type Attribute struct {
Channel string
Subtopic string
PersistState bool
Channel string `json:"channel"`
Subtopic string `json:"subtopic"`
PersistState bool `json:"persist_state"`
}
// Definition stores entity's attributes
type Definition struct {
ID int
Created time.Time
Attributes map[string]Attribute
ID int `json:"id"`
Created time.Time `json:"created"`
Attributes map[string]Attribute `json:"attributes"`
}
// Twin represents a Mainflux thing digital twin. Each twin is owned by one thing, and