diff --git a/server/testing/factory/factory_auth.go b/server/testing/factory/auth.go similarity index 95% rename from server/testing/factory/factory_auth.go rename to server/testing/factory/auth.go index 0dd8a653..56b359c3 100644 --- a/server/testing/factory/factory_auth.go +++ b/server/testing/factory/auth.go @@ -38,7 +38,7 @@ func (f *Factory) NewAuth(opts ...AuthOpt) *orm.Auth { } boil.DebugMode = f.debug - if err := m.Insert(context.Background(), f.db, boil.Infer()); err != nil { + if err = m.Insert(context.Background(), f.db, boil.Infer()); err != nil { panic(fmt.Errorf("failed to insert user: %w", err)) } boil.DebugMode = false diff --git a/server/testing/factory/auth_test.go b/server/testing/factory/auth_test.go new file mode 100644 index 00000000..69019cca --- /dev/null +++ b/server/testing/factory/auth_test.go @@ -0,0 +1,97 @@ +//nolint:contextcheck +package factory_test + +import ( + "context" + "testing" + "time" + + "github.com/brianvoe/gofakeit/v7" + "github.com/google/uuid" + "github.com/stretchr/testify/require" + "golang.org/x/crypto/bcrypt" + + "github.com/crlssn/getstronger/server/gen/orm" + "github.com/crlssn/getstronger/server/testing/container" + "github.com/crlssn/getstronger/server/testing/factory" +) + +func TestFactory_Auth(t *testing.T) { + t.Parallel() + + ctx := context.Background() + c := container.NewContainer(ctx) + f := factory.NewFactory(c.DB) + + t.Run("Default", func(t *testing.T) { + t.Parallel() + expected := f.NewAuth() + created, err := orm.FindAuth(ctx, c.DB, expected.ID) + require.NoError(t, err) + require.Equal(t, expected.ID, created.ID) + require.Equal(t, expected.Email, created.Email) + require.Equal(t, expected.Password, created.Password) + require.Equal(t, expected.RefreshToken, created.RefreshToken) + require.Equal(t, expected.CreatedAt.Truncate(time.Millisecond), created.CreatedAt.Truncate(time.Millisecond)) + require.Equal(t, expected.EmailVerified, created.EmailVerified) + require.Equal(t, expected.EmailToken, created.EmailToken) + require.Equal(t, expected.PasswordResetToken, created.PasswordResetToken) + }) + + t.Run("AuthID", func(t *testing.T) { + t.Parallel() + id := uuid.NewString() + expected := f.NewAuth(factory.AuthID(id)) + created, err := orm.FindAuth(ctx, c.DB, expected.ID) + require.NoError(t, err) + require.Equal(t, id, created.ID) + }) + + t.Run("AuthEmail", func(t *testing.T) { + t.Parallel() + email := gofakeit.Email() + expected := f.NewAuth(factory.AuthEmail(email)) + created, err := orm.FindAuth(ctx, c.DB, expected.ID) + require.NoError(t, err) + require.Equal(t, email, created.Email) + }) + + t.Run("AuthPassword", func(t *testing.T) { + t.Parallel() + refreshToken := uuid.NewString() + expected := f.NewAuth(factory.AuthRefreshToken(refreshToken)) + created, err := orm.FindAuth(ctx, c.DB, expected.ID) + require.NoError(t, err) + require.Equal(t, refreshToken, created.RefreshToken.String) + }) + + t.Run("AuthEmailVerified", func(t *testing.T) { + t.Parallel() + passwordResetToken := uuid.NewString() + expected := f.NewAuth(factory.AuthPasswordResetToken(passwordResetToken)) + created, err := orm.FindAuth(ctx, c.DB, expected.ID) + require.NoError(t, err) + require.Equal(t, passwordResetToken, created.PasswordResetToken.String) + }) + + t.Run("AuthRefreshToken", func(t *testing.T) { + t.Parallel() + password := uuid.NewString() + expected := f.NewAuth(factory.AuthPassword(password)) + created, err := orm.FindAuth(ctx, c.DB, expected.ID) + require.NoError(t, err) + require.NoError(t, bcrypt.CompareHashAndPassword(created.Password, []byte(password))) + }) + + t.Run("AuthEmailVerified", func(t *testing.T) { + t.Parallel() + expected := f.NewAuth(factory.AuthEmailVerified()) + created, err := orm.FindAuth(ctx, c.DB, expected.ID) + require.NoError(t, err) + require.True(t, created.EmailVerified) + }) + + t.Cleanup(func() { + require.NoError(t, c.Terminate(ctx)) + }) +} diff --git a/server/testing/factory/factory_exercise.go b/server/testing/factory/exercise.go similarity index 100% rename from server/testing/factory/factory_exercise.go rename to server/testing/factory/exercise.go diff --git a/server/testing/factory/exercise_test.go b/server/testing/factory/exercise_test.go new file mode 100644 index 00000000..415535ba --- /dev/null +++ b/server/testing/factory/exercise_test.go @@ -0,0 +1,86 @@ +//nolint:contextcheck +package factory_test + +import ( + "context" + "testing" + "time" + + "github.com/brianvoe/gofakeit/v7" + "github.com/google/uuid" + "github.com/stretchr/testify/require" + "github.com/volatiletech/null/v8" + + "github.com/crlssn/getstronger/server/gen/orm" + "github.com/crlssn/getstronger/server/testing/container" + "github.com/crlssn/getstronger/server/testing/factory" +) + +func TestFactory_Exercise(t *testing.T) { + t.Parallel() + + ctx := context.Background() + c := container.NewContainer(ctx) + f := factory.NewFactory(c.DB) + + t.Run("Default", func(t *testing.T) { + t.Parallel() + expected := f.NewExercise() + created, err := orm.FindExercise(ctx, c.DB, expected.ID) + require.NoError(t, err) + require.Equal(t, expected.ID, created.ID) + require.Equal(t, expected.UserID, created.UserID) + require.Equal(t, expected.Title, created.Title) + require.Equal(t, expected.SubTitle, created.SubTitle) + require.False(t, expected.DeletedAt.Valid, created.DeletedAt.Valid) + }) + + t.Run("ExerciseID", func(t *testing.T) { + t.Parallel() + id := uuid.NewString() + expected := f.NewExercise(factory.ExerciseID(id)) + created, err := orm.FindExercise(ctx, c.DB, expected.ID) + require.NoError(t, err) + require.Equal(t, id, created.ID) + }) + + t.Run("ExerciseUserID", func(t *testing.T) { + t.Parallel() + userID := f.NewUser().ID + expected := f.NewExercise(factory.ExerciseUserID(userID)) + created, err := orm.FindExercise(ctx, c.DB, expected.ID) + require.NoError(t, err) + require.Equal(t, userID, created.UserID) + }) + + t.Run("ExerciseTitle", func(t *testing.T) { + t.Parallel() + title := gofakeit.Name() + expected := f.NewExercise(factory.ExerciseTitle(title)) + created, err := orm.FindExercise(ctx, c.DB, expected.ID) + require.NoError(t, err) + require.Equal(t, title, created.Title) + }) + + t.Run("ExerciseSubTitle", func(t *testing.T) { + t.Parallel() + subTitle := null.StringFrom(gofakeit.Name()) + expected := f.NewExercise(factory.ExerciseSubTitle(subTitle)) + created, err := orm.FindExercise(ctx, c.DB, expected.ID) + require.NoError(t, err) + require.Equal(t, subTitle, created.SubTitle) + }) + + t.Run("ExerciseDeleted", func(t *testing.T) { + t.Parallel() + expected := f.NewExercise(factory.ExerciseDeleted()) + created, err := orm.FindExercise(ctx, c.DB, expected.ID) + require.NoError(t, err) + require.True(t, created.DeletedAt.Valid) + require.WithinDuration(t, time.Now().UTC(), created.DeletedAt.Time, time.Second) + }) + + t.Cleanup(func() { + require.NoError(t, c.Terminate(ctx)) + }) +} diff --git a/server/testing/factory/factory_test.go b/server/testing/factory/factory_test.go new file mode 100644 index 00000000..d8b0e7fd --- /dev/null +++ b/server/testing/factory/factory_test.go @@ -0,0 +1,16 @@ +package factory_test + +import ( + "testing" + + "github.com/stretchr/testify/require" + + "github.com/crlssn/getstronger/server/testing/factory" +) + +func TestUUID(t *testing.T) { + t.Parallel() + + require.Equal(t, "00000000-0000-0000-0000-000000000000", factory.UUID(0)) + require.Equal(t, "11111111-1111-1111-1111-111111111111", factory.UUID(1)) +} diff --git a/server/testing/factory/factory_notification.go b/server/testing/factory/notification.go similarity index 97% rename from server/testing/factory/factory_notification.go rename to server/testing/factory/notification.go index b75fe0c9..2ba0aebd 100644 --- a/server/testing/factory/factory_notification.go +++ b/server/testing/factory/notification.go @@ -15,35 +15,6 @@ import ( type NotificationOpt func(notification *orm.Notification) -func NotificationUserID(userID string) NotificationOpt { - return func(notification *orm.Notification) { - notification.UserID = userID - } -} - -func NotificationPayload(payload repo.NotificationPayload) NotificationOpt { - return func(notification *orm.Notification) { - p, err := json.Marshal(payload) - if err != nil { - panic(fmt.Errorf("failed to marshal payload: %w", err)) - } - - notification.Payload = p - } -} - -func NotificationType(t orm.NotificationType) NotificationOpt { - return func(notification *orm.Notification) { - notification.Type = t - } -} - -func NotificationRead() NotificationOpt { - return func(notification *orm.Notification) { - notification.ReadAt = null.TimeFrom(time.Now()) - } -} - func (f *Factory) NewNotification(opts ...NotificationOpt) *orm.Notification { m := &orm.Notification{ ID: "", @@ -69,6 +40,10 @@ func (f *Factory) NewNotification(opts ...NotificationOpt) *orm.Notification { })) } + if m.Payload == nil { + m.Payload = []byte("{}") + } + boil.DebugMode = f.debug if err := m.Insert(context.Background(), f.db, boil.Infer()); err != nil { panic(fmt.Errorf("failed to insert notification: %w", err)) @@ -78,6 +53,35 @@ func (f *Factory) NewNotification(opts ...NotificationOpt) *orm.Notification { return m } +func NotificationUserID(userID string) NotificationOpt { + return func(notification *orm.Notification) { + notification.UserID = userID + } +} + +func NotificationPayload(payload repo.NotificationPayload) NotificationOpt { + return func(notification *orm.Notification) { + p, err := json.Marshal(payload) + if err != nil { + panic(fmt.Errorf("failed to marshal payload: %w", err)) + } + + notification.Payload = p + } +} + +func NotificationType(t orm.NotificationType) NotificationOpt { + return func(notification *orm.Notification) { + notification.Type = t + } +} + +func NotificationRead() NotificationOpt { + return func(notification *orm.Notification) { + notification.ReadAt = null.TimeFrom(time.Now()) + } +} + func NotificationID(id string) NotificationOpt { return func(notification *orm.Notification) { notification.ID = id diff --git a/server/testing/factory/notification_test.go b/server/testing/factory/notification_test.go new file mode 100644 index 00000000..dadfe5b0 --- /dev/null +++ b/server/testing/factory/notification_test.go @@ -0,0 +1,101 @@ +//nolint:contextcheck +package factory_test + +import ( + "context" + "encoding/json" + "testing" + "time" + + "github.com/google/uuid" + "github.com/stretchr/testify/require" + + "github.com/crlssn/getstronger/server/gen/orm" + "github.com/crlssn/getstronger/server/repo" + "github.com/crlssn/getstronger/server/testing/container" + "github.com/crlssn/getstronger/server/testing/factory" +) + +func TestFactory_Notification(t *testing.T) { + t.Parallel() + + ctx := context.Background() + c := container.NewContainer(ctx) + f := factory.NewFactory(c.DB) + + t.Run("Default", func(t *testing.T) { + t.Parallel() + expected := f.NewNotification() + created, err := orm.FindNotification(ctx, c.DB, expected.ID) + require.NoError(t, err) + require.Equal(t, expected.ID, created.ID) + require.Equal(t, expected.UserID, created.UserID) + require.Equal(t, expected.Type, created.Type) + require.Equal(t, expected.Payload, created.Payload) + require.Equal(t, expected.ReadAt, created.ReadAt) + require.Equal(t, expected.CreatedAt.Truncate(time.Millisecond), created.CreatedAt.Truncate(time.Millisecond)) + }) + + t.Run("NotificationID", func(t *testing.T) { + t.Parallel() + id := uuid.NewString() + expected := f.NewNotification(factory.NotificationID(id)) + created, err := orm.FindNotification(ctx, c.DB, expected.ID) + require.NoError(t, err) + require.Equal(t, id, created.ID) + }) + + t.Run("NotificationUserID", func(t *testing.T) { + t.Parallel() + userID := f.NewUser().ID + expected := f.NewNotification(factory.NotificationUserID(userID)) + created, err := orm.FindNotification(ctx, c.DB, expected.ID) + require.NoError(t, err) + require.Equal(t, userID, created.UserID) + }) + + t.Run("NotificationType", func(t *testing.T) { + t.Parallel() + notificationType := orm.NotificationTypeFollow + expected := f.NewNotification(factory.NotificationType(notificationType)) + created, err := orm.FindNotification(ctx, c.DB, expected.ID) + require.NoError(t, err) + require.Equal(t, notificationType, created.Type) + }) + + t.Run("NotificationPayload", func(t *testing.T) { + t.Parallel() + payload := repo.NotificationPayload{ + ActorID: uuid.NewString(), + WorkoutID: uuid.NewString(), + } + expected := f.NewNotification(factory.NotificationPayload(payload)) + created, err := orm.FindNotification(ctx, c.DB, expected.ID) + require.NoError(t, err) + var createdPayload repo.NotificationPayload + require.NoError(t, json.Unmarshal(created.Payload, &createdPayload)) + require.Equal(t, payload, createdPayload) + }) + + t.Run("NotificationRead", func(t *testing.T) { + t.Parallel() + expected := f.NewNotification(factory.NotificationRead()) + created, err := orm.FindNotification(ctx, c.DB, expected.ID) + require.NoError(t, err) + require.True(t, created.ReadAt.Valid) + require.WithinDuration(t, time.Now().UTC(), created.ReadAt.Time, time.Second) + }) + + t.Run("NotificationCreatedAt", func(t *testing.T) { + t.Parallel() + createdAt := time.Now().Add(-24 * time.Hour) + expected := f.NewNotification(factory.NotificationCreatedAt(createdAt)) + created, err := orm.FindNotification(ctx, c.DB, expected.ID) + require.NoError(t, err) + require.WithinDuration(t, createdAt, created.CreatedAt, time.Second) + }) + + t.Cleanup(func() { + require.NoError(t, c.Terminate(ctx)) + }) +} diff --git a/server/testing/factory/factory_routine.go b/server/testing/factory/routine.go similarity index 100% rename from server/testing/factory/factory_routine.go rename to server/testing/factory/routine.go diff --git a/server/testing/factory/routine_test.go b/server/testing/factory/routine_test.go new file mode 100644 index 00000000..a4e38f5f --- /dev/null +++ b/server/testing/factory/routine_test.go @@ -0,0 +1,107 @@ +//nolint:contextcheck +package factory_test + +import ( + "context" + "encoding/json" + "testing" + "time" + + "github.com/google/uuid" + "github.com/stretchr/testify/require" + + "github.com/crlssn/getstronger/server/gen/orm" + "github.com/crlssn/getstronger/server/testing/container" + "github.com/crlssn/getstronger/server/testing/factory" +) + +func TestFactory_Routine(t *testing.T) { + t.Parallel() + + ctx := context.Background() + c := container.NewContainer(ctx) + f := factory.NewFactory(c.DB) + + t.Run("Default", func(t *testing.T) { + t.Parallel() + expected := f.NewRoutine() + created, err := orm.FindRoutine(ctx, c.DB, expected.ID) + require.NoError(t, err) + require.Equal(t, expected.ID, created.ID) + require.Equal(t, expected.UserID, created.UserID) + require.Equal(t, expected.Title, created.Title) + require.Equal(t, expected.ExerciseOrder, created.ExerciseOrder) + require.Equal(t, expected.DeletedAt.Valid, created.DeletedAt.Valid) + require.Equal(t, expected.CreatedAt.Truncate(time.Millisecond), created.CreatedAt.Truncate(time.Millisecond)) + }) + + t.Run("RoutineID", func(t *testing.T) { + t.Parallel() + id := uuid.NewString() + expected := f.NewRoutine(factory.RoutineID(id)) + created, err := orm.FindRoutine(ctx, c.DB, expected.ID) + require.NoError(t, err) + require.Equal(t, id, created.ID) + }) + + t.Run("RoutineUserID", func(t *testing.T) { + t.Parallel() + userID := f.NewUser().ID + expected := f.NewRoutine(factory.RoutineUserID(userID)) + created, err := orm.FindRoutine(ctx, c.DB, expected.ID) + require.NoError(t, err) + require.Equal(t, userID, created.UserID) + }) + + t.Run("RoutineName", func(t *testing.T) { + t.Parallel() + name := "Custom Routine Name" + expected := f.NewRoutine(factory.RoutineName(name)) + created, err := orm.FindRoutine(ctx, c.DB, expected.ID) + require.NoError(t, err) + require.Equal(t, name, created.Title) + }) + + t.Run("RoutineExerciseOrder", func(t *testing.T) { + t.Parallel() + exerciseIDs := []string{uuid.NewString(), uuid.NewString()} + expected := f.NewRoutine(factory.RoutineExerciseOrder(exerciseIDs)) + created, err := orm.FindRoutine(ctx, c.DB, expected.ID) + require.NoError(t, err) + + var createdOrder []string + require.NoError(t, json.Unmarshal(created.ExerciseOrder, &createdOrder)) + require.Equal(t, exerciseIDs, createdOrder) + }) + + t.Run("AddRoutineExercise", func(t *testing.T) { + t.Parallel() + routine := f.NewRoutine() + exercises := []*orm.Exercise{ + f.NewExercise(), + f.NewExercise(), + } + + f.AddRoutineExercise(routine, exercises...) + + require.NoError(t, routine.Reload(ctx, c.DB)) + routineExercises, err := routine.Exercises().All(ctx, c.DB) + require.NoError(t, err) + require.Len(t, routineExercises, len(exercises)) + + for _, exercise := range exercises { + found := false + for _, routineExercise := range routineExercises { + if exercise.ID == routineExercise.ID { + found = true + break + } + } + require.True(t, found, "Exercise should be associated with the routine") + } + }) + + t.Cleanup(func() { + require.NoError(t, c.Terminate(ctx)) + }) +} diff --git a/server/testing/factory/factory_set.go b/server/testing/factory/set.go similarity index 100% rename from server/testing/factory/factory_set.go rename to server/testing/factory/set.go diff --git a/server/testing/factory/set_test.go b/server/testing/factory/set_test.go new file mode 100644 index 00000000..bb32e780 --- /dev/null +++ b/server/testing/factory/set_test.go @@ -0,0 +1,84 @@ +//nolint:contextcheck +package factory_test + +import ( + "context" + "testing" + "time" + + "github.com/stretchr/testify/require" + + "github.com/crlssn/getstronger/server/gen/orm" + "github.com/crlssn/getstronger/server/testing/container" + "github.com/crlssn/getstronger/server/testing/factory" +) + +func TestFactory_Set(t *testing.T) { + t.Parallel() + + ctx := context.Background() + c := container.NewContainer(ctx) + f := factory.NewFactory(c.DB) + + t.Run("Default", func(t *testing.T) { + t.Parallel() + expected := f.NewSet() + created, err := orm.FindSet(ctx, c.DB, expected.ID) + require.NoError(t, err) + require.Equal(t, expected.ID, created.ID) + require.Equal(t, expected.WorkoutID, created.WorkoutID) + require.Equal(t, expected.ExerciseID, created.ExerciseID) + require.InEpsilon(t, expected.Weight, created.Weight, 0) + require.Equal(t, expected.Reps, created.Reps) + require.Equal(t, expected.CreatedAt.Truncate(time.Millisecond), created.CreatedAt.Truncate(time.Millisecond)) + }) + + t.Run("SetWorkoutID", func(t *testing.T) { + t.Parallel() + workoutID := f.NewWorkout().ID + expected := f.NewSet(factory.SetWorkoutID(workoutID)) + created, err := orm.FindSet(ctx, c.DB, expected.ID) + require.NoError(t, err) + require.Equal(t, workoutID, created.WorkoutID) + }) + + t.Run("SetExerciseID", func(t *testing.T) { + t.Parallel() + exerciseID := f.NewExercise().ID + expected := f.NewSet(factory.SetExerciseID(exerciseID)) + created, err := orm.FindSet(ctx, c.DB, expected.ID) + require.NoError(t, err) + require.Equal(t, exerciseID, created.ExerciseID) + }) + + t.Run("SetReps", func(t *testing.T) { + t.Parallel() + reps := 12 + expected := f.NewSet(factory.SetReps(reps)) + created, err := orm.FindSet(ctx, c.DB, expected.ID) + require.NoError(t, err) + require.Equal(t, reps, created.Reps) + }) + + t.Run("SetWeight", func(t *testing.T) { + t.Parallel() + weight := 75.5 + expected := f.NewSet(factory.SetWeight(weight)) + created, err := orm.FindSet(ctx, c.DB, expected.ID) + require.NoError(t, err) + require.InEpsilon(t, weight, created.Weight, 0) + }) + + t.Run("SetCreatedAt", func(t *testing.T) { + t.Parallel() + createdAt := time.Now().Add(-24 * time.Hour) + expected := f.NewSet(factory.SetCreatedAt(createdAt)) + created, err := orm.FindSet(ctx, c.DB, expected.ID) + require.NoError(t, err) + require.WithinDuration(t, createdAt, created.CreatedAt, time.Second) + }) + + t.Cleanup(func() { + require.NoError(t, c.Terminate(ctx)) + }) +} diff --git a/server/testing/factory/factory_user.go b/server/testing/factory/user.go similarity index 100% rename from server/testing/factory/factory_user.go rename to server/testing/factory/user.go diff --git a/server/testing/factory/user_test.go b/server/testing/factory/user_test.go new file mode 100644 index 00000000..49dd11d5 --- /dev/null +++ b/server/testing/factory/user_test.go @@ -0,0 +1,76 @@ +//nolint:contextcheck +package factory_test + +import ( + "context" + "testing" + "time" + + "github.com/brianvoe/gofakeit/v7" + "github.com/google/uuid" + "github.com/stretchr/testify/require" + + "github.com/crlssn/getstronger/server/gen/orm" + "github.com/crlssn/getstronger/server/testing/container" + "github.com/crlssn/getstronger/server/testing/factory" +) + +func TestFactory_User(t *testing.T) { + t.Parallel() + + ctx := context.Background() + c := container.NewContainer(ctx) + f := factory.NewFactory(c.DB) + + t.Run("Default", func(t *testing.T) { + t.Parallel() + expected := f.NewUser() + created, err := orm.FindUser(ctx, c.DB, expected.ID) + require.NoError(t, err) + require.Equal(t, expected.ID, created.ID) + require.Equal(t, expected.AuthID, created.AuthID) + require.Equal(t, expected.FirstName, created.FirstName) + require.Equal(t, expected.LastName, created.LastName) + require.Equal(t, expected.CreatedAt.Truncate(time.Millisecond), created.CreatedAt.Truncate(time.Millisecond)) + }) + + t.Run("UserID", func(t *testing.T) { + t.Parallel() + id := uuid.NewString() + expected := f.NewUser(factory.UserID(id)) + created, err := orm.FindUser(ctx, c.DB, expected.ID) + require.NoError(t, err) + require.Equal(t, id, created.ID) + }) + + t.Run("UserAuthID", func(t *testing.T) { + t.Parallel() + authID := f.NewAuth().ID + expected := f.NewUser(factory.UserAuthID(authID)) + created, err := orm.FindUser(ctx, c.DB, expected.ID) + require.NoError(t, err) + require.Equal(t, authID, created.AuthID) + }) + + t.Run("UserFirstName", func(t *testing.T) { + t.Parallel() + firstName := gofakeit.FirstName() + expected := f.NewUser(factory.UserFirstName(firstName)) + created, err := orm.FindUser(ctx, c.DB, expected.ID) + require.NoError(t, err) + require.Equal(t, firstName, created.FirstName) + }) + + t.Run("UserLastName", func(t *testing.T) { + t.Parallel() + lastName := gofakeit.LastName() + expected := f.NewUser(factory.UserLastName(lastName)) + created, err := orm.FindUser(ctx, c.DB, expected.ID) + require.NoError(t, err) + require.Equal(t, lastName, created.LastName) + }) + + t.Cleanup(func() { + require.NoError(t, c.Terminate(ctx)) + }) +} diff --git a/server/testing/factory/factory_workout.go b/server/testing/factory/workout.go similarity index 96% rename from server/testing/factory/factory_workout.go rename to server/testing/factory/workout.go index 9aba3d3f..a468207b 100644 --- a/server/testing/factory/factory_workout.go +++ b/server/testing/factory/workout.go @@ -14,12 +14,13 @@ import ( type WorkoutOpt func(workout *orm.Workout) func (f *Factory) NewWorkout(opts ...WorkoutOpt) *orm.Workout { + startedAt := f.faker.Date() m := &orm.Workout{ ID: uuid.NewString(), Name: f.faker.RandomString([]string{"Legs", "Chest", "Back", "Shoulders", "Arms", "Push", "Pull", "Upper Body", "Lower Body", "Full Body"}), UserID: "", - StartedAt: f.faker.Date(), - FinishedAt: f.faker.Date(), + StartedAt: startedAt, + FinishedAt: startedAt.Add(time.Hour), CreatedAt: time.Time{}, } diff --git a/server/testing/factory/workout_test.go b/server/testing/factory/workout_test.go new file mode 100644 index 00000000..490141db --- /dev/null +++ b/server/testing/factory/workout_test.go @@ -0,0 +1,120 @@ +//nolint:contextcheck +package factory_test + +import ( + "context" + "testing" + "time" + + "github.com/brianvoe/gofakeit/v7" + "github.com/google/uuid" + "github.com/stretchr/testify/require" + + "github.com/crlssn/getstronger/server/gen/orm" + "github.com/crlssn/getstronger/server/testing/container" + "github.com/crlssn/getstronger/server/testing/factory" +) + +func TestFactory_Workout(t *testing.T) { + t.Parallel() + + ctx := context.Background() + c := container.NewContainer(ctx) + f := factory.NewFactory(c.DB) + + t.Run("Default", func(t *testing.T) { + t.Parallel() + expected := f.NewWorkout() + created, err := orm.FindWorkout(ctx, c.DB, expected.ID) + require.NoError(t, err) + require.Equal(t, expected.ID, created.ID) + require.Equal(t, expected.UserID, created.UserID) + require.Equal(t, expected.Name, created.Name) + require.Equal(t, expected.StartedAt.Truncate(time.Millisecond), created.StartedAt.Truncate(time.Millisecond)) + require.Equal(t, expected.FinishedAt.Truncate(time.Millisecond), created.FinishedAt.Truncate(time.Millisecond)) + require.Equal(t, expected.CreatedAt.Truncate(time.Millisecond), created.CreatedAt.Truncate(time.Millisecond)) + require.True(t, created.StartedAt.Before(created.FinishedAt)) + }) + + t.Run("WorkoutID", func(t *testing.T) { + t.Parallel() + id := uuid.NewString() + expected := f.NewWorkout(factory.WorkoutID(id)) + created, err := orm.FindWorkout(ctx, c.DB, expected.ID) + require.NoError(t, err) + require.Equal(t, id, created.ID) + }) + + t.Run("WorkoutUserID", func(t *testing.T) { + t.Parallel() + userID := f.NewUser().ID + expected := f.NewWorkout(factory.WorkoutUserID(userID)) + created, err := orm.FindWorkout(ctx, c.DB, expected.ID) + require.NoError(t, err) + require.Equal(t, userID, created.UserID) + }) + + t.Run("WorkoutName", func(t *testing.T) { + t.Parallel() + name := gofakeit.Name() + expected := f.NewWorkout(factory.WorkoutName(name)) + created, err := orm.FindWorkout(ctx, c.DB, expected.ID) + require.NoError(t, err) + require.Equal(t, name, created.Name) + }) + + t.Run("WorkoutCreatedAt", func(t *testing.T) { + t.Parallel() + createdAt := time.Now().Add(-24 * time.Hour) + expected := f.NewWorkout(factory.WorkoutCreatedAt(createdAt)) + created, err := orm.FindWorkout(ctx, c.DB, expected.ID) + require.NoError(t, err) + require.WithinDuration(t, createdAt, created.CreatedAt, time.Second) + }) + + t.Cleanup(func() { + require.NoError(t, c.Terminate(ctx)) + }) +} + +func TestFactory_WorkoutComment(t *testing.T) { + t.Parallel() + + ctx := context.Background() + c := container.NewContainer(ctx) + f := factory.NewFactory(c.DB) + + t.Run("Default", func(t *testing.T) { + t.Parallel() + expected := f.NewWorkoutComment() + created, err := orm.FindWorkoutComment(ctx, c.DB, expected.ID) + require.NoError(t, err) + require.Equal(t, expected.ID, created.ID) + require.Equal(t, expected.UserID, created.UserID) + require.Equal(t, expected.WorkoutID, created.WorkoutID) + require.Equal(t, expected.Comment, created.Comment) + require.Equal(t, expected.CreatedAt.Truncate(time.Millisecond), created.CreatedAt.Truncate(time.Millisecond)) + }) + + t.Run("WorkoutCommentUserID", func(t *testing.T) { + t.Parallel() + userID := f.NewUser().ID + expected := f.NewWorkoutComment(factory.WorkoutCommentUserID(userID)) + created, err := orm.FindWorkoutComment(ctx, c.DB, expected.ID) + require.NoError(t, err) + require.Equal(t, userID, created.UserID) + }) + + t.Run("WorkoutCommentWorkoutID", func(t *testing.T) { + t.Parallel() + workoutID := f.NewWorkout().ID + expected := f.NewWorkoutComment(factory.WorkoutCommentWorkoutID(workoutID)) + created, err := orm.FindWorkoutComment(ctx, c.DB, expected.ID) + require.NoError(t, err) + require.Equal(t, workoutID, created.WorkoutID) + }) + + t.Cleanup(func() { + require.NoError(t, c.Terminate(ctx)) + }) +}