-
Notifications
You must be signed in to change notification settings - Fork 0
/
persist_test.go
67 lines (60 loc) · 1.41 KB
/
persist_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
// Copyright 2020 Mohammed Salman. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package persist
import (
"fmt"
"os"
"testing"
)
func TestPut(t *testing.T) {
s := NewStore("testdb.db")
if err := s.Open(); err != nil {
t.Errorf("Failed to create/open db file %s. %s", s.path, err)
}
defer s.Close()
defer os.Remove(s.path)
if err := s.Put("key", "value"); err != nil {
t.Errorf("Failed to put to db %s. %s", s.path, err)
}
key := "key"
got, err := s.Get(key)
if err != nil {
t.Errorf("Failed to get key \"%s\" from db %s. %s", key, s.path, err)
return
}
want := "value"
if got != want {
t.Errorf("value = \"%s\"; wanted \"%s\"", got, want)
}
}
func TestGet(t *testing.T) {
s := NewStore("testdb.db")
if err := s.Open(); err != nil {
t.Errorf("Failed to create/open db file %s. %s", s.path, err)
}
defer s.Close()
defer os.Remove(s.path)
key := "key"
_, err := s.Get(key)
if err == nil {
t.Errorf("Get should return an error for \"%s\" \"%s\"", key, s.path)
return
}
}
func Example() {
// Create a new Store
s := NewStore("mydb.db")
// Open db file
Must(s.Open())
// Always close the file
defer s.Close()
// Store a value with the key "key"
if err := s.Put("key", "value"); err != nil {
fmt.Println(err)
}
// Get the value
v, _ := s.Get("key")
value := v.(string)
fmt.Println(value) // Output: value
}