You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
92 lines
1.9 KiB
92 lines
1.9 KiB
2 years ago
|
package post
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"io"
|
||
|
"testing"
|
||
|
|
||
|
"github.com/stretchr/testify/assert"
|
||
|
)
|
||
|
|
||
|
type assetTestHarness struct {
|
||
|
store AssetStore
|
||
|
}
|
||
|
|
||
2 years ago
|
func newAssetTestHarness(t *testing.T) *assetTestHarness {
|
||
2 years ago
|
|
||
|
db := NewInMemSQLDB()
|
||
|
t.Cleanup(func() { db.Close() })
|
||
|
|
||
|
store := NewAssetStore(db)
|
||
|
|
||
2 years ago
|
return &assetTestHarness{
|
||
2 years ago
|
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) {
|
||
|
|
||
2 years ago
|
testAssetStore := func(t *testing.T, h *assetTestHarness) {
|
||
|
t.Helper()
|
||
2 years ago
|
|
||
2 years ago
|
h.assertNotFound(t, "foo")
|
||
|
h.assertNotFound(t, "bar")
|
||
2 years ago
|
|
||
2 years ago
|
err := h.store.Set("foo", bytes.NewBufferString("FOO"))
|
||
|
assert.NoError(t, err)
|
||
2 years ago
|
|
||
2 years ago
|
h.assertGet(t, "FOO", "foo")
|
||
|
h.assertNotFound(t, "bar")
|
||
2 years ago
|
|
||
2 years ago
|
err = h.store.Set("foo", bytes.NewBufferString("FOOFOO"))
|
||
|
assert.NoError(t, err)
|
||
|
|
||
|
h.assertGet(t, "FOOFOO", "foo")
|
||
|
h.assertNotFound(t, "bar")
|
||
2 years ago
|
|
||
2 years ago
|
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")
|
||
2 years ago
|
|
||
|
// test list
|
||
|
|
||
|
ids, err := h.store.List()
|
||
|
assert.NoError(t, err)
|
||
|
assert.Empty(t, ids)
|
||
|
|
||
|
err = h.store.Set("foo", bytes.NewBufferString("FOOFOO"))
|
||
|
assert.NoError(t, err)
|
||
|
err = h.store.Set("foo", bytes.NewBufferString("FOOFOO"))
|
||
|
assert.NoError(t, err)
|
||
|
err = h.store.Set("bar", bytes.NewBufferString("FOOFOO"))
|
||
|
assert.NoError(t, err)
|
||
|
|
||
|
ids, err = h.store.List()
|
||
|
assert.NoError(t, err)
|
||
|
assert.Equal(t, []string{"bar", "foo"}, ids)
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
2 years ago
|
t.Run("sql", func(t *testing.T) {
|
||
|
h := newAssetTestHarness(t)
|
||
|
testAssetStore(t, h)
|
||
|
})
|
||
2 years ago
|
}
|