Compare commits

...

2 Commits

Author SHA1 Message Date
14ae6f815b feat: update tests to verify clamping 2025-11-21 15:21:05 +01:00
73083e4188 feat: check zero/negative value in seeding 2025-11-21 15:20:57 +01:00
2 changed files with 36 additions and 29 deletions

View File

@@ -69,14 +69,33 @@ func seedDatabase(userRepo repositories.UserRepository, postRepo repositories.Po
return err return err
} }
originalUsers := *numUsers
originalPosts := *numPosts
originalVotesPerPost := *votesPerPost
if *numUsers < 0 { if *numUsers < 0 {
return fmt.Errorf("invalid value for --users: %d (must be >= 0)", *numUsers) if !IsJSONOutput() {
fmt.Fprintf(os.Stderr, "Warning: --users value %d is negative, clamping to 0\n", *numUsers)
} }
*numUsers = 0
}
if *numPosts <= 0 { if *numPosts <= 0 {
return fmt.Errorf("invalid value for --posts: %d (must be > 0)", *numPosts) if !IsJSONOutput() {
fmt.Fprintf(os.Stderr, "Warning: --posts value %d is too low, clamping to 1\n", *numPosts)
} }
*numPosts = 1
}
if *votesPerPost < 0 { if *votesPerPost < 0 {
return fmt.Errorf("invalid value for --votes-per-post: %d (must be >= 0)", *votesPerPost) if !IsJSONOutput() {
fmt.Fprintf(os.Stderr, "Warning: --votes-per-post value %d is negative, clamping to 0\n", *votesPerPost)
}
*votesPerPost = 0
}
if !IsJSONOutput() && (originalUsers != *numUsers || originalPosts != *numPosts || originalVotesPerPost != *votesPerPost) {
fmt.Fprintf(os.Stderr, "Using clamped values: --users=%d --posts=%d --votes-per-post=%d\n", *numUsers, *numPosts, *votesPerPost)
} }
if !IsJSONOutput() { if !IsJSONOutput() {

View File

@@ -179,47 +179,35 @@ func TestSeedDatabaseFlagParsing(t *testing.T) {
} }
}) })
t.Run("negative users value", func(t *testing.T) { t.Run("negative users value is clamped", func(t *testing.T) {
err := seedDatabase(userRepo, postRepo, voteRepo, []string{"--users", "-1"}) err := seedDatabase(userRepo, postRepo, voteRepo, []string{"--users", "-1", "--posts", "1"})
if err == nil { if err != nil {
t.Error("expected error for negative users value") t.Errorf("negative users should be clamped, not rejected. Got error: %v", err)
}
if err != nil && err.Error() != "invalid value for --users: -1 (must be >= 0)" {
t.Errorf("expected specific error message, got: %v", err)
} }
}) })
t.Run("negative posts value", func(t *testing.T) { t.Run("negative posts value is clamped", func(t *testing.T) {
err := seedDatabase(userRepo, postRepo, voteRepo, []string{"--posts", "-5"}) err := seedDatabase(userRepo, postRepo, voteRepo, []string{"--posts", "-5"})
if err == nil { if err != nil {
t.Error("expected error for negative posts value") t.Errorf("negative posts should be clamped, not rejected. Got error: %v", err)
}
if err != nil && err.Error() != "invalid value for --posts: -5 (must be > 0)" {
t.Errorf("expected specific error message, got: %v", err)
} }
}) })
t.Run("zero posts value", func(t *testing.T) { t.Run("zero posts value is clamped", func(t *testing.T) {
err := seedDatabase(userRepo, postRepo, voteRepo, []string{"--posts", "0"}) err := seedDatabase(userRepo, postRepo, voteRepo, []string{"--posts", "0"})
if err == nil { if err != nil {
t.Error("expected error for zero posts value") t.Errorf("zero posts should be clamped, not rejected. Got error: %v", err)
}
if err != nil && err.Error() != "invalid value for --posts: 0 (must be > 0)" {
t.Errorf("expected specific error message, got: %v", err)
} }
}) })
t.Run("negative votes-per-post value", func(t *testing.T) { t.Run("negative votes-per-post value is clamped", func(t *testing.T) {
err := seedDatabase(userRepo, postRepo, voteRepo, []string{"--votes-per-post", "-10"}) err := seedDatabase(userRepo, postRepo, voteRepo, []string{"--votes-per-post", "-10", "--posts", "1"})
if err == nil { if err != nil {
t.Error("expected error for negative votes-per-post value") t.Errorf("negative votes-per-post should be clamped, not rejected. Got error: %v", err)
}
if err != nil && err.Error() != "invalid value for --votes-per-post: -10 (must be >= 0)" {
t.Errorf("expected specific error message, got: %v", err)
} }
}) })