pelican/db_test.go
Dan Anglin c336fcd82b
feat: add database functions
Add database functions to read and write statuses to the status bucket
in a BoltDB database.

Add database functions to read and write cards to the card bucket.

Use encoding/gob to encode the Status and Card values before writing
them to the database.
2021-08-16 01:56:56 +01:00

201 lines
4.8 KiB
Go

package main
import (
"fmt"
"os"
"reflect"
"testing"
bolt "go.etcd.io/bbolt"
)
func TestEnsureBuckets(t *testing.T) {
var db *bolt.DB
var err error
testDB := "test/TestEnsureBuckets.db"
os.Remove(testDB)
if db, err = openDatabase(testDB); err != nil {
t.Fatalf("An error occurred while opening the test database %s, %s.", testDB, err)
}
defer db.Close()
for i := 0; i < 3; i++ {
if err := ensureBuckets(db); err != nil {
t.Fatalf("An error occurred while executing `ensureBuckets`, %s.", err)
}
}
expectedBuckets := []string{statusBucket, cardBucket}
if err = db.View(func(tx *bolt.Tx) error {
for _, b := range expectedBuckets {
if bucket := tx.Bucket([]byte(b)); bucket == nil {
return fmt.Errorf("bucket %s does not exist in the database", b)
}
}
return nil
}); err != nil {
t.Fatalf("An error occurred whilst checking for the buckets, %s.", err)
}
}
func TestReadAndWriteStatuses(t *testing.T) {
var db *bolt.DB
var err error
testDB := "test/TestWriteAndReadStatuses.db"
os.Remove(testDB)
if db, err = openDatabase(testDB); err != nil {
t.Fatalf("An error occurred whilst opening the test database %s, %s.", testDB, err)
}
defer db.Close()
if err = ensureBuckets(db); err != nil {
t.Fatalf("An error occurred whilst executing `ensureBuckets`, %s.", err)
}
// Begin with an initial list of statuses
initialStatusList := []Status{
{
Name: "Backlog",
CardIds: []int{1, 14, 9, 10},
},
{
Name: "Next",
CardIds: []int{2, 5, 12},
},
{
Name: "In progress",
CardIds: []int{3},
},
{
Name: "Finished!",
CardIds: []int{4, 6, 7, 8, 11, 13},
},
}
// save the initial statuses to the board bucket.
if err = saveStatuses(db, initialStatusList); err != nil {
t.Fatalf("An error occurred whilst writing the initial status list to the database, %s", err)
}
// load the status list from the database, modify it a little and write it to the database.
var modifiedStatusList []Status
if modifiedStatusList, err = loadAllStatuses(db); err != nil {
t.Fatalf("An error occurred whilst reading the initial status list to the database, %s", err)
}
modifiedStatusList[2].CardIds = []int{3, 14}
archiveStatus := Status{
Name: "Archived",
CardIds: []int{34, 51, 894},
}
modifiedStatusList = append(modifiedStatusList, archiveStatus)
if err := saveStatuses(db, modifiedStatusList); err != nil {
t.Fatalf("An error occurred whilst writing the modified status list to the database, %s", err)
}
// read the final status list from the database and compare to the expected status list.
want := []Status{
{
Id: 1,
Name: "Backlog",
CardIds: []int{1, 14, 9, 10},
},
{
Id: 2,
Name: "Next",
CardIds: []int{2, 5, 12},
},
{
Id: 3,
Name: "In progress",
CardIds: []int{3, 14},
},
{
Id: 4,
Name: "Finished!",
CardIds: []int{4, 6, 7, 8, 11, 13},
},
{
Id: 5,
Name: "Archived",
CardIds: []int{34, 51, 894},
},
}
var got []Status
if got, err = loadAllStatuses(db); err != nil {
t.Fatalf("An error occurred whilst reading the modified status list from the database, %s", err)
}
if !reflect.DeepEqual(got, want) {
t.Errorf("Unexpected status list read from the database: got %+v, want %+v", got, want)
} else {
t.Logf("Expected status list read from the database: got %+v", got)
}
}
func TestReadAndWriteCards(t *testing.T) {
var db *bolt.DB
var err error
testDB := "test/TestReadWriteCards.db"
os.Remove(testDB)
if db, err = openDatabase(testDB); err != nil {
t.Fatalf("An error occurred whilst opening the test database %s, %s.", testDB, err)
}
if err = ensureBuckets(db); err != nil {
t.Fatalf("An error occurred whilst executing `ensureBuckets`, %s.", err)
}
initialCard := Card{
Title: "A test task.",
Content: "This task should be completed.",
}
// Save the card
cardId, err := saveCard(db, initialCard)
if err != nil {
t.Fatalf("An error occurred whilst saving the initial card to the database, %s", err)
}
modifiedCard, err := loadCard(db, cardId)
if err != nil {
t.Fatalf("An error occurred whilst loading the card from the database, %s", err)
}
modifiedCard.Title = "Task: Foo bar baz."
modifiedCardId, err := saveCard(db, modifiedCard)
if err != nil {
t.Fatalf("An error occurred whilst saving the modified card to the database, %s", err)
}
want := Card{
Id: 1,
Title: "Task: Foo bar baz.",
Content: "This task should be completed.",
}
got, err := loadCard(db, modifiedCardId)
if err != nil {
t.Fatalf("An error occurred whilst loading the modified from the database, %s", err)
}
if !reflect.DeepEqual(got, want) {
t.Errorf("Unexpected card read from the database: got %+v, want %+v", got, want)
} else {
t.Logf("Expected card read from the database: got %+v", got)
}
}