package board_test import ( "os" "path/filepath" "reflect" "testing" "codeflow.dananglin.me.uk/apollo/pelican/internal/board" ) func TestStatusLifecycle(t *testing.T) { t.Log("Testing the lifecycle of a status.") projectDir, err := projectRoot() if err != nil { t.Fatalf(err.Error()) } testDBPath := filepath.Join(projectDir, "test", "databases", "Board_TestStatusLifecycle.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() }() // We've opened a new board with the default list of statuses: To Do, Doing, Done statusOnHoldName := "On Hold" statusOnHoldExpectedID := 4 statusOnHoldOrder := 4 t.Run("Test Create Status", testCreateStatus(kanban, statusOnHoldName, statusOnHoldOrder)) t.Run("Test Read Status", testReadStatus(kanban, statusOnHoldExpectedID, statusOnHoldName)) t.Run("Test Status Update", testUpdateStatus(kanban, 2, "In Progress")) // Rearrange the board so the order is To Do, On Hold, In Progress, Done // Move a Card ID from To Do to In Progress t.Run("Test Move Card To Status", testMoveCardToStatus(kanban)) } func testCreateStatus(kanban board.Board, name string, order int) func(t *testing.T) { return func(t *testing.T) { t.Log("When the status is created and saved to the database.") args := board.StatusArgs{ Name: name, Order: order, } if err := kanban.CreateStatus(args); err != nil { t.Fatalf("ERROR: Unable to create the new status, %v.", err) } t.Logf("%s\tStatus '%s' was successfully saved to the database.", success, name) } } func testReadStatus(kanban board.Board, statusID int, wantName string) func(t *testing.T) { return func(t *testing.T) { t.Log("When the status is read from the database.") status, err := kanban.Status(statusID) if err != nil { t.Fatalf("ERROR: Unable to read the test status, %v", err) } if status.Name != wantName { t.Errorf("%s\tUnexpected status received from the database, want: '%s', got: '%s'", failure, wantName, status.Name) } else { t.Logf("%s\tSuccessfully received the '%s' status from the database.", success, status.Name) } } } func testUpdateStatus(kanban board.Board, statusID int, newName string) func(t *testing.T) { return func(t *testing.T) { t.Log("When the status' name is updated in the database.") args := board.UpdateStatusArgs{ StatusID: statusID, StatusArgs: board.StatusArgs{ Name: newName, Order: -1, }, } if err := kanban.UpdateStatus(args); err != nil { t.Fatalf("ERROR: Unable to update the status, %v", err) } else { t.Logf("%s\tStatus successfully updated.", success) } t.Log("\tVerifying the new status...") status, err := kanban.Status(statusID) if err != nil { t.Fatalf("ERROR: Unable to retrieve the status from the database, %v", err) } want := board.Status{ ID: statusID, Name: newName, CardIds: nil, Position: 2, } if !reflect.DeepEqual(status, want) { t.Errorf("%s\tUnexpected status received from the database, want: %+v, got: %+v", failure, want, status) } else { t.Logf("%s\tExpected status name received from the database, got: %+v", success, status) } } } // func testRearrangeBoard() func(t *testing.T) { // return func(t *testing.T) { // } // } func testMoveCardToStatus(kanban board.Board) func(t *testing.T) { return func(t *testing.T) { t.Log("When moving a card between statuses.") title := "Test card." cardArgs := board.CardArgs{NewTitle: title, NewDescription: ""} cardID, err := kanban.CreateCard(cardArgs) if err != nil { t.Fatalf("ERROR: Unable to create the card in the database, %v", err) } statusList, err := kanban.StatusList() if err != nil { t.Fatalf("ERROR: Unable to retrieve the list of statuses from the database, %v", err) } status0, status2 := statusList[0], statusList[2] moveArgs := board.MoveToStatusArgs{CardID: cardID, CurrentStatusID: status0.ID, NextStatusID: status2.ID} if err := kanban.MoveToStatus(moveArgs); err != nil { t.Fatalf("ERROR: Unable to move the Card ID from '%s' to '%s', %v", status0.Name, status2.Name, err) } t.Logf("\tVerifying that the card has moved to '%s'...", status2.Name) statusList, err = kanban.StatusList() if err != nil { t.Fatalf("ERROR: Unable to retrieve the list of statuses from the database, %v", err) } status0, status2 = statusList[0], statusList[2] if len(status0.CardIds) != 0 { t.Errorf("%s\tUnexpected number of card IDs found in '%s', want: 0, got: %d", failure, status0.Name, len(status0.CardIds)) } else { t.Logf("%s\tThe number of card IDs in '%s' is now %d", success, status0.Name, len(status0.CardIds)) } if len(status2.CardIds) != 1 { t.Errorf("%s\tUnexpected number of card IDs found in '%s', want: 1, got: %d", failure, status2.Name, len(status2.CardIds)) } else { t.Logf("%s\tThe number of card IDs in '%s' is now %d", success, status2.Name, len(status2.CardIds)) } card, err := kanban.Card(status2.CardIds[0]) if err != nil { t.Fatalf("ERROR: Unable to retrieve the card from the database, %v", err) } if card.Title != title { t.Errorf("%s\tUnexpected card title found in '%s', want: '%s', got: '%s'", success, status2.Name, title, card.Title) } else { t.Logf("%s\tExpected card title found in '%s', got: '%s'", success, status2.Name, card.Title) } } }