diff --git a/bug/op_create.go b/bug/op_create.go index 15fb69b5..044ddd72 100644 --- a/bug/op_create.go +++ b/bug/op_create.go @@ -54,6 +54,13 @@ func (op *CreateOperation) SetMetadata(key string, value string) { } 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.addParticipant(op.Author) diff --git a/bug/op_create_test.go b/bug/op_create_test.go index 533aec2e..73a65778 100644 --- a/bug/op_create_test.go +++ b/bug/op_create_test.go @@ -29,14 +29,17 @@ func TestCreate(t *testing.T) { id := create.Id() require.NoError(t, id.Validate()) + commentId := DeriveCommentId(create.Id(), create.Id()) + comment := Comment{ - id: id, + id: commentId, Author: rene, Message: "message", UnixTime: timestamp.Timestamp(create.UnixTime), } expected := Snapshot{ + id: create.Id(), Title: "title", Comments: []Comment{ comment, @@ -47,7 +50,7 @@ func TestCreate(t *testing.T) { CreateTime: create.Time(), Timeline: []TimelineItem{ &CreateTimelineItem{ - CommentTimelineItem: NewCommentTimelineItem(id, comment), + CommentTimelineItem: NewCommentTimelineItem(commentId, comment), }, }, } diff --git a/bug/op_edit_comment_test.go b/bug/op_edit_comment_test.go index 92ee7539..a7330932 100644 --- a/bug/op_edit_comment_test.go +++ b/bug/op_edit_comment_test.go @@ -43,35 +43,35 @@ func TestEdit(t *testing.T) { id3 := comment2.Id() 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) - require.Equal(t, len(snapshot.Timeline), 4) - require.Equal(t, len(snapshot.Timeline[0].(*CreateTimelineItem).History), 2) - require.Equal(t, len(snapshot.Timeline[1].(*AddCommentTimelineItem).History), 1) - require.Equal(t, len(snapshot.Timeline[3].(*AddCommentTimelineItem).History), 1) + require.Len(t, snapshot.Timeline, 4) + require.Len(t, snapshot.Timeline[0].(*CreateTimelineItem).History, 2) + require.Len(t, snapshot.Timeline[1].(*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[1].Message, "comment 1") 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) - require.Equal(t, len(snapshot.Timeline), 4) - require.Equal(t, len(snapshot.Timeline[0].(*CreateTimelineItem).History), 2) - require.Equal(t, len(snapshot.Timeline[1].(*AddCommentTimelineItem).History), 2) - require.Equal(t, len(snapshot.Timeline[3].(*AddCommentTimelineItem).History), 1) + require.Len(t, snapshot.Timeline, 4) + require.Len(t, snapshot.Timeline[0].(*CreateTimelineItem).History, 2) + require.Len(t, snapshot.Timeline[1].(*AddCommentTimelineItem).History, 2) + require.Len(t, snapshot.Timeline[3].(*AddCommentTimelineItem).History, 1) require.Equal(t, snapshot.Comments[0].Message, "create edited") require.Equal(t, snapshot.Comments[1].Message, "comment 1 edited") 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) - require.Equal(t, len(snapshot.Timeline), 4) - require.Equal(t, len(snapshot.Timeline[0].(*CreateTimelineItem).History), 2) - require.Equal(t, len(snapshot.Timeline[1].(*AddCommentTimelineItem).History), 2) - require.Equal(t, len(snapshot.Timeline[3].(*AddCommentTimelineItem).History), 2) + require.Len(t, snapshot.Timeline, 4) + require.Len(t, snapshot.Timeline[0].(*CreateTimelineItem).History, 2) + require.Len(t, snapshot.Timeline[1].(*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[1].Message, "comment 1 edited") require.Equal(t, snapshot.Comments[2].Message, "comment 2 edited") diff --git a/bug/op_set_metadata_test.go b/bug/op_set_metadata_test.go index c90f192a..c0c91617 100644 --- a/bug/op_set_metadata_test.go +++ b/bug/op_set_metadata_test.go @@ -46,14 +46,14 @@ func TestSetMetadata(t *testing.T) { snapshot.Operations = append(snapshot.Operations, op1) createMetadata := snapshot.Operations[0].AllMetadata() - require.Equal(t, len(createMetadata), 2) + require.Len(t, createMetadata, 2) // original key is not overrided require.Equal(t, createMetadata["key"], "value") // new key is set require.Equal(t, createMetadata["key2"], "value") commentMetadata := snapshot.Operations[1].AllMetadata() - require.Equal(t, len(commentMetadata), 1) + require.Len(t, commentMetadata, 1) require.Equal(t, commentMetadata["key2"], "value2") op2 := NewSetMetadataOp(rene, unix, id2, map[string]string{ @@ -65,12 +65,12 @@ func TestSetMetadata(t *testing.T) { snapshot.Operations = append(snapshot.Operations, op2) 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["key2"], "value") commentMetadata = snapshot.Operations[1].AllMetadata() - require.Equal(t, len(commentMetadata), 2) + require.Len(t, commentMetadata, 2) // original key is not overrided require.Equal(t, commentMetadata["key2"], "value2") // new key is set @@ -85,14 +85,14 @@ func TestSetMetadata(t *testing.T) { snapshot.Operations = append(snapshot.Operations, op3) createMetadata = snapshot.Operations[0].AllMetadata() - require.Equal(t, len(createMetadata), 2) + require.Len(t, createMetadata, 2) // original key is not overrided require.Equal(t, createMetadata["key"], "value") // previously set key is not overrided require.Equal(t, createMetadata["key2"], "value") 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["key3"], "value3") }