aboutsummaryrefslogtreecommitdiffstats
path: root/storage/test/storage_suite.go
blob: b4ba3063e8ded31c0cb74e60c84e2018c91807e5 (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
154
155
156
157
158
159
160
161
package test

import (
	"io"

	. "gopkg.in/check.v1"
	"gopkg.in/src-d/go-git.v4/core"
)

type TestObject struct {
	Object core.Object
	Hash   string
	Type   core.ObjectType
}

type BaseStorageSuite struct {
	ObjectStorage core.ObjectStorage

	validTypes  []core.ObjectType
	testObjects map[core.ObjectType]TestObject
}

func NewBaseStorageSuite(s core.ObjectStorage) BaseStorageSuite {
	commit := &core.MemoryObject{}
	commit.SetType(core.CommitObject)
	tree := &core.MemoryObject{}
	tree.SetType(core.TreeObject)
	blob := &core.MemoryObject{}
	blob.SetType(core.BlobObject)
	tag := &core.MemoryObject{}
	tag.SetType(core.TagObject)

	return BaseStorageSuite{
		ObjectStorage: s,
		validTypes: []core.ObjectType{
			core.CommitObject,
			core.BlobObject,
			core.TagObject,
			core.TreeObject,
		},
		testObjects: map[core.ObjectType]TestObject{
			core.CommitObject: {commit, "dcf5b16e76cce7425d0beaef62d79a7d10fce1f5", core.CommitObject},
			core.TreeObject:   {tree, "4b825dc642cb6eb9a060e54bf8d69288fbee4904", core.TreeObject},
			core.BlobObject:   {blob, "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391", core.BlobObject},
			core.TagObject:    {tag, "d994c6bb648123a17e8f70a966857c546b2a6f94", core.TagObject},
		}}
}

func (s *BaseStorageSuite) TestObjectStorageSetAndGet(c *C) {
	for _, to := range s.testObjects {
		comment := Commentf("failed for type %s", to.Type.String())

		h, err := s.ObjectStorage.Set(to.Object)
		c.Assert(err, IsNil)
		c.Assert(h.String(), Equals, to.Hash, comment)

		o, err := s.ObjectStorage.Get(to.Type, h)
		c.Assert(err, IsNil)
		c.Assert(o, Equals, to.Object)

		o, err = s.ObjectStorage.Get(core.AnyObject, h)
		c.Assert(err, IsNil)
		c.Assert(o, Equals, to.Object)

		for _, t := range s.validTypes {
			if t == to.Type {
				continue
			}

			o, err = s.ObjectStorage.Get(t, h)
			c.Assert(o, IsNil)
			c.Assert(err, Equals, core.ErrObjectNotFound)
		}
	}
}

func (s *BaseStorageSuite) TestObjectStorageIter(c *C) {
	for _, o := range s.testObjects {
		s.ObjectStorage.Set(o.Object)
	}

	for _, t := range s.validTypes {
		comment := Commentf("failed for type %s)", t.String())
		i, err := s.ObjectStorage.Iter(t)
		c.Assert(err, IsNil, comment)

		o, err := i.Next()
		c.Assert(err, IsNil)
		c.Assert(o, Equals, s.testObjects[t].Object, comment)

		o, err = i.Next()
		c.Assert(o, IsNil)
		c.Assert(err, Equals, io.EOF, comment)
	}

	i, err := s.ObjectStorage.Iter(core.AnyObject)
	c.Assert(err, IsNil)

	foundObjects := []core.Object{}
	i.ForEach(func(o core.Object) error {
		foundObjects = append(foundObjects, o)
		return nil
	})

	c.Assert(foundObjects, HasLen, len(s.testObjects))
	for _, to := range s.testObjects {
		found := false
		for _, o := range foundObjects {
			if to.Object == o {
				found = true
				break
			}
		}
		c.Assert(found, Equals, true, Commentf("Object of type %s not found", to.Type.String()))
	}
}

func (s *BaseStorageSuite) TestTxObjectStorageSetAndCommit(c *C) {
	tx := s.ObjectStorage.Begin()
	for _, o := range s.testObjects {
		h, err := tx.Set(o.Object)
		c.Assert(err, IsNil)
		c.Assert(h.String(), Equals, o.Hash)
	}

	iter, err := s.ObjectStorage.Iter(core.AnyObject)
	c.Assert(err, IsNil)
	_, err = iter.Next()
	c.Assert(err, Equals, io.EOF)

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

	iter, err = s.ObjectStorage.Iter(core.AnyObject)
	c.Assert(err, IsNil)

	var count int
	iter.ForEach(func(o core.Object) error {
		count++
		return nil
	})

	c.Assert(count, Equals, 4)
}

func (s *BaseStorageSuite) TestTxObjectStorageSetAndRollback(c *C) {
	tx := s.ObjectStorage.Begin()
	for _, o := range s.testObjects {
		h, err := tx.Set(o.Object)
		c.Assert(err, IsNil)
		c.Assert(h.String(), Equals, o.Hash)
	}

	err := tx.Rollback()
	c.Assert(err, IsNil)

	iter, err := s.ObjectStorage.Iter(core.AnyObject)
	c.Assert(err, IsNil)
	_, err = iter.Next()
	c.Assert(err, Equals, io.EOF)
}