Compare commits
3 Commits
3b74c5eeb5
...
4d3c81d85a
Author | SHA1 | Date |
---|---|---|
Erik Winter | 4d3c81d85a | |
Erik Winter | e9ce251737 | |
Erik Winter | e4faf75aeb |
|
@ -167,7 +167,7 @@ func (d Date) DaysBetween(d2 Date) int {
|
|||
|
||||
func (d Date) String() string {
|
||||
if d.t.IsZero() {
|
||||
return "no date"
|
||||
return ""
|
||||
}
|
||||
|
||||
return strings.ToLower(d.t.Format(DateFormat))
|
||||
|
|
|
@ -230,7 +230,7 @@ func TestDateString(t *testing.T) {
|
|||
{
|
||||
name: "zero",
|
||||
date: item.NewDate(0, 0, 0),
|
||||
exp: "no date",
|
||||
exp: "",
|
||||
},
|
||||
{
|
||||
name: "normal",
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
|
@ -68,3 +69,10 @@ func NewItem(k Kind, body string) Item {
|
|||
Body: body,
|
||||
}
|
||||
}
|
||||
|
||||
func ItemDiff(exp, got Item) string {
|
||||
expJSON, _ := json.Marshal(exp)
|
||||
actJSON, _ := json.Marshal(got)
|
||||
|
||||
return cmp.Diff(string(expJSON), string(actJSON))
|
||||
}
|
||||
|
|
|
@ -0,0 +1,83 @@
|
|||
package item_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"go-mod.ewintr.nl/planner/item"
|
||||
)
|
||||
|
||||
func TestItemJSON(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
item item.Item
|
||||
expJSON string
|
||||
}{
|
||||
{
|
||||
name: "minimal",
|
||||
item: item.Item{
|
||||
ID: "a",
|
||||
Kind: item.KindTask,
|
||||
Body: `{"title":"title"}`,
|
||||
},
|
||||
expJSON: `{
|
||||
"recurrer": "",
|
||||
"id": "a",
|
||||
"kind": "task",
|
||||
"updated": "0001-01-01T00:00:00Z",
|
||||
"deleted": false,
|
||||
"date": "",
|
||||
"recurNext": "",
|
||||
"body": "{\"title\":\"title\"}"
|
||||
}`,
|
||||
},
|
||||
{
|
||||
name: "full",
|
||||
item: item.Item{
|
||||
ID: "a",
|
||||
Kind: item.KindTask,
|
||||
Updated: time.Date(2024, 12, 25, 11, 9, 0, 0, time.UTC),
|
||||
Deleted: true,
|
||||
Date: item.NewDate(2024, 12, 26),
|
||||
Recurrer: item.NewRecurrer("2024-12-25, daily"),
|
||||
RecurNext: item.NewDateFromString("2024-12-30"),
|
||||
Body: `{"title":"title"}`,
|
||||
},
|
||||
expJSON: `{
|
||||
"recurrer": "2024-12-25, daily",
|
||||
"id": "a",
|
||||
"kind": "task",
|
||||
"updated": "2024-12-25T11:09:00Z",
|
||||
"deleted": true,
|
||||
"date": "2024-12-26",
|
||||
"recurNext": "2024-12-30",
|
||||
"body": "{\"title\":\"title\"}"
|
||||
}`,
|
||||
},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
actJSON, err := json.Marshal(tc.item)
|
||||
if err != nil {
|
||||
t.Errorf("exp nil, got %v", err)
|
||||
}
|
||||
expJSON := bytes.NewBuffer([]byte(``))
|
||||
if err := json.Compact(expJSON, []byte(tc.expJSON)); err != nil {
|
||||
t.Errorf("exp nil, got %v", err)
|
||||
}
|
||||
if expJSON.String() != string(actJSON) {
|
||||
t.Errorf("exp %v, got %v", expJSON.String(), string(actJSON))
|
||||
}
|
||||
var actItem item.Item
|
||||
if err := json.Unmarshal(actJSON, &actItem); err != nil {
|
||||
t.Errorf("exp nil, got %v", err)
|
||||
}
|
||||
if diff := item.ItemDiff(tc.item, actItem); diff != "" {
|
||||
t.Errorf("(+exp, -got)%s\n", diff)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
|
@ -102,7 +102,7 @@ func TestTaskItem(t *testing.T) {
|
|||
expItem: item.Item{
|
||||
Kind: item.KindTask,
|
||||
Updated: time.Time{},
|
||||
Body: `{"duration":"0s","title":"","time":"00:00"}`,
|
||||
Body: `{"duration":"0s","title":"","time":""}`,
|
||||
},
|
||||
},
|
||||
{
|
||||
|
|
|
@ -44,6 +44,10 @@ func NewTimeFromString(timeStr string) Time {
|
|||
}
|
||||
|
||||
func (t *Time) String() string {
|
||||
if t.t.IsZero() {
|
||||
return ""
|
||||
}
|
||||
|
||||
return t.t.Format(TimeFormat)
|
||||
}
|
||||
|
||||
|
|
|
@ -44,11 +44,16 @@ func TestTimeFromString(t *testing.T) {
|
|||
}{
|
||||
{
|
||||
name: "empty",
|
||||
exp: "00:00",
|
||||
exp: "",
|
||||
},
|
||||
{
|
||||
name: "invalid",
|
||||
str: "invalid",
|
||||
exp: "",
|
||||
},
|
||||
{
|
||||
name: "00:00",
|
||||
str: "00:00",
|
||||
exp: "00:00",
|
||||
},
|
||||
{
|
||||
|
|
|
@ -43,9 +43,9 @@ func TestDelete(t *testing.T) {
|
|||
},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
eventRepo := memory.NewTask()
|
||||
taskRepo := memory.NewTask()
|
||||
syncRepo := memory.NewSync()
|
||||
if err := eventRepo.Store(e); err != nil {
|
||||
if err := taskRepo.Store(e); err != nil {
|
||||
t.Errorf("exp nil, got %v", err)
|
||||
}
|
||||
localRepo := memory.NewLocalID()
|
||||
|
@ -53,7 +53,7 @@ func TestDelete(t *testing.T) {
|
|||
t.Errorf("exp nil, got %v", err)
|
||||
}
|
||||
|
||||
cmd := command.NewDelete(localRepo, eventRepo, syncRepo)
|
||||
cmd := command.NewDelete(localRepo, taskRepo, syncRepo)
|
||||
|
||||
actErr := cmd.Execute(tc.main, tc.flags) != nil
|
||||
if tc.expErr != actErr {
|
||||
|
@ -63,7 +63,7 @@ func TestDelete(t *testing.T) {
|
|||
return
|
||||
}
|
||||
|
||||
_, repoErr := eventRepo.Find(e.ID)
|
||||
_, repoErr := taskRepo.Find(e.ID)
|
||||
if !errors.Is(repoErr, storage.ErrNotFound) {
|
||||
t.Errorf("exp %v, got %v", storage.ErrNotFound, actErr)
|
||||
}
|
||||
|
|
|
@ -35,8 +35,8 @@ func (t *Task) FindAll() ([]item.Task, error) {
|
|||
defer t.mutex.RUnlock()
|
||||
|
||||
tasks := make([]item.Task, 0, len(t.tasks))
|
||||
for _, event := range t.tasks {
|
||||
tasks = append(tasks, event)
|
||||
for _, tsk := range t.tasks {
|
||||
tasks = append(tasks, tsk)
|
||||
}
|
||||
sort.Slice(tasks, func(i, j int) bool {
|
||||
return tasks[i].ID < tasks[j].ID
|
||||
|
|
Loading…
Reference in New Issue