-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathboilerplate_test.go
87 lines (64 loc) · 2.43 KB
/
boilerplate_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
81
82
83
84
85
86
87
package boltdbboilerplate
import (
"log"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func TestMain(m *testing.M) {
initTest() //setup
retCode := m.Run()
cleanBoltDB() //Teardown
os.Exit(retCode)
}
func initTest() {
log.Println("Init BoltDB")
buckets := []string{"owner", "sensors", "beacons"}
err := InitBolt("./test.boltdb", buckets)
log.Println("After init db")
if err != nil {
log.Fatal("Can't init boltDB")
}
}
func cleanBoltDB() {
os.Remove("./test.boltdb")
}
func TestInitBolt(t *testing.T) {
_, err := os.Stat("./test.boltdb")
assert.Nil(t, err, "test.boltdb should exist")
}
func TestPut(t *testing.T) {
err := Put([]byte("owner"), []byte("owner"), []byte("testUserName"))
assert.Nil(t, err, "There should be no errors putting value")
value := Get([]byte("owner"), []byte("owner"))
assert.Equal(t, "testUserName", string(value), "value should be testUserName")
value = Get([]byte("owner"), []byte("nil"))
assert.Nil(t, value, "value should be nil")
}
func TestDelete(t *testing.T) {
err := Put([]byte("owner"), []byte("owner"), []byte("testUserName"))
err = Delete([]byte("owner"), []byte("owner"))
assert.Nil(t, err, "There should be no errors deleting")
value := Get([]byte("owner"), []byte("owner"))
assert.Nil(t, value, "value should be nil")
}
func TestGetAllKeys(t *testing.T) {
err := Put([]byte("sensors"), []byte("key1"), []byte("value1"))
err = Put([]byte("sensors"), []byte("key2"), []byte("value2"))
keys := GetAllKeys([]byte("sensors"))
assert.Nil(t, err, "There should be no errors getting all keys")
assert.Equal(t, 2, len(keys), "there should be 2 keys")
assert.Equal(t, "key1", string(keys[0]), "first key should be key1")
assert.Equal(t, "key2", string(keys[1]), "second key should be key2")
}
func TestGetAllKeyValuePairs(t *testing.T) {
err := Put([]byte("sensors"), []byte("key1"), []byte("value1"))
err = Put([]byte("sensors"), []byte("key2"), []byte("value2"))
pairs := GetAllKeyValues([]byte("sensors"))
assert.Nil(t, err, "There should be no errors getting all boltpairs")
assert.Equal(t, 2, len(pairs), "there should be 2 paris")
assert.Equal(t, "key1", string(pairs[0].Key), "key of first pair should be key1")
assert.Equal(t, "value1", string(pairs[0].Value), "value of first pair should be value1")
assert.Equal(t, "key2", string(pairs[1].Key), "key of first pair should be key2")
assert.Equal(t, "value2", string(pairs[1].Value), "value of first pair should be value2")
}