Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Commit 20b74b8

Browse files
committed
storage: idiomatic test suite
1 parent cdc374a commit 20b74b8

File tree

2 files changed

+51
-27
lines changed

2 files changed

+51
-27
lines changed

storage/memory/storage_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ import (
1111

1212
func Test(t *testing.T) { TestingT(t) }
1313

14-
type StorageSuite struct { }
14+
type StorageSuite struct {
15+
BaseStorageSuite
16+
}
1517

1618
var _ = Suite(&StorageSuite{})
1719

18-
func (s *StorageSuite) TestObjectStorage(c *C) {
19-
storage := NewStorage()
20-
RunObjectStorageSuite(c, storage.ObjectStorage())
20+
func (s *StorageSuite) SetUpSuite(c *C) {
21+
s.BaseStorageSuite = NewBaseStorageSuite(NewStorage().ObjectStorage())
2122
}
2223

2324
func (s *StorageSuite) TestStorageObjectStorage(c *C) {

storage/test/storage_suite.go

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,14 @@ type TestObject struct {
1313
Type core.ObjectType
1414
}
1515

16-
func RunObjectStorageSuite(c *C, os core.ObjectStorage) {
16+
type BaseStorageSuite struct {
17+
ObjectStorage core.ObjectStorage
18+
19+
validTypes []core.ObjectType
20+
testObjects map[core.ObjectType]TestObject
21+
}
22+
23+
func NewBaseStorageSuite(s core.ObjectStorage) BaseStorageSuite {
1724
commit := &core.MemoryObject{}
1825
commit.SetType(core.CommitObject)
1926
tree := &core.MemoryObject{}
@@ -23,64 +30,80 @@ func RunObjectStorageSuite(c *C, os core.ObjectStorage) {
2330
tag := &core.MemoryObject{}
2431
tag.SetType(core.TagObject)
2532

26-
testObjects := map[core.ObjectType]TestObject{
27-
core.CommitObject: TestObject{commit, "dcf5b16e76cce7425d0beaef62d79a7d10fce1f5", core.CommitObject},
28-
core.TreeObject: TestObject{tree, "4b825dc642cb6eb9a060e54bf8d69288fbee4904", core.TreeObject},
29-
core.BlobObject: TestObject{blob, "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391", core.BlobObject},
30-
core.TagObject: TestObject{tag, "d994c6bb648123a17e8f70a966857c546b2a6f94", core.TagObject},
31-
}
32-
33-
validTypes := []core.ObjectType{core.CommitObject, core.BlobObject, core.TagObject, core.TreeObject}
33+
return BaseStorageSuite{
34+
ObjectStorage: s,
35+
validTypes: []core.ObjectType{
36+
core.CommitObject,
37+
core.BlobObject,
38+
core.TagObject,
39+
core.TreeObject,
40+
},
41+
testObjects: map[core.ObjectType]TestObject{
42+
core.CommitObject: {commit, "dcf5b16e76cce7425d0beaef62d79a7d10fce1f5", core.CommitObject},
43+
core.TreeObject: {tree, "4b825dc642cb6eb9a060e54bf8d69288fbee4904", core.TreeObject},
44+
core.BlobObject: {blob, "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391", core.BlobObject},
45+
core.TagObject: {tag, "d994c6bb648123a17e8f70a966857c546b2a6f94", core.TagObject},
46+
}}
47+
}
3448

35-
for _, to := range testObjects {
49+
func (s *BaseStorageSuite) TestObjectStorageSetAndGet(c *C) {
50+
for _, to := range s.testObjects {
3651
comment := Commentf("failed for type %s", to.Type.String())
3752

38-
h, err := os.Set(to.Object)
53+
h, err := s.ObjectStorage.Set(to.Object)
3954
c.Assert(err, IsNil)
4055
c.Assert(h.String(), Equals, to.Hash, comment)
4156

42-
o, err := os.Get(to.Type, h)
57+
o, err := s.ObjectStorage.Get(to.Type, h)
4358
c.Assert(err, IsNil)
4459
c.Assert(o, Equals, to.Object)
4560

46-
o, err = os.Get(core.AnyObject, h)
61+
o, err = s.ObjectStorage.Get(core.AnyObject, h)
4762
c.Assert(err, IsNil)
4863
c.Assert(o, Equals, to.Object)
4964

50-
for _, validType := range validTypes {
51-
if validType == to.Type {
65+
for _, t := range s.validTypes {
66+
if t == to.Type {
5267
continue
5368
}
54-
o, err = os.Get(validType, h)
69+
70+
o, err = s.ObjectStorage.Get(t, h)
5571
c.Assert(o, IsNil)
5672
c.Assert(err, Equals, core.ErrObjectNotFound)
5773
}
5874
}
75+
}
5976

60-
for _, validType := range validTypes {
61-
comment := Commentf("failed for type %s)", validType.String())
62-
i, err := os.Iter(validType)
77+
func (s *BaseStorageSuite) TestObjectStorageIter(c *C) {
78+
for _, o := range s.testObjects {
79+
s.ObjectStorage.Set(o.Object)
80+
}
81+
82+
for _, t := range s.validTypes {
83+
comment := Commentf("failed for type %s)", t.String())
84+
i, err := s.ObjectStorage.Iter(t)
6385
c.Assert(err, IsNil, comment)
6486

6587
o, err := i.Next()
6688
c.Assert(err, IsNil)
67-
c.Assert(o, Equals, testObjects[validType].Object, comment)
89+
c.Assert(o, Equals, s.testObjects[t].Object, comment)
6890

6991
o, err = i.Next()
7092
c.Assert(o, IsNil)
7193
c.Assert(err, Equals, io.EOF, comment)
7294
}
7395

74-
i, err := os.Iter(core.AnyObject)
96+
i, err := s.ObjectStorage.Iter(core.AnyObject)
7597
c.Assert(err, IsNil)
7698

7799
foundObjects := []core.Object{}
78100
i.ForEach(func(o core.Object) error {
79101
foundObjects = append(foundObjects, o)
80102
return nil
81103
})
82-
c.Assert(foundObjects, HasLen, len(testObjects))
83-
for _, to := range testObjects {
104+
105+
c.Assert(foundObjects, HasLen, len(s.testObjects))
106+
for _, to := range s.testObjects {
84107
found := false
85108
for _, o := range foundObjects {
86109
if to.Object == o {

0 commit comments

Comments
 (0)