feat: add timestamp for card creation #9
3 changed files with 21 additions and 6 deletions
|
@ -5,6 +5,7 @@ import (
|
||||||
"encoding/gob"
|
"encoding/gob"
|
||||||
"fmt"
|
"fmt"
|
||||||
"sort"
|
"sort"
|
||||||
|
"time"
|
||||||
|
|
||||||
"codeflow.dananglin.me.uk/apollo/pelican/internal/db"
|
"codeflow.dananglin.me.uk/apollo/pelican/internal/db"
|
||||||
bolt "go.etcd.io/bbolt"
|
bolt "go.etcd.io/bbolt"
|
||||||
|
@ -205,6 +206,8 @@ type CardArgs struct {
|
||||||
|
|
||||||
// CreateCard creates a card in the database.
|
// CreateCard creates a card in the database.
|
||||||
func (b *Board) CreateCard(args CardArgs) (int, error) {
|
func (b *Board) CreateCard(args CardArgs) (int, error) {
|
||||||
|
timestamp := time.Now().Format(time.DateOnly)
|
||||||
|
|
||||||
statusList, err := b.StatusList()
|
statusList, err := b.StatusList()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, fmt.Errorf("unable to read the status list, %w", err)
|
return 0, fmt.Errorf("unable to read the status list, %w", err)
|
||||||
|
@ -218,6 +221,7 @@ func (b *Board) CreateCard(args CardArgs) (int, error) {
|
||||||
ID: -1,
|
ID: -1,
|
||||||
Title: args.NewTitle,
|
Title: args.NewTitle,
|
||||||
Content: args.NewContent,
|
Content: args.NewContent,
|
||||||
|
Created: timestamp,
|
||||||
}
|
}
|
||||||
|
|
||||||
cardID, err := db.Write(b.db, db.CardBucket, &card)
|
cardID, err := db.Write(b.db, db.CardBucket, &card)
|
||||||
|
|
|
@ -15,6 +15,7 @@ type Card struct {
|
||||||
ID int
|
ID int
|
||||||
Title string
|
Title string
|
||||||
Content string
|
Content string
|
||||||
|
Created string
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateId updates the ID of the Card value.
|
// UpdateId updates the ID of the Card value.
|
||||||
|
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"codeflow.dananglin.me.uk/apollo/pelican/internal/board"
|
"codeflow.dananglin.me.uk/apollo/pelican/internal/board"
|
||||||
)
|
)
|
||||||
|
@ -34,19 +35,20 @@ func TestCardLifecycle(t *testing.T) {
|
||||||
initialCardContent := "Ensure that this card is safely stored in the database."
|
initialCardContent := "Ensure that this card is safely stored in the database."
|
||||||
expectedCardID := 1
|
expectedCardID := 1
|
||||||
expectedStatusID := 1
|
expectedStatusID := 1
|
||||||
|
timestamp := time.Now().Format(time.DateOnly)
|
||||||
|
|
||||||
t.Run("Test Create Card", testCreateCard(kanban, initialCardTitle, initialCardContent, expectedCardID, expectedStatusID))
|
t.Run("Test Create Card", testCreateCard(kanban, initialCardTitle, initialCardContent, expectedCardID, expectedStatusID))
|
||||||
|
|
||||||
t.Run("Test Read Card", testReadCard(kanban, expectedCardID, initialCardTitle, initialCardContent))
|
t.Run("Test Read Card", testReadCard(kanban, expectedCardID, initialCardTitle, initialCardContent, timestamp))
|
||||||
|
|
||||||
modifiedCardTitle := "Test card updated."
|
modifiedCardTitle := "Test card updated."
|
||||||
modifiedCardContent1 := "Ensure that this card is safely updated in the database."
|
modifiedCardContent1 := "Ensure that this card is safely updated in the database."
|
||||||
|
|
||||||
t.Run("Test Update Card", testUpdateCard(kanban, expectedCardID, modifiedCardTitle, modifiedCardContent1))
|
t.Run("Test Update Card", testUpdateCard(kanban, expectedCardID, modifiedCardTitle, modifiedCardContent1, timestamp))
|
||||||
|
|
||||||
modifiedCardContent2 := "Updated card content only."
|
modifiedCardContent2 := "Updated card content only."
|
||||||
|
|
||||||
t.Run("Test Update Card Content", testUpdateCardContent(kanban, expectedCardID, modifiedCardTitle, modifiedCardContent2))
|
t.Run("Test Update Card Content", testUpdateCardContent(kanban, expectedCardID, modifiedCardTitle, modifiedCardContent2, timestamp))
|
||||||
|
|
||||||
t.Run("Test Card Delete", testDeleteCard(kanban, expectedCardID, expectedStatusID))
|
t.Run("Test Card Delete", testDeleteCard(kanban, expectedCardID, expectedStatusID))
|
||||||
}
|
}
|
||||||
|
@ -85,7 +87,7 @@ func testCreateCard(kanban board.Board, title, content string, expectedCardID, e
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testReadCard(kanban board.Board, cardID int, wantTitle, wantContent string) func(t *testing.T) {
|
func testReadCard(kanban board.Board, cardID int, wantTitle, wantContent, wantTimestamp string) func(t *testing.T) {
|
||||||
return func(t *testing.T) {
|
return func(t *testing.T) {
|
||||||
t.Log("When a card is read from the database.")
|
t.Log("When a card is read from the database.")
|
||||||
|
|
||||||
|
@ -105,10 +107,16 @@ func testReadCard(kanban board.Board, cardID int, wantTitle, wantContent string)
|
||||||
} else {
|
} else {
|
||||||
t.Logf("%s\tExpected card content received, got: %s.", success, card.Content)
|
t.Logf("%s\tExpected card content received, got: %s.", success, card.Content)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if card.Created != wantTimestamp {
|
||||||
|
t.Errorf("%s\tUnexpected timestamp received for the created card, want: %s, got %s.", failure, wantTimestamp, card.Created)
|
||||||
|
} else {
|
||||||
|
t.Logf("%s\tExpected timestamp received for the created card, got: %s.", success, card.Created)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testUpdateCard(kanban board.Board, cardID int, newTitle, newContent string) func(t *testing.T) {
|
func testUpdateCard(kanban board.Board, cardID int, newTitle, newContent, timestamp string) func(t *testing.T) {
|
||||||
return func(t *testing.T) {
|
return func(t *testing.T) {
|
||||||
t.Log("When a card is updated in the database.")
|
t.Log("When a card is updated in the database.")
|
||||||
|
|
||||||
|
@ -133,6 +141,7 @@ func testUpdateCard(kanban board.Board, cardID int, newTitle, newContent string)
|
||||||
ID: cardID,
|
ID: cardID,
|
||||||
Title: newTitle,
|
Title: newTitle,
|
||||||
Content: newContent,
|
Content: newContent,
|
||||||
|
Created: timestamp,
|
||||||
}
|
}
|
||||||
|
|
||||||
if !reflect.DeepEqual(got, want) {
|
if !reflect.DeepEqual(got, want) {
|
||||||
|
@ -143,7 +152,7 @@ func testUpdateCard(kanban board.Board, cardID int, newTitle, newContent string)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testUpdateCardContent(kanban board.Board, cardID int, expectedTitle, newContent string) func(t *testing.T) {
|
func testUpdateCardContent(kanban board.Board, cardID int, expectedTitle, newContent, timestamp string) func(t *testing.T) {
|
||||||
return func(t *testing.T) {
|
return func(t *testing.T) {
|
||||||
t.Log("When (and only when) a card's content is updated in the database.")
|
t.Log("When (and only when) a card's content is updated in the database.")
|
||||||
|
|
||||||
|
@ -168,6 +177,7 @@ func testUpdateCardContent(kanban board.Board, cardID int, expectedTitle, newCon
|
||||||
ID: cardID,
|
ID: cardID,
|
||||||
Title: expectedTitle,
|
Title: expectedTitle,
|
||||||
Content: newContent,
|
Content: newContent,
|
||||||
|
Created: timestamp,
|
||||||
}
|
}
|
||||||
|
|
||||||
if !reflect.DeepEqual(got, want) {
|
if !reflect.DeepEqual(got, want) {
|
||||||
|
|
Loading…
Reference in a new issue