test: add storages unittest

This commit is contained in:
Aiden X 2020-10-20 13:09:31 +08:00
parent 1f80275fc7
commit f3eeb39cbf
2 changed files with 38 additions and 2 deletions

View File

@ -32,10 +32,10 @@ func Initialize(repoRoot string) error {
} }
func Get(name string) (storage.Storage, error) { func Get(name string) (storage.Storage, error) {
strg, ok := s.storages[name] storage, ok := s.storages[name]
if !ok { if !ok {
return nil, fmt.Errorf("wrong storage name") return nil, fmt.Errorf("wrong storage name")
} }
return strg, nil return storage, nil
} }

View File

@ -0,0 +1,36 @@
package storages
import (
"io/ioutil"
"testing"
"github.com/stretchr/testify/require"
)
func TestInitialize(t *testing.T) {
dir, err := ioutil.TempDir("", "TestInitialize")
require.Nil(t, err)
err = Initialize(dir)
require.Nil(t, err)
// Initialize twice
err = Initialize(dir)
require.Contains(t, err.Error(), "create blockchain storage: resource temporarily unavailable")
}
func TestGet(t *testing.T) {
dir, err := ioutil.TempDir("", "TestGet")
require.Nil(t, err)
err = Initialize(dir)
require.Nil(t, err)
s, err := Get(BlockChain)
require.Nil(t, err)
require.NotNil(t, s)
s, err = Get("WrongName")
require.NotNil(t, err)
require.Nil(t, s)
}