177 lines
4.9 KiB
Go
177 lines
4.9 KiB
Go
package board_test
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"codeflow.dananglin.me.uk/apollo/pelican/internal/board"
|
|
)
|
|
|
|
func TestCardLifecycle(t *testing.T) {
|
|
t.Log("Testing the lifecycle of a card.")
|
|
|
|
projectDir, err := projectRoot()
|
|
if err != nil {
|
|
t.Fatalf(err.Error())
|
|
}
|
|
|
|
testDBPath := filepath.Join(projectDir, "test", "databases", "Board_TestCardLifecycle.db")
|
|
os.Remove(testDBPath)
|
|
|
|
kanban, err := board.Open(testDBPath)
|
|
if err != nil {
|
|
t.Fatalf("Unable to open the test Kanban board, %s.", err)
|
|
}
|
|
|
|
defer func() {
|
|
_ = kanban.Close()
|
|
}()
|
|
|
|
initialCardTitle := "A test card."
|
|
initialCardContent := "Ensure that this card is safely stored in the database."
|
|
expectedCardID := 1
|
|
|
|
t.Run("Test Create Card", testCreateCard(kanban, initialCardTitle, initialCardContent, expectedCardID))
|
|
|
|
t.Run("Test Read Card", testReadCard(kanban, expectedCardID, initialCardTitle, initialCardContent))
|
|
|
|
modifiedCardTitle := "Test card updated."
|
|
modifiedCardContent1 := "Ensure that this card is safely updated in the database."
|
|
|
|
t.Run("Test Update Card", testUpdateCard(kanban, expectedCardID, modifiedCardTitle, modifiedCardContent1))
|
|
|
|
modifiedCardContent2 := "Updated card content only."
|
|
|
|
t.Run("Test Update Card Content", testUpdateCardContent(kanban, expectedCardID, modifiedCardTitle, modifiedCardContent2))
|
|
}
|
|
|
|
func testCreateCard(kanban board.Board, title, content string, wantID int) func(t *testing.T) {
|
|
return func(t *testing.T) {
|
|
t.Log("When the card is created and saved to the database.")
|
|
|
|
args := board.CardArgs{
|
|
NewTitle: title,
|
|
NewContent: content,
|
|
}
|
|
|
|
if _, err := kanban.CreateCard(args); err != nil {
|
|
t.Fatalf("ERROR: Unable to create the test card, %s.", err)
|
|
}
|
|
|
|
statusList, err := kanban.StatusList()
|
|
if err != nil {
|
|
t.Fatalf("ERROR: Unable to run `ReadStatusList`, %s.", err)
|
|
}
|
|
|
|
if len(statusList) == 0 {
|
|
t.Fatal("ERROR: The status list appears to be empty.")
|
|
}
|
|
|
|
cardIDs := statusList[0].CardIds
|
|
|
|
if len(cardIDs) != 1 {
|
|
t.Fatalf("ERROR: Unexpected number of cards in the default status, want: %d, got %d.", 1, len(cardIDs))
|
|
}
|
|
|
|
if gotID := cardIDs[0]; wantID != gotID {
|
|
t.Errorf("%s\tUnexpected card ID found in the default status, want: %d, got %d.", failure, wantID, gotID)
|
|
} else {
|
|
t.Logf("%s\tExpected card ID found in the default status, got %d.", success, gotID)
|
|
}
|
|
}
|
|
}
|
|
|
|
func testReadCard(kanban board.Board, cardID int, wantTitle, wantContent string) func(t *testing.T) {
|
|
return func(t *testing.T) {
|
|
t.Log("When a card is read from the database.")
|
|
|
|
card, err := kanban.Card(cardID)
|
|
if err != nil {
|
|
t.Fatalf("ERROR: Unable to read test card, %s.", err)
|
|
}
|
|
|
|
if card.Title != wantTitle {
|
|
t.Errorf("%s\tUnexpected card title received, want: %s, got: %s.", failure, wantTitle, card.Title)
|
|
} else {
|
|
t.Logf("%s\tExpected card title received, got: %s.", success, card.Title)
|
|
}
|
|
|
|
if card.Content != wantContent {
|
|
t.Errorf("%s\tUnexpected card content received, want: %s, got: %s.", failure, wantContent, card.Content)
|
|
} else {
|
|
t.Logf("%s\tExpected card content received, got: %s.", success, card.Content)
|
|
}
|
|
}
|
|
}
|
|
|
|
func testUpdateCard(kanban board.Board, cardID int, newTitle, newContent string) func(t *testing.T) {
|
|
return func(t *testing.T) {
|
|
t.Log("When a card is updated in the database.")
|
|
|
|
args := board.UpdateCardArgs{
|
|
CardID: cardID,
|
|
CardArgs: board.CardArgs{
|
|
NewTitle: newTitle,
|
|
NewContent: newContent,
|
|
},
|
|
}
|
|
|
|
if err := kanban.UpdateCard(args); err != nil {
|
|
t.Fatalf("ERROR: Unable to update the test card, %s", err)
|
|
}
|
|
|
|
got, err := kanban.Card(cardID)
|
|
if err != nil {
|
|
t.Fatalf("ERROR: 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("%s\tUnexpected card read from the database: want %+v, got %+v", failure, want, got)
|
|
} else {
|
|
t.Logf("%s\tExpected card read from the database: got %+v", success, got)
|
|
}
|
|
}
|
|
}
|
|
|
|
func testUpdateCardContent(kanban board.Board, cardID int, expectedTitle, newContent string) func(t *testing.T) {
|
|
return func(t *testing.T) {
|
|
t.Log("When (and only when) a card's content is updated in the database.")
|
|
|
|
args := board.UpdateCardArgs{
|
|
CardID: cardID,
|
|
CardArgs: board.CardArgs{
|
|
NewTitle: "",
|
|
NewContent: newContent,
|
|
},
|
|
}
|
|
|
|
if err := kanban.UpdateCard(args); err != nil {
|
|
t.Fatalf("ERROR: Unable to update the test card, %s", err)
|
|
}
|
|
|
|
got, err := kanban.Card(cardID)
|
|
if err != nil {
|
|
t.Fatalf("ERROR: 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("%s\tUnexpected card read from the database, want: %+v, got: %+v", failure, want, got)
|
|
} else {
|
|
t.Logf("%s\tExpected card read from the database, got: %+v", success, got)
|
|
}
|
|
}
|
|
}
|