pavel-avilov commented on a change in pull request #17000:
URL: https://github.com/apache/beam/pull/17000#discussion_r822341642
##########
File path: playground/backend/internal/utils/preparares_utils_test.go
##########
@@ -50,3 +60,347 @@ func TestSpacesToEqualsOption(t *testing.T) {
})
}
}
+
+func TestInitVars(t *testing.T) {
+ tests := []struct {
+ name string
+ want string
+ want1 string
+ want2 error
+ want3 bool
+ want4 PipelineDefinitionType
Review comment:
Done
##########
File path: playground/backend/internal/utils/preparares_utils_test.go
##########
@@ -50,3 +60,347 @@ func TestSpacesToEqualsOption(t *testing.T) {
})
}
}
+
+func TestInitVars(t *testing.T) {
+ tests := []struct {
+ name string
+ want string
+ want1 string
+ want2 error
+ want3 bool
+ want4 PipelineDefinitionType
+ }{
+ {
+ name: "Create empty variables",
+ want: EmptyLine,
+ want1: EmptyLine,
+ want2: errors.New(EmptyLine),
+ want3: false,
+ want4: RegularDefinition,
+ },
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ got, got1, got2, got3, got4 := InitVars()
+ if got != tt.want {
+ t.Errorf("InitVars() got = %v, want %v", got,
tt.want)
+ }
+ if got1 != tt.want1 {
+ t.Errorf("InitVars() got1 = %v, want %v", got1,
tt.want1)
+ }
+ if !reflect.DeepEqual(got2, tt.want2) {
+ t.Errorf("InitVars() got2 = %v, want %v", got2,
tt.want2)
+ }
+ if got3 != tt.want3 {
+ t.Errorf("InitVars() got3 = %v, want %v", got3,
tt.want3)
+ }
+ if got4 != tt.want4 {
+ t.Errorf("InitVars() got4 = %v, want %v", got4,
tt.want4)
+ }
+ })
+ }
+}
+
+func TestAddGraphToEndOfFile(t *testing.T) {
+ txtFilePath := filepath.Join(sourceDir, fileName)
+ txtFile, err := os.OpenFile(txtFilePath, os.O_APPEND|os.O_WRONLY,
os.ModeAppend)
+ if err != nil {
+ panic(err)
+ }
+ defer txtFile.Close()
+ incorrectFile, err := os.Open(txtFilePath)
+ if err != nil {
+ panic(err)
+ }
+ defer incorrectFile.Close()
+ type args struct {
+ spaces string
+ err error
+ tempFile *os.File
+ pipelineName string
+ }
+ type fields struct {
+ fileContent string
+ filePath string
+ }
+ tests := []struct {
+ name string
+ args args
+ fields fields
+ wantErr bool
+ }{
+ {
+ name: "Add graph to the end of an existing file",
+ args: args{
+ spaces: "",
+ err: nil,
+ tempFile: txtFile,
+ pipelineName: uuid.New().String(),
+ },
+ fields: fields{
+ fileContent: fileContent,
+ filePath: txtFilePath,
+ },
+ wantErr: false,
+ },
+ {
+ name: "Error during write data to file",
+ args: args{
+ spaces: "",
+ err: nil,
+ tempFile: incorrectFile,
+ pipelineName: uuid.New().String(),
+ },
+ fields: fields{
+ fileContent: fileContent,
+ filePath: txtFilePath,
+ },
+ wantErr: true,
+ },
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ AddGraphToEndOfFile(tt.args.spaces, tt.args.err,
tt.args.tempFile, tt.args.pipelineName)
+ data, err := os.ReadFile(tt.fields.filePath)
+ if err != nil {
+ t.Errorf("AddGraphToEndOfFile() error during
reading from a file = %v", err)
+ }
+ graphCode := fmt.Sprintf(pythonGraphCodePattern,
tt.args.pipelineName, GraphFileName)
+ graphCodeWithIndentation :=
strings.ReplaceAll(graphCode, indentationReplacement, tt.args.spaces)
+ fileContentWithGraph := fileContent + "\n" +
graphCodeWithIndentation
+ if (string(data) != fileContentWithGraph) != tt.wantErr
{
+ t.Error("AddGraphToEndOfFile() wrong graph
addition")
+ }
+ })
+ }
+}
+
+func TestGetPublicClassName(t *testing.T) {
+ javaPublicClassNamePattern := "public class (.*?) [{|implements(.*)]"
+ type args struct {
+ filePath string
+ pattern string
+ }
+ tests := []struct {
+ name string
+ args args
+ want string
+ wantErr bool
+ }{
+ {
+ name: "Get public class name from existing java file",
+ args: args{
+ filePath: filepath.Join(sourceDir,
javaFileName),
+ pattern: javaPublicClassNamePattern,
+ },
+ want: "MinimalWordCount",
+ wantErr: false,
+ },
+ {
+ name: "Get public class name from non-existent file",
+ args: args{
+ filePath: filepath.Join(sourceDir,
"file1.java"),
+ pattern: javaPublicClassNamePattern,
+ },
+ want: "",
+ wantErr: true,
+ },
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ got, err := GetPublicClassName(tt.args.filePath,
tt.args.pattern)
+ if (err != nil) != tt.wantErr {
+ t.Errorf("GetPublicClassName() error = %v,
wantErr %v", err, tt.wantErr)
+ return
+ }
+ if got != tt.want {
+ t.Errorf("GetPublicClassName() got = %v, want
%v", got, tt.want)
+ }
+ })
+ }
+}
+
+func TestAddNewLine(t *testing.T) {
+ javaFile, err := os.OpenFile(filepath.Join(sourceDir, javaFileName),
os.O_APPEND|os.O_WRONLY, os.ModeAppend)
+ if err != nil {
+ panic(err)
+ }
+ defer javaFile.Close()
+ txtFile, err := os.Open(filepath.Join(sourceDir, fileName))
+ if err != nil {
+ panic(err)
+ }
+ defer txtFile.Close()
+ type args struct {
+ newLine bool
+ file *os.File
+ }
+ tests := []struct {
+ name string
+ args args
+ wantErr bool
+ }{
+ {
+ name: "With newLine = false",
+ args: args{
+ newLine: false,
+ file: nil,
+ },
+ wantErr: false,
+ },
+ {
+ name: "Add a new line to an existing javaFile",
+ args: args{
+ newLine: true,
+ file: javaFile,
+ },
+ wantErr: false,
+ },
+ {
+ name: "Error during write data to file",
+ args: args{
+ newLine: true,
+ file: txtFile,
+ },
+ wantErr: true,
+ },
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ if err := AddNewLine(tt.args.newLine, tt.args.file);
(err != nil) != tt.wantErr {
+ t.Errorf("AddNewLine() error = %v, wantErr %v",
err, tt.wantErr)
+ }
+ })
+ }
+}
+
+func TestProcessLine(t *testing.T) {
+ pipelineName := uuid.New().String()
+ pythonExample, err := os.OpenFile(filepath.Join(sourceDir,
pythonExampleName), os.O_RDWR, 0755)
+ if err != nil {
+ panic(err)
+ }
+ defer pythonExample.Close()
+ findPipelinePattern := `^(\s*)(.+) = beam.Pipeline`
+ findWithPipelinePattern := `(\s*)with.+Pipeline.+as (.+):`
+ emptyLine := EmptyLine
+
+ type args struct {
+ curLine string
+ pipelineName *string
+ spaces *string
+ regs *[]*regexp.Regexp
+ tempFile *os.File
+ err error
+ }
+ tests := []struct {
+ name string
+ args args
+ want bool
+ want1 PipelineDefinitionType
Review comment:
Done.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]