Skip to content
This repository has been archived by the owner on Apr 5, 2023. It is now read-only.

Commit

Permalink
Merge pull request #10 from DRK3/PreventDuplicateStore
Browse files Browse the repository at this point in the history
refactor: Prevent creation of duplicate store and return custom error
  • Loading branch information
Derek Trider authored Feb 19, 2020
2 parents 4be75a3 + 708fbe1 commit 6de326a
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 7 deletions.
7 changes: 6 additions & 1 deletion pkg/storage/couchdb/couchdbstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,15 @@ func NewProvider(hostURL string) (*Provider, error) {
// CreateStore creates a new store with the given name.
func (p *Provider) CreateStore(name string) error {
p.mux.Lock()
defer p.mux.Unlock()

err := p.couchDBClient.CreateDB(context.Background(), name)

p.mux.Unlock()

if err != nil && err.Error() == "Precondition Failed: The database could not be created, the file already exists." {
return storage.ErrDuplicateStore
}

return err
}

Expand Down
3 changes: 1 addition & 2 deletions pkg/storage/couchdb/couchdbstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ func TestProvider_CreateStore(t *testing.T) {
require.NoError(t, err)

err = provider.CreateStore(testStoreName)
require.Equal(t, "Precondition Failed: The database could not be created, the file already exists.",
err.Error())
require.Equal(t, storage.ErrDuplicateStore.Error(), err.Error())
})
t.Run("Attempt to create a store with an incompatible name", func(t *testing.T) {
provider := initializeTest(t)
Expand Down
5 changes: 5 additions & 0 deletions pkg/storage/memstore/memstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ func (p *Provider) CreateStore(name string) error {
p.mux.Lock()
defer p.mux.Unlock()

_, exists := p.dbs[name]
if exists {
return storage.ErrDuplicateStore
}

store := MemStore{db: make(map[string][]byte)}

p.dbs[name] = &store
Expand Down
17 changes: 14 additions & 3 deletions pkg/storage/memstore/memstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,21 @@ import (
const testStoreName = "teststore"

func TestProvider_CreateStore(t *testing.T) {
provider := NewProvider()
t.Run("Successfully create a new store", func(t *testing.T) {
provider := NewProvider()

err := provider.CreateStore(testStoreName)
require.NoError(t, err)
err := provider.CreateStore(testStoreName)
require.NoError(t, err)
})
t.Run("Attempt to create a duplicate store", func(t *testing.T) {
provider := NewProvider()

err := provider.CreateStore(testStoreName)
require.NoError(t, err)

err = provider.CreateStore(testStoreName)
require.Equal(t, storage.ErrDuplicateStore, err)
})
}

func TestMemStore_OpenStore(t *testing.T) {
Expand Down
5 changes: 4 additions & 1 deletion pkg/storage/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ package storage

import "errors"

// ErrDuplicateStore is used when an attempt is made to create a duplicate store.
var ErrDuplicateStore = errors.New("store already exists")

// ErrStoreNotFound is used when a given store was not found in a provider.
var ErrStoreNotFound = errors.New("store not found")

// ErrValueNotFound is used when an attempt is made to retrieve a value from key
// ErrValueNotFound is used when an attempt is made to retrieve a value using a key that isn't in the store.
var ErrValueNotFound = errors.New("store does not have a value associated with this key")

// Provider represents a storage provider.
Expand Down

0 comments on commit 6de326a

Please sign in to comment.