-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathindex_test.go
84 lines (69 loc) · 2.03 KB
/
index_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
// indextest runs some index related queries and check their outputs. Before
// running, you need to get the golden dataset and load it either using the
// loader or by mutation calls to main dgraph client. In the tests below,
// we will send queries to localhost and check their responses.
package indextest
import (
"bytes"
"encoding/json"
"flag"
"io/ioutil"
// "log"
"net/http"
"testing"
"github.com/stretchr/testify/require"
)
var (
dgraph = flag.String("d", "http://127.0.0.1:8236/query", "Dgraph server address")
)
func testHelper(t *testing.T, prefix string) {
var r map[string]interface{}
input, err := ioutil.ReadFile(prefix + ".in")
require.NoError(t, err)
expectedOutput, err := ioutil.ReadFile(prefix + ".out")
require.NoError(t, err)
// Checking expected output.
require.NoError(t, json.Unmarshal(expectedOutput, &r))
_, found := r["me"]
require.True(t, found)
expectedJS, err := json.Marshal(r["me"])
require.NoError(t, err)
// Post the query.
var client http.Client
req, err := http.NewRequest("POST", *dgraph, bytes.NewReader(input))
require.NoError(t, err)
res, err := client.Do(req)
require.NoError(t, err)
body, err := ioutil.ReadAll(res.Body)
require.NoError(t, err)
// Check the response.
require.NoError(t, json.Unmarshal(body, &r))
_, found = r["me"]
require.True(t, found)
js, err := json.Marshal(r["me"])
require.NoError(t, err)
require.JSONEq(t, string(expectedJS), string(js))
}
func TestFilterStrings(t *testing.T) {
for _, s := range []string{"basic", "allof_the", "allof_the_a",
"allof_the_count", "anyof_the_a", "allof_the_first"} {
t.Run(s, func(t *testing.T) {
testHelper(t, "data/"+s)
})
}
}
func TestSortReleaseDates(t *testing.T) {
for _, s := range []string{"releasedate", "releasedate_sort",
"releasedate_sort_count", "releasedate_sort_first_offset"} {
t.Run(s, func(t *testing.T) {
testHelper(t, "data/"+s)
})
}
}
func TestGenerator(t *testing.T) {
for _, s := range []string{"gen_anyof_good_bad"} {
t.Run(s, func(t *testing.T) {
testHelper(t, "data/"+s)
})
}
}