-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(allsrv): add tests for the in-mem db
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
Showing
1 changed file
with
154 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) | ||
} |