forked from bborbe/kv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kv_basic-test-suite.go
120 lines (107 loc) · 2.84 KB
/
kv_basic-test-suite.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
// Copyright (c) 2024 Benjamin Borbe All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package kv
import (
"context"
"errors"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
func BasicTestSuite(provider Provider) {
GinkgoHelper()
Context("Basic", func() {
var db DB
var err error
var bucketName BucketName
var ctx context.Context
BeforeEach(func() {
ctx = context.Background()
db, err = provider.Get(ctx)
Expect(db).NotTo(BeNil())
Expect(err).To(BeNil())
bucketName = NewBucketName("mybucket")
})
It("Get not found", func() {
err = db.Update(ctx, func(ctx context.Context, tx Tx) error {
bucket, err := tx.CreateBucketIfNotExists(ctx, bucketName)
Expect(err).To(BeNil())
{
item, err := bucket.Get(ctx, []byte("key"))
Expect(err).To(BeNil())
Expect(item.Key()).To(Equal([]byte("key")))
var result []byte
err = item.Value(func(val []byte) error {
result = val
return nil
})
Expect(err).To(BeNil())
Expect(result).To(BeNil())
}
return nil
})
Expect(err).To(BeNil())
})
It("Put and Get", func() {
err = db.Update(ctx, func(ctx context.Context, tx Tx) error {
bucket, err := tx.CreateBucketIfNotExists(ctx, bucketName)
Expect(err).To(BeNil())
{
err = bucket.Put(ctx, []byte("key"), []byte("value"))
Expect(err).To(BeNil())
}
{
item, err := bucket.Get(ctx, []byte("key"))
Expect(err).To(BeNil())
Expect(item.Key()).To(Equal([]byte("key")))
var result []byte
err = item.Value(func(val []byte) error {
result = val
return nil
})
Expect(err).To(BeNil())
Expect(result).To(Equal([]byte("value")))
}
return nil
})
Expect(err).To(BeNil())
})
It("Put, Delete and Get", func() {
err = db.Update(ctx, func(ctx context.Context, tx Tx) error {
bucket, err := tx.CreateBucketIfNotExists(ctx, bucketName)
Expect(err).To(BeNil())
{
err = bucket.Put(ctx, []byte("key"), []byte("value"))
Expect(err).To(BeNil())
}
{
err = bucket.Delete(ctx, []byte("key"))
Expect(err).To(BeNil())
}
{
item, err := bucket.Get(ctx, []byte("key"))
Expect(err).To(BeNil())
Expect(item.Key()).To(Equal([]byte("key")))
var result []byte
err = item.Value(func(val []byte) error {
result = val
return nil
})
Expect(err).To(BeNil())
Expect(result).To(BeNil())
}
return nil
})
Expect(err).To(BeNil())
})
It("tx in tx return error", func() {
err = db.Update(ctx, func(ctx context.Context, tx Tx) error {
return db.Update(ctx, func(ctx context.Context, tx Tx) error {
return nil
})
})
Expect(err).NotTo(BeNil())
Expect(errors.Is(err, TransactionAlreadyOpenError)).To(BeTrue())
})
})
}