stra_test.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. package http
  2. import (
  3. "context"
  4. "net/url"
  5. "strconv"
  6. "testing"
  7. . "github.com/smartystreets/goconvey/convey"
  8. )
  9. const (
  10. _getListURL = "http://localhost:8801/openplatform/admin/abtest/list"
  11. _getVersionIDURL = "http://localhost:8801/openplatform/internal/abtest/versionid"
  12. _getVersionURL = "http://localhost:8801/openplatform/internal/abtest/version"
  13. _AddURL = "http://localhost:8801/openplatform/admin/abtest/add"
  14. _DelURL = "http://localhost:8801/openplatform/admin/abtest/delete"
  15. _UpdateURL = "http://localhost:8801/openplatform/admin/abtest/update"
  16. _UpdateStatusURL = "http://localhost:8801/openplatform/admin/abtest/status"
  17. )
  18. type TestData map[string]string
  19. type Shoulds []interface{}
  20. type TestCase struct {
  21. tag string
  22. testData TestData
  23. should Shoulds
  24. }
  25. var gvcs = []TestCase{
  26. TestCase{tag: "TestGetVersionID: valid parameters", testData: TestData{"group": "1"}, should: Shoulds{0}},
  27. TestCase{tag: "TestGetVersionID: empty parameters", testData: TestData{"group": ""}, should: Shoulds{-400}},
  28. TestCase{tag: "TestGetVersionID: invalid parameters", testData: TestData{"group": "asd"}, should: Shoulds{-400}},
  29. TestCase{tag: "TestGetVersionID: no parameters", testData: TestData{}, should: Shoulds{-400}},
  30. }
  31. func TestGetVersionID(t *testing.T) {
  32. for _, td := range gvcs {
  33. Convey(td.tag, t, func() {
  34. params := url.Values{}
  35. for k, v := range td.testData {
  36. params.Set(k, v)
  37. }
  38. req, _ := client.NewRequest("GET", _getVersionIDURL, "127.0.0.1", params)
  39. var res struct {
  40. Code int `json:"code"`
  41. }
  42. if err := client.Do(context.TODO(), req, &res); err != nil {
  43. t.Errorf("client.Do() error(%v)", err)
  44. t.FailNow()
  45. }
  46. So(res.Code, ShouldEqual, td.should[0])
  47. })
  48. }
  49. }
  50. var guscs = []TestCase{
  51. TestCase{tag: "TestGetVersion: valid parameters", testData: TestData{"group": "1", "key": "23232", "version": "{}"}, should: Shoulds{0}},
  52. TestCase{tag: "TestGetVersion: no version", testData: TestData{"group": "1", "key": ""}, should: Shoulds{0}},
  53. TestCase{tag: "TestGetVersion: no key", testData: TestData{"group": "1", "version": "{}"}, should: Shoulds{0}},
  54. TestCase{tag: "TestGetVersion: no group", testData: TestData{"group": "1", "version": "{}"}, should: Shoulds{0}},
  55. }
  56. func TestGetVersion(t *testing.T) {
  57. for _, td := range guscs {
  58. Convey(td.tag, t, func() {
  59. params := url.Values{}
  60. for k, v := range td.testData {
  61. params.Set(k, v)
  62. }
  63. req, _ := client.NewRequest("GET", _getVersionURL, "127.0.0.1", params)
  64. var res struct {
  65. Code int `json:"code"`
  66. Data struct {
  67. V int `json:"v"`
  68. D interface{} `json:"d"`
  69. } `json:"data"`
  70. }
  71. if err := client.Do(context.TODO(), req, &res); err != nil {
  72. t.Errorf("client.Do() error(%v)", err)
  73. t.FailNow()
  74. }
  75. So(res.Code, ShouldEqual, td.should[0])
  76. })
  77. }
  78. }
  79. var glcs = []TestCase{
  80. TestCase{tag: "TestGetListAb: valid parameters", testData: TestData{"pn": "1", "ps": "20", "mstatus": "1,2,0"}, should: Shoulds{0}},
  81. TestCase{tag: "TestGetListAb: no pn", testData: TestData{"ps": "1", "mstatus": "1,2,0"}, should: Shoulds{-400}},
  82. TestCase{tag: "TestGetListAb: no ps", testData: TestData{"pn": "1", "mstatus": "1,2,0"}, should: Shoulds{-400}},
  83. TestCase{tag: "TestGetListAb: no mstatus", testData: TestData{"pn": "1", "ps": "10"}, should: Shoulds{-400}},
  84. TestCase{tag: "TestGetListAb: invalid pn", testData: TestData{"pn": "a", "ps": "20", "mstatus": "1,2,0"}, should: Shoulds{-400}},
  85. TestCase{tag: "TestGetListAb: invalid ps", testData: TestData{"pn": "1", "ps": "a", "mstatus": "1,2,0"}, should: Shoulds{-400}},
  86. TestCase{tag: "TestGetListAb: invalid mstatus", testData: TestData{"pn": "1", "ps": "20", "mstatus": "a"}, should: Shoulds{-400}},
  87. }
  88. func TestGetListAb(t *testing.T) {
  89. for _, td := range glcs {
  90. Convey(td.tag, t, func() {
  91. params := url.Values{}
  92. for k, v := range td.testData {
  93. params.Set(k, v)
  94. }
  95. req, _ := client.NewRequest("GET", _getListURL, "127.0.0.1", params)
  96. var res struct {
  97. Code int `json:"code"`
  98. Data struct {
  99. Result interface{} `json:"result"`
  100. Total interface{} `json:"total"`
  101. } `json:"data"`
  102. }
  103. if err := client.Do(context.TODO(), req, &res); err != nil {
  104. t.Errorf("client.Do() error(%v)", err)
  105. t.FailNow()
  106. }
  107. So(res.Code, ShouldEqual, td.should[0])
  108. })
  109. }
  110. }
  111. var testID int
  112. var adcs = []TestCase{
  113. TestCase{tag: "TestAddAb: valid json", testData: TestData{"data": `{"name":"test1","desc":"test","stra":{"precision":100,"ratio":[20,80]},"result":1,"status":0,"group":1,"author":"test","modifer":"test"}`}, should: Shoulds{0}},
  114. TestCase{tag: "TestAddAb: no permission", testData: TestData{"data": `{"name":"test1","desc":"test","stra":{"precision":100,"ratio":[20,80]},"result":1,"status":0,"group":1,"author":"test","modifer":"test"}`, "group": "2"}, should: Shoulds{-400}},
  115. TestCase{tag: "TestAddAb: not json", testData: TestData{"data": `{"name":"test2","desc""test""stra":"{"precision":100,"ratio":[20,80]},"result":1,"status":0,"group":1,"author":"test","modifer":"test"}`}, should: Shoulds{-400}},
  116. TestCase{tag: "TestAddAb: invalid stra", testData: TestData{"data": `{"name":"test3","desc":"test","stra":{"precision":100,"ratio":[20,70]},"result":1,"status":0,"group":1,"author":"test","modifer":"test"}`}, should: Shoulds{-400}},
  117. TestCase{tag: "TestAddAb: no data", testData: TestData{}, should: Shoulds{-400}},
  118. }
  119. func TestAddAb(t *testing.T) {
  120. for _, td := range adcs {
  121. Convey(td.tag, t, func() {
  122. var res struct {
  123. Code int `json:"code"`
  124. Data struct {
  125. Newid int `json:"newid"`
  126. } `json:"data"`
  127. }
  128. params := url.Values{}
  129. for k, v := range td.testData {
  130. params.Set(k, v)
  131. }
  132. req, _ := client.NewRequest("GET", _AddURL, "127.0.0.1", params)
  133. if err := client.Do(context.TODO(), req, &res); err != nil {
  134. t.Errorf("client.Do() error(%v)", err)
  135. t.FailNow()
  136. }
  137. So(res.Code, ShouldEqual, td.should[0])
  138. if res.Code == 0 {
  139. testID = res.Data.Newid
  140. }
  141. })
  142. }
  143. }
  144. func TestUpdateAb(t *testing.T) {
  145. var upcs = []TestCase{
  146. TestCase{tag: "TestUpdateAb: valid params", testData: TestData{"id": strconv.Itoa(testID), "data": `{"name":"test","desc":"update","stra":{"precision":100,"ratio":[20,80]},"result":1,"status":0,"group":1,"author":"test","modifer":"test"}`}, should: Shoulds{0}},
  147. TestCase{tag: "TestUpdateAb: no permission", testData: TestData{"id": strconv.Itoa(testID), "data": `{"name":"test1","desc":"test","stra":{"precision":100,"ratio":[20,80]},"result":1,"status":0,"group":2,"author":"test","modifer":"test"}`, "group": "2"}, should: Shoulds{-500}},
  148. TestCase{tag: "TestUpdateAb: invalid params", testData: TestData{}, should: Shoulds{-400}},
  149. TestCase{tag: "TestUpdateAb: invalid id", testData: TestData{"id": "11111", "data": "aa"}, should: Shoulds{-500}},
  150. TestCase{tag: "TestUpdateAb: invalid data", testData: TestData{"id": strconv.Itoa(testID), "data": "aa"}, should: Shoulds{-400}},
  151. TestCase{tag: "TestUpdateAb: valid stra", testData: TestData{"id": strconv.Itoa(testID), "data": `{"name":"test","desc":"update","stra":{"precision":100,"ratio":[20,81]},"result":1,"status":0,"group":1,"author":"test","modifer":"test"}`}, should: Shoulds{-400}},
  152. TestCase{tag: "TestUpdateAb: valid params group 0", testData: TestData{"id": strconv.Itoa(testID), "data": `{"name":"test","desc":"update2","stra":{"precision":100,"ratio":[20,80]},"result":1,"status":0,"group":0,"author":"test","modifer":"test"}`}, should: Shoulds{0}},
  153. }
  154. for _, td := range upcs {
  155. Convey(td.tag, t, func() {
  156. params := url.Values{}
  157. for k, v := range td.testData {
  158. params.Set(k, v)
  159. }
  160. req, _ := client.NewRequest("GET", _UpdateURL, "127.0.0.1", params)
  161. var res struct {
  162. Code int `json:"code"`
  163. }
  164. if err := client.Do(context.TODO(), req, &res); err != nil {
  165. t.Errorf("client.Do() error(%v)", err)
  166. t.FailNow()
  167. }
  168. So(res.Code, ShouldEqual, td.should[0])
  169. })
  170. }
  171. }
  172. func TestUpdateStatusAb(t *testing.T) {
  173. var upscs = []TestCase{
  174. TestCase{tag: "TestUpdateStatusAb: valid params", testData: TestData{"id": strconv.Itoa(testID), "status": "1", "modifier": "test2"}, should: Shoulds{0}},
  175. TestCase{tag: "TestUpdateStatusAb: no permission", testData: TestData{"id": strconv.Itoa(testID), "status": "1", "modifier": "test2", "group": "2"}, should: Shoulds{-500}},
  176. TestCase{tag: "TestUpdateStatusAb: invalid params", testData: TestData{}, should: Shoulds{-400}},
  177. TestCase{tag: "TestUpdateStatusAb: invalid id", testData: TestData{"id": "11111", "data": "aa"}, should: Shoulds{-400}},
  178. TestCase{tag: "TestUpdateStatusAb: invalid status", testData: TestData{"id": strconv.Itoa(testID), "status": "4", "modifier": "test2"}, should: Shoulds{-400}},
  179. TestCase{tag: "TestUpdateStatusAb: valid params", testData: TestData{"id": strconv.Itoa(testID), "status": "3", "modifier": "test2"}, should: Shoulds{0}},
  180. }
  181. for _, td := range upscs {
  182. Convey(td.tag, t, func() {
  183. params := url.Values{}
  184. for k, v := range td.testData {
  185. params.Set(k, v)
  186. }
  187. req, _ := client.NewRequest("GET", _UpdateStatusURL, "127.0.0.1", params)
  188. var res struct {
  189. Code int `json:"code"`
  190. }
  191. if err := client.Do(context.TODO(), req, &res); err != nil {
  192. t.Errorf("client.Do() error(%v)", err)
  193. t.FailNow()
  194. }
  195. So(res.Code, ShouldEqual, td.should[0])
  196. })
  197. }
  198. }
  199. func TestDelAb(t *testing.T) {
  200. var dacs = []TestCase{
  201. TestCase{tag: "TestDelAb: no permission", testData: TestData{"id": strconv.Itoa(testID), "group": "2"}, should: Shoulds{-500}},
  202. TestCase{tag: "TestDelAb: valid id", testData: TestData{"id": strconv.Itoa(testID)}, should: Shoulds{0}},
  203. TestCase{tag: "TestDelAb: invalid id", testData: TestData{"id": "x"}, should: Shoulds{-400}},
  204. }
  205. for _, td := range dacs {
  206. Convey(td.tag, t, func() {
  207. params := url.Values{}
  208. for k, v := range td.testData {
  209. params.Set(k, v)
  210. }
  211. req, _ := client.NewRequest("GET", _DelURL, "127.0.0.1", params)
  212. var res struct {
  213. Code int `json:"code"`
  214. }
  215. if err := client.Do(context.TODO(), req, &res); err != nil {
  216. t.Errorf("client.Do() error(%v)", err)
  217. t.FailNow()
  218. }
  219. So(res.Code, ShouldEqual, td.should[0])
  220. })
  221. }
  222. }