-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage_test.go
190 lines (157 loc) · 5.07 KB
/
storage_test.go
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package main
import (
"context"
"database/sql"
"fmt"
"html/template"
"io/ioutil"
"net/url"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func setupTestDB(t *testing.T) *sql.DB {
dbPassphrase := t.Name()
dbURI := fmt.Sprintf(":memory:?_pragma_key=%s&_pragma_cipher_page_size=4096&_foreign_keys=1", url.QueryEscape(dbPassphrase))
db, err := sql.Open("sqlite3", dbURI)
require.NoError(t, err)
require.NoError(t, migrateDatabase(db))
return db
}
func testConvert(t *testing.T, prefix string) func(string) (string, error) {
return func(s string) (string, error) { return prefix + s, nil }
}
func TestInsert(t *testing.T) {
db := setupTestDB(t)
ns, err := newSQLCipherNotes(db, testConvert(t, t.Name()))
require.NoError(t, err)
notes, err := ns.Notes(context.Background())
require.NoError(t, err)
require.Empty(t, notes)
id, err := ns.Insert(context.Background(), "A bit of ~~asciidoc~~ _markdown_ content. 🎉")
require.NoError(t, err)
note, err := ns.Note(context.Background(), id)
require.NoError(t, err)
require.NotNil(t, note)
require.Equal(t, id, note.ID)
require.Equal(t, "A bit of ~~asciidoc~~ _markdown_ content. 🎉", note.Markdown)
require.Equal(t, template.HTML(t.Name()+"A bit of ~~asciidoc~~ _markdown_ content. 🎉"), note.HTML)
require.True(t, note.DateUpdated.IsZero())
require.WithinDuration(t, time.Now(), note.DateCreated, 1*time.Second)
}
func TestEdit(t *testing.T) {
db := setupTestDB(t)
ns, err := newSQLCipherNotes(db, testConvert(t, t.Name()))
require.NoError(t, err)
id, err := ns.Insert(context.Background(), "Does not matter much.")
require.NoError(t, err)
tCases := []struct {
name string
id int64
markdown,
errMsg string
}{
{"existing-note", id, "An updated note.", ""},
{"invalid-id", -123, "Does not matter much.", "expected one row to change but was 0"},
}
parentTestName := t.Name()
for _, tCase := range tCases {
t.Run(tCase.name, func(t *testing.T) {
err = ns.Update(context.Background(), tCase.id, tCase.markdown)
if tCase.errMsg == "" {
require.NoError(t, err)
} else {
require.EqualError(t, err, tCase.errMsg)
return
}
note, err := ns.Note(context.Background(), id)
require.NoError(t, err)
require.NotNil(t, note)
require.Equal(t, tCase.markdown, note.Markdown)
require.Equal(t, template.HTML(parentTestName+tCase.markdown), note.HTML)
require.WithinDuration(t, time.Now(), note.DateCreated, 1*time.Second)
require.WithinDuration(t, time.Now(), note.DateCreated, 1*time.Second)
})
}
}
func TestSearch(t *testing.T) {
db := setupTestDB(t)
ns, err := newSQLCipherNotes(db, testConvert(t, t.Name()))
require.NoError(t, err)
mustRead := func(p string) string {
data, err := ioutil.ReadFile(p)
require.NoError(t, err, "mustRead")
return string(data)
}
tCases := []struct {
name,
markdown,
searchPattern string
}{
{"english with symbol", "Remember the milk™", "milk"},
{"japanese", "おすし大好き!", "大好き"},
{"russian", "Здравствуй, мир", "мир"},
{"inside markdown link text", mustRead("README.md"), "Releases"},
{"link inside text file", mustRead("LICENSE"), "fsf.org"},
}
for _, tCase := range tCases {
t.Run(tCase.name, func(t *testing.T) {
id, err := ns.Insert(context.Background(), tCase.markdown)
require.NoError(t, err)
notes, err := ns.Search(context.Background(), tCase.searchPattern)
require.NoError(t, err)
require.NotNil(t, notes)
require.Len(t, notes, 1)
require.Equal(t, tCase.markdown, notes[0].Markdown)
require.Equal(t, int64(id), notes[0].ID)
})
}
}
func TestDelete(t *testing.T) {
db := setupTestDB(t)
ns, err := newSQLCipherNotes(db, testConvert(t, t.Name()))
require.NoError(t, err)
id, err := ns.Insert(context.Background(), "I will be deleted 😥")
require.NoError(t, err)
tCases := []struct {
name string
id int64
errMsg string
}{
{"existing-note", id, ""},
{"already-deleted", id, "expected to delete one row but was 0"},
{"invalid-id", -123, "expected to delete one row but was 0"},
}
for _, tCase := range tCases {
t.Run(tCase.name, func(t *testing.T) {
err = ns.Delete(context.Background(), tCase.id)
if tCase.errMsg == "" {
require.NoError(t, err)
} else {
require.EqualError(t, err, tCase.errMsg)
return
}
})
}
}
func TestRenew(t *testing.T) {
db := setupTestDB(t)
ns, err := newSQLCipherNotes(db, testConvert(t, t.Name()))
require.NoError(t, err)
// prepare some notes
for i := 0; i < 10; i++ {
_, err = ns.Insert(context.Background(), "I will be renewed ✔️")
require.NoError(t, err)
}
// create a new instance to simulate a changed markdown configuration
ns, err = newSQLCipherNotes(db, func(s string) (string, error) { return "this simulates a changed markdown parser congifuration", nil })
require.NoError(t, err)
err = ns.Renew(context.Background())
require.NoError(t, err)
notes, err := ns.Notes(context.Background())
require.NoError(t, err)
require.Len(t, notes, 10)
for _, note := range notes {
require.Equal(t, template.HTML("this simulates a changed markdown parser congifuration"), note.HTML)
}
}