operation_iterator_test.go

 1package bug
 2
 3import (
 4	"github.com/MichaelMure/git-bug/identity"
 5	"github.com/MichaelMure/git-bug/repository"
 6	"github.com/stretchr/testify/assert"
 7
 8	"testing"
 9	"time"
10)
11
12var (
13// Beware, don't those test data in multi-repo situation !
14// As an example, the Identity would be considered commited after a commit
15// in one repo,
16// rene = identity.NewIdentity("René Descartes", "rene@descartes.fr")
17// unix = time.Now().Unix()
18
19// createOp      = NewCreateOp(rene, unix, "title", "message", nil)
20// setTitleOp    = NewSetTitleOp(rene, unix, "title2", "title1")
21// addCommentOp  = NewAddCommentOp(rene, unix, "message2", nil)
22// setStatusOp   = NewSetStatusOp(rene, unix, ClosedStatus)
23// labelChangeOp = NewLabelChangeOperation(rene, unix, []Label{"added"}, []Label{"removed"})
24)
25
26func ExampleOperationIterator() {
27	b := NewBug()
28
29	// add operations
30
31	it := NewOperationIterator(b)
32
33	for it.Next() {
34		// do something with each operations
35		_ = it.Value()
36	}
37}
38
39func TestOpIterator(t *testing.T) {
40	mockRepo := repository.NewMockRepoForTest()
41
42	rene := identity.NewIdentity("René Descartes", "rene@descartes.fr")
43	unix := time.Now().Unix()
44
45	createOp := NewCreateOp(rene, unix, "title", "message", nil)
46	setTitleOp := NewSetTitleOp(rene, unix, "title2", "title1")
47	addCommentOp := NewAddCommentOp(rene, unix, "message2", nil)
48	setStatusOp := NewSetStatusOp(rene, unix, ClosedStatus)
49	labelChangeOp := NewLabelChangeOperation(rene, unix, []Label{"added"}, []Label{"removed"})
50
51	bug1 := NewBug()
52
53	// first pack
54	bug1.Append(createOp)
55	bug1.Append(setTitleOp)
56	bug1.Append(addCommentOp)
57	bug1.Append(setStatusOp)
58	bug1.Append(labelChangeOp)
59	err := bug1.Commit(mockRepo)
60	assert.NoError(t, err)
61
62	// second pack
63	bug1.Append(setTitleOp)
64	bug1.Append(setTitleOp)
65	bug1.Append(setTitleOp)
66	err = bug1.Commit(mockRepo)
67	assert.NoError(t, err)
68
69	// staging
70	bug1.Append(setTitleOp)
71	bug1.Append(setTitleOp)
72	bug1.Append(setTitleOp)
73
74	it := NewOperationIterator(bug1)
75
76	counter := 0
77	for it.Next() {
78		_ = it.Value()
79		counter++
80	}
81
82	if counter != 11 {
83		t.Fatalf("Wrong count of value iterated (%d instead of 8)", counter)
84	}
85}