Skip to content

Commit

Permalink
Merge wrong rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
runnerdave committed Jul 17, 2019
2 parents 4eca254 + 9e94955 commit d6e4e0f
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 80 deletions.
6 changes: 3 additions & 3 deletions 07_errors/runnerdave/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ func Errorf(code uint16, format string, args ...interface{}) *Error {
return &Error{code, fmt.Sprintf(format, args...)}
}

func validateValue(value float32) (bool, error) {
func validateValue(value float32) error {
if value < 0 {
return false, Errorf(ErrInvalidValue, "Puppy has invalid value (%f)", value)
return Errorf(ErrInvalidValue, "puppy has invalid value (%f)", value)
}
return true, nil
return nil
}
4 changes: 2 additions & 2 deletions 07_errors/runnerdave/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ func main() {
fmt.Fprintf(out, "Puppy created in sync of Breed:%s, value updated to:%f, error at creation:%v, error in update:%v\n",
puppySync.Breed, puppySync.Value, syncCreateErr, syncUpdateErr)

puppyMap, error := mapStore.ReadPuppy(12)
fmt.Fprint(out, error)
puppyMap, err := mapStore.ReadPuppy(12)
fmt.Fprint(out, err)
}
2 changes: 1 addition & 1 deletion 07_errors/runnerdave/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestMainOutput(t *testing.T) {

expected := strconv.Quote(`Puppy created in map of Breed:Chihuahua, errors at creation:<nil>
Puppy created in sync of Breed:Chihuahua, value updated to:10.300000, error at creation:<nil>, error in update:<nil>
2: Puppy with ID:12 not found`)
2: puppy with ID:12 not found`)
actual := strconv.Quote(buf.String())
t.Logf("expected:%s", expected)
t.Logf("actual:%s", actual)
Expand Down
26 changes: 12 additions & 14 deletions 07_errors/runnerdave/map_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ type MapStore struct {

// NewMapStore creates a new in-memory store with map intialised
func NewMapStore() *MapStore {
var m MapStore
m.puppies = make(map[uint16]Puppy)
return &m
return &MapStore{puppies: map[uint16]Puppy{}}
}

// CreatePuppy saves new puppy if not in store, if it is already returns error
func (m *MapStore) CreatePuppy(p *Puppy) error {
if isValid, invalidValueError := validateValue(p.Value); !isValid {
return invalidValueError
if err := validateValue(p.Value); err != nil {
return err
}
if _, ok := m.puppies[p.ID]; ok {
return Errorf(ErrUnknown, "puppy with id %d already exists", p.ID)
Expand All @@ -25,30 +23,30 @@ 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
func (m *MapStore) UpdatePuppy(id uint16, p *Puppy) error {
if isValid, invalidValueError := validateValue(p.Value); !isValid {
return invalidValueError
if err := validateValue(p.Value); err != nil {
return err
}
if _, ok := m.puppies[id]; !ok {
return Errorf(ErrIDNotFound, "Puppy with ID:%d not found", id)
return Errorf(ErrIDNotFound, "puppy with ID:%d not found", id)
}
m.puppies[id] = *p
return nil
}

// DeletePuppy deletes a puppy by id from the store
func (m *MapStore) DeletePuppy(id uint16) (bool, error) {
func (m *MapStore) DeletePuppy(id uint16) error {
if _, ok := m.puppies[id]; ok {
delete(m.puppies, id)
return true, nil
return nil
}
return false, Errorf(ErrIDNotFound, "Puppy with ID:%d not found", id)
return Errorf(ErrIDNotFound, "puppy with ID:%d not found", id)
}
93 changes: 46 additions & 47 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,8 +20,6 @@ var (
}
)

type storerImpl int

type storerSuite struct {
suite.Suite
store Storer
Expand All @@ -40,26 +35,26 @@ func (s *storerSuite) TestUpdatePuppyIDDoesNotExist() {
err := s.store.UpdatePuppy(13, &testPuppy)

// then
assert.Error(err, "Should error if id not found")
assert.Error(err, "Should produce an error if id not found")
serr, ok := err.(*Error)
if ok {
assert.Equal(uint16(0x2), serr.Code)
}
assert.True(ok)
assert.Equal(uint16(0x2), serr.Code)
}

func (s *storerSuite) TestUpdatePuppy() {
// given
assert := tassert.New(s.T())
testPuppy := puppy1()
updatePuppy := puppy2()
createError := s.store.CreatePuppy(&testPuppy)
s.T().Log(createError)
targetPuppy := puppy2()
cerr := s.store.CreatePuppy(&testPuppy)
r := require.New(s.T())
r.NoError(cerr, "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)
r.NoError(err, "Should be able to read updated puppy")
assert.Equal(targetPuppy, updatedPuppy, "Updated puppy should be equal to puppy2")
Expand All @@ -69,8 +64,9 @@ func (s *storerSuite) TestReadPuppy() {
// given
assert := tassert.New(s.T())
testPuppy := puppy1()
createError := s.store.CreatePuppy(&testPuppy)
s.T().Log(createError)
cerr := s.store.CreatePuppy(&testPuppy)
r := require.New(s.T())
r.NoError(cerr, "Create should not produce an error")

// when
newPuppy, err := s.store.ReadPuppy(11)
Expand All @@ -83,60 +79,59 @@ func (s *storerSuite) TestReadPuppy() {
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 produce an error when puppy is not found")
serr, ok := err.(*Error)
assert.True(ok)
assert.Equal(uint16(0x2), serr.Code)
}

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

// when
isDeleted, err := s.store.DeletePuppy(11)
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")
_, rerr := s.store.ReadPuppy(11)
serr, ok := rerr.(*Error)
assert.True(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)
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")
serr, ok := err.(*Error)
assert.True(ok)
assert.Equal(uint16(0x2), serr.Code)
}

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

// when
err := s.store.CreatePuppy(&testPuppy)
Expand All @@ -149,16 +144,16 @@ 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)
}
assert.True(ok)
assert.Equal(uint16(0x1), serr.Code)
}

func (s *storerSuite) TestUpdatePuppyWithInvalidValue() {
Expand All @@ -167,13 +162,17 @@ 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)
assert.True(ok)
assert.Equal(uint16(0x1), serr.Code)
}

func (s *storerSuite) SetupTest() {
Expand Down
22 changes: 11 additions & 11 deletions 07_errors/runnerdave/sync_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ func NewSyncStore() *SyncStore {

// CreatePuppy saves new puppy if not in store, if it is already returns error
func (s *SyncStore) CreatePuppy(p *Puppy) error {
if isValid, invalidValueError := validateValue(p.Value); !isValid {
return invalidValueError
if err := validateValue(p.Value); err != nil {
return err
}
s.Lock()
defer s.Unlock()
Expand All @@ -30,33 +30,33 @@ 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
func (s *SyncStore) UpdatePuppy(id uint16, puppy *Puppy) error {
if isValid, invalidValueError := validateValue(puppy.Value); !isValid {
return invalidValueError
if err := validateValue(puppy.Value); err != nil {
return err
}
if _, ok := s.Load(id); !ok {
return Errorf(ErrIDNotFound, "Puppy with ID:%d not found", id)
return Errorf(ErrIDNotFound, "puppy with ID:%d not found", id)
}
s.Store(id, *puppy)
return nil
}

// DeletePuppy deletes a puppy from the store
func (s *SyncStore) DeletePuppy(id uint16) (bool, error) {
func (s *SyncStore) DeletePuppy(id uint16) error {
s.Lock()
defer s.Unlock()
if _, ok := s.Load(id); !ok {
return false, Errorf(ErrIDNotFound, "Puppy with ID:%d not found", id)
return Errorf(ErrIDNotFound, "puppy with ID:%d not found", id)
}
s.Delete(id)
return true, nil
return nil
}
4 changes: 2 additions & 2 deletions 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)
DeletePuppy(ID uint16) error
}

0 comments on commit d6e4e0f

Please sign in to comment.