Skip to content

Commit

Permalink
chore(allsrv): add tests for the in-mem db
Browse files Browse the repository at this point in the history
This helps us close the gap in our testing. This time we're putting the
in-mem db under test. This is partially under test via the server tests,
but we have limited visibility into the stack. Once we have the basics
in place, we can start to ask more interesting quetsions of our system.

Try to create a test that will trigger the race condition in the in-mem
operations for each destructive operation? Hint: use the `-race` flag:

```shell
go test -race
```
  • Loading branch information
jsteenb2 committed Jul 5, 2024
1 parent edfa256 commit ecfd463
Showing 1 changed file with 154 additions and 0 deletions.
154 changes: 154 additions & 0 deletions allsrv/db_inmem_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package allsrv_test

import (
"context"
"errors"
"testing"

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

"github.com/jsteenb2/mess/allsrv"
)

func TestInmemDB(t *testing.T) {
t.Run("create foo", func(t *testing.T) {
t.Run("with valid foo should pass", func(t *testing.T) {
db := new(allsrv.InmemDB)

want := allsrv.Foo{
ID: "1",
Name: "name",
Note: "note",
}
err := db.CreateFoo(context.TODO(), want)
require.NoError(t, err)

got, err := db.ReadFoo(context.TODO(), "1")
require.NoError(t, err)

assert.Equal(t, want, got)
})

t.Run("with foo containing name that already exists should fail", func(t *testing.T) {
db := new(allsrv.InmemDB)

want := allsrv.Foo{
ID: "1",
Name: "collision",
Note: "note",
}
err := db.CreateFoo(context.TODO(), want)
require.NoError(t, err)

err = db.CreateFoo(context.TODO(), want)

// this is pretty gross, we're matching against a raw error/text value
// any change in the error message means we have to update tests too
wantErr := errors.New("foo collision exists")
assert.Equal(t, wantErr, err)
})
})

t.Run("read foo", func(t *testing.T) {
t.Run("with id for existing foo should pass", func(t *testing.T) {
db := new(allsrv.InmemDB)

want := allsrv.Foo{
ID: "1",
Name: "name",
Note: "note",
}
err := db.CreateFoo(context.TODO(), want)
require.NoError(t, err)

got, err := db.ReadFoo(context.TODO(), "1")
require.NoError(t, err)

assert.Equal(t, want, got)
})

t.Run("with id for non-existent foo should fail", func(t *testing.T) {
db := new(allsrv.InmemDB)

_, err := db.ReadFoo(context.TODO(), "1")

// this is pretty gross, we're matching against a raw error/text value
// any change in the error message means we have to update tests too
want := errors.New("foo not found for id: 1")
assert.Equal(t, want, err)
})
})

t.Run("update foo", func(t *testing.T) {
t.Run("with valid update for existing foo should pass", func(t *testing.T) {
db := new(allsrv.InmemDB)

want := allsrv.Foo{
ID: "1",
Name: "name",
Note: "note",
}
err := db.CreateFoo(context.TODO(), want)
require.NoError(t, err)

want.Note = "some other note"
err = db.UpdateFoo(context.TODO(), want)
require.NoError(t, err)

got, err := db.ReadFoo(context.TODO(), "1")
require.NoError(t, err)

assert.Equal(t, want, got)
})

t.Run("with update for non-existent foo should fail", func(t *testing.T) {
db := new(allsrv.InmemDB)

err := db.UpdateFoo(context.TODO(), allsrv.Foo{
ID: "1",
Name: "name",
Note: "note",
})

// this is pretty gross, we're matching against a raw error/text value
// any change in the error message means we have to update tests too
want := errors.New("foo not found for id: 1")
assert.Equal(t, want, err)
})
})

t.Run("delete foo", func(t *testing.T) {
t.Run("with id for existing foo should pass", func(t *testing.T) {
db := new(allsrv.InmemDB)

err := db.CreateFoo(context.TODO(), allsrv.Foo{
ID: "1",
Name: "name",
Note: "note",
})
require.NoError(t, err)

err = db.DelFoo(context.TODO(), "1")
require.NoError(t, err)

_, err = db.ReadFoo(context.TODO(), "1")

// this is pretty gross, we're matching against a raw error/text value
// any change in the error message means we have to update tests too
want := errors.New("foo not found for id: 1")
assert.Equal(t, want, err)
})

t.Run("with id for non-existent foo should fail", func(t *testing.T) {
db := new(allsrv.InmemDB)

err := db.DelFoo(context.TODO(), "1")

// this is pretty gross, we're matching against a raw error/text value
// any change in the error message means we have to update tests too
want := errors.New("foo not found for id: 1")
assert.Equal(t, want, err)
})
})
}

0 comments on commit ecfd463

Please sign in to comment.