Skip to content

Commit

Permalink
add tests for infra command
Browse files Browse the repository at this point in the history
  • Loading branch information
ksckaan1 committed Sep 7, 2024
1 parent 8a2a41d commit 5543b36
Show file tree
Hide file tree
Showing 2 changed files with 299 additions and 1 deletion.
2 changes: 1 addition & 1 deletion internal/domain/core/service/project/infra.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (p *Project) CreateInfrastructure(ctx context.Context, params dto.CreateInf

err = p.isInfraExist(ctx, params.PackageName)
if err == nil {
return "", fmt.Errorf("infrastructure already exist: %s", params.StructName)
return "", fmt.Errorf("is infra exist: %w", dto.ErrAlreadyExist)
}

infraDir := filepath.Join("internal", "infrastructure", params.PackageName)
Expand Down
298 changes: 298 additions & 0 deletions internal/domain/core/service/project/infra_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,298 @@
package project

import (
"context"
"fmt"
"path/filepath"
"testing"

"github.com/ksckaan1/hexago/internal/domain/core/dto"
"github.com/ksckaan1/hexago/internal/domain/core/service/config"
"github.com/stretchr/testify/require"
)

func TestCreateInfrastructure(t *testing.T) {
type in struct {
preRun func(p *Project) error
}
type args struct {
ctx func() context.Context
params dto.CreateInfraParams
}
type want struct {
err require.ErrorAssertionFunc
infraFilePath string
}

tests := []struct {
name string
in
args
want
}{
{
name: "valid 1",
in: in{
preRun: func(p *Project) error {
return p.InitNewProject(context.Background(), dto.InitNewProjectParams{
ProjectDirectory: t.TempDir(),
ModuleName: "my-project",
CreateModule: true,
})
},
},
args: args{
ctx: context.Background,
params: dto.CreateInfraParams{
StructName: "MyInfra",
PackageName: "myinfra",
PortParam: "",
AssertInterface: false,
},
},
want: want{
infraFilePath: filepath.Join("internal", "infrastructure", "myinfra", "myinfra.go"),
err: require.NoError,
},
},
{
name: "valid with port",
in: in{
preRun: func(p *Project) error {
return p.InitNewProject(context.Background(), dto.InitNewProjectParams{
ProjectDirectory: t.TempDir(),
ModuleName: "my-project",
CreateModule: true,
})
},
},
args: args{
ctx: context.Background,
params: dto.CreateInfraParams{
StructName: "MyInfra",
PackageName: "myinfra",
PortParam: "io.Writer",
AssertInterface: true,
},
},
want: want{
infraFilePath: filepath.Join("internal", "infrastructure", "myinfra", "myinfra.go"),
err: require.NoError,
},
},
{
name: "invalid port",
in: in{
preRun: func(p *Project) error {
return p.InitNewProject(context.Background(), dto.InitNewProjectParams{
ProjectDirectory: t.TempDir(),
ModuleName: "my-project",
CreateModule: true,
})
},
},
args: args{
ctx: context.Background,
params: dto.CreateInfraParams{
StructName: "MyApp",
PackageName: "myapp",
PortParam: "notexisting",
AssertInterface: true,
},
},
want: want{
infraFilePath: "",
err: func(tt require.TestingT, err error, i ...interface{}) {
require.ErrorIs(tt, err, dto.ErrInvalidPortName{PortName: "notexisting"})
},
},
},
{
name: "already existing",
in: in{
preRun: func(p *Project) error {
err := p.InitNewProject(context.Background(), dto.InitNewProjectParams{
ProjectDirectory: t.TempDir(),
ModuleName: "my-project",
CreateModule: true,
})
if err != nil {
return err
}
_, err = p.CreateInfrastructure(context.Background(), dto.CreateInfraParams{
StructName: "MyApp",
PackageName: "myapp",
})
return err
},
},
args: args{
ctx: context.Background,
params: dto.CreateInfraParams{
StructName: "MyApp",
PackageName: "myapp",
},
},
want: want{
infraFilePath: "",
err: func(tt require.TestingT, err error, i ...interface{}) {
require.ErrorIs(tt, err, dto.ErrAlreadyExist)
},
},
},
{
name: "invalid instance name",
in: in{
preRun: func(p *Project) error {
return p.InitNewProject(context.Background(), dto.InitNewProjectParams{
ProjectDirectory: t.TempDir(),
ModuleName: "my-project",
CreateModule: true,
})
},
},
args: args{
ctx: context.Background,
params: dto.CreateInfraParams{
StructName: "myApp",
PackageName: "myapp",
PortParam: "",
AssertInterface: false,
},
},
want: want{
infraFilePath: "",
err: func(tt require.TestingT, err error, i ...interface{}) {
require.ErrorIs(tt, err, dto.ErrInvalidInstanceName)
},
},
},
{
name: "invalid folder name",
in: in{
preRun: func(p *Project) error {
return p.InitNewProject(context.Background(), dto.InitNewProjectParams{
ProjectDirectory: t.TempDir(),
ModuleName: "my-project",
CreateModule: true,
})
},
},
args: args{
ctx: context.Background,
params: dto.CreateInfraParams{
StructName: "MyApp",
PackageName: "my-app",
PortParam: "",
AssertInterface: false,
},
},
want: want{
infraFilePath: "",
err: func(tt require.TestingT, err error, i ...interface{}) {
require.ErrorIs(tt, err, dto.ErrInvalidPkgName)
},
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
projectService := &Project{
cfg: &config.Config{},
}
require.NoError(t, tt.in.preRun(projectService))

serviceFile, err := projectService.CreateInfrastructure(tt.args.ctx(), tt.args.params)
tt.want.err(t, err)
require.Equal(t, tt.want.infraFilePath, serviceFile)
})
}
}

func TestGetAllInfrastructures(t *testing.T) {
type in struct {
preRun func(p *Project) error
}
type args struct {
ctx func() context.Context
}
type want struct {
err require.ErrorAssertionFunc
services []string
}

tests := []struct {
name string
in
args
want
}{
{
name: "valid",
in: in{
preRun: func(p *Project) error {
err := p.InitNewProject(context.Background(), dto.InitNewProjectParams{
ProjectDirectory: t.TempDir(),
ModuleName: "my-project",
CreateModule: true,
})
if err != nil {
return err
}

for i := range 3 {
_, err = p.CreateInfrastructure(context.Background(), dto.CreateInfraParams{
StructName: fmt.Sprintf("Example%d", i),
PackageName: fmt.Sprintf("example%d", i),
})
if err != nil {
return err
}
}

return nil
},
},
args: args{
ctx: context.Background,
},
want: want{
err: require.NoError,
services: []string{"example0", "example1", "example2"},
},
},
{
name: "valid empty list",
in: in{
preRun: func(p *Project) error {
return p.InitNewProject(context.Background(), dto.InitNewProjectParams{
ProjectDirectory: t.TempDir(),
ModuleName: "my-project",
CreateModule: true,
})
},
},
args: args{
ctx: context.Background,
},
want: want{
err: require.NoError,
services: []string{},
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
projectService := &Project{
cfg: &config.Config{},
}
require.NoError(t, tt.in.preRun(projectService))

services, err := projectService.GetAllInfrastructes(tt.args.ctx())
tt.want.err(t, err)
require.Equal(t, tt.want.services, services)
})
}
}

0 comments on commit 5543b36

Please sign in to comment.