-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: server: factory methods (#263)
- Loading branch information
Showing
15 changed files
with
724 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Oops, something went wrong.