Skip to content

Commit

Permalink
test: server: factory methods (#263)
Browse files Browse the repository at this point in the history
  • Loading branch information
crlssn authored Dec 19, 2024
1 parent fe7f4ed commit b1d9b19
Show file tree
Hide file tree
Showing 15 changed files with 724 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
97 changes: 97 additions & 0 deletions server/testing/factory/auth_test.go
Original file line number Diff line number Diff line change
@@ -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))
})
}
File renamed without changes.
86 changes: 86 additions & 0 deletions server/testing/factory/exercise_test.go
Original file line number Diff line number Diff line change
@@ -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))
})
}
16 changes: 16 additions & 0 deletions server/testing/factory/factory_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
Original file line number Diff line number Diff line change
Expand Up @@ -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: "",
Expand All @@ -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))
Expand All @@ -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
Expand Down
101 changes: 101 additions & 0 deletions server/testing/factory/notification_test.go
Original file line number Diff line number Diff line change
@@ -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))
})
}
File renamed without changes.
Loading

0 comments on commit b1d9b19

Please sign in to comment.