Fix twins update revision counter (#1011)
Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com>
This commit is contained in:
parent
5714a6dfcb
commit
0d361f3df1
|
@ -28,6 +28,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
twinName = "name"
|
||||||
contentType = "application/json"
|
contentType = "application/json"
|
||||||
email = "user@example.com"
|
email = "user@example.com"
|
||||||
token = "token"
|
token = "token"
|
||||||
|
@ -201,9 +202,11 @@ func TestUpdateTwin(t *testing.T) {
|
||||||
|
|
||||||
twin := twins.Twin{ThingID: thingID}
|
twin := twins.Twin{ThingID: thingID}
|
||||||
def := twins.Definition{}
|
def := twins.Definition{}
|
||||||
data := toJSON(twin)
|
|
||||||
stw, _ := svc.AddTwin(context.Background(), token, twin, def)
|
stw, _ := svc.AddTwin(context.Background(), token, twin, def)
|
||||||
|
|
||||||
|
twin.Name = twinName
|
||||||
|
data := toJSON(twin)
|
||||||
|
|
||||||
tw := twin
|
tw := twin
|
||||||
tw.Name = invalidName
|
tw.Name = invalidName
|
||||||
invalidData := toJSON(tw)
|
invalidData := toJSON(tw)
|
||||||
|
@ -230,7 +233,7 @@ func TestUpdateTwin(t *testing.T) {
|
||||||
id: stw.ID,
|
id: stw.ID,
|
||||||
contentType: contentType,
|
contentType: contentType,
|
||||||
auth: token,
|
auth: token,
|
||||||
status: http.StatusOK,
|
status: http.StatusBadRequest,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
desc: "update non-existent twin",
|
desc: "update non-existent twin",
|
||||||
|
|
|
@ -154,27 +154,38 @@ func (ts *twinsService) UpdateTwin(ctx context.Context, token string, twin Twin,
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
tw.Updated = time.Now()
|
|
||||||
tw.Revision++
|
revision := false
|
||||||
|
|
||||||
if twin.Name != "" {
|
if twin.Name != "" {
|
||||||
|
revision = true
|
||||||
tw.Name = twin.Name
|
tw.Name = twin.Name
|
||||||
}
|
}
|
||||||
|
|
||||||
if twin.ThingID != "" {
|
if twin.ThingID != "" {
|
||||||
|
revision = true
|
||||||
tw.ThingID = twin.ThingID
|
tw.ThingID = twin.ThingID
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(def.Attributes) > 0 {
|
if len(def.Attributes) > 0 {
|
||||||
|
revision = true
|
||||||
def.Created = time.Now()
|
def.Created = time.Now()
|
||||||
def.ID = tw.Definitions[len(tw.Definitions)-1].ID + 1
|
def.ID = tw.Definitions[len(tw.Definitions)-1].ID + 1
|
||||||
tw.Definitions = append(tw.Definitions, def)
|
tw.Definitions = append(tw.Definitions, def)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(twin.Metadata) == 0 {
|
if len(twin.Metadata) > 0 {
|
||||||
|
revision = true
|
||||||
tw.Metadata = twin.Metadata
|
tw.Metadata = twin.Metadata
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !revision {
|
||||||
|
return ErrMalformedEntity
|
||||||
|
}
|
||||||
|
|
||||||
|
tw.Updated = time.Now()
|
||||||
|
tw.Revision++
|
||||||
|
|
||||||
if err := ts.twins.Update(ctx, tw); err != nil {
|
if err := ts.twins.Update(ctx, tw); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -86,6 +86,8 @@ func TestUpdateTwin(t *testing.T) {
|
||||||
saved, err := svc.AddTwin(context.Background(), token, twin, def)
|
saved, err := svc.AddTwin(context.Background(), token, twin, def)
|
||||||
require.Nil(t, err, fmt.Sprintf("unexpected error: %s\n", err))
|
require.Nil(t, err, fmt.Sprintf("unexpected error: %s\n", err))
|
||||||
|
|
||||||
|
saved.Name = twinName
|
||||||
|
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
desc string
|
desc string
|
||||||
twin twins.Twin
|
twin twins.Twin
|
||||||
|
|
|
@ -13,16 +13,16 @@ type Metadata map[string]interface{}
|
||||||
|
|
||||||
// Attribute stores individual attribute data
|
// Attribute stores individual attribute data
|
||||||
type Attribute struct {
|
type Attribute struct {
|
||||||
Channel string
|
Channel string `json:"channel"`
|
||||||
Subtopic string
|
Subtopic string `json:"subtopic"`
|
||||||
PersistState bool
|
PersistState bool `json:"persist_state"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Definition stores entity's attributes
|
// Definition stores entity's attributes
|
||||||
type Definition struct {
|
type Definition struct {
|
||||||
ID int
|
ID int `json:"id"`
|
||||||
Created time.Time
|
Created time.Time `json:"created"`
|
||||||
Attributes map[string]Attribute
|
Attributes map[string]Attribute `json:"attributes"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Twin represents a Mainflux thing digital twin. Each twin is owned by one thing, and
|
// Twin represents a Mainflux thing digital twin. Each twin is owned by one thing, and
|
||||||
|
|
Loading…
Reference in New Issue