This repository has been archived by the owner on May 3, 2021. It is now read-only.
/
db_test.go
178 lines (154 loc) · 2.81 KB
/
db_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
package main
import (
"io/ioutil"
"os"
"testing"
)
// Test basic primitives.
func TestReadWrite(t *testing.T) {
//
// Create a temporary directory
//
p, err := ioutil.TempDir(os.TempDir(), "prefix")
if err == nil {
PREFIX = p + "/"
} else {
t.Fatal(err)
}
//
// Reading a file will fail
//
key := "this-is-a-key"
var out string
_, err = readFile(key)
if err == nil {
t.Errorf("No error reading a missing file, when we expected one")
}
//
// Now write some content
//
err = writeFile(key, key)
if err != nil {
t.Errorf("Error writing to a test file")
}
//
// At this point reading it back should succeed
//
out, err = readFile(key)
if err != nil {
t.Errorf("Error reading file - we didn't expect that")
}
if out != key {
t.Errorf("Got unexpected result reading file")
}
//
// Because of the way we work reading the KeyFromAuth will
// have the same result
//
out2, err2 := KeyFromAuth(key)
if out2 != out {
t.Errorf("Data mismatch")
}
if err2 != nil {
t.Errorf("Error reading data again")
}
//
// Cleanup our temporary directory
//
os.RemoveAll(p)
}
//
// Test fetching a markdown file, and editing it.
//
func TestReadEditMarkdown(t *testing.T) {
//
// Create a temporary directory
//
p, err := ioutil.TempDir(os.TempDir(), "prefix")
if err == nil {
PREFIX = p + "/"
} else {
t.Fatal(err)
}
//
// Write some content
//
key := "this-is-a-key"
err = writeFile(key+".TEXT", "_Italic_")
if err != nil {
t.Errorf("Error writing 'markdown'")
}
//
// Get the "markdown"
//
var data string
data = getMarkdown(key)
if data != "_Italic_" {
t.Errorf("Got unexpected result reading file")
}
//
// Update it
//
err = UpdateMarkdown(key, "__Bold__")
if err != nil {
t.Errorf("Error updating markdown")
}
//
// Read it again
//
data = getMarkdown(key)
if data != "__Bold__" {
t.Errorf("Got unexpected result reading file")
}
//
// Cleanup our temporary directory
//
os.RemoveAll(p)
}
//
// Test deleting a markdown file
//
func TestDeleteMarkdown(t *testing.T) {
//
// Create a temporary directory
//
p, err := ioutil.TempDir(os.TempDir(), "prefix")
if err == nil {
PREFIX = p + "/"
} else {
t.Fatal(err)
}
//
// Save some markdown
//
key, auth, err := SaveMarkdown("__bold__", "127.127.53.53")
if err != nil {
t.Errorf("Error saving markdown")
}
//
// Get the markdown, to ensure it worked
//
var data string
data = getMarkdown(key)
if data != "__bold__" {
t.Errorf("Got unexpected result reading file")
}
//
// Now delete
//
err = DeleteMarkdown(auth)
if err != nil {
t.Errorf("Unexpected error deleting: %s", err.Error())
}
//
// Get the markdown, again
//
data = getMarkdown(key)
if data != "" {
t.Errorf("Got unexpected result reading file")
}
//
// Cleanup our temporary directory
//
os.RemoveAll(p)
}