aboutsummaryrefslogtreecommitdiffstats
path: root/storage/transactional/object_test.go
blob: e6344099db10a55b5c807ee6887933a5c2bbc45e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package transactional

import (
	. "gopkg.in/check.v1"
	"github.com/go-git/go-git/v5/plumbing"
	"github.com/go-git/go-git/v5/storage/memory"
)

var _ = Suite(&ObjectSuite{})

type ObjectSuite struct{}

func (s *ObjectSuite) TestHasEncodedObject(c *C) {
	base := memory.NewStorage()
	temporal := memory.NewStorage()

	os := NewObjectStorage(base, temporal)

	commit := base.NewEncodedObject()
	commit.SetType(plumbing.CommitObject)

	ch, err := base.SetEncodedObject(commit)
	c.Assert(ch.IsZero(), Equals, false)
	c.Assert(err, IsNil)

	tree := base.NewEncodedObject()
	tree.SetType(plumbing.TreeObject)

	th, err := os.SetEncodedObject(tree)
	c.Assert(th.IsZero(), Equals, false)
	c.Assert(err, IsNil)

	err = os.HasEncodedObject(th)
	c.Assert(err, IsNil)

	err = os.HasEncodedObject(ch)
	c.Assert(err, IsNil)

	err = base.HasEncodedObject(th)
	c.Assert(err, Equals, plumbing.ErrObjectNotFound)
}

func (s *ObjectSuite) TestEncodedObjectAndEncodedObjectSize(c *C) {
	base := memory.NewStorage()
	temporal := memory.NewStorage()

	os := NewObjectStorage(base, temporal)

	commit := base.NewEncodedObject()
	commit.SetType(plumbing.CommitObject)

	ch, err := base.SetEncodedObject(commit)
	c.Assert(ch.IsZero(), Equals, false)
	c.Assert(err, IsNil)

	tree := base.NewEncodedObject()
	tree.SetType(plumbing.TreeObject)

	th, err := os.SetEncodedObject(tree)
	c.Assert(th.IsZero(), Equals, false)
	c.Assert(err, IsNil)

	otree, err := os.EncodedObject(plumbing.TreeObject, th)
	c.Assert(err, IsNil)
	c.Assert(otree.Hash(), Equals, tree.Hash())

	treeSz, err := os.EncodedObjectSize(th)
	c.Assert(err, IsNil)
	c.Assert(treeSz, Equals, int64(0))

	ocommit, err := os.EncodedObject(plumbing.CommitObject, ch)
	c.Assert(err, IsNil)
	c.Assert(ocommit.Hash(), Equals, commit.Hash())

	commitSz, err := os.EncodedObjectSize(ch)
	c.Assert(err, IsNil)
	c.Assert(commitSz, Equals, int64(0))

	_, err = base.EncodedObject(plumbing.TreeObject, th)
	c.Assert(err, Equals, plumbing.ErrObjectNotFound)

	_, err = base.EncodedObjectSize(th)
	c.Assert(err, Equals, plumbing.ErrObjectNotFound)
}

func (s *ObjectSuite) TestIterEncodedObjects(c *C) {
	base := memory.NewStorage()
	temporal := memory.NewStorage()

	os := NewObjectStorage(base, temporal)

	commit := base.NewEncodedObject()
	commit.SetType(plumbing.CommitObject)

	ch, err := base.SetEncodedObject(commit)
	c.Assert(ch.IsZero(), Equals, false)
	c.Assert(err, IsNil)

	tree := base.NewEncodedObject()
	tree.SetType(plumbing.TreeObject)

	th, err := os.SetEncodedObject(tree)
	c.Assert(th.IsZero(), Equals, false)
	c.Assert(err, IsNil)

	iter, err := os.IterEncodedObjects(plumbing.AnyObject)
	c.Assert(err, IsNil)

	var hashes []plumbing.Hash
	err = iter.ForEach(func(obj plumbing.EncodedObject) error {
		hashes = append(hashes, obj.Hash())
		return nil
	})

	c.Assert(err, IsNil)
	c.Assert(hashes, HasLen, 2)
	c.Assert(hashes[0], Equals, ch)
	c.Assert(hashes[1], Equals, th)
}

func (s *ObjectSuite) TestCommit(c *C) {
	base := memory.NewStorage()
	temporal := memory.NewStorage()

	os := NewObjectStorage(base, temporal)

	commit := base.NewEncodedObject()
	commit.SetType(plumbing.CommitObject)

	_, err := os.SetEncodedObject(commit)
	c.Assert(err, IsNil)

	tree := base.NewEncodedObject()
	tree.SetType(plumbing.TreeObject)

	_, err = os.SetEncodedObject(tree)
	c.Assert(err, IsNil)

	err = os.Commit()
	c.Assert(err, IsNil)

	iter, err := base.IterEncodedObjects(plumbing.AnyObject)
	c.Assert(err, IsNil)

	var hashes []plumbing.Hash
	err = iter.ForEach(func(obj plumbing.EncodedObject) error {
		hashes = append(hashes, obj.Hash())
		return nil
	})

	c.Assert(err, IsNil)
	c.Assert(hashes, HasLen, 2)
}