bug_test.go

 1package bug
 2
 3import (
 4	"github.com/MichaelMure/git-bug/repository"
 5	"github.com/go-test/deep"
 6
 7	"testing"
 8)
 9
10func TestBugId(t *testing.T) {
11	mockRepo := repository.NewMockRepoForTest()
12
13	bug1 := NewBug()
14
15	bug1.Append(createOp)
16
17	err := bug1.Commit(mockRepo)
18
19	if err != nil {
20		t.Fatal(err)
21	}
22
23	bug1.Id()
24}
25
26func TestBugValidity(t *testing.T) {
27	mockRepo := repository.NewMockRepoForTest()
28
29	bug1 := NewBug()
30
31	if bug1.Validate() == nil {
32		t.Fatal("Empty bug should be invalid")
33	}
34
35	bug1.Append(createOp)
36
37	if bug1.Validate() != nil {
38		t.Fatal("Bug with just a CreateOp should be valid")
39	}
40
41	err := bug1.Commit(mockRepo)
42	if err != nil {
43		t.Fatal(err)
44	}
45
46	bug1.Append(createOp)
47
48	if bug1.Validate() == nil {
49		t.Fatal("Bug with multiple CreateOp should be invalid")
50	}
51
52	err = bug1.Commit(mockRepo)
53	if err == nil {
54		t.Fatal("Invalid bug should not commit")
55	}
56}
57
58func TestBugSerialisation(t *testing.T) {
59	bug1 := NewBug()
60
61	bug1.Append(createOp)
62	bug1.Append(setTitleOp)
63	bug1.Append(setTitleOp)
64	bug1.Append(addCommentOp)
65
66	repo := repository.NewMockRepoForTest()
67
68	bug1.Commit(repo)
69
70	bug2, err := ReadLocalBug(repo, bug1.Id())
71	if err != nil {
72		t.Error(err)
73	}
74
75	// ignore some fields
76	bug2.packs[0].commitHash = bug1.packs[0].commitHash
77	for i := range bug1.packs[0].Operations {
78		bug2.packs[0].Operations[i].base().hash = bug1.packs[0].Operations[i].base().hash
79	}
80
81	// check hashes
82	for i := range bug1.packs[0].Operations {
83		if !bug2.packs[0].Operations[i].base().hash.IsValid() {
84			t.Fatal("invalid hash")
85		}
86	}
87
88	deep.CompareUnexportedFields = true
89	if diff := deep.Equal(bug1, bug2); diff != nil {
90		t.Fatal(diff)
91	}
92}