-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_test.go
131 lines (110 loc) · 3.28 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
/*
* Copyright 2025 Hypermode Inc.
* Licensed under the terms of the Apache License, Version 2.0
* See the LICENSE file that accompanied this code for further details.
*
* SPDX-FileCopyrightText: 2025 Hypermode Inc. <[email protected]>
* SPDX-License-Identifier: Apache-2.0
*/
package modusdb_test
import (
"bytes"
"context"
"encoding/binary"
"fmt"
"testing"
"github.com/dgraph-io/dgo/v240/protos/api"
"github.com/stretchr/testify/require"
"github.com/hypermodeinc/modusdb"
)
func TestRestart(t *testing.T) {
dataDir := t.TempDir()
db, err := modusdb.New(modusdb.NewDefaultConfig(dataDir))
require.NoError(t, err)
defer func() { db.Close() }()
require.NoError(t, db.DropAll(context.Background()))
require.NoError(t, db.AlterSchema(context.Background(), "name: string @index(term) ."))
_, err = db.Mutate(context.Background(), []*api.Mutation{
{
Set: []*api.NQuad{
{
Namespace: 0,
Subject: "_:aman",
Predicate: "name",
ObjectValue: &api.Value{Val: &api.Value_StrVal{StrVal: "A"}},
},
},
},
})
require.NoError(t, err)
query := `{
me(func: has(name)) {
name
}
}`
qresp, err := db.Query(context.Background(), query)
require.NoError(t, err)
require.JSONEq(t, `{"me":[{"name":"A"}]}`, string(qresp.GetJson()))
db.Close()
db, err = modusdb.New(modusdb.NewDefaultConfig(dataDir))
require.NoError(t, err)
qresp, err = db.Query(context.Background(), query)
require.NoError(t, err)
require.JSONEq(t, `{"me":[{"name":"A"}]}`, string(qresp.GetJson()))
require.NoError(t, db.DropAll(context.Background()))
}
func TestSchemaQuery(t *testing.T) {
db, err := modusdb.New(modusdb.NewDefaultConfig(t.TempDir()))
require.NoError(t, err)
defer db.Close()
require.NoError(t, db.DropAll(context.Background()))
require.NoError(t, db.AlterSchema(context.Background(), `
name: string @index(exact) .
age: int .
married: bool .
loc: geo .
dob: datetime .
`))
resp, err := db.Query(context.Background(), `schema(pred: [name, age]) {type}`)
require.NoError(t, err)
require.JSONEq(t,
`{"schema":[{"predicate":"age","type":"int"},{"predicate":"name","type":"string"}]}`,
string(resp.GetJson()))
}
func TestBasicVector(t *testing.T) {
vect := []float32{5.1, 5.1, 1.1}
buf := new(bytes.Buffer)
for _, v := range vect {
require.NoError(t, binary.Write(buf, binary.LittleEndian, v))
}
vectBytes := buf.Bytes()
db, err := modusdb.New(modusdb.NewDefaultConfig(t.TempDir()))
require.NoError(t, err)
defer db.Close()
require.NoError(t, db.DropAll(context.Background()))
require.NoError(t, db.AlterSchema(context.Background(),
`project_description_v: float32vector @index(hnsw(exponent: "5", metric: "euclidean")) .`))
uids, err := db.Mutate(context.Background(), []*api.Mutation{{
Set: []*api.NQuad{{
Subject: "_:vector",
Predicate: "project_description_v",
ObjectValue: &api.Value{
Val: &api.Value_Vfloat32Val{Vfloat32Val: vectBytes},
},
}},
}})
require.NoError(t, err)
uid := uids["_:vector"]
if uid == 0 {
t.Fatalf("Expected non-zero uid")
}
resp, err := db.Query(context.Background(), fmt.Sprintf(`query {
q (func: uid(%v)) {
project_description_v
}
}`, uid))
require.NoError(t, err)
require.Equal(t,
`{"q":[{"project_description_v":[5.1E+00,5.1E+00,1.1E+00]}]}`,
string(resp.GetJson()))
}