From f3eeb39cbfb3e5b1c6f240761c45b86b0fe6134a Mon Sep 17 00:00:00 2001 From: Aiden X Date: Tue, 20 Oct 2020 13:09:31 +0800 Subject: [PATCH] test: add storages unittest --- internal/storages/storages.go | 4 ++-- internal/storages/storages_test.go | 36 ++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 internal/storages/storages_test.go diff --git a/internal/storages/storages.go b/internal/storages/storages.go index 92633c5..9e66b9c 100644 --- a/internal/storages/storages.go +++ b/internal/storages/storages.go @@ -32,10 +32,10 @@ func Initialize(repoRoot string) error { } func Get(name string) (storage.Storage, error) { - strg, ok := s.storages[name] + storage, ok := s.storages[name] if !ok { return nil, fmt.Errorf("wrong storage name") } - return strg, nil + return storage, nil } diff --git a/internal/storages/storages_test.go b/internal/storages/storages_test.go new file mode 100644 index 0000000..0003a94 --- /dev/null +++ b/internal/storages/storages_test.go @@ -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) +}