Skip to content

Commit

Permalink
Update code with final version of lab06 improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
runnerdave committed Jul 14, 2019
1 parent 15a0210 commit 7a6f16f
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 47 deletions.
6 changes: 3 additions & 3 deletions 07_errors/runnerdave/map_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ func (m *MapStore) CreatePuppy(p *Puppy) error {
}

// ReadPuppy reads store by Puppy ID
func (m *MapStore) ReadPuppy(id uint16) (*Puppy, error) {
func (m *MapStore) ReadPuppy(id uint16) (Puppy, error) {
if puppy, ok := m.puppies[id]; ok {
return &puppy, nil
return puppy, nil
}
return nil, Errorf(ErrIDNotFound, "Puppy with ID:%d not found", id)
return Puppy{}, Errorf(ErrIDNotFound, "Puppy with ID:%d not found", id)
}

// UpdatePuppy updates puppy with new value if ID present otherwise error
Expand Down
91 changes: 51 additions & 40 deletions 07_errors/runnerdave/storer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@ import (
"testing"

tassert "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)

const (
mem storerImpl = iota
)

var (
puppy1 = func() Puppy {
return Puppy{ID: 11, Breed: "Chihuahua", Colour: "Brown", Value: 12.30}
Expand All @@ -23,12 +20,9 @@ var (
}
)

type storerImpl int

type storerSuite struct {
suite.Suite
store Storer
impl storerImpl
}

func (s *storerSuite) TestUpdatePuppyIDDoesNotExist() {
Expand All @@ -51,50 +45,58 @@ func (s *storerSuite) TestUpdatePuppy() {
// given
assert := tassert.New(s.T())
testPuppy := puppy1()
updatePuppy := puppy2()
targetPuppy := puppy2()
createError := s.store.CreatePuppy(&testPuppy)
s.T().Log(createError)
r := require.New(s.T())
r.NoError(createError, "Create should not produce an error")

// when
uerr := s.store.UpdatePuppy(11, &updatePuppy)
uerr := s.store.UpdatePuppy(11, &targetPuppy)

// then
assert.NoError(uerr, "Should be able to update store")
r.NoError(uerr, "Should be able to update store")
updatedPuppy, err := s.store.ReadPuppy(11)
if assert.NoError(err, "Should be able to read updated puppy") {
assert.Equal(&updatePuppy, updatedPuppy, "Updated puppy should be equal to puppy2")
}
r.NoError(err, "Should be able to read updated puppy")
assert.Equal(targetPuppy, updatedPuppy, "Updated puppy should be equal to puppy2")

// cleanup
_, deleteError := s.store.DeletePuppy(11)
s.T().Log(deleteError)
}

func (s *storerSuite) TestReadPuppy() {
// given
assert := tassert.New(s.T())
testPuppy := puppy1()
createError := s.store.CreatePuppy(&testPuppy)
s.T().Log(createError)
r := require.New(s.T())
r.NoError(createError, "Create should not produce an error")

// when
newPuppy, err := s.store.ReadPuppy(11)

// then
if assert.NoError(err, "Should be able to read a newly added puppy") {
assert.Equal(&testPuppy, newPuppy, "Newly added puppy should be equal to test puppy")
}
r.NoError(err, "Should be able to read a newly added puppy")
assert.Equal(testPuppy, newPuppy, "Newly added puppy should be equal to test puppy")

// cleanup
_, deleteError := s.store.DeletePuppy(11)
s.T().Log(deleteError)
}

func (s *storerSuite) TestReadNonExistingPuppy() {
// given
assert := tassert.New(s.T())
r := require.New(s.T())

// when
_, err := s.store.ReadPuppy(12)

// then
if assert.Error(err, "Should error when puppy is not found") {
serr, ok := err.(*Error)
if ok {
assert.Equal(uint16(0x2), serr.Code)
}
r.Error(err, "Should error when puppy is not found")
serr, ok := err.(*Error)
if ok {
assert.Equal(uint16(0x2), serr.Code)
}
}

Expand All @@ -103,33 +105,36 @@ func (s *storerSuite) TestDeletePuppy() {
assert := tassert.New(s.T())
testPuppy := puppy1()
createError := s.store.CreatePuppy(&testPuppy)
s.T().Log(createError)
r := require.New(s.T())
r.NoError(createError, "Create should not produce an error")

// when
isDeleted, err := s.store.DeletePuppy(11)

// then
if assert.NoError(err, "Should be able to delete a newly added puppy") {
assert.True(isDeleted)
_, err := s.store.ReadPuppy(11)
assert.Error(err, "Puppy not found")
r.NoError(err, "Should be able to delete a newly added puppy")
assert.True(isDeleted)
_, rerr := s.store.ReadPuppy(11)
serr, ok := rerr.(*Error)
if ok {
assert.Equal(uint16(0x2), serr.Code)
}
}

func (s *storerSuite) TestDeleteNonExistingPuppy() {
// given
assert := tassert.New(s.T())
r := require.New(s.T())

// when
isDeleted, err := s.store.DeletePuppy(11)

// then
if assert.Error(err, "Should not be able to delete a non existing puppy") {
assert.False(isDeleted)
serr, ok := err.(*Error)
if ok {
assert.Equal(uint16(0x2), serr.Code)
}
r.Error(err, "Should not be able to delete a non existing puppy")
assert.False(isDeleted)
serr, ok := err.(*Error)
if ok {
assert.Equal(uint16(0x2), serr.Code)
}
}

Expand All @@ -138,13 +143,15 @@ func (s *storerSuite) TestCreateExistingPuppy() {
assert := tassert.New(s.T())
testPuppy := puppy1()
createError := s.store.CreatePuppy(&testPuppy)
s.T().Log(createError)
r := require.New(s.T())
r.NoError(createError, "Create should not produce an error")

// when
err := s.store.CreatePuppy(&testPuppy)

// then
assert.Error(err, "Should not be able to create twice a the same puppy")

// cleanup
_, deleteError := s.store.DeletePuppy(11)
s.T().Log(deleteError)
Expand All @@ -154,12 +161,13 @@ func (s *storerSuite) TestCreatePuppyWithInvalidValue() {
// given
assert := tassert.New(s.T())
testPuppy := puppy3()
r := require.New(s.T())

// when
createError := s.store.CreatePuppy(&testPuppy)

// then
assert.Error(createError, "Should not allow to create a puppy with invalid value")
r.Error(createError, "Should not allow to create a puppy with invalid value")
serr, ok := createError.(*Error)
if ok {
assert.Equal(uint16(0x1), serr.Code)
Expand All @@ -172,23 +180,26 @@ func (s *storerSuite) TestUpdatePuppyWithInvalidValue() {
testPuppy := puppy1()
updatePuppy := puppy3()
createError := s.store.CreatePuppy(&testPuppy)
s.T().Log(createError)
r := require.New(s.T())
r.NoError(createError, "Create should not produce an error")

// when
uerr := s.store.UpdatePuppy(11, &updatePuppy)

// then
assert.Error(uerr, "Should not allow to update a puppy with invalid value")
r.Error(uerr, "Should not allow to update a puppy with invalid value")
serr, ok := uerr.(*Error)
if ok {
assert.Equal(uint16(0x1), serr.Code)
}
}

func TestStorer(t *testing.T) {
syncSuite := storerSuite{
store: NewSyncStore(),
impl: mem,
}
mapSuite := storerSuite{
store: NewMapStore(),
impl: mem,
}
suite.Run(t, &syncSuite)
suite.Run(t, &mapSuite)
Expand Down
6 changes: 3 additions & 3 deletions 07_errors/runnerdave/sync_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ func (s *SyncStore) CreatePuppy(p *Puppy) error {
}

// ReadPuppy gets a puppy from the store given an ID
func (s *SyncStore) ReadPuppy(id uint16) (*Puppy, error) {
func (s *SyncStore) ReadPuppy(id uint16) (Puppy, error) {
if puppyData, ok := s.Load(id); ok {
puppy, _ := puppyData.(Puppy)
return &puppy, nil
return puppy, nil
}
return nil, Errorf(ErrIDNotFound, "Puppy with ID:%d not found", id)
return Puppy{}, Errorf(ErrIDNotFound, "Puppy with ID:%d not found", id)
}

// UpdatePuppy puts new puppy data to the store, error if id does not exist
Expand Down
2 changes: 1 addition & 1 deletion 07_errors/runnerdave/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type Puppy struct {
// Storer CRUD methods for the Puppy store
type Storer interface {
CreatePuppy(*Puppy) error
ReadPuppy(ID uint16) (*Puppy, error)
ReadPuppy(ID uint16) (Puppy, error)
UpdatePuppy(ID uint16, p *Puppy) error
DeletePuppy(ID uint16) (bool, error)
}

0 comments on commit 7a6f16f

Please sign in to comment.