-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathentity_create_test.go
108 lines (76 loc) · 2.65 KB
/
entity_create_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/gorilla/context"
"github.com/stretchr/testify/assert"
)
func TestCreateBadRequestJson(t *testing.T) {
postBody := `foo`
req := postRequest("/Project", postBody)
w := httptest.NewRecorder()
server, client, config := mockShotgun(200, `foo`)
defer server.Close()
context.Set(req, "sgConn", *client)
router(config).ServeHTTP(w, req)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestCreateSimple(t *testing.T) {
postBody := `{"name": "My Project"}`
req := postRequest("/Project", postBody)
w := httptest.NewRecorder()
server, client, config := mockShotgun(200, `{"results":{"id":75,"name":"My Project","type":"Project"}}`)
defer server.Close()
context.Set(req, "sgConn", *client)
router(config).ServeHTTP(w, req)
assert.Equal(t, http.StatusCreated, w.Code)
type Entity struct {
Type string `json:"type"`
Id int `json:"id"`
Name string `json:"name"`
}
expectedResponse := Entity{
Type: "Project",
Id: 75,
Name: "My Project",
}
var jsonResp Entity
err := json.Unmarshal(w.Body.Bytes(), &jsonResp)
assert.Nil(t, err)
assert.Equal(t, expectedResponse, jsonResp)
}
//{"exception":true,"message":"API create() CRUD ERROR #61: Create failed for [Project]. The value for the Project Name field is required to be unique. <br>","error_code":104}
func TestCreateConflict(t *testing.T) {
postBody := `{"name": "My Project"}`
req := postRequest("/Project", postBody)
w := httptest.NewRecorder()
server, client, config := mockShotgun(200,
`{"exception":true,"message":"API create() CRUD ERROR #61: Create failed for [Project]. The value for the Project Name field is required to be unique. <br>","error_code":104}`)
defer server.Close()
context.Set(req, "sgConn", *client)
router(config).ServeHTTP(w, req)
assert.Equal(t, http.StatusConflict, w.Code)
}
func TestCreateBadResponseJson(t *testing.T) {
postBody := `{"name": "My Project"}`
req := postRequest("/Project", postBody)
w := httptest.NewRecorder()
server, client, config := mockShotgun(200, `foo`)
defer server.Close()
context.Set(req, "sgConn", *client)
router(config).ServeHTTP(w, req)
assert.Equal(t, http.StatusBadGateway, w.Code)
}
func TestCreateShotgunError(t *testing.T) {
postBody := `{"name": "My Project"}`
req := postRequest("/Project", postBody)
w := httptest.NewRecorder()
server, client, config := mockShotgun(200,
`{"exception":true,"message":"API create() CRUD ERROR Some other error","error_code":104}`)
defer server.Close()
context.Set(req, "sgConn", *client)
router(config).ServeHTTP(w, req)
assert.Equal(t, http.StatusBadRequest, w.Code)
}