-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage_test.go
80 lines (67 loc) · 1.5 KB
/
storage_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
package memory_ttl_storage
import (
"encoding/hex"
"math/rand"
"os"
"path/filepath"
"testing"
)
type MyTestStruct struct {
A string
B map[string]Content
}
type Content struct {
A string
B string
}
func TestStoreRestore(t *testing.T) {
randBytes := make([]byte, 16)
rand.Read(randBytes)
filePath := filepath.Join(os.TempDir(), "mts_test_" + hex.EncodeToString(randBytes)+".dat")
back := StorageManager{backupFilePath: filePath}
c := make(map[string]Content)
c["item"] = Content{
A: "a",
B: "b",
}
m := MyTestStruct{A: "a", B: c}
err := back.Store(m)
if err!=nil{
t.Error("the store must be created", err)
}
result := MyTestStruct{}
err = back.Restore(&result)
if err != nil {
t.Error("unable to restore data", err)
}
if m.A != result.A {
t.Error("those items must be equals")
}
}
func TestMap(t *testing.T) {
randBytes := make([]byte, 16)
rand.Read(randBytes)
filePath := filepath.Join(os.TempDir(), "mts_test_" + hex.EncodeToString(randBytes)+".dat")
back := StorageManager{backupFilePath: filePath}
c := make(map[string]Content)
c["item"] = Content{
A: "a",
B: "b",
}
m := MyTestStruct{A: "a", B: c}
myMap := make(map[string]MyTestStruct)
myMap["key"] = m
err := back.Store(myMap)
if err!=nil{
t.Error("the store must be created", err)
}
result := map[string]MyTestStruct{}
err = back.Restore(&result)
if err != nil {
t.Error("unable to restore data", err)
}
restoredItem := result["key"]
if m.A != restoredItem.A {
t.Error("this should be false")
}
}