From ecfd46362259c63ac7cfbee79e89223772b1e68f Mon Sep 17 00:00:00 2001 From: Johnny Steenbergen Date: Sun, 14 Jan 2024 12:21:41 -0600 Subject: [PATCH] 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 ``` --- allsrv/db_inmem_test.go | 154 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 154 insertions(+) create mode 100644 allsrv/db_inmem_test.go diff --git a/allsrv/db_inmem_test.go b/allsrv/db_inmem_test.go new file mode 100644 index 0000000..1f3e249 --- /dev/null +++ b/allsrv/db_inmem_test.go @@ -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) + }) + }) +}