summaryrefslogtreecommitdiff
path: root/src/post/draft_post_test.go
blob: f7b7de2bc8b31db1d7f5efc3d1082d459c6241ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package post

import (
	"sort"
	"testing"

	"github.com/stretchr/testify/assert"
)

type draftStoreTestHarness struct {
	store DraftStore
}

func newDraftStoreTestHarness(t *testing.T) draftStoreTestHarness {

	db := NewInMemSQLDB()
	t.Cleanup(func() { db.Close() })

	store := NewDraftStore(db)

	return draftStoreTestHarness{
		store: store,
	}
}

func TestDraftStore(t *testing.T) {

	assertPostEqual := func(t *testing.T, exp, got Post) {
		t.Helper()
		sort.Strings(exp.Tags)
		sort.Strings(got.Tags)
		assert.Equal(t, exp, got)
	}

	assertPostsEqual := func(t *testing.T, exp, got []Post) {
		t.Helper()

		if !assert.Len(t, got, len(exp), "exp:%+v\ngot: %+v", exp, got) {
			return
		}

		for i := range exp {
			assertPostEqual(t, exp[i], got[i])
		}
	}

	t.Run("not_found", func(t *testing.T) {
		h := newDraftStoreTestHarness(t)

		_, err := h.store.GetByID("foo")
		assert.ErrorIs(t, err, ErrPostNotFound)
	})

	t.Run("set_get_delete", func(t *testing.T) {
		h := newDraftStoreTestHarness(t)

		post := testPost(0)
		post.Tags = []string{"foo", "bar"}

		err := h.store.Set(post)
		assert.NoError(t, err)

		gotPost, err := h.store.GetByID(post.ID)
		assert.NoError(t, err)

		assertPostEqual(t, post, gotPost)

		// we will now try updating the post, and ensure it updates properly

		post.Title = "something else"
		post.Description = "some description"
		post.Series = "whatever"
		post.Body = "anything"
		post.Tags = []string{"bar", "baz"}
		post.Format = FormatGemtext

		err = h.store.Set(post)
		assert.NoError(t, err)

		gotPost, err = h.store.GetByID(post.ID)
		assert.NoError(t, err)

		assertPostEqual(t, post, gotPost)

		// delete the post, it should go away
		assert.NoError(t, h.store.Delete(post.ID))

		_, err = h.store.GetByID(post.ID)
		assert.ErrorIs(t, err, ErrPostNotFound)
	})

	t.Run("get", func(t *testing.T) {
		h := newDraftStoreTestHarness(t)

		posts := []Post{
			testPost(0),
			testPost(1),
			testPost(2),
			testPost(3),
		}

		for _, post := range posts {
			err := h.store.Set(post)
			assert.NoError(t, err)
		}

		gotPosts, hasMore, err := h.store.Get(0, 2)
		assert.NoError(t, err)
		assert.True(t, hasMore)
		assertPostsEqual(t, posts[:2], gotPosts)

		gotPosts, hasMore, err = h.store.Get(1, 2)
		assert.NoError(t, err)
		assert.False(t, hasMore)
		assertPostsEqual(t, posts[2:4], gotPosts)

		posts = append(posts, testPost(4))
		err = h.store.Set(posts[4])
		assert.NoError(t, err)

		gotPosts, hasMore, err = h.store.Get(1, 2)
		assert.NoError(t, err)
		assert.True(t, hasMore)
		assertPostsEqual(t, posts[2:4], gotPosts)

		gotPosts, hasMore, err = h.store.Get(2, 2)
		assert.NoError(t, err)
		assert.False(t, hasMore)
		assertPostsEqual(t, posts[4:], gotPosts)
	})

}