henwen/back/store_test.go

174 lines
4.6 KiB
Go
Raw Normal View History

2020-04-16 20:49:28 +00:00
package back_test
import (
"context"
"testing"
"github.com/matryer/is"
2020-04-21 14:25:15 +00:00
"github.com/rickb777/date"
2020-04-16 20:49:28 +00:00
"gitlab.codemonkeysoftware.net/b/henwen/back"
)
func TestNewMemoryStore(t *testing.T) {
is := is.New(t)
store, err := back.NewMemoryStore(back.SecureGenString)
is.NoErr(err)
is.True(store != nil)
store.Close()
}
func TestCreateEvent(t *testing.T) {
is := is.New(t)
store, err := back.NewMemoryStore(back.SecureGenString)
is.NoErr(err)
defer store.Close()
2020-04-21 14:25:15 +00:00
earliest := date.New(2020, 4, 5)
latest := date.New(2020, 4, 5)
2020-04-16 20:49:28 +00:00
const name = "abc"
const description = "def"
createResult, err := store.CreateEvent(context.Background(), back.CreateEventCommand{
2020-04-21 14:25:15 +00:00
Name: name,
Description: description,
Earliest: earliest,
Latest: latest,
2020-04-16 20:49:28 +00:00
})
is.NoErr(err)
metadataResult, err := store.GetEventMetadata(context.Background(), back.GetEventMetadataQuery{
AlphaID: createResult.AlphaID,
})
is.NoErr(err)
is.Equal(metadataResult.Name, name)
is.Equal(metadataResult.Description, description)
2020-04-21 14:25:15 +00:00
is.True(metadataResult.Earliest.Equal(earliest))
is.True(metadataResult.Latest.Equal(latest))
2020-04-16 20:49:28 +00:00
}
2020-04-17 02:36:48 +00:00
2020-09-27 21:44:04 +00:00
func TestGetEventResponseSummary(t *testing.T) {
2020-04-17 02:36:48 +00:00
store, err := back.NewMemoryStore(back.SecureGenString)
is.New(t).NoErr(err)
2020-09-27 21:44:04 +00:00
defer store.Close()
2020-04-17 02:36:48 +00:00
createEvent := func(is *is.I) (eventID string) {
event, err := store.CreateEvent(context.Background(), back.CreateEventCommand{
2020-04-21 14:25:15 +00:00
Name: "blah",
2020-04-17 02:36:48 +00:00
Description: "stuff happening",
2020-04-21 14:25:15 +00:00
Earliest: date.Today(),
Latest: date.Today().Add(1),
2020-04-17 02:36:48 +00:00
})
is.NoErr(err)
return event.AlphaID
}
getTotalResponses := func(is *is.I, eventID string) int {
2020-09-27 21:44:04 +00:00
responses, err := store.GetEventResponseSummary(context.Background(), back.GetEventResponseSummaryQuery{
2020-04-17 02:36:48 +00:00
AlphaID: eventID,
})
is.NoErr(err)
return responses.TotalResponses
}
createEmptyResponse := func(is *is.I, eventID string) {
_, err = store.CreateEventResponse(context.Background(), back.CreateEventResponseCommand{
EventAlphaID: eventID,
})
is.NoErr(err)
}
t.Run("TotalResponses counts the number of responses created", func(t *testing.T) {
is := is.New(t)
eventID := createEvent(is)
is.Equal(getTotalResponses(is, eventID), 0)
const respondTimes = 5
for i := 1; i <= respondTimes; i++ {
createEmptyResponse(is, eventID)
is.Equal(getTotalResponses(is, eventID), i)
}
})
t.Run("TotalResponses does not count responses to other events", func(t *testing.T) {
is := is.New(t)
eventID := createEvent(is)
createEmptyResponse(is, eventID)
// Create a response to another event
createEmptyResponse(is, createEvent(is))
is.Equal(getTotalResponses(is, eventID), 1)
})
2020-04-21 14:25:15 +00:00
}
2020-09-27 21:44:04 +00:00
func pending(t *testing.T) {
t.Fatalf("pending")
}
func TestCreateEventResponse(t *testing.T) {
store, err := back.NewMemoryStore(back.SecureGenString)
is.New(t).NoErr(err)
createEvent := func(is *is.I) (eventID string) {
event, err := store.CreateEvent(context.Background(), back.CreateEventCommand{
Name: "blah",
Description: "stuff happening",
Earliest: date.Today(),
Latest: date.Today().Add(1),
})
is.NoErr(err)
return event.AlphaID
}
t.Run("saves GuestName", func(t *testing.T) {
is := is.New(t)
eventID := createEvent(is)
const guestName = "Etaoin Shrdlu"
createResult, err := store.CreateEventResponse(context.Background(), back.CreateEventResponseCommand{
EventAlphaID: eventID,
GuestName: guestName,
})
is.NoErr(err)
getResult, err := store.GetEventResponse(context.Background(), back.GetEventResponseQuery{
EventAlphaID: eventID,
ResponseAlphaID: createResult.ResponseAlphaID,
})
is.NoErr(err)
is.Equal(getResult.GuestName, guestName)
})
t.Run("DateHours saves multiple times on multiple dates", func(t *testing.T) {
is := is.New(t)
earliest := date.Today()
latest := earliest.Add(1)
event, err := store.CreateEvent(context.Background(), back.CreateEventCommand{
Name: "blah",
Description: "stuff happening",
Earliest: earliest,
Latest: latest,
})
is.NoErr(err)
var dateHours = map[date.Date]map[int]struct{}{
earliest: {1: {}, 3: {}, 5: {}, 7: {}},
latest: {2: {}, 4: {}, 6: {}},
}
createResponseResult, err := store.CreateEventResponse(context.Background(), back.CreateEventResponseCommand{
EventAlphaID: event.AlphaID,
GuestName: "Etaoin Shrdlu",
DateHours: dateHours,
})
is.NoErr(err)
response, err := store.GetEventResponse(context.Background(), back.GetEventResponseQuery{
EventAlphaID: event.AlphaID,
ResponseAlphaID: createResponseResult.ResponseAlphaID,
})
is.NoErr(err)
is.Equal(dateHours, response.DateHours)
})
}