z_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. }
  174. type LObj struct {
  175. ID int64 `rest:"default"`
  176. Name string `rest:"default"`
  177. }
  178. type LList struct {
  179. Result []*LObj `rest:"fixed"`
  180. }
  181. func (s *LList) RestFields(fieldName string, names ...any) (any, IErrorArgs) {
  182. if fieldName == "" {
  183. l, err := FieldsAny(&s.Result, nil, names...)
  184. if err != nil {
  185. return nil, err
  186. }
  187. rm := json.Map{
  188. "llist": l,
  189. }
  190. return rm, nil
  191. }
  192. return nil, ErrorFiled(fieldName, "field is not exists")
  193. }
  194. func TestFielderList(t *testing.T) {
  195. obj := &LList{
  196. Result: []*LObj{
  197. {
  198. ID: 1,
  199. Name: "Val 1",
  200. },
  201. {
  202. ID: 2,
  203. Name: "Val 2",
  204. },
  205. },
  206. }
  207. m, err := Fields(obj, nil, "id")
  208. if err != nil {
  209. t.Fatal(err)
  210. }
  211. m.LogPretty()
  212. }
  213. type Tickers struct {
  214. TickerIDS []string
  215. }
  216. func TestSerialize(t *testing.T) {
  217. m := json.Map{
  218. "ticker_ids": []string{"1", "2", "3"},
  219. }
  220. var ti Tickers
  221. if err := Serialize(m, &ti); err != nil {
  222. t.Fatal(err)
  223. }
  224. log.Println(ti.TickerIDS)
  225. }
  226. func TestServer(t *testing.T) {
  227. }
  228. func TestOrder(t *testing.T) {
  229. order := json.Map{
  230. "one": true,
  231. "two": false,
  232. "three": json.Map{
  233. "c_one": true,
  234. "c_two": false,
  235. },
  236. }
  237. obj, err := parseOrder(order, "")
  238. if err != nil {
  239. t.Fatal(err)
  240. }
  241. t.Log("obj ============================")
  242. t.Log(obj)
  243. t.Log("fields ============================")
  244. t.Log(obj.MapFields())
  245. t.Log("objects ============================")
  246. t.Log(obj.MapObjects())
  247. t.Log("order ============================")
  248. t.Log(obj.MapOrder())
  249. }
  250. func TestConditions(t *testing.T) {
  251. src := json.Map{
  252. "field": "id",
  253. "operator": "=",
  254. "logic": "and",
  255. "value": 1,
  256. }
  257. cond, err := parseCondition(src, 0)
  258. if err != nil {
  259. t.Fatal(err)
  260. }
  261. t.Log(cond)
  262. }
  263. func TestFieldKeys(t *testing.T) {
  264. l := []any{
  265. "id",
  266. "name",
  267. json.Map{
  268. "name": "parent",
  269. "fields": []any{
  270. "id",
  271. "name",
  272. json.Map{
  273. "name": "child1",
  274. "fields": []any{},
  275. },
  276. json.Map{
  277. "name": "child2",
  278. "fields": []any{
  279. "one",
  280. },
  281. },
  282. },
  283. },
  284. }
  285. jm := ObjectFieldKeys(l)
  286. jm.LogPretty()
  287. }