2018-10-21 01:55:58 +03:00
|
|
|
package bug
|
|
|
|
|
|
|
|
import (
|
2019-01-19 21:23:31 +03:00
|
|
|
"encoding/json"
|
2018-10-21 01:55:58 +03:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2018-11-21 20:56:12 +03:00
|
|
|
"github.com/MichaelMure/git-bug/identity"
|
2018-10-21 01:55:58 +03:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-02-16 19:32:30 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-10-21 01:55:58 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSetMetadata(t *testing.T) {
|
|
|
|
snapshot := Snapshot{}
|
|
|
|
|
2019-02-16 19:32:30 +03:00
|
|
|
rene := identity.NewBare("René Descartes", "rene@descartes.fr")
|
2018-10-21 01:55:58 +03:00
|
|
|
unix := time.Now().Unix()
|
|
|
|
|
|
|
|
create := NewCreateOp(rene, unix, "title", "create", nil)
|
|
|
|
create.SetMetadata("key", "value")
|
|
|
|
create.Apply(&snapshot)
|
|
|
|
snapshot.Operations = append(snapshot.Operations, create)
|
|
|
|
|
2019-08-11 15:08:03 +03:00
|
|
|
id1 := create.Id()
|
|
|
|
require.NoError(t, id1.Validate())
|
2018-10-21 01:55:58 +03:00
|
|
|
|
|
|
|
comment := NewAddCommentOp(rene, unix, "comment", nil)
|
|
|
|
comment.SetMetadata("key2", "value2")
|
|
|
|
comment.Apply(&snapshot)
|
|
|
|
snapshot.Operations = append(snapshot.Operations, comment)
|
|
|
|
|
2019-08-11 15:08:03 +03:00
|
|
|
id2 := comment.Id()
|
|
|
|
require.NoError(t, id2.Validate())
|
2018-10-21 01:55:58 +03:00
|
|
|
|
2019-08-11 15:08:03 +03:00
|
|
|
op1 := NewSetMetadataOp(rene, unix, id1, map[string]string{
|
2018-10-21 01:55:58 +03:00
|
|
|
"key": "override",
|
|
|
|
"key2": "value",
|
|
|
|
})
|
|
|
|
|
|
|
|
op1.Apply(&snapshot)
|
|
|
|
snapshot.Operations = append(snapshot.Operations, op1)
|
|
|
|
|
|
|
|
createMetadata := snapshot.Operations[0].AllMetadata()
|
|
|
|
assert.Equal(t, len(createMetadata), 2)
|
|
|
|
// original key is not overrided
|
|
|
|
assert.Equal(t, createMetadata["key"], "value")
|
|
|
|
// new key is set
|
|
|
|
assert.Equal(t, createMetadata["key2"], "value")
|
|
|
|
|
|
|
|
commentMetadata := snapshot.Operations[1].AllMetadata()
|
|
|
|
assert.Equal(t, len(commentMetadata), 1)
|
|
|
|
assert.Equal(t, commentMetadata["key2"], "value2")
|
|
|
|
|
2019-08-11 15:08:03 +03:00
|
|
|
op2 := NewSetMetadataOp(rene, unix, id2, map[string]string{
|
2018-10-21 01:55:58 +03:00
|
|
|
"key2": "value",
|
|
|
|
"key3": "value3",
|
|
|
|
})
|
|
|
|
|
|
|
|
op2.Apply(&snapshot)
|
|
|
|
snapshot.Operations = append(snapshot.Operations, op2)
|
|
|
|
|
|
|
|
createMetadata = snapshot.Operations[0].AllMetadata()
|
|
|
|
assert.Equal(t, len(createMetadata), 2)
|
|
|
|
assert.Equal(t, createMetadata["key"], "value")
|
|
|
|
assert.Equal(t, createMetadata["key2"], "value")
|
|
|
|
|
|
|
|
commentMetadata = snapshot.Operations[1].AllMetadata()
|
|
|
|
assert.Equal(t, len(commentMetadata), 2)
|
|
|
|
// original key is not overrided
|
|
|
|
assert.Equal(t, commentMetadata["key2"], "value2")
|
|
|
|
// new key is set
|
|
|
|
assert.Equal(t, commentMetadata["key3"], "value3")
|
|
|
|
|
2019-08-11 15:08:03 +03:00
|
|
|
op3 := NewSetMetadataOp(rene, unix, id1, map[string]string{
|
2018-10-21 01:55:58 +03:00
|
|
|
"key": "override",
|
|
|
|
"key2": "override",
|
|
|
|
})
|
|
|
|
|
|
|
|
op3.Apply(&snapshot)
|
|
|
|
snapshot.Operations = append(snapshot.Operations, op3)
|
|
|
|
|
|
|
|
createMetadata = snapshot.Operations[0].AllMetadata()
|
|
|
|
assert.Equal(t, len(createMetadata), 2)
|
|
|
|
// original key is not overrided
|
|
|
|
assert.Equal(t, createMetadata["key"], "value")
|
|
|
|
// previously set key is not overrided
|
|
|
|
assert.Equal(t, createMetadata["key2"], "value")
|
|
|
|
|
|
|
|
commentMetadata = snapshot.Operations[1].AllMetadata()
|
|
|
|
assert.Equal(t, len(commentMetadata), 2)
|
|
|
|
assert.Equal(t, commentMetadata["key2"], "value2")
|
|
|
|
assert.Equal(t, commentMetadata["key3"], "value3")
|
|
|
|
}
|
2019-01-19 21:23:31 +03:00
|
|
|
|
|
|
|
func TestSetMetadataSerialize(t *testing.T) {
|
|
|
|
var rene = identity.NewBare("René Descartes", "rene@descartes.fr")
|
|
|
|
unix := time.Now().Unix()
|
|
|
|
before := NewSetMetadataOp(rene, unix, "message", map[string]string{
|
|
|
|
"key1": "value1",
|
|
|
|
"key2": "value2",
|
|
|
|
})
|
|
|
|
|
|
|
|
data, err := json.Marshal(before)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
var after SetMetadataOperation
|
|
|
|
err = json.Unmarshal(data, &after)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2019-08-11 15:08:03 +03:00
|
|
|
// enforce creating the IDs
|
|
|
|
before.Id()
|
|
|
|
rene.Id()
|
2019-08-07 16:31:38 +03:00
|
|
|
|
2019-01-19 21:23:31 +03:00
|
|
|
assert.Equal(t, before, &after)
|
|
|
|
}
|