-
Notifications
You must be signed in to change notification settings - Fork 5
/
kv_test.go
97 lines (78 loc) · 2.64 KB
/
kv_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
package freedb
import (
"context"
"fmt"
"testing"
"github.com/FreeLeh/GoFreeDB/google/auth"
"github.com/stretchr/testify/assert"
)
func TestGoogleSheetKVStore_AppendOnly_Integration(t *testing.T) {
spreadsheetID, authJSON, shouldRun := getIntegrationTestInfo()
if !shouldRun {
t.Skip("integration test should be run only in GitHub Actions")
}
sheetName := fmt.Sprintf("integration_append_only_%d", currentTimeMs())
googleAuth, err := auth.NewServiceFromJSON([]byte(authJSON), auth.GoogleSheetsReadWrite, auth.ServiceConfig{})
if err != nil {
t.Fatalf("error when instantiating google auth: %s", err)
}
kv := NewGoogleSheetKVStore(
googleAuth,
spreadsheetID,
sheetName,
GoogleSheetKVStoreConfig{Mode: KVModeAppendOnly},
)
defer func() {
deleteSheet(t, kv.wrapper, spreadsheetID, []string{kv.sheetName, kv.scratchpadSheetName})
_ = kv.Close(context.Background())
}()
value, err := kv.Get(context.Background(), "k1")
assert.Nil(t, value)
assert.ErrorIs(t, err, ErrKeyNotFound)
err = kv.Set(context.Background(), "k1", []byte("test"))
assert.Nil(t, err)
value, err = kv.Get(context.Background(), "k1")
assert.Equal(t, []byte("test"), value)
assert.Nil(t, err)
err = kv.Delete(context.Background(), "k1")
assert.Nil(t, err)
value, err = kv.Get(context.Background(), "k1")
assert.Nil(t, value)
assert.ErrorIs(t, err, ErrKeyNotFound)
}
func TestNewGoogleSheetKVStore_Default_Integration(t *testing.T) {
spreadsheetID, authJSON, shouldRun := getIntegrationTestInfo()
if !shouldRun {
t.Skip("integration test should be run only in GitHub Actions")
}
sheetName := fmt.Sprintf("integration_default_%d", currentTimeMs())
googleAuth, err := auth.NewServiceFromJSON([]byte(authJSON), auth.GoogleSheetsReadWrite, auth.ServiceConfig{})
if err != nil {
t.Fatalf("error when instantiating google auth: %s", err)
}
kv := NewGoogleSheetKVStore(
googleAuth,
spreadsheetID,
sheetName,
GoogleSheetKVStoreConfig{Mode: KVModeDefault},
)
defer func() {
deleteSheet(t, kv.wrapper, spreadsheetID, []string{kv.sheetName, kv.scratchpadSheetName})
_ = kv.Close(context.Background())
}()
value, err := kv.Get(context.Background(), "k1")
assert.Nil(t, value)
assert.ErrorIs(t, err, ErrKeyNotFound)
err = kv.Set(context.Background(), "k1", []byte("test"))
assert.Nil(t, err)
value, err = kv.Get(context.Background(), "k1")
assert.Equal(t, []byte("test"), value)
assert.Nil(t, err)
err = kv.Set(context.Background(), "k1", []byte("test2"))
assert.Nil(t, err)
err = kv.Delete(context.Background(), "k1")
assert.Nil(t, err)
value, err = kv.Get(context.Background(), "k1")
assert.Nil(t, value)
assert.ErrorIs(t, err, ErrKeyNotFound)
}