pelican/internal/board/board_test.go

178 lines
4.3 KiB
Go

package board_test
import (
"fmt"
"os"
"path/filepath"
"reflect"
"testing"
"codeflow.dananglin.me.uk/apollo/canal/internal/board"
)
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)
b, err := board.Open(testDBPath)
if err != nil {
t.Fatalf("Unable to open the test database %s, %s.", testDBPath, err)
}
defer func() {
_ = b.Close()
}()
initialCardTitle := "A test card."
initialCardContent := "Ensure that this card is safely stored in the database."
expectedCardID := 1
testCreateCard(t, b, initialCardTitle, initialCardContent, expectedCardID)
testReadCard(t, b, expectedCardID, initialCardTitle, initialCardContent)
modifiedCardTitle := "Test card updated."
modifiedCardContent1 := "Ensure that this card is safely updated in the database."
testUpdateCard(t, b, expectedCardID, modifiedCardTitle, modifiedCardContent1)
modifiedCardContent2 := "Updated card content only."
testUpdateCardContent(t, b, expectedCardID, modifiedCardTitle, modifiedCardContent2)
}
func testCreateCard(t *testing.T, b board.Board, title, content string, wantID int) {
t.Helper()
args := board.CardArgs{
NewTitle: title,
NewContent: content,
}
if err := b.CreateCard(args); err != nil {
t.Fatalf("Unable to create the test card, %s.", err)
}
statusList, err := b.StatusList()
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, b board.Board, cardID int, wantTitle, wantContent string) {
t.Helper()
card, err := b.Card(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, b board.Board, cardID int, newTitle, newContent string) {
t.Helper()
args := board.UpdateCardArgs{
CardID: cardID,
CardArgs: board.CardArgs{
NewTitle: newTitle,
NewContent: newContent,
},
}
if err := b.UpdateCard(args); err != nil {
t.Fatalf("Unable to update the test card, %s", err)
}
got, err := b.Card(cardID)
if err != nil {
t.Fatalf("Unable to read the modified test card, %s", err)
}
want := board.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 testUpdateCardContent(t *testing.T, b board.Board, cardID int, expectedTitle, newContent string) {
t.Helper()
args := board.UpdateCardArgs{
CardID: cardID,
CardArgs: board.CardArgs{
NewTitle: "",
NewContent: newContent,
},
}
if err := b.UpdateCard(args); err != nil {
t.Fatalf("Unable to update the test card, %s", err)
}
got, err := b.Card(cardID)
if err != nil {
t.Fatalf("Unable to read the modified test card, %s", err)
}
want := board.Card{
ID: cardID,
Title: expectedTitle,
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
}