From 3088c9d76ca2c88996bb794b17e87cd1d5e76064 Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Sat, 7 May 2022 18:07:27 -0600 Subject: Implement AssetStore interface --- srv/src/post/asset_test.go | 67 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 srv/src/post/asset_test.go (limited to 'srv/src/post/asset_test.go') diff --git a/srv/src/post/asset_test.go b/srv/src/post/asset_test.go new file mode 100644 index 0000000..4b88000 --- /dev/null +++ b/srv/src/post/asset_test.go @@ -0,0 +1,67 @@ +package post + +import ( + "bytes" + "io" + "testing" + + "github.com/stretchr/testify/assert" +) + +type assetTestHarness struct { + store AssetStore +} + +func newAssetTestHarness(t *testing.T) assetTestHarness { + + db := NewInMemSQLDB() + t.Cleanup(func() { db.Close() }) + + store := NewAssetStore(db) + + return assetTestHarness{ + store: store, + } +} + +func (h *assetTestHarness) assertGet(t *testing.T, exp, id string) { + t.Helper() + buf := new(bytes.Buffer) + err := h.store.Get(id, buf) + assert.NoError(t, err) + assert.Equal(t, exp, buf.String()) +} + +func (h *assetTestHarness) assertNotFound(t *testing.T, id string) { + t.Helper() + err := h.store.Get(id, io.Discard) + assert.ErrorIs(t, ErrAssetNotFound, err) +} + +func TestAssetStore(t *testing.T) { + + h := newAssetTestHarness(t) + + h.assertNotFound(t, "foo") + h.assertNotFound(t, "bar") + + err := h.store.Set("foo", bytes.NewBufferString("FOO")) + assert.NoError(t, err) + + h.assertGet(t, "FOO", "foo") + h.assertNotFound(t, "bar") + + err = h.store.Set("foo", bytes.NewBufferString("FOOFOO")) + assert.NoError(t, err) + + h.assertGet(t, "FOOFOO", "foo") + h.assertNotFound(t, "bar") + + assert.NoError(t, h.store.Delete("foo")) + h.assertNotFound(t, "foo") + h.assertNotFound(t, "bar") + + assert.NoError(t, h.store.Delete("bar")) + h.assertNotFound(t, "foo") + h.assertNotFound(t, "bar") +} -- cgit v1.2.3