bug: fix tests

This commit is contained in:
Michael Muré 2020-11-30 00:41:50 +01:00
parent d96284da64
commit fcf43915e1
No known key found for this signature in database
GPG Key ID: A4457C029293126F
4 changed files with 33 additions and 23 deletions

View File

@ -54,6 +54,13 @@ func (op *CreateOperation) SetMetadata(key string, value string) {
} }
func (op *CreateOperation) Apply(snapshot *Snapshot) { func (op *CreateOperation) Apply(snapshot *Snapshot) {
// sanity check: will fail when adding a second Create
if snapshot.id != "" && snapshot.id != entity.UnsetId && snapshot.id != op.Id() {
panic("adding a second Create operation")
}
snapshot.id = op.Id()
snapshot.addActor(op.Author) snapshot.addActor(op.Author)
snapshot.addParticipant(op.Author) snapshot.addParticipant(op.Author)

View File

@ -29,14 +29,17 @@ func TestCreate(t *testing.T) {
id := create.Id() id := create.Id()
require.NoError(t, id.Validate()) require.NoError(t, id.Validate())
commentId := DeriveCommentId(create.Id(), create.Id())
comment := Comment{ comment := Comment{
id: id, id: commentId,
Author: rene, Author: rene,
Message: "message", Message: "message",
UnixTime: timestamp.Timestamp(create.UnixTime), UnixTime: timestamp.Timestamp(create.UnixTime),
} }
expected := Snapshot{ expected := Snapshot{
id: create.Id(),
Title: "title", Title: "title",
Comments: []Comment{ Comments: []Comment{
comment, comment,
@ -47,7 +50,7 @@ func TestCreate(t *testing.T) {
CreateTime: create.Time(), CreateTime: create.Time(),
Timeline: []TimelineItem{ Timeline: []TimelineItem{
&CreateTimelineItem{ &CreateTimelineItem{
CommentTimelineItem: NewCommentTimelineItem(id, comment), CommentTimelineItem: NewCommentTimelineItem(commentId, comment),
}, },
}, },
} }

View File

@ -43,35 +43,35 @@ func TestEdit(t *testing.T) {
id3 := comment2.Id() id3 := comment2.Id()
require.NoError(t, id3.Validate()) require.NoError(t, id3.Validate())
edit := NewEditCommentOp(rene, unix, id1, "create edited", nil) edit := NewEditCommentOp(rene, unix, snapshot.Comments[0].Id(), "create edited", nil)
edit.Apply(&snapshot) edit.Apply(&snapshot)
require.Equal(t, len(snapshot.Timeline), 4) require.Len(t, snapshot.Timeline, 4)
require.Equal(t, len(snapshot.Timeline[0].(*CreateTimelineItem).History), 2) require.Len(t, snapshot.Timeline[0].(*CreateTimelineItem).History, 2)
require.Equal(t, len(snapshot.Timeline[1].(*AddCommentTimelineItem).History), 1) require.Len(t, snapshot.Timeline[1].(*AddCommentTimelineItem).History, 1)
require.Equal(t, len(snapshot.Timeline[3].(*AddCommentTimelineItem).History), 1) require.Len(t, snapshot.Timeline[3].(*AddCommentTimelineItem).History, 1)
require.Equal(t, snapshot.Comments[0].Message, "create edited") require.Equal(t, snapshot.Comments[0].Message, "create edited")
require.Equal(t, snapshot.Comments[1].Message, "comment 1") require.Equal(t, snapshot.Comments[1].Message, "comment 1")
require.Equal(t, snapshot.Comments[2].Message, "comment 2") require.Equal(t, snapshot.Comments[2].Message, "comment 2")
edit2 := NewEditCommentOp(rene, unix, id2, "comment 1 edited", nil) edit2 := NewEditCommentOp(rene, unix, snapshot.Comments[1].Id(), "comment 1 edited", nil)
edit2.Apply(&snapshot) edit2.Apply(&snapshot)
require.Equal(t, len(snapshot.Timeline), 4) require.Len(t, snapshot.Timeline, 4)
require.Equal(t, len(snapshot.Timeline[0].(*CreateTimelineItem).History), 2) require.Len(t, snapshot.Timeline[0].(*CreateTimelineItem).History, 2)
require.Equal(t, len(snapshot.Timeline[1].(*AddCommentTimelineItem).History), 2) require.Len(t, snapshot.Timeline[1].(*AddCommentTimelineItem).History, 2)
require.Equal(t, len(snapshot.Timeline[3].(*AddCommentTimelineItem).History), 1) require.Len(t, snapshot.Timeline[3].(*AddCommentTimelineItem).History, 1)
require.Equal(t, snapshot.Comments[0].Message, "create edited") require.Equal(t, snapshot.Comments[0].Message, "create edited")
require.Equal(t, snapshot.Comments[1].Message, "comment 1 edited") require.Equal(t, snapshot.Comments[1].Message, "comment 1 edited")
require.Equal(t, snapshot.Comments[2].Message, "comment 2") require.Equal(t, snapshot.Comments[2].Message, "comment 2")
edit3 := NewEditCommentOp(rene, unix, id3, "comment 2 edited", nil) edit3 := NewEditCommentOp(rene, unix, snapshot.Comments[2].Id(), "comment 2 edited", nil)
edit3.Apply(&snapshot) edit3.Apply(&snapshot)
require.Equal(t, len(snapshot.Timeline), 4) require.Len(t, snapshot.Timeline, 4)
require.Equal(t, len(snapshot.Timeline[0].(*CreateTimelineItem).History), 2) require.Len(t, snapshot.Timeline[0].(*CreateTimelineItem).History, 2)
require.Equal(t, len(snapshot.Timeline[1].(*AddCommentTimelineItem).History), 2) require.Len(t, snapshot.Timeline[1].(*AddCommentTimelineItem).History, 2)
require.Equal(t, len(snapshot.Timeline[3].(*AddCommentTimelineItem).History), 2) require.Len(t, snapshot.Timeline[3].(*AddCommentTimelineItem).History, 2)
require.Equal(t, snapshot.Comments[0].Message, "create edited") require.Equal(t, snapshot.Comments[0].Message, "create edited")
require.Equal(t, snapshot.Comments[1].Message, "comment 1 edited") require.Equal(t, snapshot.Comments[1].Message, "comment 1 edited")
require.Equal(t, snapshot.Comments[2].Message, "comment 2 edited") require.Equal(t, snapshot.Comments[2].Message, "comment 2 edited")

View File

@ -46,14 +46,14 @@ func TestSetMetadata(t *testing.T) {
snapshot.Operations = append(snapshot.Operations, op1) snapshot.Operations = append(snapshot.Operations, op1)
createMetadata := snapshot.Operations[0].AllMetadata() createMetadata := snapshot.Operations[0].AllMetadata()
require.Equal(t, len(createMetadata), 2) require.Len(t, createMetadata, 2)
// original key is not overrided // original key is not overrided
require.Equal(t, createMetadata["key"], "value") require.Equal(t, createMetadata["key"], "value")
// new key is set // new key is set
require.Equal(t, createMetadata["key2"], "value") require.Equal(t, createMetadata["key2"], "value")
commentMetadata := snapshot.Operations[1].AllMetadata() commentMetadata := snapshot.Operations[1].AllMetadata()
require.Equal(t, len(commentMetadata), 1) require.Len(t, commentMetadata, 1)
require.Equal(t, commentMetadata["key2"], "value2") require.Equal(t, commentMetadata["key2"], "value2")
op2 := NewSetMetadataOp(rene, unix, id2, map[string]string{ op2 := NewSetMetadataOp(rene, unix, id2, map[string]string{
@ -65,12 +65,12 @@ func TestSetMetadata(t *testing.T) {
snapshot.Operations = append(snapshot.Operations, op2) snapshot.Operations = append(snapshot.Operations, op2)
createMetadata = snapshot.Operations[0].AllMetadata() createMetadata = snapshot.Operations[0].AllMetadata()
require.Equal(t, len(createMetadata), 2) require.Len(t, createMetadata, 2)
require.Equal(t, createMetadata["key"], "value") require.Equal(t, createMetadata["key"], "value")
require.Equal(t, createMetadata["key2"], "value") require.Equal(t, createMetadata["key2"], "value")
commentMetadata = snapshot.Operations[1].AllMetadata() commentMetadata = snapshot.Operations[1].AllMetadata()
require.Equal(t, len(commentMetadata), 2) require.Len(t, commentMetadata, 2)
// original key is not overrided // original key is not overrided
require.Equal(t, commentMetadata["key2"], "value2") require.Equal(t, commentMetadata["key2"], "value2")
// new key is set // new key is set
@ -85,14 +85,14 @@ func TestSetMetadata(t *testing.T) {
snapshot.Operations = append(snapshot.Operations, op3) snapshot.Operations = append(snapshot.Operations, op3)
createMetadata = snapshot.Operations[0].AllMetadata() createMetadata = snapshot.Operations[0].AllMetadata()
require.Equal(t, len(createMetadata), 2) require.Len(t, createMetadata, 2)
// original key is not overrided // original key is not overrided
require.Equal(t, createMetadata["key"], "value") require.Equal(t, createMetadata["key"], "value")
// previously set key is not overrided // previously set key is not overrided
require.Equal(t, createMetadata["key2"], "value") require.Equal(t, createMetadata["key2"], "value")
commentMetadata = snapshot.Operations[1].AllMetadata() commentMetadata = snapshot.Operations[1].AllMetadata()
require.Equal(t, len(commentMetadata), 2) require.Len(t, commentMetadata, 2)
require.Equal(t, commentMetadata["key2"], "value2") require.Equal(t, commentMetadata["key2"], "value2")
require.Equal(t, commentMetadata["key3"], "value3") require.Equal(t, commentMetadata["key3"], "value3")
} }