mem tests
This commit is contained in:
parent
16547ab7b8
commit
94a85d2117
2
go.mod
2
go.mod
|
@ -1,3 +1,5 @@
|
||||||
module code.ewintr.nl/planner
|
module code.ewintr.nl/planner
|
||||||
|
|
||||||
go 1.21.5
|
go 1.21.5
|
||||||
|
|
||||||
|
require github.com/google/go-cmp v0.6.0 // indirect
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||||
|
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
|
@ -16,6 +16,15 @@ func (m *Memory) StoreProject(project Project) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *Memory) RemoveProject(id string) error {
|
||||||
|
if _, ok := m.projects[id]; !ok {
|
||||||
|
return ErrNotFound
|
||||||
|
}
|
||||||
|
delete(m.projects, id)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (m *Memory) FindProject(id string) (Project, error) {
|
func (m *Memory) FindProject(id string) (Project, error) {
|
||||||
project, ok := m.projects[id]
|
project, ok := m.projects[id]
|
||||||
if !ok {
|
if !ok {
|
||||||
|
|
|
@ -4,12 +4,15 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"code.ewintr.nl/planner/service"
|
"code.ewintr.nl/planner/service"
|
||||||
|
"github.com/google/go-cmp/cmp"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestMemoryProjects(t *testing.T) {
|
func TestMemoryProjects(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
mem := service.NewMemory()
|
mem := service.NewMemory()
|
||||||
|
|
||||||
|
t.Log("start empty")
|
||||||
actProjects, actErr := mem.FindAllProjects()
|
actProjects, actErr := mem.FindAllProjects()
|
||||||
if actErr != nil {
|
if actErr != nil {
|
||||||
t.Errorf("exp nil, got %v", actErr)
|
t.Errorf("exp nil, got %v", actErr)
|
||||||
|
@ -17,4 +20,51 @@ func TestMemoryProjects(t *testing.T) {
|
||||||
if len(actProjects) != 0 {
|
if len(actProjects) != 0 {
|
||||||
t.Errorf("exp 0, got %d", len(actProjects))
|
t.Errorf("exp 0, got %d", len(actProjects))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
t.Log("add one")
|
||||||
|
p1 := service.Project{
|
||||||
|
ID: "p1",
|
||||||
|
Name: "project 1",
|
||||||
|
}
|
||||||
|
p2 := service.Project{
|
||||||
|
ID: "p2",
|
||||||
|
Name: "project 2",
|
||||||
|
}
|
||||||
|
if actErr := mem.StoreProject(p1); actErr != nil {
|
||||||
|
t.Errorf("exp nil, got %v", actErr)
|
||||||
|
}
|
||||||
|
actProjects, actErr = mem.FindAllProjects()
|
||||||
|
if actErr != nil {
|
||||||
|
t.Errorf("exp nil, got %v", actErr)
|
||||||
|
}
|
||||||
|
expProjects := []service.Project{p1}
|
||||||
|
if diff := cmp.Diff(expProjects, actProjects); diff != "" {
|
||||||
|
t.Errorf("(-exp, +got):\n%s", diff)
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Log("add second")
|
||||||
|
if actErr := mem.StoreProject(p2); actErr != nil {
|
||||||
|
t.Errorf("exp nil, got %v", actErr)
|
||||||
|
}
|
||||||
|
actProjects, actErr = mem.FindAllProjects()
|
||||||
|
if actErr != nil {
|
||||||
|
t.Errorf("exp nil, got %v", actErr)
|
||||||
|
}
|
||||||
|
expProjects = []service.Project{p1, p2}
|
||||||
|
if diff := cmp.Diff(expProjects, actProjects); diff != "" {
|
||||||
|
t.Errorf("(-exp, +act):\n%s", diff)
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Log("remove first")
|
||||||
|
if actErr := mem.RemoveProject(p1.ID); actErr != nil {
|
||||||
|
t.Errorf("exp nil, got %v", actErr)
|
||||||
|
}
|
||||||
|
actProjects, actErr = mem.FindAllProjects()
|
||||||
|
if actErr != nil {
|
||||||
|
t.Errorf("exp nil, got %v", actErr)
|
||||||
|
}
|
||||||
|
expProjects = []service.Project{p2}
|
||||||
|
if diff := cmp.Diff(expProjects, actProjects); diff != "" {
|
||||||
|
t.Errorf("-exp, +act:\b%s", diff)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue