Browse Source

Clean up tests

Signed-off-by: jolheiser <john.olheiser@gmail.com>
main
jolheiser 1 year ago
parent
commit
c3d7ddbbd7
No known key found for this signature in database GPG Key ID: 83E486E71AFEB820
  1. 17
      example_key_test.go
  2. 17
      example_seq_test.go
  3. 27
      serial_test.go
  4. 6
      shock_test.go
  5. 48
      store_test.go

17
example_key_test.go

@ -2,7 +2,6 @@ package shock
import (
"fmt"
"io/ioutil"
"os"
"path"
)
@ -14,19 +13,14 @@ type TestUserSettings struct {
}
func ExampleTestUserSettings() {
dir, err := ioutil.TempDir(os.TempDir(), "shock")
if err != nil {
panic(err)
}
dbPath := path.Join(dir, "shock.db")
db, err = Open(dbPath, os.ModePerm, DefaultOptions)
dbPath := path.Join(tmpDir, "key.db")
keyDB, err := Open(dbPath, os.ModePerm, DefaultOptions)
if err != nil {
panic(err)
}
// Get a bucket to work with instead of specifying each time
bucket := db.Bucket("test-user-settings")
bucket := keyDB.Bucket("test-user-settings")
if err := bucket.Init(); err != nil {
panic(err)
}
@ -46,10 +40,7 @@ func ExampleTestUserSettings() {
panic(err)
}
if err := db.Bolt.Close(); err != nil {
if err := keyDB.Bolt.Close(); err != nil {
fmt.Printf("Could not close DB %s: %v\n", dbPath, err)
}
if err := os.RemoveAll(dir); err != nil {
fmt.Printf("Could not delete temp dir %s: %v\n", dir, err)
}
}

17
example_seq_test.go

@ -2,7 +2,6 @@ package shock
import (
"fmt"
"io/ioutil"
"os"
"path"
)
@ -21,19 +20,14 @@ func (t *TestUser) AssignSeq(seq uint64) {
}
func ExampleTestUser() {
dir, err := ioutil.TempDir(os.TempDir(), "shock")
if err != nil {
panic(err)
}
dbPath := path.Join(dir, "shock.db")
db, err = Open(dbPath, os.ModePerm, DefaultOptions)
dbPath := path.Join(tmpDir, "seq.db")
seqDB, err := Open(dbPath, os.ModePerm, DefaultOptions)
if err != nil {
panic(err)
}
// Get a bucket to work with instead of specifying each time
bucket := db.Bucket("test-user")
bucket := seqDB.Bucket("test-user")
if err := bucket.Init(); err != nil {
panic(err)
}
@ -54,10 +48,7 @@ func ExampleTestUser() {
panic(err)
}
if err := db.Bolt.Close(); err != nil {
if err := seqDB.Bolt.Close(); err != nil {
fmt.Printf("Could not close DB %s: %v\n", dbPath, err)
}
if err := os.RemoveAll(dir); err != nil {
fmt.Printf("Could not delete temp dir %s: %v\n", dir, err)
}
}

27
serial_test.go

@ -11,7 +11,7 @@ import (
func TestGob(t *testing.T) {
dbPath := path.Join(tmpDir, "gob.db")
db, err := Open(dbPath, os.ModePerm, &Options{
gobDB, err := Open(dbPath, os.ModePerm, &Options{
Bolt: bbolt.DefaultOptions,
Serializer: GobSerializer{},
})
@ -19,34 +19,15 @@ func TestGob(t *testing.T) {
panic(err)
}
bucket := db.Bucket("test")
bucket := gobDB.Bucket("gob")
if err := bucket.Init(); err != nil {
t.Log(err)
t.FailNow()
}
for _, tc := range tt {
t.Run(tc.Name, func(t *testing.T) {
if err := bucket.Put(tc); err != nil {
t.Log(err)
t.FailNow()
}
testStore(t, bucket)
var tcc TestUser
err := bucket.Get(tc.ID, &tcc)
if err != nil {
t.Log(err)
t.FailNow()
}
if !tcc.Equal(*tc) {
t.Log("Serialized struct is not the same")
t.FailNow()
}
})
}
if err := db.Bolt.Close(); err != nil {
if err := gobDB.Bolt.Close(); err != nil {
fmt.Printf("Could not close DB %s: %v\n", dbPath, err)
}
}

6
shock_test.go

@ -9,7 +9,7 @@ import (
)
var (
db *DB
testDB *DB
tmpDir string
)
@ -21,14 +21,14 @@ func TestMain(m *testing.M) {
}
dbPath := path.Join(tmpDir, "shock.db")
db, err = Open(dbPath, os.ModePerm, nil)
testDB, err = Open(dbPath, os.ModePerm, nil)
if err != nil {
panic(err)
}
exit := m.Run()
if err := db.Bolt.Close(); err != nil {
if err := testDB.Bolt.Close(); err != nil {
fmt.Printf("Could not close DB %s: %v\n", dbPath, err)
}
if err := os.RemoveAll(tmpDir); err != nil {

48
store_test.go

@ -35,32 +35,13 @@ func TestStore(t *testing.T) {
},
}
bucket := db.Bucket("test")
bucket := testDB.Bucket("store")
if err := bucket.Init(); err != nil {
t.Log(err)
t.FailNow()
}
for _, tc := range tt {
t.Run(tc.Name, func(t *testing.T) {
if err := bucket.Put(tc); err != nil {
t.Log(err)
t.FailNow()
}
var tcc TestUser
err := bucket.Get(tc.ID, &tcc)
if err != nil {
t.Log(err)
t.FailNow()
}
if !tcc.Equal(*tc) {
t.Log("Serialized struct is not the same")
t.FailNow()
}
})
}
testStore(t, bucket)
t.Run("count", func(t *testing.T) {
count, err := bucket.Count()
@ -79,7 +60,7 @@ func TestStore(t *testing.T) {
list := make([]TestUser, 0, len(tt))
if err := bucket.ViewEach(func(_, serial []byte) error {
var t TestUser
if err := db.Serializer.Unmarshal(serial, &t); err != nil {
if err := testDB.Serializer.Unmarshal(serial, &t); err != nil {
return err
}
list = append(list, t)
@ -119,6 +100,29 @@ func TestStore(t *testing.T) {
}
}
func testStore(t *testing.T, bucket *Bucket) {
for _, tc := range tt {
t.Run(tc.Name, func(t *testing.T) {
if err := bucket.Put(tc); err != nil {
t.Log(err)
t.FailNow()
}
var tcc TestUser
err := bucket.Get(tc.ID, &tcc)
if err != nil {
t.Log(err)
t.FailNow()
}
if !tcc.Equal(*tc) {
t.Log("Serialized struct is not the same")
t.FailNow()
}
})
}
}
func (t TestUser) Equal(tt TestUser) bool {
return t.ID == tt.ID && t.Name == tt.Name &&
t.Age == tt.Age && t.Admin == tt.Admin

Loading…
Cancel
Save