z_test.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. expected := []any{"id", "name"}
  19. if len(fields) != len(expected) {
  20. t.Errorf("expected %v, real %v", expected, fields)
  21. }
  22. for i, val := range expected {
  23. if fields[i] != val {
  24. t.Errorf("expected %v, real %v", expected, fields)
  25. }
  26. }
  27. }
  28. func TestFielderName(t *testing.T) {
  29. name := "Name"
  30. if field, err := parseName(reflect.ValueOf(name)); field.Name != name || err != nil {
  31. if err != nil {
  32. t.Error(err)
  33. }
  34. t.Errorf("expected %v, real %v", name, field.Name)
  35. }
  36. obj := json.Map{
  37. "name": "Object",
  38. "fields": []any{
  39. "okko",
  40. "cooler",
  41. },
  42. }
  43. field, err := parseName(reflect.ValueOf(obj))
  44. if err != nil {
  45. t.Error(err)
  46. }
  47. // check name
  48. if field.Name != "object" {
  49. t.Errorf("expected %v, real %v", "object", name)
  50. }
  51. // check fields
  52. for i, val := range []any{"okko", "cooler"} {
  53. if field.Names[i] != val {
  54. t.Errorf("expected %v, real %v", val, field.Names[i])
  55. }
  56. }
  57. }
  58. type RR struct {
  59. ID int64 `rest:"default"`
  60. Name string `rest:"default"`
  61. GroupID int64 `rest:"ignore"`
  62. Child *RR
  63. Nums []int64
  64. Childs []*RR
  65. MM json.Map
  66. }
  67. func (s *RR) RestFields(result json.Map, fields FieldList) {
  68. if _, check := fields.Field("group_id"); check {
  69. result["group_id"] = s.GroupID * 1000
  70. }
  71. }
  72. func TestFielderVal(t *testing.T) {
  73. rr := &RR{
  74. ID: 1,
  75. Name: "okko",
  76. GroupID: 2,
  77. MM: json.Map{
  78. "one": 1,
  79. "obj": &RR{
  80. ID: 1,
  81. Name: "mm-obj",
  82. },
  83. },
  84. Child: &RR{
  85. ID: 3,
  86. Name: "cooler",
  87. GroupID: 4,
  88. },
  89. Nums: []int64{1, 2, 3},
  90. Childs: []*RR{
  91. {
  92. ID: 5,
  93. Name: "okko1",
  94. GroupID: 6,
  95. },
  96. {
  97. ID: 7,
  98. Name: "cooler1",
  99. GroupID: 8,
  100. },
  101. },
  102. }
  103. val, err := fieldVal(
  104. reflect.ValueOf(rr),
  105. "",
  106. nil,
  107. "id",
  108. "name",
  109. "nums",
  110. "group_id",
  111. "mm",
  112. json.Map{
  113. "name": "childs",
  114. "fields": []any{"id", "name", "childs"},
  115. },
  116. json.Map{
  117. "name": "child",
  118. "fields": []any{"id"},
  119. },
  120. )
  121. if err != nil {
  122. t.Error(err, err.Args())
  123. }
  124. m := val.Interface().(json.Map)
  125. m.LogPretty()
  126. mVal, err := Fields(
  127. rr,
  128. nil,
  129. "id",
  130. "name",
  131. "nums",
  132. "mm",
  133. "group_id",
  134. json.Map{
  135. "name": "childs",
  136. "fields": []any{"id", "name", "childs"},
  137. },
  138. json.Map{
  139. "name": "child",
  140. "fields": []any{"id"},
  141. },
  142. )
  143. if err != nil {
  144. t.Error(err, err.Args())
  145. }
  146. log.Println("========================================")
  147. mVal.LogPretty()
  148. }
  149. type RTime struct {
  150. Time
  151. }
  152. type TimeObj struct {
  153. Created RTime `rest:"default"`
  154. }
  155. func TestTime(t *testing.T) {
  156. obj := TimeObj{
  157. Created: RTime{Time(time.Now())},
  158. }
  159. log.Println(obj)
  160. f, _ := Fields(&obj, nil)
  161. f.LogPretty()
  162. var rObj TimeObj
  163. if err := Serialize(f, &rObj); err != nil {
  164. t.Fatal(err.Map().JSONPrettyString())
  165. }
  166. log.Println(rObj)
  167. jm := json.Map{
  168. "created": 12321311,
  169. }
  170. if err := Serialize(jm, &rObj); err != nil {
  171. t.Fatal(err.Map().JSONPrettyString())
  172. }
  173. t.Log(rObj)
  174. }
  175. type LObj struct {
  176. ID int64 `rest:"default"`
  177. Name string `rest:"default"`
  178. }
  179. type LList struct {
  180. Result []*LObj `rest:"fixed"`
  181. }
  182. func (s *LList) RestFields(fieldName string, names ...any) (any, IErrorArgs) {
  183. if fieldName == "" {
  184. l, err := FieldsAny(&s.Result, nil, names...)
  185. if err != nil {
  186. return nil, err
  187. }
  188. rm := json.Map{
  189. "llist": l,
  190. }
  191. return rm, nil
  192. }
  193. return nil, ErrorFiled(fieldName, "field is not exists")
  194. }
  195. func TestFielderList(t *testing.T) {
  196. obj := &LList{
  197. Result: []*LObj{
  198. {
  199. ID: 1,
  200. Name: "Val 1",
  201. },
  202. {
  203. ID: 2,
  204. Name: "Val 2",
  205. },
  206. },
  207. }
  208. m, err := Fields(obj, nil, "id")
  209. if err != nil {
  210. t.Fatal(err)
  211. }
  212. m.LogPretty()
  213. }
  214. type RRequest struct {
  215. Count int
  216. Okko string `rest:"required"`
  217. }
  218. type Tickers struct {
  219. *RRequest
  220. Cool *RRequest
  221. TickerIDS []string
  222. }
  223. func TestSerializeRaw(t *testing.T) {
  224. m := json.Map{
  225. "ticker_ids": []string{"1", "2", "3"},
  226. "okko": "10",
  227. }
  228. var ti Tickers
  229. if err := Serialize(m, &ti); err != nil {
  230. t.Fatal(err)
  231. }
  232. log.Println(ti.TickerIDS)
  233. log.Println(ti.RRequest)
  234. log.Println(ti.Cool)
  235. }
  236. func TestServer(t *testing.T) {
  237. }
  238. func TestFieldKeys(t *testing.T) {
  239. l := []any{
  240. "id",
  241. "name",
  242. json.Map{
  243. "name": "parent",
  244. "fields": []any{
  245. "id",
  246. "name",
  247. json.Map{
  248. "name": "child1",
  249. "fields": []any{},
  250. },
  251. json.Map{
  252. "name": "child2",
  253. "fields": []any{
  254. "one",
  255. },
  256. },
  257. },
  258. },
  259. }
  260. jm := ObjectFieldKeys(l)
  261. jm.LogPretty()
  262. }
  263. type FieldNamesTest struct {
  264. ID int
  265. Name string
  266. ParentID int
  267. }
  268. func TestFieldNames(t *testing.T) {
  269. v := FieldNamesTest{
  270. ID: 10,
  271. Name: "Namerrrr",
  272. ParentID: 1,
  273. }
  274. var iv any = &v
  275. fields, err := FieldNames(iv)
  276. if err != nil {
  277. t.Fatal(err)
  278. }
  279. t.Log(fields)
  280. }
  281. type User struct {
  282. ID int
  283. Name string
  284. }
  285. type AuthEmail struct {
  286. Email string `rest:"required" example:"mail@mail.ml"`
  287. ThemeName string `rest:"required"`
  288. UUser *User
  289. }
  290. func (s *AuthEmail) Validate(req IRequestIn) IRequestOut {
  291. s.Email = strings.TrimSpace(s.Email)
  292. return nil
  293. }
  294. // AuthEmail godoc
  295. //
  296. // @Summary Автризация при помощи email
  297. // @Description Если пользователь не существует, он будет создан
  298. // @Description После выполнения команды на указанный email отправляется письмо с кодом,
  299. // @Description который необходимо обменять на токен авторизации
  300. // @Tags client
  301. // @Accept json
  302. // @Produce json
  303. // @Param object body AuthEmail true "Входные параметры"
  304. // @Success 200 {object} result.ResultOK "result"
  305. // @Router /users/auth_email [post]
  306. func (s *AuthEmail) Execute(req IRequestIn) IRequestOut {
  307. return req.OutSuccess(json.Map{}, nil)
  308. }
  309. func TestSerializeEmail(t *testing.T) {
  310. m := json.Map{
  311. "email": "flint@77i.su",
  312. "themeName": "th-name",
  313. "UUser": json.Map{
  314. "ID": 100,
  315. "Name": "User 100",
  316. },
  317. }
  318. r := &AuthEmail{}
  319. err := Serialize(m, r)
  320. if err != nil {
  321. t.Fatal(err)
  322. }
  323. t.Log(r)
  324. }