1 package chromem
2
3 import (
4 "compress/gzip"
5 "encoding/gob"
6 "math/rand"
7 "os"
8 "path/filepath"
9 "reflect"
10 "testing"
11 )
12
13 func TestPersistenceWrite(t *testing.T) {
14 tempDir, err := os.MkdirTemp("", "chromem-go")
15 if err != nil {
16 t.Fatal("expected nil, got", err)
17 }
18 defer os.RemoveAll(tempDir)
19
20 type s struct {
21 Foo string
22 Bar []float32
23 }
24 obj := s{
25 Foo: "test",
26 Bar: []float32{-0.40824828, 0.40824828, 0.81649655},
27 }
28
29 t.Run("gob", func(t *testing.T) {
30 tempFilePath := tempDir + ".gob"
31 if err := persistToFile(tempFilePath, obj, false, ""); err != nil {
32 t.Fatal("expected nil, got", err)
33 }
34
35
36 _, err = os.Stat(tempFilePath)
37 if err != nil {
38 t.Fatal("expected nil, got", err)
39 }
40
41
42 f, err := os.Open(tempFilePath)
43 if err != nil {
44 t.Fatal("expected nil, got", err)
45 }
46 defer f.Close()
47 d := gob.NewDecoder(f)
48 res := s{}
49 err = d.Decode(&res)
50 if err != nil {
51 t.Fatal("expected nil, got", err)
52 }
53
54
55 if !reflect.DeepEqual(obj, res) {
56 t.Fatalf("expected %+v, got %+v", obj, res)
57 }
58 })
59
60 t.Run("gob gzipped", func(t *testing.T) {
61 tempFilePath := tempDir + ".gob.gz"
62 if err := persistToFile(tempFilePath, obj, true, ""); err != nil {
63 t.Fatal("expected nil, got", err)
64 }
65
66
67 _, err = os.Stat(tempFilePath)
68 if err != nil {
69 t.Fatal("expected nil, got", err)
70 }
71
72
73 f, err := os.Open(tempFilePath)
74 if err != nil {
75 t.Fatal("expected nil, got", err)
76 }
77 defer f.Close()
78 gzr, err := gzip.NewReader(f)
79 if err != nil {
80 t.Fatal("expected nil, got", err)
81 }
82 d := gob.NewDecoder(gzr)
83 res := s{}
84 err = d.Decode(&res)
85 if err != nil {
86 t.Fatal("expected nil, got", err)
87 }
88
89
90 if !reflect.DeepEqual(obj, res) {
91 t.Fatalf("expected %+v, got %+v", obj, res)
92 }
93 })
94 }
95
96 func TestPersistenceRead(t *testing.T) {
97 tempDir, err := os.MkdirTemp("", "chromem-go")
98 if err != nil {
99 t.Fatal("expected nil, got", err)
100 }
101 defer os.RemoveAll(tempDir)
102
103 type s struct {
104 Foo string
105 Bar []float32
106 }
107 obj := s{
108 Foo: "test",
109 Bar: []float32{-0.40824828, 0.40824828, 0.81649655},
110 }
111
112 t.Run("gob", func(t *testing.T) {
113 tempFilePath := tempDir + ".gob"
114 f, err := os.Create(tempFilePath)
115 if err != nil {
116 t.Fatal("expected nil, got", err)
117 }
118 enc := gob.NewEncoder(f)
119 err = enc.Encode(obj)
120 if err != nil {
121 t.Fatal("expected nil, got", err)
122 }
123 err = f.Close()
124 if err != nil {
125 t.Fatal("expected nil, got", err)
126 }
127
128
129 var res s
130 err = readFromFile(tempFilePath, &res, "")
131 if err != nil {
132 t.Fatal("expected nil, got", err)
133 }
134
135
136 if !reflect.DeepEqual(obj, res) {
137 t.Fatalf("expected %+v, got %+v", obj, res)
138 }
139 })
140
141 t.Run("gob gzipped", func(t *testing.T) {
142 tempFilePath := tempDir + ".gob.gz"
143 f, err := os.Create(tempFilePath)
144 if err != nil {
145 t.Fatal("expected nil, got", err)
146 }
147 gzw := gzip.NewWriter(f)
148 enc := gob.NewEncoder(gzw)
149 err = enc.Encode(obj)
150 if err != nil {
151 t.Fatal("expected nil, got", err)
152 }
153 err = gzw.Close()
154 if err != nil {
155 t.Fatal("expected nil, got", err)
156 }
157 err = f.Close()
158 if err != nil {
159 t.Fatal("expected nil, got", err)
160 }
161
162
163 var res s
164 err = readFromFile(tempFilePath, &res, "")
165 if err != nil {
166 t.Fatal("expected nil, got", err)
167 }
168
169
170 if !reflect.DeepEqual(obj, res) {
171 t.Fatalf("expected %+v, got %+v", obj, res)
172 }
173 })
174 }
175
176 func TestPersistenceEncryption(t *testing.T) {
177
178
179
180 r := rand.New(rand.NewSource(rand.Int63()))
181
182 path := filepath.Join(os.TempDir(), "a", "chromem-go")
183
184
185 type s struct {
186 Foo string
187 Bar []float32
188 }
189 obj := s{
190 Foo: "test",
191 Bar: []float32{-0.40824828, 0.40824828, 0.81649655},
192 }
193 encryptionKey := randomString(r, 32)
194
195 tt := []struct {
196 name string
197 filePath string
198 compress bool
199 }{
200 {
201 name: "compress false",
202 filePath: path + ".gob.enc",
203 compress: false,
204 },
205 {
206 name: "compress true",
207 filePath: path + ".gob.gz.enc",
208 compress: true,
209 },
210 }
211
212 for _, tc := range tt {
213 t.Run(tc.name, func(t *testing.T) {
214 err := persistToFile(tc.filePath, obj, tc.compress, encryptionKey)
215 if err != nil {
216 t.Fatal("expected nil, got", err)
217 }
218
219
220 _, err = os.Stat(tc.filePath)
221 if err != nil {
222 t.Fatal("expected nil, got", err)
223 }
224
225
226 var res s
227 err = readFromFile(tc.filePath, &res, encryptionKey)
228 if err != nil {
229 t.Fatal("expected nil, got", err)
230 }
231
232
233 if !reflect.DeepEqual(obj, res) {
234 t.Fatalf("expected %+v, got %+v", obj, res)
235 }
236 })
237 }
238 }
239
View as plain text