-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex_test.go
More file actions
134 lines (119 loc) · 3.7 KB
/
index_test.go
File metadata and controls
134 lines (119 loc) · 3.7 KB
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
package envector
import (
"context"
"reflect"
"testing"
es2pb "github.com/CryptoLabInc/envector-go-sdk/internal/transport/pb/es2"
)
func TestGetIndexList_RoundTrip(t *testing.T) {
c, fake := newFakeClient(t)
fake.indexList = []string{"demo", "extra"}
got, err := c.GetIndexList(context.Background())
if err != nil {
t.Fatalf("GetIndexList: %v", err)
}
if !reflect.DeepEqual(got, []string{"demo", "extra"}) {
t.Errorf("got %v", got)
}
}
func TestClient_Index_RequiresName(t *testing.T) {
c, _ := newFakeClient(t)
if _, err := c.Index(context.Background()); err == nil {
t.Error("expected error when WithIndexName absent")
}
}
func TestClient_Index_IdempotentWhenExisting(t *testing.T) {
c, fake := newFakeClient(t)
fake.indexList = []string{"demo"}
idx, err := c.Index(context.Background(), WithIndexName("demo"))
if err != nil {
t.Fatalf("Index: %v", err)
}
if idx.Name() != "demo" {
t.Errorf("Name = %q", idx.Name())
}
if fake.createIndexInfo != nil {
t.Error("CreateIndex must not be called when index already exists")
}
}
func TestClient_Index_CreatesWhenMissing(t *testing.T) {
c, fake := newFakeClient(t)
fake.indexList = nil
_, err := c.Index(context.Background(),
WithIndexName("sample"),
WithIndexKeys(openTestKeys(t)),
WithIndexDescription("sample index"),
)
if err != nil {
t.Fatalf("Index: %v", err)
}
info := fake.createIndexInfo
if info == nil {
t.Fatal("CreateIndex was not invoked")
}
if info.GetIndexName() != "sample" {
t.Errorf("IndexName = %q", info.GetIndexName())
}
if info.GetDim() != 128 {
t.Errorf("Dim = %d, want %d (sourced from Keys.Dim())", info.GetDim(), 128)
}
if info.GetIndexType() != es2pb.IndexType_FLAT {
t.Errorf("IndexType = %v, want FLAT", info.GetIndexType())
}
if info.GetIndexEncryption() != "cipher" {
t.Errorf("IndexEncryption = %q, want default cipher", info.GetIndexEncryption())
}
if info.GetQueryEncryption() != "plain" {
t.Errorf("QueryEncryption = %q, want default plain", info.GetQueryEncryption())
}
if info.GetKeyId() != "test-key" {
t.Errorf("KeyId = %q, want test-key", info.GetKeyId())
}
}
func TestIndex_Drop(t *testing.T) {
c, fake := newFakeClient(t)
fake.indexList = []string{"demo"}
idx, _ := c.Index(context.Background(), WithIndexName("demo"))
if err := idx.Drop(context.Background()); err != nil {
t.Fatalf("Drop: %v", err)
}
if !reflect.DeepEqual(fake.deleteIndexCalls, []string{"demo"}) {
t.Errorf("deleteIndexCalls = %v", fake.deleteIndexCalls)
}
}
func TestIndex_GetMetadata(t *testing.T) {
c, fake := newFakeClient(t)
fake.indexList = []string{"demo"}
fake.metadataRows = []*es2pb.Metadata{
{Id: 1, Data: `{"a":"first"}`},
{Id: 2, Data: `{"a":"second"}`},
}
idx, _ := c.Index(context.Background(), WithIndexName("demo"))
got, err := idx.GetMetadata(context.Background(),
[]MetadataRef{{ShardIdx: 0, RowIdx: 1}, {ShardIdx: 0, RowIdx: 2}},
[]string{"metadata"})
if err != nil {
t.Fatalf("GetMetadata: %v", err)
}
want := []Metadata{{ID: 1, Data: `{"a":"first"}`}, {ID: 2, Data: `{"a":"second"}`}}
if !reflect.DeepEqual(got, want) {
t.Errorf("got %+v, want %+v", got, want)
}
if fake.getMetadataReq.GetIndexName() != "demo" {
t.Errorf("IndexName = %q", fake.getMetadataReq.GetIndexName())
}
if len(fake.getMetadataReq.GetIdx()) != 2 {
t.Errorf("Idx len = %d", len(fake.getMetadataReq.GetIdx()))
}
if !reflect.DeepEqual(fake.getMetadataReq.GetOutputFields(), []string{"metadata"}) {
t.Errorf("OutputFields = %v", fake.getMetadataReq.GetOutputFields())
}
}
func TestCheckHeader_PropagatesServerError(t *testing.T) {
c, fake := newFakeClient(t)
fake.headerErr = es2pb.ReturnCode_NoSuchIndex
_, err := c.GetIndexList(context.Background())
if err == nil {
t.Fatal("expected error")
}
}