pelican/internal/board/board_test.go
Dan Anglin 3e5cd598d0
refactor: project restructure
- Introduced internal packages for different components of the kanban
  board.
- Simplified and refactored the database tests based on linting feedback.
- Add tests for the board package.
2021-09-18 01:03:09 +01:00

130 lines
3.3 KiB
Go

package board_test
import (
"fmt"
"os"
"path/filepath"
"reflect"
"testing"
"forge.dananglin.me.uk/code/dananglin/pelican/internal/board"
"forge.dananglin.me.uk/code/dananglin/pelican/internal/card"
bolt "go.etcd.io/bbolt"
)
func TestCardLifecycle(t *testing.T) {
t.Parallel()
projectDir, err := projectRoot()
if err != nil {
t.Fatalf(err.Error())
}
testDBPath := filepath.Join(projectDir, "test", "databases", "Board_TestCardLifecycle.db")
os.Remove(testDBPath)
db, err := board.LoadBoard(testDBPath)
if err != nil {
t.Fatalf("Unable to open the test database %s, %s.", testDBPath, err)
}
defer func() {
_ = db.Close()
}()
cardTitle := "A test card."
cardContent := "Ensure that this card is safely stored in the database."
expectedCardID := 1
testCreateCard(t, db, cardTitle, cardContent, expectedCardID)
testReadCard(t, db, expectedCardID, cardTitle, cardContent)
newCardTitle := "Test card updated."
newCardContent := "Ensure that this card is safely updated in the database."
testUpdateCard(t, db, expectedCardID, newCardTitle, newCardContent)
}
func testCreateCard(t *testing.T, db *bolt.DB, title, content string, wantID int) {
t.Helper()
if err := board.CreateCard(db, title, content); err != nil {
t.Fatalf("Unable to create the test card, %s.", err)
}
statusList, err := board.ReadStatusList(db)
if err != nil {
t.Fatalf("Unable to run `ReadStatusList`, %s.", err)
}
if len(statusList) == 0 {
t.Fatal("The status list appears to be empty.")
}
cardIDs := statusList[0].CardIds
if len(cardIDs) != 1 {
t.Fatalf("Unexpected number of cards in the default status, want: %d, got %d.", 1, len(cardIDs))
}
if gotID := cardIDs[0]; wantID != gotID {
t.Errorf("Unexpected card ID found in the default status, want: %d, got %d.", wantID, gotID)
} else {
t.Logf("Expected card ID found in the default status, got %d.", gotID)
}
}
func testReadCard(t *testing.T, db *bolt.DB, cardID int, wantTitle, wantContent string) {
t.Helper()
card, err := board.ReadCard(db, cardID)
if err != nil {
t.Fatalf("Unable to read test card, %s.", err)
}
if card.Title != wantTitle {
t.Errorf("Unexpected card title received, want: %s, got: %s.", wantTitle, card.Title)
} else {
t.Logf("Expected card title received, got: %s.", card.Title)
}
if card.Content != wantContent {
t.Errorf("Unexpected card content received, want: %s, got: %s.", wantContent, card.Content)
} else {
t.Logf("Expected card title received, got: %s.", card.Content)
}
}
func testUpdateCard(t *testing.T, db *bolt.DB, cardID int, newTitle, newContent string) {
t.Helper()
if err := board.UpdateCard(db, cardID, newTitle, newContent); err != nil {
t.Fatalf("Unable to update the test card, %s", err)
}
got, err := board.ReadCard(db, cardID)
if err != nil {
t.Fatalf("Unable to read the modified test card, %s", err)
}
want := card.Card{
ID: cardID,
Title: newTitle,
Content: newContent,
}
if !reflect.DeepEqual(got, want) {
t.Errorf("Unexpected card read from the database: want %+v, got %+v", want, got)
} else {
t.Logf("Expected card read from the database: got %+v", got)
}
}
func projectRoot() (string, error) {
cwd, err := os.Getwd()
if err != nil {
return "", fmt.Errorf("unable to get the current working directory, %w", err)
}
return filepath.Join(cwd, "..", ".."), nil
}