Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lab6 - Puppy Storer - alfredxiao #476

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions 06_puppy/alfredxiao/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import (
"fmt"
"io"
"os"
)

var out io.Writer = os.Stdout

func main() {
store := NewMapStore()
id := store.CreatePuppy(Puppy{
Colour: "Red",
})

puppy, _ := store.ReadPuppy(id)
fmt.Fprint(out, puppy.Colour)
}
16 changes: 16 additions & 0 deletions 06_puppy/alfredxiao/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package main

import (
"bytes"
"testing"

"github.com/stretchr/testify/assert"
)

func TestMainOutput(t *testing.T) {
var buf bytes.Buffer
out = &buf
main()

assert.Equal(t, "Red", buf.String())
}
58 changes: 58 additions & 0 deletions 06_puppy/alfredxiao/mapstore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package main

import (
"fmt"

"github.com/google/uuid"
)

type mapStore struct {
alfredxiao marked this conversation as resolved.
Show resolved Hide resolved
data map[string]Puppy
}

func (s *mapStore) CreatePuppy(p Puppy) string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use godoc for all public functions.

id := uuid.New().String()

p.ID = id
s.data[id] = p
return id
}

func (s *mapStore) ReadPuppy(id string) (Puppy, error) {
p, ok := s.data[id]
if !ok {
return Puppy{}, fmt.Errorf("puppy with ID[%s] does not exists", p.ID)
}

return p, nil
}

func (s *mapStore) UpdatePuppy(id string, p Puppy) error {
if id != p.ID {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure why you'd have separate id field?

return fmt.Errorf("bad update request, two IDs (%s, %s) do not match",
id, p.ID)
}

_, ok := s.data[id]
if !ok {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if _, ok := s.data[id]; !ok {

same below.

return fmt.Errorf("puppy with ID[%s] does not exists", p.ID)
}

s.data[id] = p
return nil
}

func (s *mapStore) DeletePuppy(id string) error {
p, ok := s.data[id]
if !ok {
return fmt.Errorf("puppy with ID[%s] does not exists", p.ID)
}
delete(s.data, id)
return nil
}

func NewMapStore() Storer {
return &mapStore{
data: make(map[string]Puppy),
}
}
82 changes: 82 additions & 0 deletions 06_puppy/alfredxiao/store_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package main

import (
"testing"

"github.com/stretchr/testify/suite"
)

type storerSuite struct {
suite.Suite
store Storer
storeFactory func() Storer
}

func (s *storerSuite) SetupTest() {
s.store = s.storeFactory()
}

func (s *storerSuite) TestCreatePuppyHappyCase() {
puppy := Puppy{Colour: "Black"}
id := s.store.CreatePuppy(puppy)
puppyRead, err := s.store.ReadPuppy(id)
s.Require().NoError(err)

puppy.ID = id
s.Equal(puppy, puppyRead)
}

func (s *storerSuite) TestReadPuppyHappyCase() {
id := s.store.CreatePuppy(Puppy{Colour: "Blue"})
p, err := s.store.ReadPuppy(id)
s.Require().NoError(err)
s.Equal("Blue", p.Colour)
}

func (s *storerSuite) TestReadPuppyNonExisting() {
_, err := s.store.ReadPuppy("id_that_does_not_exist")
s.Error(err)
}

func (s *storerSuite) TestUpdatePuppyHappyCase() {
id := s.store.CreatePuppy(Puppy{Colour: "Brown"})
err := s.store.UpdatePuppy(id, Puppy{ID: id, Colour: "Green"})
s.Require().NoError(err)
p, err := s.store.ReadPuppy(id)
s.Require().NoError(err)
s.Equal("Green", p.Colour)
}

func (s *storerSuite) TestUpdatePuppyMismatchIDs() {
err := s.store.UpdatePuppy("id1", Puppy{ID: "id2"})
s.Error(err)
}

func (s *storerSuite) TestUpdatePuppyNonExisting() {
id := "id_that_does_not_exist_either"
err := s.store.UpdatePuppy(id, Puppy{ID: id})
s.Error(err)
}

func (s *storerSuite) TestDeletePuppyHappyCase() {
id := s.store.CreatePuppy(Puppy{Colour: "Brown"})
err := s.store.DeletePuppy(id)
s.Require().NoError(err)

_, err = s.store.ReadPuppy(id)
s.Error(err, "Puppy should be gone after deletion")
}

func (s *storerSuite) TestDeletePuppyNonExisting() {
err := s.store.DeletePuppy("id_that_does_not_exist_again")
s.Require().Error(err)
}

func TestStorers(t *testing.T) {
suite.Run(t, &storerSuite{
storeFactory: NewMapStore,
})
suite.Run(t, &storerSuite{
storeFactory: NewSyncStore,
})
}
57 changes: 57 additions & 0 deletions 06_puppy/alfredxiao/syncstore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package main

import (
"fmt"
"sync"

"github.com/google/uuid"
)

type syncStore struct {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting choice to make this private.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't make it private :)

data sync.Map
}

func (s *syncStore) CreatePuppy(p Puppy) string {
id := uuid.New().String()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


p.ID = id
s.data.Store(id, p)
return id
}

func (s *syncStore) ReadPuppy(id string) (Puppy, error) {
p, ok := s.data.Load(id)
if !ok {
return Puppy{}, fmt.Errorf("puppy with ID[%s] does not exists", id)
}

return p.(Puppy), nil
}

func (s *syncStore) UpdatePuppy(id string, p Puppy) error {
if id != p.ID {
return fmt.Errorf("bad update request, two IDs (%s, %s) do not match",
id, p.ID)
}

_, ok := s.data.Load(id)
if !ok {
return fmt.Errorf("puppy with ID[%s] does not exists", p.ID)
}

s.data.Store(id, p)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

load and store need to be guarded with Mutex or RWMutex to avoid race conditions.
dame for delete.

return nil
}

func (s *syncStore) DeletePuppy(id string) error {
_, ok := s.data.Load(id)
if !ok {
return fmt.Errorf("puppy with ID[%s] does not exists", id)
}
s.data.Delete(id)
return nil
}

func NewSyncStore() Storer {
return &syncStore{}
}
15 changes: 15 additions & 0 deletions 06_puppy/alfredxiao/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

type Puppy struct {
ID string
Breed string
Colour string
Value string
}

type Storer interface {
CreatePuppy(p Puppy) string
ReadPuppy(ID string) (Puppy, error)
UpdatePuppy(ID string, p Puppy) error
DeletePuppy(ID string) error
}