-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache_test.go
49 lines (40 loc) · 1019 Bytes
/
cache_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
package main
import (
"bytes"
"io/ioutil"
"os"
"testing"
)
func TestCache(t *testing.T) {
// Create a temporary directory for the cache
tempDir, err := ioutil.TempDir("", "cache-test")
if err != nil {
t.Fatal("Failed to create temporary directory for cache:", err)
}
defer os.RemoveAll(tempDir)
cache := NewCache(tempDir)
testKey := "test_key"
testValue := []byte("test_value")
// Test the Get method on an empty cache
value, ok := cache.Get(testKey)
if ok {
t.Error("Expected cache miss, got cache hit")
}
// Test the Set method
cache.Set(testKey, testValue)
// Test the Get method after setting a value
value, ok = cache.Get(testKey)
if !ok {
t.Error("Expected cache hit, got cache miss")
}
if !bytes.Equal(value, testValue) {
t.Errorf("Expected value %q, got %q", testValue, value)
}
// Test the Delete method
cache.Delete(testKey)
// Test the Get method after deleting a value
value, ok = cache.Get(testKey)
if ok {
t.Error("Expected cache miss, got cache hit")
}
}