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