diff options
author | Brian Picciano <mediocregopher@gmail.com> | 2022-05-07 18:07:27 -0600 |
---|---|---|
committer | Brian Picciano <mediocregopher@gmail.com> | 2022-05-07 18:07:27 -0600 |
commit | 3088c9d76ca2c88996bb794b17e87cd1d5e76064 (patch) | |
tree | 9b09a41288ae65bfd20d1e408c7fdebd3358d19c /srv/src/post/asset_test.go | |
parent | cfb633b3b5eed1a65b23cc641b1b250adffdbc8f (diff) |
Implement AssetStore interface
Diffstat (limited to 'srv/src/post/asset_test.go')
-rw-r--r-- | srv/src/post/asset_test.go | 67 |
1 files changed, 67 insertions, 0 deletions
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") +} |