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