Repository: googleapis/genai-toolbox
File: tests/dataform/dataform_integration_test.go (Go)

Code before:
	out, err := testutils.WaitForString(waitCtx, regexp.MustCompile(`Server ready to serve`), cmd.Out)
	if err != nil {
		t.Logf("toolbox command logs: \n%s", out)
		t.Fatalf("toolbox didn't start successfully: %s", err)
	}

	nonExistentDir := filepath.Join(os.TempDir(), "non-existent-dir-for-test-12345")
	os.Remove(nonExistentDir)

	testCases := []struct {
		name       string
		reqBody    string
		wantStatus int
		wantBody   string // Substring to check for in the response
	}{
		{
			name:       "success case",
			reqBody:    fmt.Sprintf(`{"project_dir":"%s"}`, projectDir),
			wantStatus: http.StatusOK,
			wantBody:   "test_col",
		},
		{
			name:       "missing parameter",
			reqBody:    `{}`,
			wantStatus: http.StatusBadRequest,
			wantBody:   "parameter \\\"project_dir\\\" is required",
		},
		{
			name:       "non-existent directory",
			reqBody:    fmt.Sprintf(`{"project_dir":"%s"}`, nonExistentDir),
			wantStatus: http.StatusBadRequest,
			wantBody:   "error executing dataform compile",
		},
	}

	api := "http://127.0.0.1:5000/api/tool/my-dataform-compiler/invoke"

	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			resp, bodyBytes := tests.RunRequest(t, http.MethodPost, api, strings.NewReader(tc.reqBody), nil)

			if resp.StatusCode != tc.wantStatus {
				t.Fatalf("unexpected status: got %d, want %d. Body: %s", resp.StatusCode, tc.wantStatus, string(bodyBytes))
			}

			if tc.wantBody != "" && !strings.Contains(string(bodyBytes), tc.wantBody) {
				t.Fatalf("expected body to contain %q, got: %s", tc.wantBody, string(bodyBytes))
			}
		})
	}
}

Review comment (suggestion):
```suggestion
			wantBody:   `parameter \"project_dir\" is required`,
```
more readable

Code after:
	out, err := testutils.WaitForString(waitCtx, regexp.MustCompile(`Server ready to serve`), cmd.Out)
	if err != nil {
		t.Logf("toolbox command logs: \n%s", out)
		t.Fatalf("toolbox didn't start successfully: %s", err)
	}

	nonExistentDir := filepath.Join(os.TempDir(), "non-existent-dir")

	testCases := []struct {
		name       string
		reqBody    string
		wantStatus int
		wantBody   string // Substring to check for in the response
	}{
		{
			name:       "success case",
			reqBody:    fmt.Sprintf(`{"project_dir":"%s"}`, projectDir),
			wantStatus: http.StatusOK,
			wantBody:   "test_col",
		},
		{
			name:       "missing parameter",
			reqBody:    `{}`,
			wantStatus: http.StatusBadRequest,
			wantBody:   `parameter \"project_dir\" is required`,
		},
		{
			name:       "non-existent directory",
			reqBody:    fmt.Sprintf(`{"project_dir":"%s"}`, nonExistentDir),
			wantStatus: http.StatusBadRequest,
			wantBody:   "error executing dataform compile",
		},
	}

	api := "http://127.0.0.1:5000/api/tool/my-dataform-compiler/invoke"

	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			resp, bodyBytes := tests.RunRequest(t, http.MethodPost, api, strings.NewReader(tc.reqBody), nil)

			if resp.StatusCode != tc.wantStatus {
				t.Fatalf("unexpected status: got %d, want %d. Body: %s", resp.StatusCode, tc.wantStatus, string(bodyBytes))
			}

			if tc.wantBody != "" && !strings.Contains(string(bodyBytes), tc.wantBody) {
				t.Fatalf("expected body to contain %q, got: %s", tc.wantBody, string(bodyBytes))
			}
		})
	}
}