git-bug/bug/op_create_test.go

83 lines
1.7 KiB
Go
Raw Normal View History

package bug
2018-07-12 22:31:41 +03:00
import (
2019-01-19 21:23:31 +03:00
"encoding/json"
2018-07-12 22:31:41 +03:00
"testing"
"time"
"github.com/stretchr/testify/require"
2018-11-21 20:56:12 +03:00
"github.com/MichaelMure/git-bug/identity"
"github.com/MichaelMure/git-bug/repository"
"github.com/MichaelMure/git-bug/util/timestamp"
2018-07-12 22:31:41 +03:00
)
func TestCreate(t *testing.T) {
snapshot := Snapshot{}
2018-07-12 22:31:41 +03:00
repo := repository.NewMockRepoClock()
rene, err := identity.NewIdentity(repo, "René Descartes", "rene@descartes.fr")
require.NoError(t, err)
unix := time.Now().Unix()
create := NewCreateOp(rene, unix, "title", "message", nil)
2018-07-12 22:31:41 +03:00
create.Apply(&snapshot)
2018-07-12 22:31:41 +03:00
id := create.Id()
require.NoError(t, id.Validate())
comment := Comment{
id: id,
Author: rene,
Message: "message",
UnixTime: timestamp.Timestamp(create.UnixTime),
}
expected := Snapshot{
2018-07-12 22:31:41 +03:00
Title: "title",
Comments: []Comment{
comment,
2018-07-12 22:31:41 +03:00
},
Author: rene,
Participants: []identity.Interface{rene},
Actors: []identity.Interface{rene},
CreateTime: create.Time(),
Timeline: []TimelineItem{
&CreateTimelineItem{
CommentTimelineItem: NewCommentTimelineItem(id, comment),
},
},
2018-07-12 22:31:41 +03:00
}
require.Equal(t, expected, snapshot)
2018-07-12 22:31:41 +03:00
}
2019-01-19 21:23:31 +03:00
func TestCreateSerialize(t *testing.T) {
repo := repository.NewMockRepo()
rene, err := identity.NewIdentity(repo, "René Descartes", "rene@descartes.fr")
require.NoError(t, err)
2019-01-19 21:23:31 +03:00
unix := time.Now().Unix()
before := NewCreateOp(rene, unix, "title", "message", nil)
data, err := json.Marshal(before)
require.NoError(t, err)
2019-01-19 21:23:31 +03:00
var after CreateOperation
err = json.Unmarshal(data, &after)
require.NoError(t, err)
2019-01-19 21:23:31 +03:00
// enforce creating the ID
before.Id()
// Replace the identity stub with the real thing
require.Equal(t, rene.Id(), after.base().Author.Id())
after.Author = rene
require.Equal(t, before, &after)
2019-01-19 21:23:31 +03:00
}