Skip to content

Commit

Permalink
Fixed sync_map copy of puppy as highlighted in the last pull request
Browse files Browse the repository at this point in the history
  • Loading branch information
James Hejtmanek authored and James Hejtmanek committed Nov 4, 2019
1 parent a673773 commit 6e41419
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
37 changes: 24 additions & 13 deletions 06_puppy/jimbotech/storer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ type storesSuite struct {
mapper mapTest
}

const brown = "brown"
const black = "black"

func TestSuite(t *testing.T) {
var ms Storer = NewMapStore()
var sms Storer = &SyncMapStore{}
Expand All @@ -36,41 +39,49 @@ func (s *storesSuite) SetupTest() {
// TestCreateSuccess add to the store and verify
// by reading that it is in the store
func (s *storesSuite) TestCreateSuccess() {
_, pup := create(s)
pup := create(s)
// Now modify the original and make sure the
// value in the store will not change
pup.Colour = black
// now check by reading the value back and compare
pup2, err2 := s.store.ReadPuppy(pup.ID)
s.Require().NoError(err2)
s.Equal("kelpie", pup2.Breed)
s.Equal("brown", pup2.Colour)
s.Equal(brown, pup2.Colour)
s.Equal("indispensable", pup2.Value)
s.Equal(pup, pup2)
s.True(pup2.Colour == brown)
s.True(pup.Colour == black)
s.NotEqual(pup, pup2)
}

func create(s *storesSuite) (bool, *Puppy) {
pup := Puppy{Breed: "kelpie", Colour: "brown", Value: "indispensable"}
func create(s *storesSuite) *Puppy {
pup := Puppy{Breed: "kelpie", Colour: brown, Value: "indispensable"}
id, err := s.store.CreatePuppy(&pup)
s.Require().NoError(err)
s.Require().NotEqual(pup.ID, uint32(1))
s.Require().Equal(id, pup.ID, "Pup id must be set to actual id")
return true, &pup
return &pup
}

func (s *storesSuite) TestUpdateSuccess() {
_, pup := create(s)
pup2 := Puppy{Breed: "kelpie", Colour: "black", Value: "indispensable"}
pup := create(s)
pup2 := Puppy{Breed: "kelpie", Colour: black, Value: "indispensable"}
err := s.store.UpdatePuppy(pup.ID, &pup2)
s.Require().NoError(err)
pup2.Colour = brown
// now check by reading the updated value back and compare
pup3, err2 := s.store.ReadPuppy(pup.ID)
if s.Nil(err2, "Reading back updated value should work") {
s.Equal(pup2, *pup3)
s.True(pup2.Colour == brown)
s.True(pup3.Colour == black)
s.NotEqual(pup2, *pup3)
}
}

//TestUpdateFailure checks the error returned when updating with an invalid id
func (s *storesSuite) TestUpdateFailure() {
create(s)
pup2 := Puppy{Breed: "kelpie", Colour: "black", Value: "indispensable"}
pup2 := Puppy{Breed: "kelpie", Colour: black, Value: "indispensable"}
err := s.store.UpdatePuppy(1, &pup2)
success := s.NotNil(err, "Update on id 1 should have failed")
if !success {
Expand All @@ -81,7 +92,7 @@ func (s *storesSuite) TestUpdateFailure() {
}

func (s *storesSuite) TestDeleteSuccess() {
_, pup := create(s)
pup := create(s)
err := s.store.DeletePuppy(pup.ID)
s.Require().NoError(err)
_, err = s.store.ReadPuppy(pup.ID)
Expand All @@ -98,11 +109,11 @@ func (s *storesSuite) TestReadFailure() {

func (s *storesSuite) TestMapChanges() {
s.Equal(0, s.mapper.length())
pup := Puppy{Breed: "kelpie", Colour: "brown", Value: "high"}
pup := Puppy{Breed: "kelpie", Colour: brown, Value: "high"}
id, err := s.store.CreatePuppy(&pup)
s.Require().Nil(err, "Create puppy failed")
s.Equal(1, s.mapper.length())
pup2 := Puppy{Breed: "kelpie", Colour: "black", Value: "low"}
pup2 := Puppy{Breed: "kelpie", Colour: black, Value: "low"}
err = s.store.UpdatePuppy(id, &pup2)
s.Require().Nil(err, "Update puppy failed")
s.Equal(1, s.mapper.length())
Expand Down
8 changes: 4 additions & 4 deletions 06_puppy/jimbotech/sync_mapstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func (s *SyncMapStore) length() int {
// but will modify the member ID.
func (s *SyncMapStore) CreatePuppy(p *Puppy) (uint32, error) {
p.ID = uuid.New().ID()
sp := p
s.Store(p.ID, sp)
sp := *p
s.Store(p.ID, &sp)
return p.ID, nil
}

Expand All @@ -50,8 +50,8 @@ func (s *SyncMapStore) UpdatePuppy(id uint32, puppy *Puppy) error {
_, found := s.Load(id)
if found {
puppy.ID = id
sp := puppy
s.Store(id, sp)
sp := *puppy
s.Store(id, &sp)
return nil
}
return fmt.Errorf("no puppy with ID %v found", id)
Expand Down

0 comments on commit 6e41419

Please sign in to comment.