158 lines
4.1 KiB
Go
158 lines
4.1 KiB
Go
package board_test
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"codeflow.dananglin.me.uk/apollo/canal/internal/board"
|
|
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.OpenProject(testDBPath)
|
|
if err != nil {
|
|
t.Fatalf("Unable to open the test database %s, %s.", testDBPath, err)
|
|
}
|
|
|
|
defer func() {
|
|
_ = db.Close()
|
|
}()
|
|
|
|
initialCardTitle := "A test card."
|
|
initialCardContent := "Ensure that this card is safely stored in the database."
|
|
expectedCardID := 1
|
|
|
|
testCreateCard(t, db, initialCardTitle, initialCardContent, expectedCardID)
|
|
testReadCard(t, db, expectedCardID, initialCardTitle, initialCardContent)
|
|
|
|
modifiedCardTitle := "Test card updated."
|
|
modifiedCardContent1 := "Ensure that this card is safely updated in the database."
|
|
|
|
testUpdateCard(t, db, expectedCardID, modifiedCardTitle, modifiedCardContent1)
|
|
|
|
modifiedCardContent2 := "Updated card content only."
|
|
|
|
testUpdateCardContent(t, db, expectedCardID, modifiedCardTitle, modifiedCardContent2)
|
|
}
|
|
|
|
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 := 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, db *bolt.DB, cardID int, expectedTitle, newContent string) {
|
|
t.Helper()
|
|
|
|
if err := board.UpdateCard(db, cardID, "", 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 := 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
|
|
}
|