service test
This commit is contained in:
parent
3c210cb172
commit
041db5abe9
|
@ -43,10 +43,10 @@ func NewRecurrer(recurStr string) Recurrer {
|
||||||
func FirstRecurAfter(r Recurrer, d Date) Date {
|
func FirstRecurAfter(r Recurrer, d Date) Date {
|
||||||
lim := NewDate(2050, 1, 1)
|
lim := NewDate(2050, 1, 1)
|
||||||
for {
|
for {
|
||||||
|
d = d.Add(1)
|
||||||
if r.RecursOn(d) || d.Equal(lim) {
|
if r.RecursOn(d) || d.Equal(lim) {
|
||||||
return d
|
return d
|
||||||
}
|
}
|
||||||
d = d.Add(1)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -73,7 +73,7 @@ func (d Daily) RecursOn(date Date) bool {
|
||||||
return date.Equal(d.Start) || date.After(d.Start)
|
return date.Equal(d.Start) || date.After(d.Start)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d Daily) First() Date { return FirstRecurAfter(d, d.Start) }
|
func (d Daily) First() Date { return FirstRecurAfter(d, d.Start.Add(-1)) }
|
||||||
|
|
||||||
func (d Daily) String() string {
|
func (d Daily) String() string {
|
||||||
return fmt.Sprintf("%s, daily", d.Start.String())
|
return fmt.Sprintf("%s, daily", d.Start.String())
|
||||||
|
@ -124,7 +124,7 @@ func (nd EveryNDays) RecursOn(date Date) bool {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (nd EveryNDays) First() Date { return FirstRecurAfter(nd, nd.Start) }
|
func (nd EveryNDays) First() Date { return FirstRecurAfter(nd, nd.Start.Add(-1)) }
|
||||||
|
|
||||||
func (nd EveryNDays) String() string {
|
func (nd EveryNDays) String() string {
|
||||||
return fmt.Sprintf("%s, every %d days", nd.Start.String(), nd.N)
|
return fmt.Sprintf("%s, every %d days", nd.Start.String(), nd.N)
|
||||||
|
@ -177,7 +177,7 @@ func (w Weekly) RecursOn(date Date) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w Weekly) First() Date { return FirstRecurAfter(w, w.Start) }
|
func (w Weekly) First() Date { return FirstRecurAfter(w, w.Start.Add(-1)) }
|
||||||
|
|
||||||
func (w Weekly) String() string {
|
func (w Weekly) String() string {
|
||||||
weekdayStrs := []string{}
|
weekdayStrs := []string{}
|
||||||
|
@ -227,7 +227,7 @@ func (enw EveryNWeeks) RecursOn(date Date) bool {
|
||||||
return enw.Start.DaysBetween(date)%intervalDays == 0
|
return enw.Start.DaysBetween(date)%intervalDays == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (enw EveryNWeeks) First() Date { return FirstRecurAfter(enw, enw.Start) }
|
func (enw EveryNWeeks) First() Date { return FirstRecurAfter(enw, enw.Start.Add(-1)) }
|
||||||
|
|
||||||
func (enw EveryNWeeks) String() string {
|
func (enw EveryNWeeks) String() string {
|
||||||
return fmt.Sprintf("%s, every %d weeks", enw.Start.String(), enw.N)
|
return fmt.Sprintf("%s, every %d weeks", enw.Start.String(), enw.N)
|
||||||
|
@ -277,7 +277,7 @@ func (enm EveryNMonths) RecursOn(date Date) bool {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (enm EveryNMonths) First() Date { return FirstRecurAfter(enm, enm.Start) }
|
func (enm EveryNMonths) First() Date { return FirstRecurAfter(enm, enm.Start.Add(-1)) }
|
||||||
|
|
||||||
func (enm EveryNMonths) String() string {
|
func (enm EveryNMonths) String() string {
|
||||||
return fmt.Sprintf("%s, every %d months", enm.Start.String(), enm.N)
|
return fmt.Sprintf("%s, every %d months", enm.Start.String(), enm.N)
|
||||||
|
|
|
@ -2,66 +2,70 @@ package storage
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"sort"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"go-mod.ewintr.nl/planner/item"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ErrNotFound = errors.New("not found")
|
ErrNotFound = errors.New("not found")
|
||||||
)
|
)
|
||||||
|
|
||||||
// type LocalID interface {
|
type LocalID interface {
|
||||||
// FindAll() (map[string]int, error)
|
FindAll() (map[string]int, error)
|
||||||
// FindOrNext(id string) (int, error)
|
FindOrNext(id string) (int, error)
|
||||||
// Next() (int, error)
|
Next() (int, error)
|
||||||
// Store(id string, localID int) error
|
Store(id string, localID int) error
|
||||||
// Delete(id string) error
|
Delete(id string) error
|
||||||
// }
|
}
|
||||||
|
|
||||||
// type Sync interface {
|
type Sync interface {
|
||||||
// FindAll() ([]item.Item, error)
|
FindAll() ([]item.Item, error)
|
||||||
// Store(i item.Item) error
|
Store(i item.Item) error
|
||||||
// DeleteAll() error
|
DeleteAll() error
|
||||||
// LastUpdate() (time.Time, error)
|
LastUpdate() (time.Time, error)
|
||||||
// }
|
}
|
||||||
|
|
||||||
// type Event interface {
|
type Event interface {
|
||||||
// Store(event item.Event) error
|
Store(event item.Event) error
|
||||||
// Find(id string) (item.Event, error)
|
Find(id string) (item.Event, error)
|
||||||
// FindAll() ([]item.Event, error)
|
FindAll() ([]item.Event, error)
|
||||||
// Delete(id string) error
|
Delete(id string) error
|
||||||
// }
|
}
|
||||||
|
|
||||||
// func NextLocalID(used []int) int {
|
func NextLocalID(used []int) int {
|
||||||
// if len(used) == 0 {
|
if len(used) == 0 {
|
||||||
// return 1
|
return 1
|
||||||
// }
|
}
|
||||||
|
|
||||||
// sort.Ints(used)
|
sort.Ints(used)
|
||||||
// usedMax := 1
|
usedMax := 1
|
||||||
// for _, u := range used {
|
for _, u := range used {
|
||||||
// if u > usedMax {
|
if u > usedMax {
|
||||||
// usedMax = u
|
usedMax = u
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
|
|
||||||
// var limit int
|
var limit int
|
||||||
// for limit = 1; limit <= len(used) || limit < usedMax; limit *= 10 {
|
for limit = 1; limit <= len(used) || limit < usedMax; limit *= 10 {
|
||||||
// }
|
}
|
||||||
|
|
||||||
// newId := used[len(used)-1] + 1
|
newId := used[len(used)-1] + 1
|
||||||
// if newId < limit {
|
if newId < limit {
|
||||||
// return newId
|
return newId
|
||||||
// }
|
}
|
||||||
|
|
||||||
// usedMap := map[int]bool{}
|
usedMap := map[int]bool{}
|
||||||
// for _, u := range used {
|
for _, u := range used {
|
||||||
// usedMap[u] = true
|
usedMap[u] = true
|
||||||
// }
|
}
|
||||||
|
|
||||||
// for i := 1; i < limit; i++ {
|
for i := 1; i < limit; i++ {
|
||||||
// if _, ok := usedMap[i]; !ok {
|
if _, ok := usedMap[i]; !ok {
|
||||||
// return i
|
return i
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
|
|
||||||
// return limit
|
return limit
|
||||||
// }
|
}
|
||||||
|
|
|
@ -46,34 +46,15 @@ func (m *Memory) Updated(kinds []item.Kind, timestamp time.Time) ([]item.Item, e
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Memory) RecursBefore(date time.Time) ([]item.Item, error) {
|
func (m *Memory) ShouldRecur(date item.Date) ([]item.Item, error) {
|
||||||
// res := make([]item.Item, 0)
|
res := make([]item.Item, 0)
|
||||||
// for _, i := range m.items {
|
for _, i := range m.items {
|
||||||
// if i.Recurrer == nil {
|
if i.Recurrer == nil {
|
||||||
// continue
|
continue
|
||||||
// }
|
}
|
||||||
// if i.RecurNext.Before(date) {
|
if date.Equal(i.RecurNext) || date.After(i.RecurNext) {
|
||||||
// res = append(res, i)
|
res = append(res, i)
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
// return res, nil
|
return res, nil
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Memory) RecursNext(id string, date time.Time, ts time.Time) error {
|
|
||||||
// i, ok := m.items[id]
|
|
||||||
// if !ok {
|
|
||||||
// return ErrNotFound
|
|
||||||
// }
|
|
||||||
// if i.Recurrer == nil {
|
|
||||||
// return ErrNotARecurrer
|
|
||||||
// }
|
|
||||||
// if !i.Recurrer.On(date) {
|
|
||||||
// return fmt.Errorf("item does not recur on %v", date)
|
|
||||||
// }
|
|
||||||
// i.RecurNext = date
|
|
||||||
// i.Updated = ts
|
|
||||||
// m.items[id] = i
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -113,19 +113,14 @@ func TestMemoryRecur(t *testing.T) {
|
||||||
mem := NewMemory()
|
mem := NewMemory()
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
earlier := now.Add(-5 * time.Minute)
|
earlier := now.Add(-5 * time.Minute)
|
||||||
today := time.Date(2024, 12, 1, 0, 0, 0, 0, time.UTC)
|
today := item.NewDate(2024, 12, 1)
|
||||||
yesterday := time.Date(2024, 11, 30, 0, 0, 0, 0, time.UTC)
|
yesterday := item.NewDate(2024, 11, 30)
|
||||||
tomorrow := time.Date(2024, 12, 2, 0, 0, 0, 0, time.UTC)
|
|
||||||
|
|
||||||
t.Log("start")
|
t.Log("start")
|
||||||
i1 := item.Item{
|
i1 := item.Item{
|
||||||
ID: "a",
|
ID: "a",
|
||||||
Updated: earlier,
|
Updated: earlier,
|
||||||
Recurrer: &item.Recur{
|
Recurrer: item.NewRecurrer("2024-11-30, daily"),
|
||||||
Start: yesterday,
|
|
||||||
Period: item.PeriodDay,
|
|
||||||
Count: 1,
|
|
||||||
},
|
|
||||||
RecurNext: yesterday,
|
RecurNext: yesterday,
|
||||||
}
|
}
|
||||||
i2 := item.Item{
|
i2 := item.Item{
|
||||||
|
@ -140,7 +135,7 @@ func TestMemoryRecur(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Log("get recurrers")
|
t.Log("get recurrers")
|
||||||
rs, err := mem.RecursBefore(today)
|
rs, err := mem.ShouldRecur(today)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("exp nil, gt %v", err)
|
t.Errorf("exp nil, gt %v", err)
|
||||||
}
|
}
|
||||||
|
@ -148,20 +143,4 @@ func TestMemoryRecur(t *testing.T) {
|
||||||
t.Errorf("(exp +, got -)\n%s", diff)
|
t.Errorf("(exp +, got -)\n%s", diff)
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Log("set next")
|
|
||||||
if err := mem.RecursNext(i1.ID, tomorrow, time.Now()); err != nil {
|
|
||||||
t.Errorf("exp nil, got %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Log("check result")
|
|
||||||
us, err := mem.Updated([]item.Kind{}, now)
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("exp nil, got %v", err)
|
|
||||||
}
|
|
||||||
if len(us) != 1 {
|
|
||||||
t.Errorf("exp 1, got %v", len(us))
|
|
||||||
}
|
|
||||||
if us[0].ID != i1.ID {
|
|
||||||
t.Errorf("exp %v, got %v", i1.ID, us[0].ID)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,23 +12,18 @@ import (
|
||||||
func TestRecur(t *testing.T) {
|
func TestRecur(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
now := time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC)
|
now := time.Now()
|
||||||
|
today := item.NewDate(2024, 1, 1)
|
||||||
mem := NewMemory()
|
mem := NewMemory()
|
||||||
rec := NewRecur(mem, mem, slog.New(slog.NewTextHandler(io.Discard, nil)))
|
rec := NewRecur(mem, mem, slog.New(slog.NewTextHandler(io.Discard, nil)))
|
||||||
|
|
||||||
// Create a recurring item
|
|
||||||
recur := &item.Recur{
|
|
||||||
Start: now,
|
|
||||||
Period: item.PeriodDay,
|
|
||||||
Count: 1,
|
|
||||||
}
|
|
||||||
testItem := item.Item{
|
testItem := item.Item{
|
||||||
ID: "test-1",
|
ID: "test-1",
|
||||||
Kind: item.KindEvent,
|
Kind: item.KindEvent,
|
||||||
Updated: now,
|
Updated: now,
|
||||||
Deleted: false,
|
Deleted: false,
|
||||||
Recurrer: recur,
|
Recurrer: item.NewRecurrer("2024-01-01, daily"),
|
||||||
RecurNext: now,
|
RecurNext: today,
|
||||||
Body: `{"title":"Test Event","start":"2024-01-01T10:00:00Z","duration":"30m"}`,
|
Body: `{"title":"Test Event","start":"2024-01-01T10:00:00Z","duration":"30m"}`,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,14 +48,14 @@ func TestRecur(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check that RecurNext was updated
|
// Check that RecurNext was updated
|
||||||
recurItems, err := mem.RecursBefore(now.Add(48 * time.Hour))
|
recurItems, err := mem.ShouldRecur(today.Add(1))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if len(recurItems) != 1 {
|
if len(recurItems) != 1 {
|
||||||
t.Errorf("expected 1 recur item, got %d", len(recurItems))
|
t.Errorf("expected 1 recur item, got %d", len(recurItems))
|
||||||
}
|
}
|
||||||
if !recurItems[0].RecurNext.After(now) {
|
if !recurItems[0].RecurNext.After(today) {
|
||||||
t.Errorf("RecurNext was not updated, still %v", recurItems[0].RecurNext)
|
t.Errorf("RecurNext was not updated, still %v", recurItems[0].RecurNext)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue