op_create_test.go

 1package bug
 2
 3import (
 4	"encoding/json"
 5	"testing"
 6	"time"
 7
 8	"github.com/stretchr/testify/require"
 9
10	"github.com/MichaelMure/git-bug/entity"
11	"github.com/MichaelMure/git-bug/identity"
12	"github.com/MichaelMure/git-bug/repository"
13	"github.com/MichaelMure/git-bug/util/timestamp"
14)
15
16func TestCreate(t *testing.T) {
17	snapshot := Snapshot{}
18
19	repo := repository.NewMockRepoClock()
20
21	rene, err := identity.NewIdentity(repo, "René Descartes", "rene@descartes.fr")
22	require.NoError(t, err)
23
24	unix := time.Now().Unix()
25
26	create := NewCreateOp(rene, unix, "title", "message", nil)
27
28	create.Apply(&snapshot)
29
30	id := create.Id()
31	require.NoError(t, id.Validate())
32
33	commentId := entity.CombineIds(create.Id(), create.Id())
34
35	comment := Comment{
36		id:       commentId,
37		Author:   rene,
38		Message:  "message",
39		UnixTime: timestamp.Timestamp(create.UnixTime),
40	}
41
42	expected := Snapshot{
43		id:    create.Id(),
44		Title: "title",
45		Comments: []Comment{
46			comment,
47		},
48		Author:       rene,
49		Participants: []identity.Interface{rene},
50		Actors:       []identity.Interface{rene},
51		CreateTime:   create.Time(),
52		Timeline: []TimelineItem{
53			&CreateTimelineItem{
54				CommentTimelineItem: NewCommentTimelineItem(commentId, comment),
55			},
56		},
57	}
58
59	require.Equal(t, expected, snapshot)
60}
61
62func TestCreateSerialize(t *testing.T) {
63	repo := repository.NewMockRepo()
64
65	rene, err := identity.NewIdentity(repo, "René Descartes", "rene@descartes.fr")
66	require.NoError(t, err)
67
68	unix := time.Now().Unix()
69	before := NewCreateOp(rene, unix, "title", "message", nil)
70
71	data, err := json.Marshal(before)
72	require.NoError(t, err)
73
74	var after CreateOperation
75	err = json.Unmarshal(data, &after)
76	require.NoError(t, err)
77
78	// enforce creating the ID
79	before.Id()
80
81	// Replace the identity stub with the real thing
82	require.Equal(t, rene.Id(), after.Author().Id())
83	after.Author_ = rene
84
85	require.Equal(t, before, &after)
86}