op_create_test.go

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