order_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. package handler
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "net/http"
  6. "net/http/httptest"
  7. "testing"
  8. "time"
  9. "job-cheng-xing/model"
  10. "job-cheng-xing/repository"
  11. "job-cheng-xing/service"
  12. "github.com/gin-gonic/gin"
  13. )
  14. func setupRouter() *gin.Engine {
  15. gin.SetMode(gin.TestMode)
  16. repo := repository.NewMemoryRepo()
  17. svc := service.NewOrderService(repo)
  18. handler := NewOrderHandler(svc)
  19. r := gin.New()
  20. r.POST("/orders", handler.CreateOrder)
  21. r.PUT("/orders/:id/accept", handler.AcceptOrder)
  22. r.PUT("/orders/:id/cancel", handler.CancelOrder)
  23. return r
  24. }
  25. func TestCreateOrder_Handler(t *testing.T) {
  26. r := setupRouter()
  27. body := map[string]interface{}{
  28. "service_time": time.Now().Add(24 * time.Hour).Format(time.RFC3339),
  29. "duration": 120,
  30. "address": "北京朝阳区xxx路xxx号",
  31. }
  32. jsonBody, _ := json.Marshal(body)
  33. req, _ := http.NewRequest("POST", "/orders", bytes.NewBuffer(jsonBody))
  34. req.Header.Set("Content-Type", "application/json")
  35. w := httptest.NewRecorder()
  36. r.ServeHTTP(w, req)
  37. if w.Code != http.StatusCreated {
  38. t.Errorf("expected 201, got %d: %s", w.Code, w.Body.String())
  39. }
  40. var order model.Order
  41. json.Unmarshal(w.Body.Bytes(), &order)
  42. if order.ID == "" {
  43. t.Error("expected non-empty ID")
  44. }
  45. if order.Status != model.StatusPending {
  46. t.Errorf("expected pending, got %s", order.Status)
  47. }
  48. }
  49. func TestAcceptOrder_Handler_Success(t *testing.T) {
  50. r := setupRouter()
  51. body := map[string]interface{}{
  52. "service_time": time.Now().Add(24 * time.Hour).Format(time.RFC3339),
  53. "duration": 120,
  54. "address": "北京朝阳",
  55. }
  56. jsonBody, _ := json.Marshal(body)
  57. req, _ := http.NewRequest("POST", "/orders", bytes.NewBuffer(jsonBody))
  58. req.Header.Set("Content-Type", "application/json")
  59. w := httptest.NewRecorder()
  60. r.ServeHTTP(w, req)
  61. var order model.Order
  62. json.Unmarshal(w.Body.Bytes(), &order)
  63. acceptBody := map[string]string{"provider_id": "prov_001"}
  64. acceptJSON, _ := json.Marshal(acceptBody)
  65. req2, _ := http.NewRequest("PUT", "/orders/"+order.ID+"/accept", bytes.NewBuffer(acceptJSON))
  66. req2.Header.Set("Content-Type", "application/json")
  67. w2 := httptest.NewRecorder()
  68. r.ServeHTTP(w2, req2)
  69. if w2.Code != http.StatusOK {
  70. t.Errorf("expected 200, got %d: %s", w2.Code, w2.Body.String())
  71. }
  72. }
  73. func TestAcceptOrder_Handler_DoubleAccept(t *testing.T) {
  74. r := setupRouter()
  75. body := map[string]interface{}{
  76. "service_time": time.Now().Add(24 * time.Hour).Format(time.RFC3339),
  77. "duration": 120,
  78. "address": "北京朝阳",
  79. }
  80. jsonBody, _ := json.Marshal(body)
  81. req, _ := http.NewRequest("POST", "/orders", bytes.NewBuffer(jsonBody))
  82. req.Header.Set("Content-Type", "application/json")
  83. w := httptest.NewRecorder()
  84. r.ServeHTTP(w, req)
  85. var order model.Order
  86. json.Unmarshal(w.Body.Bytes(), &order)
  87. acceptBody1 := map[string]string{"provider_id": "prov_001"}
  88. acceptJSON1, _ := json.Marshal(acceptBody1)
  89. req1, _ := http.NewRequest("PUT", "/orders/"+order.ID+"/accept", bytes.NewBuffer(acceptJSON1))
  90. req1.Header.Set("Content-Type", "application/json")
  91. w1 := httptest.NewRecorder()
  92. r.ServeHTTP(w1, req1)
  93. if w1.Code != http.StatusOK {
  94. t.Fatalf("first accept should succeed, got %d", w1.Code)
  95. }
  96. acceptBody2 := map[string]string{"provider_id": "prov_002"}
  97. acceptJSON2, _ := json.Marshal(acceptBody2)
  98. req2, _ := http.NewRequest("PUT", "/orders/"+order.ID+"/accept", bytes.NewBuffer(acceptJSON2))
  99. req2.Header.Set("Content-Type", "application/json")
  100. w2 := httptest.NewRecorder()
  101. r.ServeHTTP(w2, req2)
  102. if w2.Code != http.StatusConflict {
  103. t.Errorf("expected 409, got %d: %s", w2.Code, w2.Body.String())
  104. }
  105. }
  106. func TestCancelOrder_Handler_ThenAcceptFails(t *testing.T) {
  107. r := setupRouter()
  108. body := map[string]interface{}{
  109. "service_time": time.Now().Add(24 * time.Hour).Format(time.RFC3339),
  110. "duration": 120,
  111. "address": "北京朝阳",
  112. }
  113. jsonBody, _ := json.Marshal(body)
  114. req, _ := http.NewRequest("POST", "/orders", bytes.NewBuffer(jsonBody))
  115. req.Header.Set("Content-Type", "application/json")
  116. w := httptest.NewRecorder()
  117. r.ServeHTTP(w, req)
  118. var order model.Order
  119. json.Unmarshal(w.Body.Bytes(), &order)
  120. req1, _ := http.NewRequest("PUT", "/orders/"+order.ID+"/cancel", bytes.NewBuffer([]byte("{}")))
  121. req1.Header.Set("Content-Type", "application/json")
  122. w1 := httptest.NewRecorder()
  123. r.ServeHTTP(w1, req1)
  124. if w1.Code != http.StatusOK {
  125. t.Fatalf("cancel should succeed, got %d", w1.Code)
  126. }
  127. acceptBody := map[string]string{"provider_id": "prov_001"}
  128. acceptJSON, _ := json.Marshal(acceptBody)
  129. req2, _ := http.NewRequest("PUT", "/orders/"+order.ID+"/accept", bytes.NewBuffer(acceptJSON))
  130. req2.Header.Set("Content-Type", "application/json")
  131. w2 := httptest.NewRecorder()
  132. r.ServeHTTP(w2, req2)
  133. if w2.Code != http.StatusBadRequest {
  134. t.Errorf("expected 400, got %d: %s", w2.Code, w2.Body.String())
  135. }
  136. }
  137. func TestCancelOrder_Handler_DoubleCancel(t *testing.T) {
  138. r := setupRouter()
  139. body := map[string]interface{}{
  140. "service_time": time.Now().Add(24 * time.Hour).Format(time.RFC3339),
  141. "duration": 120,
  142. "address": "北京朝阳",
  143. }
  144. jsonBody, _ := json.Marshal(body)
  145. req, _ := http.NewRequest("POST", "/orders", bytes.NewBuffer(jsonBody))
  146. req.Header.Set("Content-Type", "application/json")
  147. w := httptest.NewRecorder()
  148. r.ServeHTTP(w, req)
  149. var order model.Order
  150. json.Unmarshal(w.Body.Bytes(), &order)
  151. req1, _ := http.NewRequest("PUT", "/orders/"+order.ID+"/cancel", bytes.NewBuffer([]byte("{}")))
  152. req1.Header.Set("Content-Type", "application/json")
  153. w1 := httptest.NewRecorder()
  154. r.ServeHTTP(w1, req1)
  155. req2, _ := http.NewRequest("PUT", "/orders/"+order.ID+"/cancel", bytes.NewBuffer([]byte("{}")))
  156. req2.Header.Set("Content-Type", "application/json")
  157. w2 := httptest.NewRecorder()
  158. r.ServeHTTP(w2, req2)
  159. if w2.Code != http.StatusBadRequest {
  160. t.Errorf("expected 400, got %d: %s", w2.Code, w2.Body.String())
  161. }
  162. }