1package bug
2
3import (
4 "github.com/MichaelMure/git-bug/repository"
5 "github.com/stretchr/testify/assert"
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 err := bug1.Commit(repo)
69 assert.Nil(t, err)
70
71 bug2, err := ReadLocalBug(repo, bug1.Id())
72 if err != nil {
73 t.Error(err)
74 }
75
76 // ignore some fields
77 bug2.packs[0].commitHash = bug1.packs[0].commitHash
78 for i := range bug1.packs[0].Operations {
79 bug2.packs[0].Operations[i].base().hash = bug1.packs[0].Operations[i].base().hash
80 }
81
82 // check hashes
83 for i := range bug1.packs[0].Operations {
84 if !bug2.packs[0].Operations[i].base().hash.IsValid() {
85 t.Fatal("invalid hash")
86 }
87 }
88
89 assert.Equal(t, bug1, bug2)
90}