Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/cover.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ jobs:
- uses: actions/setup-go@v5
with:
go-version-file: go.mod
- name: Set Go toolchain (workaround for Go 1.25 covdata bug)
run: go env -w GOTOOLCHAIN=go1.25.1+auto
- run: make test
- name: Send coverage
env:
Expand Down
86 changes: 34 additions & 52 deletions cmd/api/handlers/v1/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,64 +6,46 @@ import (

"github.com/gofiber/fiber/v2"
"github.com/pgconfig/api/pkg/input/profile"
. "github.com/pgconfig/api/pkg/tests"
)

func TestParseConfigArgs_ProfileCaseInsensitive(t *testing.T) {
// Regression test for issue #37: environment_name should be case-insensitive
tests := []struct {
name string
environmentName string
expectedProfile profile.Profile
}{
{
name: "uppercase MIXED",
environmentName: "MIXED",
expectedProfile: profile.Mixed,
},
{
name: "mixed case Mixed",
environmentName: "Mixed",
expectedProfile: profile.Mixed,
},
{
name: "lowercase mixed",
environmentName: "mixed",
expectedProfile: profile.Mixed,
},
{
name: "mixed case WEB",
environmentName: "Web",
expectedProfile: profile.Web,
},
{
name: "lowercase web",
environmentName: "web",
expectedProfile: profile.Web,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
app := fiber.New()

app.Get("/test", func(c *fiber.Ctx) error {
args, err := parseConfigArgs(c)
if err != nil {
return err
Describe("parseConfigArgs", t, func() {
Context("profile parsing (issue #37)", func() {
It("should accept environment_name case insensitively", func() {
tests := []struct {
name string
environmentName string
expectedProfile profile.Profile
}{
{"uppercase MIXED", "MIXED", profile.Mixed},
{"mixed case Mixed", "Mixed", profile.Mixed},
{"lowercase mixed", "mixed", profile.Mixed},
{"mixed case WEB", "Web", profile.Web},
{"lowercase web", "web", profile.Web},
}

if args.envName != tt.expectedProfile {
t.Errorf("expected profile %v, got %v", tt.expectedProfile, args.envName)
for _, tt := range tests {
app := fiber.New()
var capturedArgs *configArgs

app.Get("/test", func(c *fiber.Ctx) error {
args, err := parseConfigArgs(c)
if err != nil {
return err
}
capturedArgs = args
return c.SendStatus(200)
})

req := httptest.NewRequest("GET", "/test?environment_name="+tt.environmentName, nil)
_, err := app.Test(req)

Expect(err, ShouldBeNil)
Expect(capturedArgs, ShouldNotBeNil)
Expect(capturedArgs.envName, ShouldEqual, tt.expectedProfile)
}

return c.SendStatus(200)
})

req := httptest.NewRequest("GET", "/test?environment_name="+tt.environmentName, nil)
_, err := app.Test(req)
if err != nil {
t.Fatalf("request failed: %v", err)
}
})
}
})
}
20 changes: 12 additions & 8 deletions cmd/api/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@ import (
"testing"

v1 "github.com/pgconfig/api/cmd/api/handlers/v1"
. "github.com/pgconfig/api/pkg/tests"
)

func TestLoadConfiguration(t *testing.T) {
// Paths relative to cmd/api
rulesPath := "../../rules.yml"
docsPath := "../../pg-docs.yml"
Describe("API Configuration", t, func() {
It("should load rules and docs successfully", func() {
// Paths relative to cmd/api
rulesPath := "../../rules.yml"
docsPath := "../../pg-docs.yml"

// We still need to call LoadConfig because the API handlers depend on
// allRules and pgDocs global variables being initialized.
if err := v1.LoadConfig(rulesPath, docsPath); err != nil {
t.Fatalf("Failed to load configuration: %v", err)
}
// We still need to call LoadConfig because the API handlers depend on
// allRules and pgDocs global variables being initialized.
err := v1.LoadConfig(rulesPath, docsPath)
Expect(err, ShouldBeNil)
})
})
}
147 changes: 49 additions & 98 deletions cmd/pgconfigctl/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,115 +3,66 @@ package main
import (
"bytes"
"os"
"strings"
"testing"

"github.com/pgconfig/api/cmd/pgconfigctl/cmd"
. "github.com/pgconfig/api/pkg/tests"
)

// TestTuneProfileParsing validates that all profile types parse correctly
// This addresses issue #22 where Mixed and Desktop profiles were rejected
func TestTuneProfileParsing(t *testing.T) {
tests := []struct {
name string
args []string
wantError bool
errorMsg string
}{
{
name: "Mixed profile - mixed case (issue #22)",
args: []string{"tune", "--profile=Mixed"},
wantError: false,
},
{
name: "Mixed profile - uppercase",
args: []string{"tune", "--profile=MIXED"},
wantError: false,
},
{
name: "Mixed profile - lowercase",
args: []string{"tune", "--profile=mixed"},
wantError: false,
},
{
name: "Desktop profile - mixed case (issue #22)",
args: []string{"tune", "--profile=Desktop"},
wantError: false,
},
{
name: "Desktop profile - uppercase",
args: []string{"tune", "--profile=DESKTOP"},
wantError: false,
},
{
name: "Desktop profile - lowercase",
args: []string{"tune", "--profile=desktop"},
wantError: false,
},
{
name: "Web profile - uppercase",
args: []string{"tune", "--profile=WEB"},
wantError: false,
},
{
name: "Web profile - lowercase",
args: []string{"tune", "--profile=web"},
wantError: false,
},
{
name: "OLTP profile",
args: []string{"tune", "--profile=OLTP"},
wantError: false,
},
{
name: "DW profile",
args: []string{"tune", "--profile=DW"},
wantError: false,
},
{
name: "Invalid profile",
args: []string{"tune", "--profile=invalid"},
wantError: true,
errorMsg: "must be one of",
},
}
Describe("tune command", t, func() {
Context("profile parsing (issue #22)", func() {
It("should accept all profile types case insensitively", func() {
tests := []struct {
name string
args []string
wantError bool
errorMsg string
}{
{"Mixed profile - mixed case", []string{"tune", "--profile=Mixed"}, false, ""},
{"Mixed profile - uppercase", []string{"tune", "--profile=MIXED"}, false, ""},
{"Mixed profile - lowercase", []string{"tune", "--profile=mixed"}, false, ""},
{"Desktop profile - mixed case", []string{"tune", "--profile=Desktop"}, false, ""},
{"Desktop profile - uppercase", []string{"tune", "--profile=DESKTOP"}, false, ""},
{"Desktop profile - lowercase", []string{"tune", "--profile=desktop"}, false, ""},
{"Web profile - uppercase", []string{"tune", "--profile=WEB"}, false, ""},
{"Web profile - lowercase", []string{"tune", "--profile=web"}, false, ""},
{"OLTP profile", []string{"tune", "--profile=OLTP"}, false, ""},
{"DW profile", []string{"tune", "--profile=DW"}, false, ""},
{"Invalid profile", []string{"tune", "--profile=invalid"}, true, "must be one of"},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Capture output
outBuf := new(bytes.Buffer)
errBuf := new(bytes.Buffer)
for _, tt := range tests {
// Capture output
outBuf := new(bytes.Buffer)
errBuf := new(bytes.Buffer)

// Save and restore original output
originalOut := os.Stdout
originalErr := os.Stderr
t.Cleanup(func() {
os.Stdout = originalOut
os.Stderr = originalErr
})
// Save and restore original output
originalOut := os.Stdout
originalErr := os.Stderr
defer func() {
os.Stdout = originalOut
os.Stderr = originalErr
}()

// Set command output
cmd.RootCmd.SetOut(outBuf)
cmd.RootCmd.SetErr(errBuf)
cmd.RootCmd.SetArgs(tt.args)
// Set command output
cmd.RootCmd.SetOut(outBuf)
cmd.RootCmd.SetErr(errBuf)
cmd.RootCmd.SetArgs(tt.args)

// Execute command
err := cmd.RootCmd.Execute()
// Execute command
err := cmd.RootCmd.Execute()

if tt.wantError {
if err == nil {
t.Errorf("Expected error but got none")
return
}
if tt.errorMsg != "" && !strings.Contains(err.Error(), tt.errorMsg) {
t.Errorf("Error message = %v, want to contain %v", err.Error(), tt.errorMsg)
if tt.wantError {
Expect(err, ShouldNotBeNil)
if tt.errorMsg != "" {
Expect(err.Error(), ShouldContainSubstring, tt.errorMsg)
}
} else {
Expect(err, ShouldBeNil)
}
}
} else {
if err != nil {
t.Errorf("Unexpected error: %v\nOutput: %s\nError output: %s",
err, outBuf.String(), errBuf.String())
}
}
})
})
}
})
}
Loading