This repository was archived by the owner on Apr 28, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathmongo_test.go
More file actions
64 lines (48 loc) · 1.56 KB
/
mongo_test.go
File metadata and controls
64 lines (48 loc) · 1.56 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
package mongo
import (
"fmt"
"testing"
"time"
"go.mongodb.org/mongo-driver/v2/mongo"
"go.mongodb.org/mongo-driver/v2/mongo/options"
)
const (
testKeyMongo = "foo1"
testValueMongo = "bar"
)
func TestMongo(t *testing.T) {
// Set client options
clientOptions := options.Client().ApplyURI("mongodb://localhost:27017")
// Connect to MongoDB
client, err := mongo.Connect(clientOptions)
if err != nil {
t.Skip(err)
}
collection := client.Database("cache").Collection("cache")
cache := New(collection)
if err := cache.Save(testKeyMongo, testValueMongo, 1*time.Nanosecond); err != nil {
t.Errorf("save fail: expected nil, got %v", err)
}
if v, err := cache.Fetch(testKeyMongo); err == nil {
t.Errorf("fetch fail: expected an error, got %v value %v", err, v)
}
_ = cache.Save(testKeyMongo, testValueMongo, 10*time.Second)
if res, _ := cache.Fetch(testKeyMongo); res != testValueMongo {
t.Errorf("fetch fail, wrong value : expected %s, got %s", testValueMongo, res)
}
_ = cache.Save(testKeyMongo, testValueMongo, 0)
if !cache.Contains(testKeyMongo) {
t.Errorf("contains failed: the key %s should be exist", testKeyMongo)
}
_ = cache.Save("bar", testValueMongo, 0)
if values := cache.FetchMulti([]string{testKeyMongo, "bar"}); len(values) != 2 {
fmt.Println(values)
t.Errorf("fetch multi failed: expected %d, got %d", 2, len(values))
}
if err := cache.Flush(); err != nil {
t.Errorf("flush failed: expected nil, got %v", err)
}
if cache.Contains(testKeyMongo) {
t.Errorf("contains failed: the key %s should not be exist", testKeyMongo)
}
}