z_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package rest
  2. import (
  3. "log"
  4. "reflect"
  5. "testing"
  6. "time"
  7. "git.ali33.ru/fcg-xvii/go-tools/json"
  8. )
  9. func TestFieldsDefault(t *testing.T) {
  10. type RR struct {
  11. ID int64 `rest:"default"`
  12. Name string `rest:"default"`
  13. GroupID int64
  14. }
  15. rr := RR{}
  16. fields := fieldsDefault(reflect.TypeOf(rr))
  17. expected := []any{"id", "name"}
  18. if len(fields) != len(expected) {
  19. t.Errorf("expected %v, real %v", expected, fields)
  20. }
  21. for i, val := range expected {
  22. if fields[i] != val {
  23. t.Errorf("expected %v, real %v", expected, fields)
  24. }
  25. }
  26. }
  27. func TestFielderName(t *testing.T) {
  28. name := "Name"
  29. if field, err := parseName(reflect.ValueOf(name)); field.Name != camelToSnake(name) || err != nil {
  30. if err != nil {
  31. t.Error(err)
  32. }
  33. t.Errorf("expected %v, real %v", camelToSnake(name), field.Name)
  34. }
  35. obj := json.Map{
  36. "name": "Object",
  37. "fields": []any{
  38. "okko",
  39. "cooler",
  40. },
  41. }
  42. field, err := parseName(reflect.ValueOf(obj))
  43. if err != nil {
  44. t.Error(err)
  45. }
  46. // check name
  47. if field.Name != "object" {
  48. t.Errorf("expected %v, real %v", "object", name)
  49. }
  50. // check fields
  51. for i, val := range []any{"okko", "cooler"} {
  52. if field.Names[i] != val {
  53. t.Errorf("expected %v, real %v", val, field.Names[i])
  54. }
  55. }
  56. }
  57. type RR struct {
  58. ID int64 `rest:"default"`
  59. Name string `rest:"default"`
  60. GroupID int64 `rest:"ignore"`
  61. Child *RR
  62. Nums []int64
  63. Childs []*RR
  64. MM json.Map
  65. }
  66. func (s *RR) RestFields(result json.Map, fields FieldList) {
  67. if _, check := fields.Field("group_id"); check {
  68. result["group_id"] = s.GroupID * 1000
  69. }
  70. }
  71. func TestFielderVal(t *testing.T) {
  72. rr := &RR{
  73. ID: 1,
  74. Name: "okko",
  75. GroupID: 2,
  76. MM: json.Map{
  77. "one": 1,
  78. "obj": &RR{
  79. ID: 1,
  80. Name: "mm-obj",
  81. },
  82. },
  83. Child: &RR{
  84. ID: 3,
  85. Name: "cooler",
  86. GroupID: 4,
  87. },
  88. Nums: []int64{1, 2, 3},
  89. Childs: []*RR{
  90. {
  91. ID: 5,
  92. Name: "okko1",
  93. GroupID: 6,
  94. },
  95. {
  96. ID: 7,
  97. Name: "cooler1",
  98. GroupID: 8,
  99. },
  100. },
  101. }
  102. val, err := fieldVal(
  103. reflect.ValueOf(rr),
  104. "",
  105. nil,
  106. "id",
  107. "name",
  108. "nums",
  109. "group_id",
  110. "mm",
  111. json.Map{
  112. "name": "childs",
  113. "fields": []any{"id", "name", "childs"},
  114. },
  115. json.Map{
  116. "name": "child",
  117. "fields": []any{"id"},
  118. },
  119. )
  120. if err != nil {
  121. t.Error(err, err.Args())
  122. }
  123. m := val.Interface().(json.Map)
  124. m.LogPretty()
  125. mVal, err := Fields(
  126. rr,
  127. nil,
  128. "id",
  129. "name",
  130. "nums",
  131. "mm",
  132. "group_id",
  133. json.Map{
  134. "name": "childs",
  135. "fields": []any{"id", "name", "childs"},
  136. },
  137. json.Map{
  138. "name": "child",
  139. "fields": []any{"id"},
  140. },
  141. )
  142. if err != nil {
  143. t.Error(err, err.Args())
  144. }
  145. log.Println("========================================")
  146. mVal.LogPretty()
  147. }
  148. type RTime struct {
  149. Time
  150. }
  151. type TimeObj struct {
  152. Created RTime `rest:"default"`
  153. }
  154. func TestTime(t *testing.T) {
  155. obj := TimeObj{
  156. Created: RTime{Time(time.Now())},
  157. }
  158. log.Println(obj)
  159. f, _ := Fields(&obj, nil)
  160. f.LogPretty()
  161. var rObj TimeObj
  162. if err := Serialize(f, &rObj); err != nil {
  163. t.Fatal(err.Map().JSONPrettyString())
  164. }
  165. log.Println(rObj)
  166. jm := json.Map{
  167. "created": 12321311,
  168. }
  169. if err := Serialize(jm, &rObj); err != nil {
  170. t.Fatal(err.Map().JSONPrettyString())
  171. }
  172. t.Log(rObj)
  173. }