z_test.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. package rest
  2. import (
  3. "log"
  4. "reflect"
  5. "strings"
  6. "testing"
  7. "time"
  8. "git.ali33.ru/fcg-xvii/go-tools/json"
  9. )
  10. func TestFieldsDefault(t *testing.T) {
  11. type RR struct {
  12. ID int64 `rest:"default"`
  13. Name string `rest:"default"`
  14. GroupID int64
  15. }
  16. rr := RR{}
  17. fields := fieldsDefault(reflect.TypeOf(rr))
  18. //log.Println(*fields[0])
  19. expected := []any{"id", "name"}
  20. if len(fields) != len(expected) {
  21. t.Errorf("expected %v, real %v", expected, fields)
  22. }
  23. for i, val := range expected {
  24. if fields[i] != val {
  25. t.Errorf("expected %v, real %v", expected, fields)
  26. }
  27. }
  28. }
  29. //////////////////////////////////////////////////
  30. type FPermissions struct {
  31. ID int `rest:"default"`
  32. ParentID int
  33. Name string
  34. }
  35. type FUser struct {
  36. ID int
  37. Name string
  38. Group *FPermissions
  39. Groups []*FPermissions
  40. }
  41. func TestFieldsList(t *testing.T) {
  42. u := &FUser{
  43. ID: 1,
  44. Name: "okko",
  45. /*
  46. Group: &FPermissions{
  47. ID: 100,
  48. ParentID: 50,
  49. Name: "OGroup",
  50. },
  51. */
  52. Groups: []*FPermissions{
  53. {
  54. ID: 100,
  55. ParentID: 5,
  56. Name: "OGroup",
  57. },
  58. {
  59. ID: 200,
  60. ParentID: 0,
  61. Name: "GGroup",
  62. },
  63. },
  64. }
  65. fList := FieldList{
  66. FieldFromName("ID"),
  67. FieldFromName("Name"),
  68. &Field{
  69. Name: "Group",
  70. Names: []*Field{
  71. {
  72. Name: "ID",
  73. },
  74. },
  75. },
  76. &Field{
  77. Name: "Groups",
  78. Names: []*Field{
  79. {
  80. Name: "ParentID",
  81. },
  82. {
  83. Name: "Name",
  84. },
  85. },
  86. },
  87. }
  88. m, err := Fields(u, nil, fList)
  89. if err != nil {
  90. t.Error(err)
  91. }
  92. m.LogPretty()
  93. }
  94. func TestFielderName(t *testing.T) {
  95. name := "Name"
  96. if field, err := parseName(reflect.ValueOf(name)); field.Name != name || err != nil {
  97. if err != nil {
  98. t.Error(err)
  99. }
  100. t.Errorf("expected %v, real %v", name, field.Name)
  101. }
  102. obj := json.Map{
  103. "name": "Object",
  104. "fields": []any{
  105. "okko",
  106. "cooler",
  107. },
  108. }
  109. field, err := parseName(reflect.ValueOf(obj))
  110. if err != nil {
  111. t.Error(err)
  112. }
  113. // check name
  114. if field.Name != "object" {
  115. t.Errorf("expected %v, real %v", "object", name)
  116. }
  117. // check fields
  118. for i, val := range []any{"okko", "cooler"} {
  119. if field.Names[i] != val {
  120. t.Errorf("expected %v, real %v", val, field.Names[i])
  121. }
  122. }
  123. }
  124. type RR struct {
  125. ID int64 `rest:"default"`
  126. Name string `rest:"default"`
  127. GroupID int64 `rest:"ignore"`
  128. Child *RR
  129. Nums []int64
  130. Childs []*RR
  131. MM json.Map
  132. }
  133. func (s *RR) RestFields(result json.Map, fields FieldList) {
  134. if _, check := fields.Field("group_id"); check {
  135. result["group_id"] = s.GroupID * 1000
  136. }
  137. }
  138. func TestFielderVal(t *testing.T) {
  139. rr := &RR{
  140. ID: 1,
  141. Name: "okko",
  142. GroupID: 2,
  143. MM: json.Map{
  144. "one": 1,
  145. "obj": &RR{
  146. ID: 1,
  147. Name: "mm-obj",
  148. },
  149. },
  150. Child: &RR{
  151. ID: 3,
  152. Name: "cooler",
  153. GroupID: 4,
  154. },
  155. Nums: []int64{1, 2, 3},
  156. Childs: []*RR{
  157. {
  158. ID: 5,
  159. Name: "okko1",
  160. GroupID: 6,
  161. },
  162. {
  163. ID: 7,
  164. Name: "cooler1",
  165. GroupID: 8,
  166. },
  167. },
  168. }
  169. fList := FieldListFromObjects(
  170. "id",
  171. "name",
  172. "nums",
  173. "group_id",
  174. "mm",
  175. json.Map{
  176. "name": "childs",
  177. "fields": []any{"id", "name", "childs"},
  178. },
  179. json.Map{
  180. "name": "child",
  181. "fields": []any{"id"},
  182. },
  183. )
  184. val, err := fieldVal(
  185. reflect.ValueOf(rr),
  186. "",
  187. nil,
  188. fList,
  189. )
  190. if err != nil {
  191. t.Error(err, err.Args())
  192. }
  193. m := val.Interface().(json.Map)
  194. m.LogPretty()
  195. rfList := FieldListFromObjects(
  196. "id",
  197. "name",
  198. "nums",
  199. "mm",
  200. "group_id",
  201. json.Map{
  202. "name": "childs",
  203. "fields": []any{"id", "name", "childs"},
  204. },
  205. json.Map{
  206. "name": "child",
  207. "fields": []any{"id"},
  208. },
  209. )
  210. mVal, err := Fields(
  211. rr,
  212. nil,
  213. rfList,
  214. )
  215. if err != nil {
  216. t.Error(err, err.Args())
  217. }
  218. log.Println("========================================")
  219. mVal.LogPretty()
  220. }
  221. type RTime struct {
  222. Time
  223. }
  224. type TimeObj struct {
  225. Created RTime `rest:"default"`
  226. }
  227. func TestTime(t *testing.T) {
  228. obj := TimeObj{
  229. Created: RTime{Time(time.Now())},
  230. }
  231. log.Println(obj)
  232. f, _ := Fields(&obj, nil, nil)
  233. f.LogPretty()
  234. var rObj TimeObj
  235. if err := Serialize(f, &rObj); err != nil {
  236. t.Fatal(err.Map().JSONPrettyString())
  237. }
  238. log.Println(rObj)
  239. jm := json.Map{
  240. "created": 12321311,
  241. }
  242. if err := Serialize(jm, &rObj); err != nil {
  243. t.Fatal(err.Map().JSONPrettyString())
  244. }
  245. t.Log(rObj)
  246. }
  247. type LObj struct {
  248. ID int64 `rest:"default"`
  249. Name string `rest:"default"`
  250. Created time.Time `rest:"default"`
  251. }
  252. type LList struct {
  253. Result []*LObj `rest:"fixed"`
  254. }
  255. func (s *LList) RestFields(fieldName string, names ...any) (any, IErrorArgs) {
  256. if fieldName == "" {
  257. fList := FieldListFromObjects(names...)
  258. l, err := FieldsAny(&s.Result, nil, fList)
  259. if err != nil {
  260. return nil, err
  261. }
  262. rm := json.Map{
  263. "llist": l,
  264. }
  265. return rm, nil
  266. }
  267. return nil, ErrorFiled(fieldName, "field is not exists")
  268. }
  269. func TestFielderList(t *testing.T) {
  270. obj := &LList{
  271. Result: []*LObj{
  272. {
  273. ID: 1,
  274. Name: "Val 1",
  275. Created: time.Now(),
  276. },
  277. {
  278. ID: 2,
  279. Name: "Val 2",
  280. },
  281. },
  282. }
  283. m, err := Fields(obj, nil, FieldListFromObjects("id"))
  284. if err != nil {
  285. t.Fatal(err)
  286. }
  287. m.LogPretty()
  288. }
  289. type RRequest struct {
  290. Count int
  291. Okko string `rest:"required"`
  292. }
  293. type Tickers struct {
  294. *RRequest
  295. Cool *RRequest
  296. TickerIDS []string
  297. }
  298. func TestSerializeRaw(t *testing.T) {
  299. m := json.Map{
  300. "ticker_ids": []string{"1", "2", "3"},
  301. "okko": "10",
  302. }
  303. var ti Tickers
  304. if err := Serialize(m, &ti); err != nil {
  305. t.Fatal(err)
  306. }
  307. log.Println(ti.TickerIDS)
  308. log.Println(ti.RRequest)
  309. log.Println(ti.Cool)
  310. }
  311. func TestServer(t *testing.T) {
  312. }
  313. func TestFieldKeys(t *testing.T) {
  314. l := []any{
  315. "id",
  316. "name",
  317. json.Map{
  318. "name": "parent",
  319. "fields": []any{
  320. "id",
  321. "name",
  322. json.Map{
  323. "name": "child1",
  324. "fields": []any{},
  325. },
  326. json.Map{
  327. "name": "child2",
  328. "fields": []any{
  329. "one",
  330. },
  331. },
  332. },
  333. },
  334. }
  335. jm := ObjectFieldKeys(l)
  336. jm.LogPretty()
  337. }
  338. type FieldNamesTest struct {
  339. ID int
  340. Name string
  341. ParentID int
  342. }
  343. func TestFieldNames(t *testing.T) {
  344. v := FieldNamesTest{
  345. ID: 10,
  346. Name: "Namerrrr",
  347. ParentID: 1,
  348. }
  349. var iv any = &v
  350. fields, err := FieldNames(iv)
  351. if err != nil {
  352. t.Fatal(err)
  353. }
  354. t.Log(fields)
  355. }
  356. type User struct {
  357. ID int
  358. Name string
  359. Created time.Time
  360. ParentID *int
  361. }
  362. type AuthEmail struct {
  363. Category json.Map
  364. Email string `rest:"required" example:"mail@mail.ml"`
  365. ThemeName string `rest:"required"`
  366. UUser *User
  367. Fields []any
  368. }
  369. func (s *AuthEmail) Validate(req IRequestIn) IRequestOut {
  370. s.Email = strings.TrimSpace(s.Email)
  371. return nil
  372. }
  373. // AuthEmail godoc
  374. //
  375. // @Summary Автризация при помощи email
  376. // @Description Если пользователь не существует, он будет создан
  377. // @Description После выполнения команды на указанный email отправляется письмо с кодом,
  378. // @Description который необходимо обменять на токен авторизации
  379. // @Tags client
  380. // @Accept json
  381. // @Produce json
  382. // @Param object body AuthEmail true "Входные параметры"
  383. // @Success 200 {object} result.ResultOK "result"
  384. // @Router /users/auth_email [post]
  385. func (s *AuthEmail) Execute(req IRequestIn) IRequestOut {
  386. return req.OutSuccess(json.Map{}, nil)
  387. }
  388. func TestSerializeEmail(t *testing.T) {
  389. m := json.Map{
  390. "Category": nil,
  391. "email": "flint@77i.su",
  392. "themeName": "th-name",
  393. "UUser": json.Map{
  394. "ID": 100,
  395. //"Name": "User 100",
  396. "Created": "10.11.1983 03:12",
  397. "ParentID": nil,
  398. },
  399. }
  400. r := &AuthEmail{}
  401. err := Serialize(m, r)
  402. if err != nil {
  403. t.Fatal(err)
  404. }
  405. t.Log(r, *r.UUser, r.UUser.ParentID)
  406. //t.Log(*r.UUser.ParentID)
  407. }
  408. func TestFielderMap(t *testing.T) {
  409. m := json.Map{
  410. "one": 1,
  411. "two": 2,
  412. }
  413. rm, err := Fields(m, nil, nil)
  414. if err != nil {
  415. t.Fatal(err)
  416. }
  417. rm.LogPretty()
  418. }
  419. type RFielder struct {
  420. ID int `rest:"default"`
  421. Name string
  422. Count int
  423. }
  424. func TestFielderObject(t *testing.T) {
  425. sl := []*RFielder{
  426. {ID: 1, Name: "one"},
  427. {ID: 2, Name: "two"},
  428. }
  429. rm, err := Fields(sl, nil, nil)
  430. if err != nil {
  431. t.Fatal(err)
  432. }
  433. rm.LogPretty()
  434. }