Skip to content

Commit

Permalink
Merge pull request #195 from intelops/add-tests
Browse files Browse the repository at this point in the history
Add dotnet tests
  • Loading branch information
mahendraintelops authored Dec 11, 2023
2 parents 1b87638 + 5674d8a commit 3d4b0c6
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 52 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"mode": "debug",
"program": "${workspaceRoot}/main.go",
"cwd": "${workspaceRoot}",
"args": ["generate"]
"args": ["init", "--language","dotnet"]
}
]
}
36 changes: 36 additions & 0 deletions cmd/dotnet-config.yaml.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: {{.ProjectName}}
git:
repository:
name: {{.GitRepositoryName}}
url: {{.GitRepositoryURL}}
platform:
name: {{.GitPlatformName}}
url: {{.GitPlatformURL}}
userName: {{.GitPlatformUserName}}
{{if .IsRestServer}}
compageJSON:
edges: []
nodes:
- id: node-ef
name: user-service
language: dotnet
restConfig:
server:
sqlDB: MSSQL
port: '5170'
resources:
- fields:
Name:
datatype: string
Age:
datatype: int
name: User
allowedMethods:
- POST
- LIST
- GET
- PUT
- DELETE
framework: dotnet-clean-architecture
template: compage
{{end}}
File renamed without changes.
129 changes: 89 additions & 40 deletions cmd/init.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package cmd

import (
"fmt"
"os"

"github.com/intelops/compage/internal/languages/executor"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"os"
)

var (
Expand All @@ -14,6 +16,7 @@ var (
platformUserName string
repositoryName string
serverType string
language string
overwriteConfigFile bool
)

Expand All @@ -31,9 +34,10 @@ You can change the file as per your needs and then run the compage generate comm
}

