diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2016-09-24 13:39:08 +0200 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2016-09-24 13:39:08 +0200 |
commit | b53b9073688df6cfc1698eef163ef4cc50a15758 (patch) | |
tree | 0df79a41b1c682f25c8f022db1f06fd8ffb5b0ae /storage | |
parent | 22068946ce5b83163a6c57db3ec2b7294ec549d1 (diff) | |
download | go-git-b53b9073688df6cfc1698eef163ef4cc50a15758.tar.gz |
storage: memory test improvements and coverage
Diffstat (limited to 'storage')
-rw-r--r-- | storage/memory/storage.go | 5 | ||||
-rw-r--r-- | storage/memory/storage_test.go | 43 | ||||
-rw-r--r-- | storage/test/storage_suite.go | 124 |
3 files changed, 135 insertions, 37 deletions
diff --git a/storage/memory/storage.go b/storage/memory/storage.go index 4fb6186..97e87c8 100644 --- a/storage/memory/storage.go +++ b/storage/memory/storage.go @@ -86,6 +86,10 @@ func (c *ConfigStorage) Remotes() ([]*config.RemoteConfig, error) { return o, nil } func (c *ConfigStorage) SetRemote(r *config.RemoteConfig) error { + if err := r.Validate(); err != nil { + return err + } + c.RemotesConfig[r.Name] = r return nil } @@ -124,7 +128,6 @@ func (o *ObjectStorage) Set(obj core.Object) (core.Hash, error) { case core.TagObject: o.Tags[h] = o.Objects[h] default: - fmt.Println(obj.Type()) return h, ErrUnsupportedObjectType } diff --git a/storage/memory/storage_test.go b/storage/memory/storage_test.go index 2c90738..e1d5a33 100644 --- a/storage/memory/storage_test.go +++ b/storage/memory/storage_test.go @@ -1,11 +1,9 @@ package memory import ( - "io" "testing" . "gopkg.in/check.v1" - "gopkg.in/src-d/go-git.v4/core" "gopkg.in/src-d/go-git.v4/storage/test" ) @@ -18,7 +16,12 @@ type StorageSuite struct { var _ = Suite(&StorageSuite{}) func (s *StorageSuite) SetUpTest(c *C) { - s.BaseStorageSuite = test.NewBaseStorageSuite(NewStorage().ObjectStorage()) + storage := NewStorage() + s.BaseStorageSuite = test.NewBaseStorageSuite( + storage.ObjectStorage(), + storage.ReferenceStorage(), + storage.ConfigStorage(), + ) } func (s *StorageSuite) TestStorageObjectStorage(c *C) { @@ -37,36 +40,10 @@ func (s *StorageSuite) TestStorageReferenceStorage(c *C) { c.Assert(o == e, Equals, true) } -func (s *StorageSuite) TestReferenceStorageSetAndGet(c *C) { +func (s *StorageSuite) TestStorageConfigStorage(c *C) { storage := NewStorage() - rs := storage.ReferenceStorage() - - err := rs.Set(core.NewReferenceFromStrings("foo", "bc9968d75e48de59f0870ffb71f5e160bbbdcf52")) - c.Assert(err, IsNil) - - err = rs.Set(core.NewReferenceFromStrings("bar", "482e0eada5de4039e6f216b45b3c9b683b83bfa")) - c.Assert(err, IsNil) + o := storage.ConfigStorage() + e := storage.ConfigStorage() - e, err := rs.Get(core.ReferenceName("foo")) - c.Assert(err, IsNil) - c.Assert(e.Hash().String(), Equals, "bc9968d75e48de59f0870ffb71f5e160bbbdcf52") -} - -func (s *StorageSuite) TestReferenceStorageIter(c *C) { - storage := NewStorage() - rs := storage.ReferenceStorage() - - err := rs.Set(core.NewReferenceFromStrings("foo", "bc9968d75e48de59f0870ffb71f5e160bbbdcf52")) - c.Assert(err, IsNil) - - i, err := rs.Iter() - c.Assert(err, IsNil) - - e, err := i.Next() - c.Assert(err, IsNil) - c.Assert(e.Hash().String(), Equals, "bc9968d75e48de59f0870ffb71f5e160bbbdcf52") - - e, err = i.Next() - c.Assert(e, IsNil) - c.Assert(err, Equals, io.EOF) + c.Assert(o == e, Equals, true) } diff --git a/storage/test/storage_suite.go b/storage/test/storage_suite.go index b4ba306..fd5b3c7 100644 --- a/storage/test/storage_suite.go +++ b/storage/test/storage_suite.go @@ -4,6 +4,7 @@ import ( "io" . "gopkg.in/check.v1" + "gopkg.in/src-d/go-git.v4/config" "gopkg.in/src-d/go-git.v4/core" ) @@ -14,13 +15,19 @@ type TestObject struct { } type BaseStorageSuite struct { - ObjectStorage core.ObjectStorage + ObjectStorage core.ObjectStorage + ReferenceStorage core.ReferenceStorage + ConfigStore config.ConfigStorage validTypes []core.ObjectType testObjects map[core.ObjectType]TestObject } -func NewBaseStorageSuite(s core.ObjectStorage) BaseStorageSuite { +func NewBaseStorageSuite( + os core.ObjectStorage, + rs core.ReferenceStorage, + cs config.ConfigStorage, +) BaseStorageSuite { commit := &core.MemoryObject{} commit.SetType(core.CommitObject) tree := &core.MemoryObject{} @@ -31,7 +38,10 @@ func NewBaseStorageSuite(s core.ObjectStorage) BaseStorageSuite { tag.SetType(core.TagObject) return BaseStorageSuite{ - ObjectStorage: s, + ObjectStorage: os, + ReferenceStorage: rs, + ConfigStore: cs, + validTypes: []core.ObjectType{ core.CommitObject, core.BlobObject, @@ -74,6 +84,14 @@ func (s *BaseStorageSuite) TestObjectStorageSetAndGet(c *C) { } } +func (s *BaseStorageSuite) TestObjectStorageGetIvalid(c *C) { + o := s.ObjectStorage.NewObject() + o.SetType(core.REFDeltaObject) + + _, err := s.ObjectStorage.Set(o) + c.Assert(err, NotNil) +} + func (s *BaseStorageSuite) TestObjectStorageIter(c *C) { for _, o := range s.testObjects { s.ObjectStorage.Set(o.Object) @@ -143,6 +161,25 @@ func (s *BaseStorageSuite) TestTxObjectStorageSetAndCommit(c *C) { c.Assert(count, Equals, 4) } +func (s *BaseStorageSuite) TestTxObjectStorageSetAndGet(c *C) { + tx := s.ObjectStorage.Begin() + for _, expected := range s.testObjects { + h, err := tx.Set(expected.Object) + c.Assert(err, IsNil) + c.Assert(h.String(), Equals, expected.Hash) + + o, err := tx.Get(expected.Type, core.NewHash(expected.Hash)) + c.Assert(o.Hash().String(), DeepEquals, expected.Hash) + } +} + +func (s *BaseStorageSuite) TestTxObjectStorageGetNotFound(c *C) { + tx := s.ObjectStorage.Begin() + o, err := tx.Get(core.AnyObject, core.ZeroHash) + c.Assert(o, IsNil) + c.Assert(err, Equals, core.ErrObjectNotFound) +} + func (s *BaseStorageSuite) TestTxObjectStorageSetAndRollback(c *C) { tx := s.ObjectStorage.Begin() for _, o := range s.testObjects { @@ -159,3 +196,84 @@ func (s *BaseStorageSuite) TestTxObjectStorageSetAndRollback(c *C) { _, err = iter.Next() c.Assert(err, Equals, io.EOF) } + +func (s *BaseStorageSuite) TestReferenceStorageSetAndGet(c *C) { + err := s.ReferenceStorage.Set( + core.NewReferenceFromStrings("foo", "bc9968d75e48de59f0870ffb71f5e160bbbdcf52"), + ) + c.Assert(err, IsNil) + + err = s.ReferenceStorage.Set( + core.NewReferenceFromStrings("bar", "482e0eada5de4039e6f216b45b3c9b683b83bfa"), + ) + c.Assert(err, IsNil) + + e, err := s.ReferenceStorage.Get(core.ReferenceName("foo")) + c.Assert(err, IsNil) + c.Assert(e.Hash().String(), Equals, "bc9968d75e48de59f0870ffb71f5e160bbbdcf52") +} + +func (s *BaseStorageSuite) TestReferenceStorageGetNotFound(c *C) { + r, err := s.ReferenceStorage.Get(core.ReferenceName("bar")) + c.Assert(err, Equals, core.ErrReferenceNotFound) + c.Assert(r, IsNil) +} + +func (s *BaseStorageSuite) TestReferenceStorageIter(c *C) { + err := s.ReferenceStorage.Set( + core.NewReferenceFromStrings("foo", "bc9968d75e48de59f0870ffb71f5e160bbbdcf52"), + ) + c.Assert(err, IsNil) + + i, err := s.ReferenceStorage.Iter() + c.Assert(err, IsNil) + + e, err := i.Next() + c.Assert(err, IsNil) + c.Assert(e.Hash().String(), Equals, "bc9968d75e48de59f0870ffb71f5e160bbbdcf52") + + e, err = i.Next() + c.Assert(e, IsNil) + c.Assert(err, Equals, io.EOF) +} + +func (s *BaseStorageSuite) TestConfigStorageSetGetAndDelete(c *C) { + err := s.ConfigStore.SetRemote(&config.RemoteConfig{ + Name: "foo", + URL: "http://foo/bar.git", + }) + + c.Assert(err, IsNil) + + r, err := s.ConfigStore.Remote("foo") + c.Assert(err, IsNil) + c.Assert(r.Name, Equals, "foo") + + err = s.ConfigStore.DeleteRemote("foo") + c.Assert(err, IsNil) + + r, err = s.ConfigStore.Remote("foo") + c.Assert(err, Equals, config.ErrRemoteConfigNotFound) + c.Assert(r, IsNil) +} + +func (s *BaseStorageSuite) TestConfigStorageSetInvalid(c *C) { + err := s.ConfigStore.SetRemote(&config.RemoteConfig{}) + c.Assert(err, NotNil) +} + +func (s *BaseStorageSuite) TestConfigStorageRemotes(c *C) { + s.ConfigStore.SetRemote(&config.RemoteConfig{ + Name: "foo", URL: "http://foo/bar.git", + }) + + s.ConfigStore.SetRemote(&config.RemoteConfig{ + Name: "bar", URL: "http://foo/bar.git", + }) + + r, err := s.ConfigStore.Remotes() + c.Assert(err, IsNil) + c.Assert(r, HasLen, 2) + c.Assert(r[0].Name, Equals, "foo") + c.Assert(r[1].Name, Equals, "bar") +} |