Skip to content

Commit

Permalink
Add MapStore which implements the Storer interface
Browse files Browse the repository at this point in the history
Adds the Puppy, Storer and MapStore type definitions. MapStore implements
the Storer interface and supplies a NewMapStore factory function.

- Uuid increment is handled internally by MapStore Create
- Read will return an empty Puppy struct if provided uuid doesn't exist
- Update will return the given Puppy struct if the uuid doesn't exist
- Destroy returns a bool dependent on uuid existing

Lab 06 (anz-bank#537)
  • Loading branch information
patrickmarabeas committed Jul 4, 2019
1 parent bb14bcc commit 7ad6ae8
Show file tree
Hide file tree
Showing 5 changed files with 225 additions and 0 deletions.
31 changes: 31 additions & 0 deletions 06_puppy/patrickmarabeas/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

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

var out io.Writer = os.Stdout

func main() {
store := NewMapStore()

createID := store.Create(Puppy{Breed: "Wolf", Color: "Grey", Value: "450"})
createID2 := store.Create(Puppy{Breed: "Boxer", Color: "Brown", Value: "300"})
fmt.Fprintln(out, createID)
fmt.Fprintln(out, createID2)

readPuppy := store.Read(1)
fmt.Fprintln(out, readPuppy)

updatePuppy := store.Update(1, Puppy{Breed: "Doberman", Color: "Black", Value: "500"})
fmt.Fprintln(out, updatePuppy)

deleteBool := store.Destroy(1)
fmt.Fprintln(out, deleteBool)

createID3 := store.Create(Puppy{Breed: "Greyhound", Color: "Light Brown", Value: "700"})
fmt.Fprintln(out, createID3)

}
28 changes: 28 additions & 0 deletions 06_puppy/patrickmarabeas/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
"bytes"
"testing"
)

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

main()

expected := `1
2
{1 Wolf Grey 450}
{1 Doberman Black 500}
true
3
`
got := buf.String()

t.Run("Main function", func(t *testing.T) {
if expected != got {
t.Errorf("\nExpected: %s\nGot: %s", expected, got)
}
})
}
50 changes: 50 additions & 0 deletions 06_puppy/patrickmarabeas/mapStore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package main

// NewSyncStore returns a pointer to a new instance of the MapStore struct which implements the Storer interface.
func NewMapStore() Storer {
return &MapStore{
uuid: 0,
store: map[int]Puppy{},
}
}

// Create increments the uuid and adds the provided Puppy struct to the store with this identifier.
func (store *MapStore) Create(puppy Puppy) int {
store.uuid++
puppy.ID = store.uuid
store.store[store.uuid] = puppy

return store.uuid
}

// Read returns the puppy matching the provided uuid.
// An empty Puppy struct is returned if the identifier does not exist.
func (store *MapStore) Read(id int) Puppy {
if _, ok := store.store[id]; ok {
return store.store[id]
}

return Puppy{}
}

// Update modifies the puppy matching the provided uuid in the store with the provided Puppy struct.
// No action is performed if the identifier does not exist.
func (store *MapStore) Update(id int, puppy Puppy) Puppy {
if _, ok := store.store[id]; ok {
puppy.ID = id
store.store[id] = puppy
}

return puppy
}

// Destroy removes the puppy matching the provided uuid from the store.
// It returns a bool whether a matching identifier was deleted or not.
func (store *MapStore) Destroy(id int) bool {
if _, ok := store.store[id]; ok {
delete(store.store, id)
return true
}

return false
}
96 changes: 96 additions & 0 deletions 06_puppy/patrickmarabeas/mapStore_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package main

import (
"reflect"
"testing"
)

func TestMapStore(t *testing.T) {

t.Run("NewMapStore", func(t *testing.T) {
got := NewMapStore()
expected := &MapStore{
uuid: 0,
store: map[int]Puppy{},
}

if !reflect.DeepEqual(expected, got) {
t.Errorf("\nExpected: %#v\nGot: %#v", expected, got)
}
})

t.Run("Create", func(t *testing.T) {
// Setup
store := NewMapStore()
// Create a puppy
id := store.Create(Puppy{Breed: "Wolf", Color: "Grey", Value: "450"})
expectedID := 1

expectedStore := &MapStore{
uuid: 1,
store: map[int]Puppy{
1: {ID: 1, Breed: "Wolf", Color: "Grey", Value: "450"},
},
}

if !reflect.DeepEqual(id, expectedID) {
t.Errorf("\nExpected: %#v\nGot: %#v", expectedID, id)
}

if !reflect.DeepEqual(store, expectedStore) {
t.Errorf("\nExpected: %#v\nGot: %#v", expectedStore, store)
}
})

t.Run("Read", func(t *testing.T) {
// Setup
store := NewMapStore()
// Create a puppy
store.Create(Puppy{Breed: "Wolf", Color: "Grey", Value: "450"})
got := store.Read(1)
gotFailed := store.Read(100)

expected := Puppy{ID: 1, Breed: "Wolf", Color: "Grey", Value: "450"}
expectedFailed := Puppy{}

if !reflect.DeepEqual(got, expected) {
t.Errorf("\nExpected: %#v\nGot: %#v", expected, got)
}

if !reflect.DeepEqual(gotFailed, expectedFailed) {
t.Errorf("\nExpected: %#v\nGot: %#v", expectedFailed, gotFailed)
}
})

t.Run("Update", func(t *testing.T) {
// Setup
store := NewMapStore()
// Create a puppy
store.Create(Puppy{Breed: "Wolf", Color: "Grey", Value: "450"})
got := store.Update(1, Puppy{Breed: "Doberman", Color: "Black", Value: "500"})

expected := Puppy{ID: 1, Breed: "Doberman", Color: "Black", Value: "500"}

if !reflect.DeepEqual(got, expected) {
t.Errorf("\nExpected: %#v\nGot: %#v", expected, got)
}
})

t.Run("Destroy", func(t *testing.T) {
// Setup
store := NewMapStore()
// Create a puppy
store.Create(Puppy{Breed: "Wolf", Color: "Grey", Value: "450"})
gotSuccess := store.Destroy(1)
gotFail := store.Destroy(2)

if !gotSuccess {
t.Errorf("\nExpected: true\nGot: false")
}

if gotFail {
t.Errorf("\nExpected: false\nGot: true")
}
})

}
20 changes: 20 additions & 0 deletions 06_puppy/patrickmarabeas/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

type Puppy struct {
ID int
Breed string
Color string
Value string
}

type Storer interface {
Create(puppy Puppy) int
Read(ID int) Puppy
Update(ID int, puppy Puppy) Puppy
Destroy(ID int) bool
}

type MapStore struct {
uuid int
store map[int]Puppy
}

0 comments on commit 7ad6ae8

Please sign in to comment.