Debian Codegraph @

plumbing/storer/object_test.go

raw
1 package storer
2
3 import (
4 "fmt"
5 "testing"
6
7 "github.com/go-git/go-git/v5/plumbing"
8
9 . "gopkg.in/check.v1"
10 )
11
12 func Test(t *testing.T) { TestingT(t) }
13
14 type ObjectSuite struct {
15 Objects []plumbing.EncodedObject
16 Hash []plumbing.Hash
17 }
18
19 var _ = Suite(&ObjectSuite{})
20
21 func (s *ObjectSuite) SetUpSuite(c *C) {
22 s.Objects = []plumbing.EncodedObject{
23 s.buildObject([]byte("foo")),
24 s.buildObject([]byte("bar")),
25 }
26
27 for _, o := range s.Objects {
28 s.Hash = append(s.Hash, o.Hash())
29 }
30 }
31
32 func (s *ObjectSuite) TestMultiObjectIterNext(c *C) {
33 expected := []plumbing.EncodedObject{
34 &plumbing.MemoryObject{},
35 &plumbing.MemoryObject{},
36 &plumbing.MemoryObject{},
37 &plumbing.MemoryObject{},
38 &plumbing.MemoryObject{},
39 &plumbing.MemoryObject{},
40 }
41
42 iter := NewMultiEncodedObjectIter([]EncodedObjectIter{
43 NewEncodedObjectSliceIter(expected[0:2]),
44 NewEncodedObjectSliceIter(expected[2:4]),
45 NewEncodedObjectSliceIter(expected[4:5]),
46 })
47
48 var i int
49 iter.ForEach(func(o plumbing.EncodedObject) error {
50 c.Assert(o, Equals, expected[i])
51 i++
52 return nil
53 })
54
55 iter.Close()
56 }
57
58 func (s *ObjectSuite) buildObject(content []byte) plumbing.EncodedObject {
59 o := &plumbing.MemoryObject{}
60 o.Write(content)
61
62 return o
63 }
64
65 func (s *ObjectSuite) TestObjectLookupIter(c *C) {
66 var count int
67
68 storage := &MockObjectStorage{s.Objects}
69 i := NewEncodedObjectLookupIter(storage, plumbing.CommitObject, s.Hash)
70 err := i.ForEach(func(o plumbing.EncodedObject) error {
71 c.Assert(o, NotNil)
72 c.Assert(o.Hash().String(), Equals, s.Hash[count].String())
73 count++
74 return nil
75 })
76
77 c.Assert(err, IsNil)
78 i.Close()
79 }
80
81 func (s *ObjectSuite) TestObjectSliceIter(c *C) {
82 var count int
83
84 i := NewEncodedObjectSliceIter(s.Objects)
85 err := i.ForEach(func(o plumbing.EncodedObject) error {
86 c.Assert(o, NotNil)
87 c.Assert(o.Hash().String(), Equals, s.Hash[count].String())
88 count++
89 return nil
90 })
91
92 c.Assert(count, Equals, 2)
93 c.Assert(err, IsNil)
94 c.Assert(i.series, HasLen, 0)
95 }
96
97 func (s *ObjectSuite) TestObjectSliceIterStop(c *C) {
98 i := NewEncodedObjectSliceIter(s.Objects)
99
100 var count = 0
101 err := i.ForEach(func(o plumbing.EncodedObject) error {
102 c.Assert(o, NotNil)
103 c.Assert(o.Hash().String(), Equals, s.Hash[count].String())
104 count++
105 return ErrStop
106 })
107
108 c.Assert(count, Equals, 1)
109 c.Assert(err, IsNil)
110 }
111
112 func (s *ObjectSuite) TestObjectSliceIterError(c *C) {
113 i := NewEncodedObjectSliceIter([]plumbing.EncodedObject{
114 s.buildObject([]byte("foo")),
115 })
116
117 err := i.ForEach(func(plumbing.EncodedObject) error {
118 return fmt.Errorf("a random error")
119 })
120
121 c.Assert(err, NotNil)
122 }
123
124 type MockObjectStorage struct {
125 db []plumbing.EncodedObject
126 }
127
128 func (o *MockObjectStorage) NewEncodedObject() plumbing.EncodedObject {
129 return nil
130 }
131
132 func (o *MockObjectStorage) SetEncodedObject(obj plumbing.EncodedObject) (plumbing.Hash, error) {
133 return plumbing.ZeroHash, nil
134 }
135
136 func (o *MockObjectStorage) HasEncodedObject(h plumbing.Hash) error {
137 for _, o := range o.db {
138 if o.Hash() == h {
139 return nil
140 }
141 }
142 return plumbing.ErrObjectNotFound
143 }
144
145 func (o *MockObjectStorage) EncodedObjectSize(h plumbing.Hash) (
146 size int64, err error) {
147 for _, o := range o.db {
148 if o.Hash() == h {
149 return o.Size(), nil
150 }
151 }
152 return 0, plumbing.ErrObjectNotFound
153 }
154
155 func (o *MockObjectStorage) EncodedObject(t plumbing.ObjectType, h plumbing.Hash) (plumbing.EncodedObject, error) {
156 for _, o := range o.db {
157 if o.Hash() == h {
158 return o, nil
159 }
160 }
161 return nil, plumbing.ErrObjectNotFound
162 }
163
164 func (o *MockObjectStorage) IterEncodedObjects(t plumbing.ObjectType) (EncodedObjectIter, error) {
165 return nil, nil
166 }
167
168 func (o *MockObjectStorage) Begin() Transaction {
169 return nil
170 }
171
172 func (o *MockObjectStorage) AddAlternate(remote string) error {
173 return nil
174 }