All checks were successful
Server Check Build (NixCN CMS) TeamCity build finished
Signed-off-by: Asai Neko <sugar@sne.moe>
234 lines
5.7 KiB
Go
234 lines
5.7 KiB
Go
package data_test
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"nixcn-cms/data"
|
|
"nixcn-cms/testutil"
|
|
)
|
|
|
|
func seedAgendaFixture(t *testing.T, ctx context.Context) (eventId, attendanceId uuid.UUID) {
|
|
t.Helper()
|
|
eId, uId := seedEventAndUser(t, ctx)
|
|
a := seedAttendance(t, ctx, eId, uId)
|
|
return eId, a.AttendanceId
|
|
}
|
|
|
|
func TestAgendaCreate(t *testing.T) {
|
|
testutil.Setup(t)
|
|
ctx := context.Background()
|
|
|
|
_, attId := seedAgendaFixture(t, ctx)
|
|
|
|
ag := data.NewAgenda(
|
|
data.WithAttendanceId(attId),
|
|
data.WithAgendaName("My Talk"),
|
|
data.WithAgendaDescription("base64content"),
|
|
data.WithAgendaStatus("pending"),
|
|
)
|
|
require.NoError(t, ag.Create(ctx))
|
|
assert.NotEqual(t, uuid.Nil, ag.AgendaId)
|
|
}
|
|
|
|
func TestAgendaGetByAgendaId(t *testing.T) {
|
|
testutil.Setup(t)
|
|
ctx := context.Background()
|
|
|
|
_, attId := seedAgendaFixture(t, ctx)
|
|
ag := data.NewAgenda(
|
|
data.WithAttendanceId(attId),
|
|
data.WithAgendaName("Talk A"),
|
|
data.WithAgendaDescription("desc"),
|
|
data.WithAgendaStatus("pending"),
|
|
)
|
|
require.NoError(t, ag.Create(ctx))
|
|
|
|
got, err := new(data.Agenda).GetByAgendaId(ctx, ag.AgendaId)
|
|
require.NoError(t, err)
|
|
require.NotNil(t, got)
|
|
assert.Equal(t, "Talk A", got.Name)
|
|
}
|
|
|
|
func TestAgendaGetByAgendaIdNotFound(t *testing.T) {
|
|
testutil.Setup(t)
|
|
ctx := context.Background()
|
|
|
|
got, err := new(data.Agenda).GetByAgendaId(ctx, uuid.New())
|
|
require.NoError(t, err)
|
|
assert.Nil(t, got)
|
|
}
|
|
|
|
func TestAgendaGetListByAttendanceId(t *testing.T) {
|
|
testutil.Setup(t)
|
|
ctx := context.Background()
|
|
|
|
_, attId := seedAgendaFixture(t, ctx)
|
|
|
|
for i := 0; i < 3; i++ {
|
|
ag := data.NewAgenda(
|
|
data.WithAttendanceId(attId),
|
|
data.WithAgendaName(uuid.New().String()),
|
|
data.WithAgendaDescription("desc"),
|
|
data.WithAgendaStatus("pending"),
|
|
)
|
|
require.NoError(t, ag.Create(ctx))
|
|
}
|
|
|
|
list, err := new(data.Agenda).GetListByAttendanceId(ctx, attId)
|
|
require.NoError(t, err)
|
|
assert.Len(t, *list, 3)
|
|
}
|
|
|
|
func TestAgendaGetListByEventId(t *testing.T) {
|
|
testutil.Setup(t)
|
|
ctx := context.Background()
|
|
|
|
evId, attId := seedAgendaFixture(t, ctx)
|
|
|
|
for i := 0; i < 2; i++ {
|
|
ag := data.NewAgenda(
|
|
data.WithAttendanceId(attId),
|
|
data.WithAgendaName(uuid.New().String()),
|
|
data.WithAgendaDescription("desc"),
|
|
data.WithAgendaStatus("approved"),
|
|
)
|
|
require.NoError(t, ag.Create(ctx))
|
|
}
|
|
|
|
list, err := new(data.Agenda).GetListByEventId(ctx, evId)
|
|
require.NoError(t, err)
|
|
assert.Len(t, *list, 2)
|
|
}
|
|
|
|
func TestAgendaGetScheduledByEventId(t *testing.T) {
|
|
testutil.Setup(t)
|
|
ctx := context.Background()
|
|
|
|
evId, attId := seedAgendaFixture(t, ctx)
|
|
|
|
now := time.Now()
|
|
ag1 := data.NewAgenda(
|
|
data.WithAttendanceId(attId),
|
|
data.WithAgendaName("Scheduled Talk"),
|
|
data.WithAgendaDescription("desc"),
|
|
data.WithAgendaStatus("approved"),
|
|
data.WithAgendaStartTime(now.Add(time.Hour)),
|
|
data.WithAgendaEndTime(now.Add(2*time.Hour)),
|
|
)
|
|
require.NoError(t, ag1.Create(ctx))
|
|
|
|
ag2 := data.NewAgenda(
|
|
data.WithAttendanceId(attId),
|
|
data.WithAgendaName("Pending Talk"),
|
|
data.WithAgendaDescription("desc"),
|
|
data.WithAgendaStatus("pending"),
|
|
)
|
|
require.NoError(t, ag2.Create(ctx))
|
|
|
|
list, err := new(data.Agenda).GetScheduledByEventId(ctx, evId)
|
|
require.NoError(t, err)
|
|
require.Len(t, *list, 1)
|
|
assert.Equal(t, ag1.AgendaId, (*list)[0].AgendaId)
|
|
}
|
|
|
|
func TestAgendaPatch(t *testing.T) {
|
|
testutil.Setup(t)
|
|
ctx := context.Background()
|
|
|
|
_, attId := seedAgendaFixture(t, ctx)
|
|
ag := data.NewAgenda(
|
|
data.WithAttendanceId(attId),
|
|
data.WithAgendaName("Original"),
|
|
data.WithAgendaDescription("desc"),
|
|
data.WithAgendaStatus("pending"),
|
|
)
|
|
require.NoError(t, ag.Create(ctx))
|
|
|
|
require.NoError(t, ag.PatchByAgendaId(ctx, ag.AgendaId,
|
|
data.WithAgendaName("Updated"),
|
|
data.WithAgendaStatus("approved"),
|
|
))
|
|
assert.Equal(t, "Updated", ag.Name)
|
|
assert.Equal(t, "approved", ag.Status)
|
|
}
|
|
|
|
func TestAgendaCountPendingByAttendanceId(t *testing.T) {
|
|
testutil.Setup(t)
|
|
ctx := context.Background()
|
|
|
|
_, attId := seedAgendaFixture(t, ctx)
|
|
|
|
for _, status := range []string{"pending", "pending", "approved"} {
|
|
ag := data.NewAgenda(
|
|
data.WithAttendanceId(attId),
|
|
data.WithAgendaName(uuid.New().String()),
|
|
data.WithAgendaDescription("desc"),
|
|
data.WithAgendaStatus(status),
|
|
)
|
|
require.NoError(t, ag.Create(ctx))
|
|
}
|
|
|
|
count, err := new(data.Agenda).CountPendingByAttendanceId(ctx, attId)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, int64(2), count)
|
|
}
|
|
|
|
func TestAgendaDelete(t *testing.T) {
|
|
testutil.Setup(t)
|
|
ctx := context.Background()
|
|
|
|
_, attId := seedAgendaFixture(t, ctx)
|
|
ag := data.NewAgenda(
|
|
data.WithAttendanceId(attId),
|
|
data.WithAgendaName("Delete me"),
|
|
data.WithAgendaDescription("desc"),
|
|
data.WithAgendaStatus("pending"),
|
|
)
|
|
require.NoError(t, ag.Create(ctx))
|
|
|
|
require.NoError(t, new(data.Agenda).Delete(ctx, ag.AgendaId))
|
|
|
|
got, err := new(data.Agenda).GetByAgendaId(ctx, ag.AgendaId)
|
|
require.NoError(t, err)
|
|
assert.Nil(t, got)
|
|
}
|
|
|
|
func TestAgendaCountByEventId(t *testing.T) {
|
|
testutil.Setup(t)
|
|
ctx := context.Background()
|
|
|
|
evId, attId := seedAgendaFixture(t, ctx)
|
|
|
|
for _, status := range []string{"pending", "approved", "rejected"} {
|
|
ag := data.NewAgenda(
|
|
data.WithAttendanceId(attId),
|
|
data.WithAgendaName(uuid.New().String()),
|
|
data.WithAgendaDescription("desc"),
|
|
data.WithAgendaStatus(status),
|
|
)
|
|
require.NoError(t, ag.Create(ctx))
|
|
}
|
|
|
|
count, err := new(data.Agenda).CountByEventId(ctx, evId)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, int64(3), count)
|
|
}
|
|
|
|
func TestAgendaCountByEventIdEmpty(t *testing.T) {
|
|
testutil.Setup(t)
|
|
ctx := context.Background()
|
|
|
|
owner := uuid.New()
|
|
ev := makeEvent(owner)
|
|
require.NoError(t, ev.Create(ctx))
|
|
|
|
count, err := new(data.Agenda).CountByEventId(ctx, ev.EventId)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, int64(0), count)
|
|
}
|