diff --git a/06_puppy/patrickmarabeas/main.go b/06_puppy/patrickmarabeas/main.go new file mode 100644 index 000000000..41fd5bb96 --- /dev/null +++ b/06_puppy/patrickmarabeas/main.go @@ -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) + +} diff --git a/06_puppy/patrickmarabeas/main_test.go b/06_puppy/patrickmarabeas/main_test.go new file mode 100644 index 000000000..dd8bf604e --- /dev/null +++ b/06_puppy/patrickmarabeas/main_test.go @@ -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) + } + }) +} diff --git a/06_puppy/patrickmarabeas/mapStore.go b/06_puppy/patrickmarabeas/mapStore.go new file mode 100644 index 000000000..69d5f7dd1 --- /dev/null +++ b/06_puppy/patrickmarabeas/mapStore.go @@ -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 +} diff --git a/06_puppy/patrickmarabeas/mapStore_test.go b/06_puppy/patrickmarabeas/mapStore_test.go new file mode 100644 index 000000000..8a9ceeaba --- /dev/null +++ b/06_puppy/patrickmarabeas/mapStore_test.go @@ -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") + } + }) + +} diff --git a/06_puppy/patrickmarabeas/types.go b/06_puppy/patrickmarabeas/types.go new file mode 100644 index 000000000..371ded4ed --- /dev/null +++ b/06_puppy/patrickmarabeas/types.go @@ -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 +}