From 30badb8cb21f01d8c08896f4f524eb9db0111e0d Mon Sep 17 00:00:00 2001 From: Patrick Marabeas Date: Mon, 19 Aug 2019 14:34:04 +1000 Subject: [PATCH] Add MapStore which implements the Storer interface Adds the Puppy, Storer and MapStore type definitions. MapStore implements the Storer interface and supplies a NewMapStore factory function. - Uuid increment is handled internally by MapStore Create - Read will return an empty Puppy struct if provided uuid doesn't exist - Update returns a bool whether a matching identifier was modified - Destroy returns a bool whether a matching identifier was modified Lab 06 (#537) --- 06_puppy/patrickmarabeas/mapStore.go | 51 +++++++++++++++++ 06_puppy/patrickmarabeas/store_test.go | 77 ++++++++++++++++++++++++++ 06_puppy/patrickmarabeas/types.go | 20 +++++++ 3 files changed, 148 insertions(+) create mode 100644 06_puppy/patrickmarabeas/mapStore.go create mode 100644 06_puppy/patrickmarabeas/store_test.go create mode 100644 06_puppy/patrickmarabeas/types.go diff --git a/06_puppy/patrickmarabeas/mapStore.go b/06_puppy/patrickmarabeas/mapStore.go new file mode 100644 index 000000000..187bb55d3 --- /dev/null +++ b/06_puppy/patrickmarabeas/mapStore.go @@ -0,0 +1,51 @@ +package main + +// NewMapStore returns a pointer to a new instance of the MapStore struct which implements the Storer interface. +func NewMapStore() Storer { + return &MapStore{ + uuid: 0, + store: map[int]Puppy{}, + } +} + +// Create increments the uuid and adds the provided Puppy struct to the store with this identifier. +func (store *MapStore) Create(puppy Puppy) int { + puppy.ID = store.uuid + store.store[puppy.ID] = puppy + store.uuid++ + + return puppy.ID +} + +// Read returns the puppy matching the provided uuid. +// An empty Puppy struct is returned if the identifier does not exist. +func (store *MapStore) Read(id int) Puppy { + if _, ok := store.store[id]; ok { + return store.store[id] + } + + return Puppy{} +} + +// Update modifies the puppy matching the provided uuid in the store with the provided Puppy struct. +// It returns a bool whether a matching identifier was modified or not. +func (store *MapStore) Update(id int, puppy Puppy) bool { + if _, ok := store.store[id]; !ok { + return false + } + + puppy.ID = id + store.store[id] = puppy + return true +} + +// Destroy removes the puppy matching the provided uuid from the store. +// It returns a bool whether a matching identifier was deleted or not. +func (store *MapStore) Destroy(id int) bool { + if _, ok := store.store[id]; !ok { + return false + } + + delete(store.store, id) + return true +} diff --git a/06_puppy/patrickmarabeas/store_test.go b/06_puppy/patrickmarabeas/store_test.go new file mode 100644 index 000000000..7cca9666f --- /dev/null +++ b/06_puppy/patrickmarabeas/store_test.go @@ -0,0 +1,77 @@ +package main + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/suite" +) + +type StoreSuite struct { + suite.Suite + store Storer +} + +func (suite *StoreSuite) TestCreate() { + a := assert.New(suite.T()) + id := suite.store.Create(Puppy{Breed: "Wolf", Color: "Grey", Value: "450"}) + + a.Equal(id, 0) +} + +func (suite *StoreSuite) TestCreateSecond() { + a := assert.New(suite.T()) + id := suite.store.Create(Puppy{Breed: "Boxer", Color: "Brown", Value: "300"}) + + a.Equal(id, 1) +} + +func (suite *StoreSuite) TestRead() { + a := assert.New(suite.T()) + data := suite.store.Read(0) + + a.Equal(data, Puppy{ID: 0, Breed: "Wolf", Color: "Grey", Value: "450"}) +} + +func (suite *StoreSuite) TestReadNonExistent() { + a := assert.New(suite.T()) + success := suite.store.Read(100) + + a.Equal(success, Puppy{}) +} + +func (suite *StoreSuite) TestUpdate() { + a := assert.New(suite.T()) + success := suite.store.Update(0, Puppy{Breed: "Doberman", Color: "Black", Value: "500"}) + data := suite.store.Read(0) + + a.Equal(success, true) + a.Equal(data, Puppy{ID: 0, Breed: "Doberman", Color: "Black", Value: "500"}) +} + +func (suite *StoreSuite) TestUpdateNonExistent() { + a := assert.New(suite.T()) + success := suite.store.Update(100, Puppy{Breed: "Doberman", Color: "Black", Value: "500"}) + + a.Equal(success, false) +} + +func (suite *StoreSuite) TestDestroy() { + a := assert.New(suite.T()) + success := suite.store.Destroy(1) + data := suite.store.Read(1) + + a.Equal(success, true) + a.Equal(data, Puppy{}) +} + +func (suite *StoreSuite) TestDestroyNonExistent() { + a := assert.New(suite.T()) + success := suite.store.Destroy(100) + + a.Equal(success, false) +} + +func TestStore(t *testing.T) { + suite.Run(t, &StoreSuite{store: NewMapStore()}) +} diff --git a/06_puppy/patrickmarabeas/types.go b/06_puppy/patrickmarabeas/types.go new file mode 100644 index 000000000..f2050d1c1 --- /dev/null +++ b/06_puppy/patrickmarabeas/types.go @@ -0,0 +1,20 @@ +package main + +type Puppy struct { + ID int + Breed string + Color string + Value string +} + +type Storer interface { + Create(puppy Puppy) int + Read(ID int) Puppy + Update(ID int, puppy Puppy) bool + Destroy(ID int) bool +} + +type MapStore struct { + uuid int + store map[int]Puppy +}