package back_test import ( "context" "testing" "time" "github.com/matryer/is" "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() earliest := time.Date(2020, 04, 05, 0, 0, 0, 0, time.UTC) latest := time.Date(2020, 04, 05, 0, 0, 0, 0, time.UTC) const name = "abc" const description = "def" createResult, err := store.CreateEvent(context.Background(), back.CreateEventCommand{ Name: name, Description: description, EarliestDate: earliest, LatestDate: latest, }) 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) is.True(metadataResult.EarliestDate.Equal(earliest)) is.True(metadataResult.LatestDate.Equal(latest)) } func TestGetEventResponses(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", EarliestDate: time.Now(), LatestDate: time.Now().AddDate(0,0,1), }) is.NoErr(err) return event.AlphaID } getTotalResponses := func(is *is.I, eventID string) int { responses, err := store.GetEventResponses(context.Background(), back.GetEventResponsesQuery{ 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) }) }