func createOrUpdateDefaultConfigFile() error {
var err error
// create default config file
configFilePath := "config.yaml"
_, err := os.Stat(configFilePath)
_, err = os.Stat(configFilePath)
if err == nil {
log.Warnf("config file already exists at %s", configFilePath)
if !overwriteConfigFile {
Expand All @@ -48,50 +52,94 @@ func createOrUpdateDefaultConfigFile() error {
}
}

_, err = os.Create(configFilePath)
if err != nil {
log.Errorf("error while creating the config file %s", err)
return err
}
contentData, err := Content.ReadFile("config.yaml.tmpl")
if err != nil {
log.Errorf("error while reading the config file %s", err)
return err
if language != "go" && language != "dotnet" {
message := fmt.Sprintf("language %s is not supported", language)
log.Errorf(message)
return fmt.Errorf(message)
}
// copy the default config file and use go template to replace the values
err = os.WriteFile(configFilePath, contentData, 0644)

_, err = os.Create(configFilePath)
if err != nil {
log.Errorf("error while creating the config file %s", err)
return err
}
var filePaths []*string
filePaths = append(filePaths, &configFilePath)
data := make(map[string]interface{})
data["ProjectName"] = projectName
data["GitRepositoryName"] = repositoryName
data["GitRepositoryURL"] = platformURL + "/" + platformUserName + "/" + repositoryName
data["GitPlatformName"] = platformName
data["GitPlatformURL"] = platformURL
data["GitPlatformUserName"] = platformUserName
if serverType == "grpc" {
data["IsRestAndGrpcServer"] = false
data["IsGrpcServer"] = true
data["IsRestServer"] = false
} else if serverType == "rest-grpc" {
data["IsRestAndGrpcServer"] = true
data["IsGrpcServer"] = false
data["IsRestServer"] = false
} else {
data["IsRestAndGrpcServer"] = false
data["IsGrpcServer"] = false
data["IsRestServer"] = true
}
err = executor.Execute(filePaths, data)
if err != nil {
log.Errorf("error while creating the config file %s", err)
return err

if language == "go" {
var goContentData []byte
goContentData, err = GoConfigContent.ReadFile("go-config.yaml.tmpl")
if err != nil {
log.Errorf("error while reading the config file %s", err)
return err
}
// copy the default config file and use go template to replace the values
err = os.WriteFile(configFilePath, goContentData, 0644)
if err != nil {
log.Errorf("error while creating the config file %s", err)
return err
}
var filePaths []*string
filePaths = append(filePaths, &configFilePath)
data := make(map[string]interface{})
data["ProjectName"] = projectName
data["GitRepositoryName"] = repositoryName
data["GitRepositoryURL"] = platformURL + "/" + platformUserName + "/" + repositoryName
data["GitPlatformName"] = platformName
data["GitPlatformURL"] = platformURL
data["GitPlatformUserName"] = platformUserName
if serverType == "grpc" {
data["IsRestAndGrpcServer"] = false
data["IsGrpcServer"] = true
data["IsRestServer"] = false
} else if serverType == "rest-grpc" {
data["IsRestAndGrpcServer"] = true
data["IsGrpcServer"] = false
data["IsRestServer"] = false
} else {
data["IsRestAndGrpcServer"] = false
data["IsGrpcServer"] = false
data["IsRestServer"] = true
}
err = executor.Execute(filePaths, data)
if err != nil {
log.Errorf("error while creating the config file %s", err)
return err
}
log.Infof("config file created at %s", configFilePath)
} else if language == "dotnet" {
var dotnetContentData []byte
dotnetContentData, err = DotNetConfigContent.ReadFile("dotnet-config.yaml.tmpl")
if err != nil {
log.Errorf("error while reading the config file %s", err)
return err
}
// copy the default config file and use go template to replace the values
err = os.WriteFile(configFilePath, dotnetContentData, 0644)
if err != nil {
log.Errorf("error while creating the config file %s", err)
return err
}
var filePaths []*string
filePaths = append(filePaths, &configFilePath)
data := make(map[string]interface{})
data["ProjectName"] = projectName
data["GitRepositoryName"] = repositoryName
data["GitRepositoryURL"] = platformURL + "/" + platformUserName + "/" + repositoryName
data["GitPlatformName"] = platformName
data["GitPlatformURL"] = platformURL
data["GitPlatformUserName"] = platformUserName
if serverType == "rest" {
data["IsRestAndGrpcServer"] = false
data["IsGrpcServer"] = false
data["IsRestServer"] = true
}
err = executor.Execute(filePaths, data)
if err != nil {
log.Errorf("error while creating the config file %s", err)
return err
}
log.Infof("config file created at %s", configFilePath)
}
log.Infof("config file created at %s", configFilePath)

return nil
}

Expand All @@ -114,4 +162,5 @@ func init() {
initCmd.Flags().StringVar(&repositoryName, "repositoryName", "myproject", "Git Repository Name")
initCmd.Flags().StringVar(&serverType, "serverType", "rest", "Server Type (rest, grpc, rest-grpc)")
initCmd.Flags().BoolVar(&overwriteConfigFile, "overwriteConfigFile", false, "Overwrite the config file if it already exists")
initCmd.Flags().StringVar(&language, "language", "go", "Language (go, dotnet)")
}
7 changes: 5 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ import (

var cfgFile string

//go:embed config.yaml.tmpl
var Content embed.FS
//go:embed go-config.yaml.tmpl
var GoConfigContent embed.FS

//go:embed dotnet-config.yaml.tmpl
var DotNetConfigContent embed.FS

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dotnetcleanarchitecture

import (
"errors"
"github.com/gertd/go-pluralize"
"github.com/iancoleman/strcase"
corenode "github.com/intelops/compage/internal/core/node"
Expand Down Expand Up @@ -112,12 +113,11 @@ const TestsGlobalUsingsCSFile = "/Tests/Application.Tests/GlobalUsings.cs.tmpl"
const MicroServiceNameSlnFile = "/MicroServiceName.sln.tmpl"

// handlers
const TestsHandlersPath = "/Tests/Application.Tests/Handlers"
const TestsHandlersResourceNameServicePath = "/Tests/Application.Tests/Handlers/ResourceNameService"
const TestsApplicationTestsHandlersPath = "/Tests/Application.Tests/Handlers"
const TestsHandlersCreateResourceNameCommandHandlerTestsCSFile = "/Tests/Application.Tests/Handlers/ResourceNameService/CreateResourceNameCommandHandlerTests.cs.tmpl"
const TestsHandlersDeleteResourceNameCommandHandlerTestsCSFile = "/Tests/Application.Tests/Handlers/ResourceNameService/DeleteResourceNameCommandHandlerTests.cs.tmpl"
const TestsHandlersUpdateResourceNameCommandHandlerTestsCSFile = "/Tests/Application.Tests/Handlers/ResourceNameService/UpdateResourceNameCommandHandlerTests.cs.tmpl"
const TestsHandlersTestsHandlersGetResourceNameByIDQueryHandlerTestsCSFile = "/Tests/Application.Tests/Handlers/ResourceNameService/GetResourceNameByIdQueryHandlerTests.cs.tmpl"
const TestsHandlersGetResourceNameByIDQueryHandlerTestsCSFile = "/Tests/Application.Tests/Handlers/ResourceNameService/GetResourceNameByIdQueryHandlerTests.cs.tmpl"
const TestsHandlersGetAllResourceNamesQueryHandlerTestsCSFile = "/Tests/Application.Tests/Handlers/ResourceNameService/GetAllResourceNamesQueryHandlerTests.cs.tmpl"

// Copier Language specific *Copier
Expand Down Expand Up @@ -300,7 +300,7 @@ func (c *Copier) createRestServerDirectories() error {
}

testsDirectory := c.NodeDirectoryName + TestsPath
testsHandlersDirectory := c.NodeDirectoryName + TestsHandlersPath
testsHandlersDirectory := c.NodeDirectoryName + TestsApplicationTestsHandlersPath
if err := utils.CreateDirectories(testsDirectory); err != nil {
log.Errorf("error creating tests directory: %v", err)
return err
Expand All @@ -318,9 +318,9 @@ func (c *Copier) copyRestServerResourceFiles(resource *corenode.Resource) error
filePaths := &[]*string{}
var err error
// copy sql files (core)
//if c.IsSQLDB {
// // add files to filePaths and copy them to the generated project
//}
if !c.IsSQLDB || c.SQLDB != "MSSQL" {
return errors.New("only MSSQL is supported")
}

// add files for application
err = c.addApplicationRelatedDirectoriesAndFiles(resource, filePaths)
Expand Down Expand Up @@ -350,6 +350,12 @@ func (c *Copier) copyRestServerResourceFiles(resource *corenode.Resource) error
return err
}

// add files for tests
err = c.addTestsRelatedDirectoriesAndFiles(resource, filePaths)
if err != nil {
log.Errorf("error adding tests related directories and files: %v", err)
return err
}
// add resource-specific data to map in c needed for templates.
err = c.addResourceSpecificTemplateData(resource)
if err != nil {
Expand Down Expand Up @@ -640,6 +646,63 @@ func (c *Copier) addMicroServiceNameRelatedDirectoriesAndFiles(resource *corenod
return nil
}

func (c *Copier) addTestsRelatedDirectoriesAndFiles(resource *corenode.Resource, filePaths *[]*string) error {
var err error

// create a directory for resource Application.Tests/handlers/ResourceNameService
resourceHandlersResourceNameDirectory := c.NodeDirectoryName + TestsApplicationTestsHandlersPath + "/" + resource.Name + "Service"
if err = utils.CreateDirectories(resourceHandlersResourceNameDirectory); err != nil {
log.Errorf("error creating resource handlers resource name directory: %v", err)
return err
}
// copy tests/Application.Tests/Handlers/ResourceNameService/CreateResourceNameCommandHandlerTests.cs
targetTestsApplicationTestsHandlersResourceNameServiceCreateResourceNameCommandHandlerTestsFileName := resourceHandlersResourceNameDirectory + "/" + "Create" + resource.Name + "CommandHandlerTests.cs"
_, err = utils.CopyFile(targetTestsApplicationTestsHandlersResourceNameServiceCreateResourceNameCommandHandlerTestsFileName, c.TemplatesRootPath+TestsHandlersCreateResourceNameCommandHandlerTestsCSFile)
if err != nil {
log.Errorf("error copying tests application tests handlers resource name service create resource name command handler tests cs file: %v", err)
return err
}
*filePaths = append(*filePaths, &targetTestsApplicationTestsHandlersResourceNameServiceCreateResourceNameCommandHandlerTestsFileName)

// copy tests/Application.Tests/Handlers/ResourceNameService/GetAllResourceNamesQueryHandlerTests.cs
targetTestsApplicationTestsHandlersResourceNameServiceGetAllResourceNamesQueryHandlerTestsFileName := resourceHandlersResourceNameDirectory + "/" + "GetAll" + c.PluralizeClient.Plural(resource.Name) + "QueryHandlerTests.cs"
_, err = utils.CopyFile(targetTestsApplicationTestsHandlersResourceNameServiceGetAllResourceNamesQueryHandlerTestsFileName, c.TemplatesRootPath+TestsHandlersGetAllResourceNamesQueryHandlerTestsCSFile)
if err != nil {
log.Errorf("error copying tests application tests handlers resource name service get all resource names query handler tests cs file: %v", err)
return err
}
*filePaths = append(*filePaths, &targetTestsApplicationTestsHandlersResourceNameServiceGetAllResourceNamesQueryHandlerTestsFileName)

// copy tests/Application.Tests/Handlers/ResourceNameService/GetResourceNameByIdQueryHandlerTests.cs
targetTestsApplicationTestsHandlersResourceNameServiceGetResourceNameByIDQueryHandlerTestsFileName := resourceHandlersResourceNameDirectory + "/" + "Get" + resource.Name + "ByIdQueryHandlerTests.cs"
_, err = utils.CopyFile(targetTestsApplicationTestsHandlersResourceNameServiceGetResourceNameByIDQueryHandlerTestsFileName, c.TemplatesRootPath+TestsHandlersGetResourceNameByIDQueryHandlerTestsCSFile)
if err != nil {
log.Errorf("error copying tests application tests handlers resource name service get resource name by id query handler tests cs file: %v", err)
return err
}
*filePaths = append(*filePaths, &targetTestsApplicationTestsHandlersResourceNameServiceGetResourceNameByIDQueryHandlerTestsFileName)

// copy tests/Application.Tests/Handlers/ResourceNameService/UpdateResourceNameCommandHandlerTests.cs
targetTestsApplicationTestsHandlersResourceNameServiceUpdateResourceNameCommandHandlerTestsFileName := resourceHandlersResourceNameDirectory + "/" + "Update" + resource.Name + "CommandHandlerTests.cs"
_, err = utils.CopyFile(targetTestsApplicationTestsHandlersResourceNameServiceUpdateResourceNameCommandHandlerTestsFileName, c.TemplatesRootPath+TestsHandlersUpdateResourceNameCommandHandlerTestsCSFile)
if err != nil {
log.Errorf("error copying tests application tests handlers resource name service update resource name command handler tests cs file: %v", err)
return err
}
*filePaths = append(*filePaths, &targetTestsApplicationTestsHandlersResourceNameServiceUpdateResourceNameCommandHandlerTestsFileName)

// copy tests/Application.Tests/Handlers/ResourceNameService/DeleteResourceNameCommandHandlerTests.cs
targetTestsApplicationTestsHandlersResourceNameServiceDeleteResourceNameCommandHandlerTestsFileName := resourceHandlersResourceNameDirectory + "/" + "Delete" + resource.Name + "CommandHandlerTests.cs"
_, err = utils.CopyFile(targetTestsApplicationTestsHandlersResourceNameServiceDeleteResourceNameCommandHandlerTestsFileName, c.TemplatesRootPath+TestsHandlersDeleteResourceNameCommandHandlerTestsCSFile)
if err != nil {
log.Errorf("error copying tests application tests handlers resource name service delete resource name command handler tests cs file: %v", err)
return err
}
*filePaths = append(*filePaths, &targetTestsApplicationTestsHandlersResourceNameServiceDeleteResourceNameCommandHandlerTestsFileName)

return nil
}

func (c *Copier) addResourceSpecificTemplateData(resource *corenode.Resource) error {
// make every field public by making its first character capital.
fields := map[string]string{}
Expand Down
4 changes: 2 additions & 2 deletions test/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1380,7 +1380,7 @@ func TestDotNetCleanArchitectureGenerator(t *testing.T) {
"language": "dotnet",
"restConfig": {
"server": {
"sqlDB": "SQLite",
"sqlDB": "MSSQL",
"port": "5005",
"resources": [
{
Expand Down Expand Up @@ -1424,7 +1424,7 @@ func TestDotNetCleanArchitectureGenerator(t *testing.T) {
ProjectJSON: restServerConfigJSON,
}
defer func() {
//_ = os.RemoveAll("/tmp/first-rest-server-project-dotnet")
_ = os.RemoveAll("/tmp/first-rest-server-project-dotnet")
}()

// retrieve project struct
Expand Down

0 comments on commit 3d4b0c6

Please sign in to comment.