sqlstate_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package httpserver
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "net/http"
  7. "net/http/httptest"
  8. "strings"
  9. "testing"
  10. "github.com/danfragoso/pizzasql-next/pkg/storage"
  11. )
  12. type httpErrorBody struct {
  13. Error struct {
  14. Code string `json:"code"`
  15. Message string `json:"message"`
  16. Details map[string]interface{} `json:"details"`
  17. } `json:"error"`
  18. }
  19. func TestHTTPUniqueViolationReturns23505(t *testing.T) {
  20. server := setupKVTestServer(t)
  21. queryHTTP(t, server, "CREATE TABLE users (id INTEGER PRIMARY KEY, email TEXT UNIQUE)")
  22. queryHTTP(t, server, "INSERT INTO users (id, email) VALUES (1, 'a@x')")
  23. body, _ := json.Marshal(QueryRequest{SQL: "INSERT INTO users (id, email) VALUES (2, 'a@x')"})
  24. r := httptest.NewRequest(http.MethodPost, "/query", strings.NewReader(string(body)))
  25. w := httptest.NewRecorder()
  26. server.handleQuery(w, r)
  27. if w.Code != http.StatusConflict {
  28. t.Fatalf("status = %d, want 409; body %s", w.Code, w.Body.String())
  29. }
  30. var resp httpErrorBody
  31. if err := json.NewDecoder(w.Body).Decode(&resp); err != nil {
  32. t.Fatalf("decode: %v", err)
  33. }
  34. if resp.Error.Code != "23505" {
  35. t.Fatalf("code = %q, want 23505", resp.Error.Code)
  36. }
  37. constraint, _ := resp.Error.Details["constraint"].(string)
  38. if constraint == "" || !strings.Contains(resp.Error.Message, constraint) {
  39. t.Fatalf("details.constraint = %q, message = %q", constraint, resp.Error.Message)
  40. }
  41. }
  42. func TestSQLStateHTTPErrorMapping(t *testing.T) {
  43. tests := []struct {
  44. name string
  45. err error
  46. status int
  47. code string
  48. }{
  49. {"unique", fmt.Errorf("UNIQUE constraint failed: uniq_users_email"), http.StatusConflict, "23505"},
  50. {"serialization", fmt.Errorf("commit: %w", storage.ErrSerialization), http.StatusConflict, "40001"},
  51. {"unsupported", fmt.Errorf("feature not supported: DEFAULT is not constant"), http.StatusBadRequest, "0A000"},
  52. {"other", errors.New("boom"), http.StatusInternalServerError, "EXECUTION_ERROR"},
  53. }
  54. for _, tt := range tests {
  55. t.Run(tt.name, func(t *testing.T) {
  56. got := sqlStateHTTPError(tt.err)
  57. if got.Status != tt.status || got.Code != tt.code {
  58. t.Fatalf("got status=%d code=%s, want status=%d code=%s", got.Status, got.Code, tt.status, tt.code)
  59. }
  60. })
  61. }
  62. }