From 7872296b838f4d1b26c6a0a01d79d27fe5ab44cc Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Sat, 15 Apr 2023 21:07:16 +0200 Subject: Move asset store into its own package --- src/post/asset/asset_test.go | 92 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 src/post/asset/asset_test.go (limited to 'src/post/asset/asset_test.go') diff --git a/src/post/asset/asset_test.go b/src/post/asset/asset_test.go new file mode 100644 index 0000000..574cc08 --- /dev/null +++ b/src/post/asset/asset_test.go @@ -0,0 +1,92 @@ +package asset + +import ( + "bytes" + "io" + "testing" + + "github.com/mediocregopher/blog.mediocregopher.com/srv/post" + "github.com/stretchr/testify/assert" +) + +type testHarness struct { + store Store +} + +func newTestHarness(t *testing.T) *testHarness { + + db := post.NewInMemSQLDB() + t.Cleanup(func() { db.Close() }) + + store := NewStore(db) + + return &testHarness{ + store: store, + } +} + +func (h *testHarness) 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 *testHarness) assertNotFound(t *testing.T, id string) { + t.Helper() + err := h.store.Get(id, io.Discard) + assert.ErrorIs(t, ErrNotFound, err) +} + +func TestStore(t *testing.T) { + + testStore := func(t *testing.T, h *testHarness) { + t.Helper() + + 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") + + // 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) + } + + t.Run("sql", func(t *testing.T) { + h := newTestHarness(t) + testStore(t, h) + }) +} -- cgit v1.2.3