-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain_test.go
84 lines (64 loc) · 1.52 KB
/
main_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
package main
import (
"crypto/rand"
"encoding/hex"
"fmt"
"os"
"path/filepath"
"testing"
badger "github.com/dgraph-io/badger"
"github.com/stretchr/testify/assert"
)
func TempFileName(prefix, suffix string) string {
randBytes := make([]byte, 16)
rand.Read(randBytes)
return filepath.Join(os.TempDir(), prefix+hex.EncodeToString(randBytes)+suffix)
}
func refreshDb() (string, *badger.DB, error) {
fname := TempFileName("wonitor", "")
db, err := initDb(fname)
return fname, db, err
}
func bucketCount(db *badger.DB, name string) (int, error) {
count := 0
err := db.View(func(tx *badger.Txn) error {
it := tx.NewIterator(badger.DefaultIteratorOptions)
defer it.Close()
for it.Rewind(); it.Valid(); it.Next() {
count++
}
return nil
})
return count, err
}
func TestAdd(t *testing.T) {
fname, db, err := refreshDb()
fmt.Println(fname)
if err != nil {
panic(err)
}
defer os.RemoveAll(fname)
cnt, err := bucketCount(db, "urls")
assert.Nil(t, err)
assert.Equal(t, 0, cnt)
addUrl(db, "https://robinverton.de", false)
cnt, err = bucketCount(db, "urls")
assert.Nil(t, err)
assert.Equal(t, 1, cnt)
}
func TestRemove(t *testing.T) {
fname, db, err := refreshDb()
if err != nil {
panic(err)
}
defer os.RemoveAll(fname)
cnt, err := bucketCount(db, "urls")
assert.Nil(t, err)
assert.Equal(t, cnt, 0)
addUrl(db, "https://robinverton.de", false)
cnt, _ = bucketCount(db, "urls")
assert.Equal(t, cnt, 1)
removeUrl(db, "https://robinverton.de")
cnt, _ = bucketCount(db, "urls")
assert.Equal(t, cnt, 0)
}