Browse Source

Add tests for db.go

master
Peter Stuifzand 3 years ago
parent
commit
7a064d33df
  1. 65
      db_test.go

65
db_test.go

@ -0,0 +1,65 @@
package main
import (
"os"
"testing"
"time"
bolt "go.etcd.io/bbolt"
)
func TestLoadEmptyMoments(t *testing.T) {
filename := "test.db"
db, err := bolt.Open(filename, 0666, nil)
if err != nil {
t.Fatalf("couldn't open db: %s: %s", filename, err)
}
defer func() {
os.Remove(filename)
}()
moments, err := loadMoments(db, "2018-01-01")
if err != nil {
t.Errorf("error while loading moments: %s", err)
}
if len(moments) != 0 {
t.Errorf("contains moments, which it should not")
}
}
func TestSaveMemoLoadMoments(t *testing.T) {
filename := "test.db"
db, err := bolt.Open(filename, 0666, nil)
if err != nil {
t.Fatalf("couldn't open db: %s: %s", filename, err)
}
defer func() {
os.Remove(filename)
}()
now := time.Now()
memo := "test"
err = saveMemo(db, now, memo)
if err != nil {
t.Errorf("error while saving a memo: %s", err)
}
prefix := now.Format("2006-01-02")
moments, err := loadMoments(db, prefix)
if err != nil {
t.Errorf("error while loading moments: %s", err)
}
if len(moments) != 1 {
t.Errorf("does not contain one moment which it should")
}
if moments[0].Time.Format(time.RFC3339) != now.Format(time.RFC3339) {
t.Errorf("%q != %q", moments[0].Time, now)
}
if moments[0].Memo != memo {
t.Errorf("%q != %q", moments[0].Memo, memo)
}
}
Loading…
Cancel
Save