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 11 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
20 changes: 20 additions & 0 deletions 06_puppy/alfredxiao/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

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

var out io.Writer = os.Stdout

func main() {
store1 := NewSyncStore()
alfredxiao marked this conversation as resolved.
Show resolved Hide resolved
_ = store1.CreatePuppy(Puppy{
ID: "1",
Colour: "Red",
})

puppy, _ := store1.ReadPuppy("1")
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())
}
52 changes: 52 additions & 0 deletions 06_puppy/alfredxiao/mapstore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package main

import (
"fmt"
)

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

func (s *mapStore) CreatePuppy(p Puppy) error {
_, ok := s.data[p.ID]
alfredxiao marked this conversation as resolved.
Show resolved Hide resolved
if ok {
return fmt.Errorf("puppy with ID[%s] already exists", p.ID)
}

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

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(p Puppy) error {
alfredxiao marked this conversation as resolved.
Show resolved Hide resolved
_, ok := s.data[p.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[p.ID] = p
return nil
}

func (s *mapStore) DeletePuppy(id string) (bool, error) {
p, ok := s.data[id]
if !ok {
return false, fmt.Errorf("puppy with ID[%s] does not exists", p.ID)
}
delete(s.data, id)
return true, nil
alfredxiao marked this conversation as resolved.
Show resolved Hide resolved
}

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/require"
"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() {
err := s.store.CreatePuppy(Puppy{ID: "1", Colour: "Black"})
s.NoError(err, "Happy case puppy creation")

p, _ := s.store.ReadPuppy("1")
alfredxiao marked this conversation as resolved.
Show resolved Hide resolved
s.Equal("Black", p.Colour)
}

func (s *storerSuite) TestCreatePuppyAlreadyExists() {
_ = s.store.CreatePuppy(Puppy{ID: "2"})
err := s.store.CreatePuppy(Puppy{ID: "2", Colour: "Red"})
s.Error(err, "Puppy creation fails if ID already exists")
}

func (s *storerSuite) TestReadPuppyHappyCase() {
_ = s.store.CreatePuppy(Puppy{ID: "3", Colour: "Blue"})
p, err := s.store.ReadPuppy("3")
require.NoError(s.T(), err)
s.Equal("Blue", p.Colour)
}

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

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

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

func (s *storerSuite) TestDeletePuppyHappyCase() {
_ = s.store.CreatePuppy(Puppy{ID: "7", Colour: "Brown"})
deleted, err := s.store.DeletePuppy("7")
require.NoError(s.T(), err)
require.Equal(s.T(), true, deleted)

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

func (s *storerSuite) TestDeletePuppyNonExisting() {
deleted, err := s.store.DeletePuppy("8")
require.Error(s.T(), err)
s.Equal(false, deleted)
}

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

import (
"fmt"
"sync"
)

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) error {
_, ok := s.data.Load(p.ID)
if ok {
return fmt.Errorf("puppy with ID[%s] already exists", p.ID)
}

s.data.Store(p.ID, p)
return nil
}

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(p Puppy) error {
_, ok := s.data.Load(p.ID)
if !ok {
return fmt.Errorf("puppy with ID[%s] does not exists", p.ID)
}
s.data.Store(p.ID, p)
return nil
}

func (s *syncStore) DeletePuppy(id string) (bool, error) {
_, ok := s.data.Load(id)
if !ok {
return false, fmt.Errorf("puppy with ID[%s] does not exists", id)
}
s.data.Delete(id)
return true, 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) error
ReadPuppy(ID string) (Puppy, error)
UpdatePuppy(p Puppy) error
DeletePuppy(ID string) (bool, error)
}