You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by GitBox <gi...@apache.org> on 2022/03/09 14:27:11 UTC

[GitHub] [beam] daria-malkova commented on a change in pull request #17000: [BEAM-13881] [Playground] Increase test coverage for the utils package

daria-malkova commented on a change in pull request #17000:
URL: https://github.com/apache/beam/pull/17000#discussion_r822702663



##########
File path: playground/backend/internal/utils/preparares_utils_test.go
##########
@@ -50,3 +60,328 @@ func TestSpacesToEqualsOption(t *testing.T) {
 		})
 	}
 }
+
+func TestInitVars(t *testing.T) {
+	tests := []struct {
+		name string
+		want []interface{}
+	}{
+		{
+			name: "Create empty variables",
+			want: []interface{}{EmptyLine, EmptyLine, errors.New(EmptyLine), false, RegularDefinition},
+		},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			got, got1, got2, got3, got4 := InitVars()
+			variables := append([]interface{}{}, got, got1, got2, got3, got4)
+			if !reflect.DeepEqual(variables, tt.want) {
+				t.Errorf("InitVars() variables = %v, want %v", variables, tt.want)
+			}
+		})
+	}
+}
+
+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",

Review comment:
       Can be renamed like "No line added to file"

##########
File path: playground/backend/internal/utils/preparares_utils_test.go
##########
@@ -50,3 +60,328 @@ func TestSpacesToEqualsOption(t *testing.T) {
 		})
 	}
 }
+
+func TestInitVars(t *testing.T) {
+	tests := []struct {
+		name string
+		want []interface{}
+	}{
+		{
+			name: "Create empty variables",
+			want: []interface{}{EmptyLine, EmptyLine, errors.New(EmptyLine), false, RegularDefinition},
+		},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			got, got1, got2, got3, got4 := InitVars()
+			variables := append([]interface{}{}, got, got1, got2, got3, got4)
+			if !reflect.DeepEqual(variables, tt.want) {
+				t.Errorf("InitVars() variables = %v, want %v", variables, tt.want)
+			}
+		})
+	}
+}
+
+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, fullPermission)
+	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
+		wantDefType PipelineDefinitionType
+		wantErr     bool
+	}{
+		{
+			name: "Empty curLine",

Review comment:
       Empty current line (like below)




-- 
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: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org