Skip to content

Commit

Permalink
feat(FilesProvider): Implement and test SplitEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
ja-he committed Aug 16, 2024
1 parent 13efb19 commit d664a4f
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 2 deletions.
36 changes: 34 additions & 2 deletions internal/storage/providers/files_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -544,8 +544,40 @@ func (p *FilesDataProvider) GetEventsCoveringTimerange(start, end time.Time) ([]
}

// TODO: doc SplitEvent
func (p *FilesDataProvider) SplitEvent(model.EventID, time.Time) error {
p.log.Fatal().Msg("TODO IMPL(SplitEvent)")
func (p *FilesDataProvider) SplitEvent(id model.EventID, splitTime time.Time) error {

e, err := p.GetEvent(id)
if err != nil {
return fmt.Errorf("error getting event with ID '%s' (%w)", id, err)
}

p.log.Debug().Msgf("will try to split event '%s' (%s til %s) at %s", id, e.Start, e.End, splitTime.String())

if !(splitTime.After(e.Start) && splitTime.Before(e.End)) {
return fmt.Errorf("split time is not between start and end time of event in question")
}

fh, err := p.getFileHandler(model.DateFromGotime(e.Start))
if err != nil {
return fmt.Errorf("error loading file handler for date (%w)", err)
}

firstHalfEvent := e
secondHalfEvent := *e

firstHalfEvent.End = splitTime
secondHalfEvent.Start = splitTime
secondHalfEvent.ID = filesProviderIDGenerator()

err = fh.UpdateEvent(firstHalfEvent)
if err != nil {
return fmt.Errorf("error updating first half event (%w)", err)
}
err = fh.AddEvent(&secondHalfEvent)
if err != nil {
return fmt.Errorf("error adding second half event (%w)", err)
}

return nil
}

Expand Down
116 changes: 116 additions & 0 deletions internal/storage/providers/files_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,122 @@ func TestFilesProvider(t *testing.T) {
})
})

t.Run("split-event", func(t *testing.T) {
originalStart := time.Date(2023, 1, 1, 10, 0, 0, 0, time.UTC)
originalEnd := time.Date(2023, 1, 1, 14, 0, 0, 0, time.UTC)

t.Run("basic", func(t *testing.T) {
id := setupWithSingleEventTimes(t, originalStart, originalEnd)
splitTime := time.Date(2023, 1, 1, 12, 0, 0, 0, time.UTC)

err := p.SplitEvent(id, splitTime)
assert.Nil(t, err)

// Verify the original event
event, err := p.GetEvent(id)
assert.Nil(t, err)
assert.Equal(t, originalStart, event.Start)
assert.Equal(t, splitTime, event.End)

// Verify the new split event
events, err := p.GetEventsCoveringTimerange(yearZero, yearTenK)
assert.Nil(t, err)
assert.Len(t, events, 2)
assert.Equal(t, id, events[0].ID)
assert.NotEqual(t, id, events[1].ID)
assert.Equal(t, originalStart, events[0].Start)
assert.Equal(t, splitTime, events[0].End)
assert.Equal(t, splitTime, events[1].Start)
assert.Equal(t, originalEnd, events[1].End)
})

t.Run("split-at-start", func(t *testing.T) {
id := setupWithSingleEventTimes(t, originalStart, originalEnd)
splitTime := originalStart

err := p.SplitEvent(id, splitTime)
assert.NotNil(t, err)

// Verify the original event
event, err := p.GetEvent(id)
assert.Nil(t, err)
assert.Equal(t, originalStart, event.Start)
assert.Equal(t, originalEnd, event.End)

// Verifiy that there is no other created event
allEvents, err := p.GetEventsCoveringTimerange(yearZero, yearTenK)
assert.Nil(t, err)
assert.Equal(t, len(allEvents), 1)

})

t.Run("split-at-end", func(t *testing.T) {
id := setupWithSingleEventTimes(t, originalStart, originalEnd)
splitTime := originalEnd

err := p.SplitEvent(id, splitTime)
assert.NotNil(t, err)

// Verify the original event
event, err := p.GetEvent(id)
assert.Nil(t, err)
assert.Equal(t, originalStart, event.Start)
assert.Equal(t, originalEnd, event.End)

// Verifiy that there is no other created event
allEvents, err := p.GetEventsCoveringTimerange(yearZero, yearTenK)
assert.Nil(t, err)
assert.Equal(t, len(allEvents), 1)

})

t.Run("invalid-split-time-before-start", func(t *testing.T) {
id := setupWithSingleEventTimes(t, originalStart, originalEnd)
splitTime := originalStart.Add(-1 * time.Minute)

err := p.SplitEvent(id, splitTime)
assert.NotNil(t, err)

// Verify the original event
event, err := p.GetEvent(id)
assert.Nil(t, err)
assert.Equal(t, originalStart, event.Start)
assert.Equal(t, originalEnd, event.End)

// Verifiy that there is no other created event
allEvents, err := p.GetEventsCoveringTimerange(yearZero, yearTenK)
assert.Nil(t, err)
assert.Equal(t, len(allEvents), 1)
})

t.Run("invalid-split-time-after-end", func(t *testing.T) {
id := setupWithSingleEventTimes(t, originalStart, originalEnd)
splitTime := originalEnd.Add(1 * time.Minute)

err := p.SplitEvent(id, splitTime)
assert.NotNil(t, err)
})

t.Run("invalid-split-same-as-end", func(t *testing.T) {
id := setupWithSingleEventTimes(t, originalStart, originalEnd)
splitTime := originalEnd

err := p.SplitEvent(id, splitTime)
assert.NotNil(t, err)

// Verify the original event
event, err := p.GetEvent(id)
assert.Nil(t, err)
assert.Equal(t, originalStart, event.Start)
assert.Equal(t, originalEnd, event.End)

// Verifiy that there is no other created event
allEvents, err := p.GetEventsCoveringTimerange(yearZero, yearTenK)
assert.Nil(t, err)
assert.Equal(t, len(allEvents), 1)
})
})

}

type testWriter struct {
Expand Down

0 comments on commit d664a4f

Please sign in to comment.