group_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package http
  2. import (
  3. "context"
  4. "net/url"
  5. "strconv"
  6. "testing"
  7. . "github.com/smartystreets/goconvey/convey"
  8. )
  9. const (
  10. _getGroupListURL = "http://localhost:8801/openplatform/admin/abtest/group/list"
  11. _AddGroupURL = "http://localhost:8801/openplatform/admin/abtest/group/add"
  12. _DelGroupURL = "http://localhost:8801/openplatform/admin/abtest/group/delete"
  13. _UpdateGroupURL = "http://localhost:8801/openplatform/admin/abtest/group/update"
  14. )
  15. var agcs = []TestCase{
  16. TestCase{tag: "TestAddGroup: valid parameters", testData: TestData{"name": "test", "desc": "test add"}, should: Shoulds{0}},
  17. TestCase{tag: "TestAddGroup: empty parameters", testData: TestData{"name": "", "desc": ""}, should: Shoulds{-400}},
  18. TestCase{tag: "TestAddGroup: no parameters", testData: TestData{}, should: Shoulds{-400}},
  19. }
  20. func TestAddGroup(t *testing.T) {
  21. for _, td := range agcs {
  22. Convey(td.tag, t, func() {
  23. params := url.Values{}
  24. for k, v := range td.testData {
  25. params.Set(k, v)
  26. }
  27. req, _ := client.NewRequest("GET", _AddGroupURL, "127.0.0.1", params)
  28. var res struct {
  29. Code int `json:"code"`
  30. Data int `json:"data"`
  31. }
  32. if err := client.Do(context.TODO(), req, &res); err != nil {
  33. t.Errorf("client.Do() error(%v)", err)
  34. t.FailNow()
  35. }
  36. So(res.Code, ShouldEqual, td.should[0])
  37. if res.Code == 0 {
  38. testID = res.Data
  39. }
  40. })
  41. }
  42. }
  43. func TestUpdateGroup(t *testing.T) {
  44. var ugcs = []TestCase{
  45. TestCase{tag: "TestUpdateGroup: valid parameters", testData: TestData{"id": strconv.Itoa(testID), "name": "test", "desc": "test update"}, should: Shoulds{0}},
  46. TestCase{tag: "TestUpdateGroup: empty parameters", testData: TestData{"id": "0", "name": "", "desc": ""}, should: Shoulds{-400}},
  47. TestCase{tag: "TestUpdateGroup: no parameters", testData: TestData{}, should: Shoulds{-400}},
  48. }
  49. for _, td := range ugcs {
  50. Convey(td.tag, t, func() {
  51. params := url.Values{}
  52. for k, v := range td.testData {
  53. params.Set(k, v)
  54. }
  55. req, _ := client.NewRequest("GET", _UpdateGroupURL, "127.0.0.1", params)
  56. var res struct {
  57. Code int `json:"code"`
  58. }
  59. if err := client.Do(context.TODO(), req, &res); err != nil {
  60. t.Errorf("client.Do() error(%v)", err)
  61. t.FailNow()
  62. }
  63. So(res.Code, ShouldEqual, td.should[0])
  64. })
  65. }
  66. }
  67. func TestDeleteGroup(t *testing.T) {
  68. var dgcs = []TestCase{
  69. TestCase{tag: "TestDeleteGroup: valid parameters", testData: TestData{"id": strconv.Itoa(testID), "name": "test", "desc": "test delete"}, should: Shoulds{0}},
  70. TestCase{tag: "TestDeleteGroup: empty parameters", testData: TestData{"id": "0", "name": "", "desc": ""}, should: Shoulds{-400}},
  71. TestCase{tag: "TestDeleteGroup: no parameters", testData: TestData{}, should: Shoulds{-400}},
  72. }
  73. for _, td := range dgcs {
  74. Convey(td.tag, t, func() {
  75. params := url.Values{}
  76. for k, v := range td.testData {
  77. params.Set(k, v)
  78. }
  79. req, _ := client.NewRequest("GET", _DelGroupURL, "127.0.0.1", params)
  80. var res struct {
  81. Code int `json:"code"`
  82. }
  83. if err := client.Do(context.TODO(), req, &res); err != nil {
  84. t.Errorf("client.Do() error(%v)", err)
  85. t.FailNow()
  86. }
  87. So(res.Code, ShouldEqual, td.should[0])
  88. })
  89. }
  90. }
  91. func TestListGroup(t *testing.T) {
  92. Convey("TestListGroup: ", t, func() {
  93. params := url.Values{}
  94. req, _ := client.NewRequest("GET", _getGroupListURL, "127.0.0.1", params)
  95. var res struct {
  96. Code int `json:"code"`
  97. }
  98. if err := client.Do(context.TODO(), req, &res); err != nil {
  99. t.Errorf("client.Do() error(%v)", err)
  100. t.FailNow()
  101. }
  102. So(res.Code, ShouldEqual, 0)
  103. })
  104. }