Skip to content

Commit

Permalink
chore: memory stores used by test are now initialized with pointer va…
Browse files Browse the repository at this point in the history
…lues

It makes it easier to assert modification of an existing record without relying on `GetByID`
  • Loading branch information
YuukanOO committed Nov 16, 2023
1 parent fa9e1ea commit d63f0e5
Show file tree
Hide file tree
Showing 18 changed files with 110 additions and 136 deletions.
5 changes: 3 additions & 2 deletions internal/auth/app/command/create_first_account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ func Test_CreateFirstAccount(t *testing.T) {
hasher := infra.NewBCryptHasher()
keygen := infra.NewKeyGenerator()

createFirstAccount := func(existingUsers ...domain.User) (func(context.Context, command.CreateFirstAccountCommand) error, memory.UsersStore) {
createFirstAccount := func(existingUsers ...*domain.User) (func(context.Context, command.CreateFirstAccountCommand) error, memory.UsersStore) {
store := memory.NewUsersStore(existingUsers...)
return command.CreateFirstAccount(store, store, hasher, keygen), store
}

t.Run("should do nothing if a user already exists", func(t *testing.T) {
uc, store := createFirstAccount(domain.NewUser("[email protected]", "password", "apikey"))
usr := domain.NewUser("[email protected]", "password", "apikey")
uc, store := createFirstAccount(&usr)

err := uc(ctx, command.CreateFirstAccountCommand{})

Expand Down
7 changes: 4 additions & 3 deletions internal/auth/app/command/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
func Test_Login(t *testing.T) {
hasher := infra.NewBCryptHasher()
password, _ := hasher.Hash("password") // Sample password hash for the string "password" for tests
login := func(existingUsers ...domain.User) func(context.Context, command.LoginCommand) (string, error) {
login := func(existingUsers ...*domain.User) func(context.Context, command.LoginCommand) (string, error) {
store := memory.NewUsersStore(existingUsers...)
return command.Login(store, hasher)
}
Expand All @@ -42,7 +42,8 @@ func Test_Login(t *testing.T) {
})

t.Run("should complains if password does not match", func(t *testing.T) {
uc := login(domain.NewUser("[email protected]", password, "apikey"))
usr := domain.NewUser("[email protected]", password, "apikey")
uc := login(&usr)
_, err := uc(context.Background(), command.LoginCommand{
Email: "[email protected]",
Password: "nobodycares",
Expand All @@ -56,7 +57,7 @@ func Test_Login(t *testing.T) {

t.Run("should returns a valid user id if it succeeds", func(t *testing.T) {
existingUser := domain.NewUser("[email protected]", password, "apikey")
uc := login(existingUser)
uc := login(&existingUser)
uid, err := uc(context.Background(), command.LoginCommand{
Email: "[email protected]",
Password: "password",
Expand Down
18 changes: 9 additions & 9 deletions internal/auth/app/command/update_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ import (

func Test_UpdateUser(t *testing.T) {
hasher := infra.NewBCryptHasher()
update := func(existingUsers ...domain.User) (func(context.Context, command.UpdateUserCommand) error, memory.UsersStore) {
update := func(existingUsers ...*domain.User) func(context.Context, command.UpdateUserCommand) error {
store := memory.NewUsersStore(existingUsers...)
return command.UpdateUser(store, store, hasher), store
return command.UpdateUser(store, store, hasher)
}

t.Run("should require valid inputs", func(t *testing.T) {
uc, _ := update()
uc := update()
err := uc(context.Background(), command.UpdateUserCommand{})

testutil.ErrorIs(t, apperr.ErrNotFound, err)
Expand All @@ -30,7 +30,7 @@ func Test_UpdateUser(t *testing.T) {
t.Run("should succeed if values are the same", func(t *testing.T) {
passwordHash, _ := hasher.Hash("apassword")
user := domain.NewUser("[email protected]", passwordHash, "anapikey")
uc, store := update(user)
uc := update(&user)

err := uc(context.Background(), command.UpdateUserCommand{
ID: string(user.ID()),
Expand All @@ -39,14 +39,13 @@ func Test_UpdateUser(t *testing.T) {
})

testutil.IsNil(t, err)

user, _ = store.GetByID(context.Background(), user.ID())
testutil.HasNEvents(t, &user, 2) // 2 since bcrypt will produce different hashes
testutil.EventIs[domain.UserPasswordChanged](t, &user, 1)
})

t.Run("should update user if everything is good", func(t *testing.T) {
user := domain.NewUser("[email protected]", "apassword", "anapikey")
uc, store := update(user)
uc := update(&user)

err := uc(context.Background(), command.UpdateUserCommand{
ID: string(user.ID()),
Expand All @@ -55,8 +54,9 @@ func Test_UpdateUser(t *testing.T) {
})

testutil.IsNil(t, err)

user, _ = store.GetByID(context.Background(), user.ID())
testutil.HasNEvents(t, &user, 3)
evt := testutil.EventIs[domain.UserEmailChanged](t, &user, 1)
testutil.Equals(t, "[email protected]", string(evt.Email))
testutil.EventIs[domain.UserPasswordChanged](t, &user, 2)
})
}
8 changes: 3 additions & 5 deletions internal/auth/infra/memory/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/YuukanOO/seelf/internal/auth/domain"
"github.com/YuukanOO/seelf/pkg/apperr"
"github.com/YuukanOO/seelf/pkg/collections"
"github.com/YuukanOO/seelf/pkg/event"
)

Expand All @@ -28,11 +27,10 @@ type (
}
)

func NewUsersStore(existingUsers ...domain.User) UsersStore {
func NewUsersStore(existingUsers ...*domain.User) UsersStore {
s := &usersStore{}
ctx := context.Background()

s.Write(ctx, collections.ToPointers(existingUsers)...)
s.Write(context.Background(), existingUsers...)

return s
}
Expand Down Expand Up @@ -131,7 +129,7 @@ func (s *usersStore) Write(ctx context.Context, users ...*domain.User) error {
default:
for _, u := range s.users {
if u.id == user.ID() {
u.value = user
*u.value = *user
break
}
}
Expand Down
13 changes: 7 additions & 6 deletions internal/deployment/app/command/cleanup_app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import (
)

type initialData struct {
existingApps []domain.App
existingDeployments []domain.Deployment
existingApps []*domain.App
existingDeployments []*domain.Deployment
}

func Test_CleanupApp(t *testing.T) {
Expand Down Expand Up @@ -51,7 +51,7 @@ func Test_CleanupApp(t *testing.T) {
t.Run("should fail if the application cleanup as not been requested", func(t *testing.T) {
app := domain.NewApp("my-app", "uid")
uc := cleanup(initialData{
existingApps: []domain.App{app},
existingApps: []*domain.App{&app},
})

err := uc(ctx, command.CleanupAppCommand{
Expand All @@ -67,8 +67,8 @@ func Test_CleanupApp(t *testing.T) {
app.RequestCleanup("uid")

uc := cleanup(initialData{
existingApps: []domain.App{app},
existingDeployments: []domain.Deployment{depl},
existingApps: []*domain.App{&app},
existingDeployments: []*domain.Deployment{&depl},
})

err := uc(ctx, command.CleanupAppCommand{
Expand All @@ -83,13 +83,14 @@ func Test_CleanupApp(t *testing.T) {
app.RequestCleanup("uid")

uc := cleanup(initialData{
existingApps: []domain.App{app},
existingApps: []*domain.App{&app},
})

err := uc(ctx, command.CleanupAppCommand{
ID: string(app.ID()),
})

testutil.IsNil(t, err)
testutil.EventIs[domain.AppDeleted](t, &app, 2)
})
}
6 changes: 4 additions & 2 deletions internal/deployment/app/command/create_app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

func Test_CreateApp(t *testing.T) {
ctx := auth.WithUserID(context.Background(), "some-uid")
create := func(existingApps ...domain.App) func(context.Context, command.CreateAppCommand) (string, error) {
create := func(existingApps ...*domain.App) func(context.Context, command.CreateAppCommand) (string, error) {
store := memory.NewAppsStore(existingApps...)
return command.CreateApp(store, store)
}
Expand All @@ -28,7 +28,9 @@ func Test_CreateApp(t *testing.T) {
})

t.Run("should fail if the name is already taken", func(t *testing.T) {
uc := create(domain.NewApp("my-app", "uid"))
app := domain.NewApp("my-app", "uid")
uc := create(&app)

_, err := uc(ctx, command.CreateAppCommand{
Name: "my-app",
})
Expand Down
23 changes: 8 additions & 15 deletions internal/deployment/app/command/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ func Test_Deploy(t *testing.T) {
deploy := func(
source domain.Source,
backend domain.Backend,
existingDeployments ...domain.Deployment,
) (func(context.Context, command.DeployCommand) error, domain.DeploymentsReader) {
existingDeployments ...*domain.Deployment,
) func(context.Context, command.DeployCommand) error {
opts := cmd.DefaultConfiguration(cmd.WithTestDefaults())
store := memory.NewDeploymentsStore(existingDeployments...)
artifactManager := infra.NewLocalArtifactManager(opts, logger)
Expand All @@ -36,11 +36,11 @@ func Test_Deploy(t *testing.T) {
os.RemoveAll(opts.DataDir())
})

return command.Deploy(store, store, artifactManager, source, backend), store
return command.Deploy(store, store, artifactManager, source, backend)
}

t.Run("should fail if the deployment does not exists", func(t *testing.T) {
uc, _ := deploy(source(nil), backend(nil))
uc := deploy(source(nil), backend(nil))
err := uc(ctx, command.DeployCommand{})

testutil.ErrorIs(t, apperr.ErrNotFound, err)
Expand All @@ -51,19 +51,16 @@ func Test_Deploy(t *testing.T) {
src := source(srcErr)
meta, _ := src.Prepare(app, 42)
depl, _ := app.NewDeployment(1, meta, domain.Production, "some-uid")
uc, reader := deploy(src, backend(nil), depl)
uc := deploy(src, backend(nil), &depl)

err := uc(ctx, command.DeployCommand{
AppID: string(app.ID()),
DeploymentNumber: 1,
})

depl, _ = reader.GetByID(ctx, domain.DeploymentIDFrom(app.ID(), 1))

testutil.ErrorIs(t, srcErr, err)

evt := testutil.EventIs[domain.DeploymentStateChanged](t, &depl, 2)

testutil.IsTrue(t, evt.State.StartedAt().HasValue())
testutil.IsTrue(t, evt.State.FinishedAt().HasValue())
testutil.Equals(t, srcErr.Error(), evt.State.ErrCode().MustGet())
Expand All @@ -76,37 +73,33 @@ func Test_Deploy(t *testing.T) {
src := source(nil)
meta, _ := src.Prepare(app, 42)
depl, _ := app.NewDeployment(1, meta, domain.Production, "some-uid")
uc, reader := deploy(src, be, depl)
uc := deploy(src, be, &depl)

err := uc(ctx, command.DeployCommand{
AppID: string(app.ID()),
DeploymentNumber: 1,
})
depl, _ = reader.GetByID(ctx, domain.DeploymentIDFrom(app.ID(), 1))

testutil.ErrorIs(t, backendErr, err)

evt := testutil.EventIs[domain.DeploymentStateChanged](t, &depl, 2)

testutil.IsTrue(t, evt.State.StartedAt().HasValue())
testutil.IsTrue(t, evt.State.FinishedAt().HasValue())
testutil.Equals(t, backendErr.Error(), evt.State.ErrCode().MustGet())
testutil.Equals(t, domain.DeploymentStatusFailed, evt.State.Status())
})

t.Run("should mark the deployment has succeeded if all is good", func(t *testing.T) {
src := source(nil)
meta, _ := src.Prepare(app, 42)
depl, _ := app.NewDeployment(1, meta, domain.Production, "some-uid")
uc, reader := deploy(src, backend(nil), depl)
uc := deploy(src, backend(nil), &depl)

err := uc(ctx, command.DeployCommand{
AppID: string(app.ID()),
DeploymentNumber: 1,
})

depl, _ = reader.GetByID(ctx, domain.DeploymentIDFrom(app.ID(), 1))
testutil.IsNil(t, err)

evt := testutil.EventIs[domain.DeploymentStateChanged](t, &depl, 2)
testutil.IsTrue(t, evt.State.StartedAt().HasValue())
testutil.IsTrue(t, evt.State.FinishedAt().HasValue())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ func Test_FailRunningDeployments(t *testing.T) {
ctx := auth.WithUserID(context.Background(), "some-uid")
app := domain.NewApp("my-app", "some-uid")

fail := func(existingDeployments ...domain.Deployment) (func(context.Context, error) error, domain.DeploymentsReader) {
fail := func(existingDeployments ...*domain.Deployment) func(context.Context, error) error {
deploymentsStore := memory.NewDeploymentsStore(existingDeployments...)
return command.FailRunningDeployments(deploymentsStore, deploymentsStore), deploymentsStore
return command.FailRunningDeployments(deploymentsStore, deploymentsStore)
}

t.Run("should reset running deployments", func(t *testing.T) {
Expand All @@ -36,18 +36,16 @@ func Test_FailRunningDeployments(t *testing.T) {

testutil.IsNil(t, err)

uc, store := fail(started, succeeded)
uc := fail(&started, &succeeded)

err = uc(ctx, errReset)

testutil.IsNil(t, err)

started, _ = store.GetByID(ctx, started.ID())
evt := testutil.EventIs[domain.DeploymentStateChanged](t, &started, 2)
testutil.Equals(t, domain.DeploymentStatusFailed, evt.State.Status())
testutil.Equals(t, errReset.Error(), evt.State.ErrCode().MustGet())

succeeded, _ = store.GetByID(ctx, succeeded.ID())
evt = testutil.EventIs[domain.DeploymentStateChanged](t, &succeeded, 2)
testutil.Equals(t, domain.DeploymentStatusSucceeded, evt.State.Status())
})
Expand Down
6 changes: 3 additions & 3 deletions internal/deployment/app/command/promote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import (
func Test_Promote(t *testing.T) {
ctx := auth.WithUserID(context.Background(), "some-uid")
app := domain.NewApp("my-app", "some-uid")
appsStore := memory.NewAppsStore(app)
appsStore := memory.NewAppsStore(&app)

promote := func(existingDeployments ...domain.Deployment) func(ctx context.Context, cmd command.PromoteCommand) (int, error) {
promote := func(existingDeployments ...*domain.Deployment) func(ctx context.Context, cmd command.PromoteCommand) (int, error) {
deploymentsStore := memory.NewDeploymentsStore(existingDeployments...)
return command.Promote(appsStore, deploymentsStore, deploymentsStore)
}
Expand All @@ -44,7 +44,7 @@ func Test_Promote(t *testing.T) {

t.Run("should correctly creates a new deployment based on the provided one", func(t *testing.T) {
dpl, _ := app.NewDeployment(1, raw.Data(""), domain.Staging, "some-uid")
uc := promote(dpl)
uc := promote(&dpl)

number, err := uc(ctx, command.PromoteCommand{
AppID: string(dpl.ID().AppID()),
Expand Down
2 changes: 1 addition & 1 deletion internal/deployment/app/command/queue_deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
func Test_QueueDeployment(t *testing.T) {
ctx := auth.WithUserID(context.Background(), "some-uid")
app := domain.NewApp("my-app", "some-uid")
appsStore := memory.NewAppsStore(app)
appsStore := memory.NewAppsStore(&app)

queue := func() func(ctx context.Context, cmd command.QueueDeploymentCommand) (int, error) {
deploymentsStore := memory.NewDeploymentsStore()
Expand Down
6 changes: 3 additions & 3 deletions internal/deployment/app/command/redeploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import (
func Test_Redeploy(t *testing.T) {
ctx := auth.WithUserID(context.Background(), "some-uid")
app := domain.NewApp("my-app", "some-uid")
appsStore := memory.NewAppsStore(app)
appsStore := memory.NewAppsStore(&app)

redeploy := func(existingDeployments ...domain.Deployment) func(ctx context.Context, cmd command.RedeployCommand) (int, error) {
redeploy := func(existingDeployments ...*domain.Deployment) func(ctx context.Context, cmd command.RedeployCommand) (int, error) {
deploymentsStore := memory.NewDeploymentsStore(existingDeployments...)
return command.Redeploy(appsStore, deploymentsStore, deploymentsStore)
}
Expand All @@ -44,7 +44,7 @@ func Test_Redeploy(t *testing.T) {

t.Run("should correctly creates a new deployment based on the provided one", func(t *testing.T) {
dpl, _ := app.NewDeployment(1, raw.Data(""), domain.Production, "some-uid")
uc := redeploy(dpl)
uc := redeploy(&dpl)

number, err := uc(ctx, command.RedeployCommand{
AppID: string(dpl.ID().AppID()),
Expand Down
5 changes: 3 additions & 2 deletions internal/deployment/app/command/request_app_cleanup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

func Test_RequestAppCleanup(t *testing.T) {
ctx := auth.WithUserID(context.Background(), "some-uid")
delete := func(existingApps ...domain.App) func(context.Context, command.RequestAppCleanupCommand) error {
delete := func(existingApps ...*domain.App) func(context.Context, command.RequestAppCleanupCommand) error {
store := memory.NewAppsStore(existingApps...)
return command.RequestAppCleanup(store, store)
}
Expand All @@ -31,12 +31,13 @@ func Test_RequestAppCleanup(t *testing.T) {

t.Run("should mark an application has ready for deletion", func(t *testing.T) {
app := domain.NewApp("my-app", "uid")
uc := delete(app)
uc := delete(&app)

err := uc(ctx, command.RequestAppCleanupCommand{
ID: string(app.ID()),
})

testutil.IsNil(t, err)
testutil.EventIs[domain.AppCleanupRequested](t, &app, 1)
})
}
Loading

0 comments on commit d63f0e5

Please sign in to comment.