Skip to content
File

Blob: kv/sqlite3/prefix_test.go

go154 lines
1package sqlite3
2 
3import (
4 "bytes"
5 "context"
6 "crypto/rand"
7 "testing"
8 
9 "github.com/stretchr/testify/assert"
10)
11 
12func TestPrefixAppend(t *testing.T) {
13 as := assert.New(t)
14 kv := testGetKV(t)
15 
16 prefix := make([]byte, 8)
17 child := make([]byte, 16)
18 rand.Read(prefix)
19 rand.Read(child)
20 
21 as.NoError(kv.PrefixAppend(context.Background(), prefix, child))
22 as.Error(kv.PrefixAppend(context.Background(), prefix, child))
23}
24 
25func TestPrefixList(t *testing.T) {
26 as := assert.New(t)
27 kv := testGetKV(t)
28 
29 numChildren := 32
30 prefix := make([]byte, 8)
31 rand.Read(prefix)
32 
33 children := make([][]byte, numChildren)
34 for i := range children {
35 children[i] = make([]byte, 16)
36 rand.Read(children[i])
37 kv.PrefixAppend(context.Background(), prefix, children[i])
38 }
39 
40 ret, err := kv.PrefixList(context.Background(), prefix)
41 as.NoError(err)
42 for _, child := range ret {
43 as.Greater(len(child), 0)
44 }
45 
46 found := 0
47 for _, needle := range children {
48 for _, haystack := range ret {
49 if bytes.Equal(haystack, needle) {
50 found++
51 }
52 }
53 }
54 
55 as.Equal(numChildren, found, "missing from prefix list")
56}
57 
58func TestPrefixContains(t *testing.T) {
59 as := assert.New(t)
60 kv := testGetKV(t)
61 
62 prefix := make([]byte, 8)
63 child := make([]byte, 16)
64 rand.Read(prefix)
65 rand.Read(child)
66 
67 as.NoError(kv.PrefixAppend(context.Background(), prefix, child))
68 
69 b, err := kv.PrefixContains(context.Background(), prefix, child)
70 as.NoError(err)
71 as.True(b)
72 
73 b, err = kv.PrefixContains(context.Background(), prefix, child[1:])
74 as.NoError(err)
75 as.False(b)
76}
77 
78func TestPrefixDelete(t *testing.T) {
79 as := assert.New(t)
80 kv := testGetKV(t)
81 
82 numChildren := 32
83 prefix := make([]byte, 8)
84 rand.Read(prefix)
85 
86 children := make([][]byte, numChildren)
87 for i := range children {
88 children[i] = make([]byte, 16)
89 rand.Read(children[i])
90 kv.PrefixAppend(context.Background(), prefix, children[i])
91 }
92 
93 ret, err := kv.PrefixList(context.Background(), prefix)
94 as.NoError(err)
95 
96 found := 0
97 for _, needle := range children {
98 for _, haystack := range ret {
99 if bytes.Equal(haystack, needle) {
100 found++
101 }
102 }
103 }
104 
105 as.Equal(numChildren, found, "missing child from prefix list")
106 
107 expectMissing := 0
108 for i := 0; i < numChildren; i += 2 {
109 kv.PrefixRemove(context.Background(), prefix, children[i])
110 expectMissing++
111 }
112 
113 ret, err = kv.PrefixList(context.Background(), prefix)
114 as.NoError(err)
115 
116 found = 0
117 for _, needle := range children {
118 for _, haystack := range ret {
119 if bytes.Equal(haystack, needle) {
120 found++
121 }
122 }
123 }
124 
125 as.Equal(numChildren-expectMissing, found, "found deleted child")
126}
127 
128func TestSharedKeyspace(t *testing.T) {
129 as := assert.New(t)
130 kv := testGetKV(t)
131 
132 key := make([]byte, 8)
133 rand.Read(key)
134 
135 plainValue := make([]byte, 16)
136 rand.Read(plainValue)
137 child := make([]byte, 32)
138 rand.Read(child)
139 
140 as.NoError(kv.Put(context.Background(), key, plainValue))
141 as.NoError(kv.PrefixAppend(context.Background(), key, child))
142 
143 // deleting the key from plain keyspace should not affect the prefix keyspace
144 as.NoError(kv.Delete(context.Background(), key))
145 val, err := kv.Get(context.Background(), key)
146 as.NoError(err)
147 as.Nil(val)
148 
149 vals, err := kv.PrefixList(context.Background(), key)
150 as.NoError(err)
151 as.Len(vals, 1)
152 as.EqualValues(child, vals[0])
153}