This commit is contained in:
Erik Winter 2024-12-22 13:27:45 +01:00
parent e5fd6a6f14
commit f6f4946c91
2 changed files with 147 additions and 149 deletions

View File

@ -70,41 +70,34 @@ func NewEvent(i Item) (Event, error) {
return e, nil return e, nil
} }
// func (e Event) Item() (Item, error) { func (e Event) Item() (Item, error) {
// body, err := json.Marshal(EventBody{ body, err := json.Marshal(e.EventBody)
// Title: e.Title, if err != nil {
// Start: e.Start, return Item{}, fmt.Errorf("could not marshal event body to json")
// Duration: e.Duration, }
// })
// if err != nil {
// return Item{}, fmt.Errorf("could not marshal event to json")
// }
// return Item{ return Item{
// ID: e.ID, ID: e.ID,
// Kind: KindEvent, Kind: KindEvent,
// Recurrer: e.Recurrer, Recurrer: e.Recurrer,
// RecurNext: e.RecurNext, RecurNext: e.RecurNext,
// Body: string(body), Body: string(body),
// }, nil }, nil
// } }
// func (e Event) Valid() bool { func (e Event) Valid() bool {
// if e.Title == "" { if e.Title == "" {
// return false return false
// } }
// if e.Start.IsZero() || e.Start.Year() < 2024 { if e.Date.IsZero() {
// return false return false
// } }
// if e.Duration.Seconds() < 1 { if e.Duration.Seconds() < 1 {
// return false return false
// } }
// // if e.Recurrer != nil && !e.Recurrer.Valid() {
// // return false
// // }
// return true return true
// } }
func EventDiff(a, b Event) string { func EventDiff(a, b Event) string {
aJSON, _ := json.Marshal(a) aJSON, _ := json.Marshal(a)

View File

@ -4,6 +4,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/google/go-cmp/cmp"
"go-mod.ewintr.nl/planner/item" "go-mod.ewintr.nl/planner/item"
) )
@ -83,125 +84,129 @@ func TestNewEvent(t *testing.T) {
} }
} }
// func TestEventItem(t *testing.T) { func TestEventItem(t *testing.T) {
// t.Parallel() t.Parallel()
// oneHour, err := time.ParseDuration("1h") oneHour, err := time.ParseDuration("1h")
// if err != nil { if err != nil {
// t.Errorf("exp nil, got %v", err) t.Errorf("exp nil, got %v", err)
// } }
// for _, tc := range []struct { for _, tc := range []struct {
// name string name string
// event item.Event event item.Event
// expItem item.Item expItem item.Item
// expErr bool expErr bool
// }{ }{
// { {
// name: "empty", name: "empty",
// expItem: item.Item{ expItem: item.Item{
// Kind: item.KindEvent, Kind: item.KindEvent,
// Updated: time.Time{}, Updated: time.Time{},
// Body: `{"start":"0001-01-01T00:00:00Z","duration":"0s","title":""}`, Body: `{"duration":"0s","title":"","date":"no date","time":"00:00"}`,
// }, },
// }, },
// { {
// name: "normal", name: "normal",
// event: item.Event{ event: item.Event{
// ID: "a", ID: "a",
// EventBody: item.EventBody{ EventBody: item.EventBody{
// Title: "title", Title: "title",
// Start: time.Date(2024, 9, 23, 8, 0, 0, 0, time.UTC), Date: item.NewDate(2024, 9, 23),
// Duration: oneHour, Time: item.NewTime(8, 0),
// }, Duration: oneHour,
// }, },
// expItem: item.Item{ },
// ID: "a", expItem: item.Item{
// Kind: item.KindEvent, ID: "a",
// Updated: time.Time{}, Kind: item.KindEvent,
// Body: `{"start":"2024-09-23T08:00:00Z","duration":"1h0m0s","title":"title"}`, Updated: time.Time{},
// }, Body: `{"duration":"1h0m0s","title":"title","date":"2024-09-23","time":"08:00"}`,
// }, },
// } { },
// t.Run(tc.name, func(t *testing.T) { } {
// actItem, actErr := tc.event.Item() t.Run(tc.name, func(t *testing.T) {
// if tc.expErr != (actErr != nil) { actItem, actErr := tc.event.Item()
// t.Errorf("exp nil, got %v", actErr) if tc.expErr != (actErr != nil) {
// } t.Errorf("exp nil, got %v", actErr)
// if tc.expErr { }
// return if tc.expErr {
// } return
// if diff := cmp.Diff(tc.expItem, actItem); diff != "" { }
// t.Errorf("(exp+, got -)\n%s", diff) if diff := cmp.Diff(tc.expItem, actItem); diff != "" {
// } t.Errorf("(exp+, got -)\n%s", diff)
// }) }
// } })
// } }
}
// func TestEventValidate(t *testing.T) { func TestEventValidate(t *testing.T) {
// t.Parallel() t.Parallel()
// oneHour, err := time.ParseDuration("1h") oneHour, err := time.ParseDuration("1h")
// if err != nil { if err != nil {
// t.Errorf("exp nil, got %v", err) t.Errorf("exp nil, got %v", err)
// } }
// for _, tc := range []struct { for _, tc := range []struct {
// name string name string
// event item.Event event item.Event
// exp bool exp bool
// }{ }{
// { {
// name: "empty", name: "empty",
// }, },
// { {
// name: "missing title", name: "missing title",
// event: item.Event{ event: item.Event{
// ID: "a", ID: "a",
// EventBody: item.EventBody{ EventBody: item.EventBody{
// Start: time.Date(2024, 9, 20, 8, 0, 0, 0, time.UTC), Date: item.NewDate(2024, 9, 20),
// Duration: oneHour, Time: item.NewTime(8, 0),
// }, Duration: oneHour,
// }, },
// }, },
// { },
// name: "no date", {
// event: item.Event{ name: "no date",
// ID: "a", event: item.Event{
// EventBody: item.EventBody{ ID: "a",
// Title: "title", EventBody: item.EventBody{
// Start: time.Date(0, 0, 0, 8, 0, 0, 0, time.UTC), Title: "title",
// Duration: oneHour, Time: item.NewTime(8, 0),
// }, Duration: oneHour,
// }, },
// }, },
// { },
// name: "no duration", {
// event: item.Event{ name: "no duration",
// ID: "a", event: item.Event{
// EventBody: item.EventBody{ ID: "a",
// Title: "title", EventBody: item.EventBody{
// Start: time.Date(2024, 9, 20, 8, 0, 0, 0, time.UTC), Title: "title",
// }, Date: item.NewDate(2024, 9, 20),
// }, Time: item.NewTime(8, 0),
// }, },
// { },
// name: "valid", },
// event: item.Event{ {
// ID: "a", name: "valid",
// EventBody: item.EventBody{ event: item.Event{
// Title: "title", ID: "a",
// Start: time.Date(2024, 9, 20, 8, 0, 0, 0, time.UTC), EventBody: item.EventBody{
// Duration: oneHour, Title: "title",
// }, Date: item.NewDate(2024, 9, 20),
// }, Time: item.NewTime(8, 0),
// exp: true, Duration: oneHour,
// }, },
// } { },
// t.Run(tc.name, func(t *testing.T) { exp: true,
// if act := tc.event.Valid(); tc.exp != act { },
// t.Errorf("exp %v, got %v", tc.exp, act) } {
// } t.Run(tc.name, func(t *testing.T) {
if act := tc.event.Valid(); tc.exp != act {
t.Errorf("exp %v, got %v", tc.exp, act)
}
// }) })
// } }
// } }