query_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package dsn
  2. import (
  3. "net/url"
  4. "reflect"
  5. "testing"
  6. "time"
  7. xtime "go-common/library/time"
  8. )
  9. type cfg1 struct {
  10. Name string `dsn:"query.name"`
  11. Def string `dsn:"query.def,hello"`
  12. DefSlice []int `dsn:"query.defslice,1,2,3,4"`
  13. Ignore string `dsn:"-"`
  14. FloatNum float64 `dsn:"query.floatNum"`
  15. }
  16. type cfg2 struct {
  17. Timeout xtime.Duration `dsn:"query.timeout"`
  18. }
  19. type cfg3 struct {
  20. Username string `dsn:"username"`
  21. Timeout xtime.Duration `dsn:"query.timeout"`
  22. }
  23. type cfg4 struct {
  24. Timeout xtime.Duration `dsn:"query.timeout,1s"`
  25. }
  26. func TestDecodeQuery(t *testing.T) {
  27. type args struct {
  28. query url.Values
  29. v interface{}
  30. assignFuncs map[string]assignFunc
  31. }
  32. tests := []struct {
  33. name string
  34. args args
  35. want url.Values
  36. cfg interface{}
  37. wantErr bool
  38. }{
  39. {
  40. name: "test generic",
  41. args: args{
  42. query: url.Values{
  43. "name": {"hello"},
  44. "Ignore": {"test"},
  45. "floatNum": {"22.33"},
  46. "adb": {"123"},
  47. },
  48. v: &cfg1{},
  49. },
  50. want: url.Values{
  51. "Ignore": {"test"},
  52. "adb": {"123"},
  53. },
  54. cfg: &cfg1{
  55. Name: "hello",
  56. Def: "hello",
  57. DefSlice: []int{1, 2, 3, 4},
  58. FloatNum: 22.33,
  59. },
  60. },
  61. {
  62. name: "test go-common/library/time",
  63. args: args{
  64. query: url.Values{
  65. "timeout": {"1s"},
  66. },
  67. v: &cfg2{},
  68. },
  69. want: url.Values{},
  70. cfg: &cfg2{xtime.Duration(time.Second)},
  71. },
  72. {
  73. name: "test empty go-common/library/time",
  74. args: args{
  75. query: url.Values{},
  76. v: &cfg2{},
  77. },
  78. want: url.Values{},
  79. cfg: &cfg2{},
  80. },
  81. {
  82. name: "test go-common/library/time",
  83. args: args{
  84. query: url.Values{},
  85. v: &cfg4{},
  86. },
  87. want: url.Values{},
  88. cfg: &cfg4{xtime.Duration(time.Second)},
  89. },
  90. {
  91. name: "test build-in value",
  92. args: args{
  93. query: url.Values{
  94. "timeout": {"1s"},
  95. },
  96. v: &cfg3{},
  97. assignFuncs: map[string]assignFunc{"username": stringsAssignFunc("hello")},
  98. },
  99. want: url.Values{},
  100. cfg: &cfg3{
  101. Timeout: xtime.Duration(time.Second),
  102. Username: "hello",
  103. },
  104. },
  105. }
  106. for _, tt := range tests {
  107. t.Run(tt.name, func(t *testing.T) {
  108. got, err := bindQuery(tt.args.query, tt.args.v, tt.args.assignFuncs)
  109. if (err != nil) != tt.wantErr {
  110. t.Errorf("DecodeQuery() error = %v, wantErr %v", err, tt.wantErr)
  111. return
  112. }
  113. if !reflect.DeepEqual(got, tt.want) {
  114. t.Errorf("DecodeQuery() = %v, want %v", got, tt.want)
  115. }
  116. if !reflect.DeepEqual(tt.args.v, tt.cfg) {
  117. t.Errorf("DecodeQuery() = %v, want %v", tt.args.v, tt.cfg)
  118. }
  119. })
  120. }
  121. }