question_test.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package http
  2. import (
  3. "context"
  4. "net/url"
  5. "testing"
  6. "go-common/app/service/openplatform/anti-fraud/model"
  7. "go-common/library/ecode"
  8. . "github.com/smartystreets/goconvey/convey"
  9. )
  10. const (
  11. _qusBankInfoURL = "http://localhost:8801/openplatform/internal/antifraud/qusb/info?qbid=100"
  12. _qusBanklistURL = "http://localhost:8801/openplatform/internal/antifraud/qusb/list"
  13. _qslistURL = "http://localhost:8801/openplatform/internal/antifraud/qs/list"
  14. _qsInfoURL = "http://localhost:8801/openplatform/internal/antifraud/qs/info"
  15. _qsGetURL = "http://localhost:8801/openplatform/internal/antifraud/qs/get"
  16. )
  17. type TestData map[string]string
  18. type Shoulds []interface{}
  19. type TestCase struct {
  20. tag string
  21. testData TestData
  22. should Shoulds
  23. }
  24. var glcs = []TestCase{
  25. {tag: "TestQusBankList: valid parameters", testData: TestData{"page": "1", "page_size": "20"}, should: Shoulds{-0}},
  26. {tag: "TestQusBankList: no page", testData: TestData{"page_size": "1"}, should: Shoulds{-400}},
  27. {tag: "TestQusBankList: no page_size", testData: TestData{"page": "1"}, should: Shoulds{-400}},
  28. {tag: "TestQusBankList: no mstatus", testData: TestData{"page": "a", "page_size": "b"}, should: Shoulds{-400}},
  29. {tag: "TestQusBankList: invalid page", testData: TestData{"page": "a", "page_size": "20"}, should: Shoulds{-400}},
  30. {tag: "TestQusBankList: invalid page_size", testData: TestData{"page": "1", "page_size": "a"}, should: Shoulds{-400}},
  31. }
  32. func TestQusBankList(t *testing.T) {
  33. for _, td := range glcs {
  34. Convey(td.tag, t, func() {
  35. params := url.Values{}
  36. for k, v := range td.testData {
  37. params.Set(k, v)
  38. }
  39. req, _ := client.NewRequest("GET", _qusBanklistURL, "127.0.0.1", params)
  40. var res struct {
  41. Code int `json:"code"`
  42. Data struct {
  43. Result interface{} `json:"result"`
  44. Total interface{} `json:"total"`
  45. PageNo interface{} `json:"page_no"`
  46. PageSize interface{} `json:"page_size"`
  47. Items interface{} `json:"items"`
  48. } `json:"data"`
  49. }
  50. if err := client.Do(context.TODO(), req, &res); err != nil {
  51. t.Errorf("client.Do() error(%v)", err)
  52. t.FailNow()
  53. }
  54. So(res.Code, ShouldEqual, td.should[0])
  55. })
  56. }
  57. }
  58. func TestQusList(t *testing.T) {
  59. for _, td := range glcs {
  60. Convey(td.tag, t, func() {
  61. params := url.Values{}
  62. for k, v := range td.testData {
  63. params.Set(k, v)
  64. }
  65. req, _ := client.NewRequest("GET", _qslistURL, "127.0.0.1", params)
  66. var res struct {
  67. Code int `json:"code"`
  68. Data struct {
  69. Result interface{} `json:"result"`
  70. Total interface{} `json:"total"`
  71. PageNo interface{} `json:"page_no"`
  72. PageSize interface{} `json:"page_size"`
  73. Items interface{} `json:"items"`
  74. } `json:"data"`
  75. }
  76. if err := client.Do(context.TODO(), req, &res); err != nil {
  77. t.Errorf("client.Do() error(%v)", err)
  78. t.FailNow()
  79. }
  80. So(res.Code, ShouldEqual, td.should[0])
  81. })
  82. }
  83. }
  84. var argsBankInfo = []TestCase{
  85. {tag: "TestQusBankInfo: valid parameters", testData: TestData{"qb_id": "1111"}, should: Shoulds{0}},
  86. {tag: "TestQusBankInfo: no qb_id", testData: TestData{"qb_id": "1"}, should: Shoulds{0}},
  87. {tag: "TestQusBankInfo: invalid qb_id", testData: TestData{"qb_id": "a"}, should: Shoulds{-400}},
  88. }
  89. func TestQusBankInfo(t *testing.T) {
  90. for _, td := range argsBankInfo {
  91. Convey(td.tag, t, func() {
  92. params := url.Values{}
  93. for k, v := range td.testData {
  94. params.Set(k, v)
  95. }
  96. req, _ := client.NewRequest("GET", _qusBankInfoURL, "127.0.0.1", params)
  97. var res struct {
  98. Code int `json:"code"`
  99. Data model.QuestionBank `json:"data"`
  100. }
  101. if err := client.Do(context.TODO(), req, &res); err != nil {
  102. t.Errorf("client.Do() error(%v)", err)
  103. t.FailNow()
  104. }
  105. So(res.Code, ShouldEqual, td.should[0])
  106. })
  107. }
  108. }
  109. var argsQusInfo = []TestCase{
  110. {tag: "TestQusBankInfo: valid parameters", testData: TestData{"qid": "1111"}, should: Shoulds{20001005}},
  111. {tag: "TestQusBankInfo: invalid qid", testData: TestData{"qid": "a"}, should: Shoulds{-400}},
  112. }
  113. func TestQusInfo(t *testing.T) {
  114. for _, td := range argsQusInfo {
  115. Convey(td.tag, t, func() {
  116. params := url.Values{}
  117. for k, v := range td.testData {
  118. params.Set(k, v)
  119. }
  120. req, _ := client.NewRequest("GET", _qsInfoURL, "127.0.0.1", params)
  121. var res struct {
  122. Code int `json:"code"`
  123. Data model.QuestionBank `json:"data"`
  124. }
  125. if err := client.Do(context.TODO(), req, &res); err != nil {
  126. t.Errorf("client.Do() error(%v)", err)
  127. t.FailNow()
  128. }
  129. So(res.Code, ShouldEqual, td.should[0])
  130. })
  131. }
  132. }
  133. var argsGetQuestion = []TestCase{
  134. {tag: "TestQusBankInfo: valid parameters", testData: TestData{"uid": "1111", "target_item": "11111", "target_item_type": "1", "source": "1", "platform": "1", "component_id": "122"},
  135. should: Shoulds{ecode.BindBankNotFound.Code(), ecode.GetComponentIDErr.Code(), ecode.SetComponentIDErr.Code(), 0}},
  136. {tag: "TestQusBankInfo: invalid ", testData: TestData{"uid": "a"}, should: Shoulds{-400, -400}},
  137. }
  138. func TestGetQuestion(t *testing.T) {
  139. for _, td := range argsGetQuestion {
  140. Convey(td.tag, t, func() {
  141. params := url.Values{}
  142. for k, v := range td.testData {
  143. params.Set(k, v)
  144. }
  145. req, _ := client.NewRequest("GET", _qsGetURL, "127.0.0.1", params)
  146. var res struct {
  147. Code int `json:"code"`
  148. Data model.QuestionBank `json:"data"`
  149. }
  150. if err := client.Do(context.TODO(), req, &res); err != nil {
  151. t.Errorf("client.Do() error(%v)", err)
  152. t.FailNow()
  153. }
  154. So(res.Code, ShouldBeIn, td.should...)
  155. })
  156. }
  157. }