-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdoc-service_test.go
151 lines (136 loc) · 3.76 KB
/
doc-service_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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"testing"
"github.com/appleboy/gofight"
"github.com/boltdb/bolt"
"github.com/labstack/echo"
"github.com/stretchr/testify/assert"
)
const (
testPort = 8123
testKey = "123"
testTitle = "test-title"
testExtractor = "test-extractor"
)
var (
engine *echo.Echo
testJSON = `{
"a": 1,
"b": 2
}`
)
func TestMain(m *testing.M) {
// Setup before tests.
dataDirExists := false
tmpDataDir := "tempDataDir"
// If there is an existing directory, move it out of the way.
if _, err := os.Stat(dataDir); err == nil {
fmt.Printf("Moving original data dir '%s'...\n", dataDir)
os.Rename(dataDir, tmpDataDir)
dataDirExists = true
}
// Setup the router.
engine = EchoEngine(testPort)
// Setup the database.
err := os.MkdirAll(dataDir, 0777)
if err != nil {
log.Fatalf("Unable to create the data directory %s\n", dataDir)
}
db = createDb(dbFilePath, dbBucket)
defer db.Close()
fmt.Printf("database created '%s'\n", dbFilePath)
// Run the tests.
retCode := m.Run()
// Teardown after tests.
fmt.Printf("Cleaning up test data dir '%s'...\n", dataDir)
if err := os.RemoveAll(dataDir); err != nil {
fmt.Printf("Unable to cleanup directory '%s': %s\n", dataDir, err.Error())
os.Exit(1)
}
if dataDirExists {
fmt.Printf("Moving original data dir '%s' back...\n", dataDir)
os.Rename(tmpDataDir, dataDir)
}
os.Exit(retCode)
}
func TestPostJSONDoc(t *testing.T) {
r := gofight.New()
r.POST("/document").
SetBody(testJSON).
SetDebug(true).
Run(engine, func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
assert.Equal(t, http.StatusOK, r.Code)
data := []byte(r.Body.String())
var resp ResponseType
err := json.Unmarshal(data, &resp)
if assert.NoError(t, err) {
assert.True(t, resp.Ok, "Response ok should be true")
}
cleanupDoc(t, resp.Key)
})
}
// The document created here is used for testing GET and DELETE.
func TestPostJSONDocWithID(t *testing.T) {
r := gofight.New()
r.POST("/document/"+testKey).
SetQuery(gofight.H{
"extractor": testExtractor,
"dc:title": testTitle,
}).
SetBody(testJSON).
SetDebug(true).
Run(engine, func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
assert.Equal(t, http.StatusOK, r.Code)
data := []byte(r.Body.String())
var resp ResponseType
err := json.Unmarshal(data, &resp)
if assert.NoError(t, err) {
assert.True(t, resp.Ok, "Response ok should be true")
assert.Equal(t, testKey, resp.Key, "ID key should be equal")
}
})
}
func TestGetJSONDoc(t *testing.T) {
r := gofight.New()
r.GET("/document/"+testKey).
SetDebug(true).
Run(engine, func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
assert.Equal(t, http.StatusOK, r.Code)
data := []byte(r.Body.String())
var resp ResponseType
err := json.Unmarshal(data, &resp)
if assert.NoError(t, err) {
assert.True(t, resp.Ok, "Response ok should be true")
assert.JSONEq(t, testJSON, resp.Document)
assert.Equal(t, testExtractor, resp.Extractor, "Extractor metadata should match")
assert.Equal(t, testTitle, resp.Title, "Title metadata should match")
}
})
}
func TestDeleteJSONDoc(t *testing.T) {
r := gofight.New()
r.DELETE("/document/"+testKey).
SetDebug(true).
Run(engine, func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
assert.Equal(t, http.StatusOK, r.Code)
data := []byte(r.Body.String())
var resp ResponseType
err := json.Unmarshal(data, &resp)
if assert.NoError(t, err) {
assert.True(t, resp.Ok, "Response ok should be true")
}
})
}
func cleanupDoc(t *testing.T, key string) {
errFile := os.Remove(dataDir + "/" + key)
assert.NoError(t, errFile)
errDB := db.Update(func(tx *bolt.Tx) error {
return tx.Bucket(dbBucket).Delete([]byte(key))
})
assert.NoError(t, errDB)
}