123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450 |
- package rest
- import (
- "log"
- "reflect"
- "strings"
- "testing"
- "time"
- "git.ali33.ru/fcg-xvii/go-tools/json"
- )
- func TestFieldsDefault(t *testing.T) {
- type RR struct {
- ID int64 `rest:"default"`
- Name string `rest:"default"`
- GroupID int64
- }
- rr := RR{}
- fields := fieldsDefault(reflect.TypeOf(rr))
- //log.Println(*fields[0])
- expected := []any{"id", "name"}
- if len(fields) != len(expected) {
- t.Errorf("expected %v, real %v", expected, fields)
- }
- for i, val := range expected {
- if fields[i] != val {
- t.Errorf("expected %v, real %v", expected, fields)
- }
- }
- }
- //////////////////////////////////////////////////
- type FPermissions struct {
- ID int `rest:"default"`
- ParentID int
- Name string
- }
- type FUser struct {
- ID int
- Name string
- Group *FPermissions
- Groups []*FPermissions
- }
- func TestFieldsList(t *testing.T) {
- u := &FUser{
- ID: 1,
- Name: "okko",
- /*
- Group: &FPermissions{
- ID: 100,
- ParentID: 50,
- Name: "OGroup",
- },
- */
- Groups: []*FPermissions{
- {
- ID: 100,
- ParentID: 5,
- Name: "OGroup",
- },
- {
- ID: 200,
- ParentID: 0,
- Name: "GGroup",
- },
- },
- }
- fList := FieldList{
- FieldFromName("ID"),
- FieldFromName("Name"),
- &Field{
- Name: "Group",
- Names: []*Field{
- {
- Name: "ID",
- },
- },
- },
- &Field{
- Name: "Groups",
- Names: []*Field{
- {
- Name: "ParentID",
- },
- {
- Name: "Name",
- },
- },
- },
- }
- m, err := Fields(u, nil, fList)
- if err != nil {
- t.Error(err)
- }
- m.LogPretty()
- }
- func TestFielderName(t *testing.T) {
- name := "Name"
- if field, err := parseName(reflect.ValueOf(name)); field.Name != name || err != nil {
- if err != nil {
- t.Error(err)
- }
- t.Errorf("expected %v, real %v", name, field.Name)
- }
- obj := json.Map{
- "name": "Object",
- "fields": []any{
- "okko",
- "cooler",
- },
- }
- field, err := parseName(reflect.ValueOf(obj))
- if err != nil {
- t.Error(err)
- }
- // check name
- if field.Name != "object" {
- t.Errorf("expected %v, real %v", "object", name)
- }
- // check fields
- for i, val := range []any{"okko", "cooler"} {
- if field.Names[i] != val {
- t.Errorf("expected %v, real %v", val, field.Names[i])
- }
- }
- }
- type RR struct {
- ID int64 `rest:"default"`
- Name string `rest:"default"`
- GroupID int64 `rest:"ignore"`
- Child *RR
- Nums []int64
- Childs []*RR
- MM json.Map
- }
- func (s *RR) RestFields(result json.Map, fields FieldList) {
- if _, check := fields.Field("group_id"); check {
- result["group_id"] = s.GroupID * 1000
- }
- }
- func TestFielderVal(t *testing.T) {
- rr := &RR{
- ID: 1,
- Name: "okko",
- GroupID: 2,
- MM: json.Map{
- "one": 1,
- "obj": &RR{
- ID: 1,
- Name: "mm-obj",
- },
- },
- Child: &RR{
- ID: 3,
- Name: "cooler",
- GroupID: 4,
- },
- Nums: []int64{1, 2, 3},
- Childs: []*RR{
- {
- ID: 5,
- Name: "okko1",
- GroupID: 6,
- },
- {
- ID: 7,
- Name: "cooler1",
- GroupID: 8,
- },
- },
- }
- fList := FieldListFromObjects(
- "id",
- "name",
- "nums",
- "group_id",
- "mm",
- json.Map{
- "name": "childs",
- "fields": []any{"id", "name", "childs"},
- },
- json.Map{
- "name": "child",
- "fields": []any{"id"},
- },
- )
- val, err := fieldVal(
- reflect.ValueOf(rr),
- "",
- nil,
- fList,
- )
- if err != nil {
- t.Error(err, err.Args())
- }
- m := val.Interface().(json.Map)
- m.LogPretty()
- rfList := FieldListFromObjects(
- "id",
- "name",
- "nums",
- "mm",
- "group_id",
- json.Map{
- "name": "childs",
- "fields": []any{"id", "name", "childs"},
- },
- json.Map{
- "name": "child",
- "fields": []any{"id"},
- },
- )
- mVal, err := Fields(
- rr,
- nil,
- rfList,
- )
- if err != nil {
- t.Error(err, err.Args())
- }
- log.Println("========================================")
- mVal.LogPretty()
- }
- type RTime struct {
- Time
- }
- type TimeObj struct {
- Created RTime `rest:"default"`
- }
- func TestTime(t *testing.T) {
- obj := TimeObj{
- Created: RTime{Time(time.Now())},
- }
- log.Println(obj)
- f, _ := Fields(&obj, nil, nil)
- f.LogPretty()
- var rObj TimeObj
- if err := Serialize(f, &rObj); err != nil {
- t.Fatal(err.Map().JSONPrettyString())
- }
- log.Println(rObj)
- jm := json.Map{
- "created": 12321311,
- }
- if err := Serialize(jm, &rObj); err != nil {
- t.Fatal(err.Map().JSONPrettyString())
- }
- t.Log(rObj)
- }
- type LObj struct {
- ID int64 `rest:"default"`
- Name string `rest:"default"`
- Created time.Time `rest:"default"`
- }
- type LList struct {
- Result []*LObj `rest:"fixed"`
- }
- func (s *LList) RestFields(fieldName string, names ...any) (any, IErrorArgs) {
- if fieldName == "" {
- fList := FieldListFromObjects(names...)
- l, err := FieldsAny(&s.Result, nil, fList)
- if err != nil {
- return nil, err
- }
- rm := json.Map{
- "llist": l,
- }
- return rm, nil
- }
- return nil, ErrorFiled(fieldName, "field is not exists")
- }
- func TestFielderList(t *testing.T) {
- obj := &LList{
- Result: []*LObj{
- {
- ID: 1,
- Name: "Val 1",
- Created: time.Now(),
- },
- {
- ID: 2,
- Name: "Val 2",
- },
- },
- }
- m, err := Fields(obj, nil, FieldListFromObjects("id"))
- if err != nil {
- t.Fatal(err)
- }
- m.LogPretty()
- }
- type RRequest struct {
- Count int
- Okko string `rest:"required"`
- }
- type Tickers struct {
- *RRequest
- Cool *RRequest
- TickerIDS []string
- }
- func TestSerializeRaw(t *testing.T) {
- m := json.Map{
- "ticker_ids": []string{"1", "2", "3"},
- "okko": "10",
- }
- var ti Tickers
- if err := Serialize(m, &ti); err != nil {
- t.Fatal(err)
- }
- log.Println(ti.TickerIDS)
- log.Println(ti.RRequest)
- log.Println(ti.Cool)
- }
- func TestServer(t *testing.T) {
- }
- func TestFieldKeys(t *testing.T) {
- l := []any{
- "id",
- "name",
- json.Map{
- "name": "parent",
- "fields": []any{
- "id",
- "name",
- json.Map{
- "name": "child1",
- "fields": []any{},
- },
- json.Map{
- "name": "child2",
- "fields": []any{
- "one",
- },
- },
- },
- },
- }
- jm := ObjectFieldKeys(l)
- jm.LogPretty()
- }
- type FieldNamesTest struct {
- ID int
- Name string
- ParentID int
- }
- func TestFieldNames(t *testing.T) {
- v := FieldNamesTest{
- ID: 10,
- Name: "Namerrrr",
- ParentID: 1,
- }
- var iv any = &v
- fields, err := FieldNames(iv)
- if err != nil {
- t.Fatal(err)
- }
- t.Log(fields)
- }
- type User struct {
- ID int
- Name string
- Created time.Time
- }
- type AuthEmail struct {
- Email string `rest:"required" example:"mail@mail.ml"`
- ThemeName string `rest:"required"`
- UUser *User
- }
- func (s *AuthEmail) Validate(req IRequestIn) IRequestOut {
- s.Email = strings.TrimSpace(s.Email)
- return nil
- }
- // AuthEmail godoc
- //
- // @Summary Автризация при помощи email
- // @Description Если пользователь не существует, он будет создан
- // @Description После выполнения команды на указанный email отправляется письмо с кодом,
- // @Description который необходимо обменять на токен авторизации
- // @Tags client
- // @Accept json
- // @Produce json
- // @Param object body AuthEmail true "Входные параметры"
- // @Success 200 {object} result.ResultOK "result"
- // @Router /users/auth_email [post]
- func (s *AuthEmail) Execute(req IRequestIn) IRequestOut {
- return req.OutSuccess(json.Map{}, nil)
- }
- func TestSerializeEmail(t *testing.T) {
- m := json.Map{
- "email": "flint@77i.su",
- "themeName": "th-name",
- "UUser": json.Map{
- "ID": 100,
- "Name": "User 100",
- "Created": "10.11.1983 03:12K",
- },
- }
- r := &AuthEmail{}
- err := Serialize(m, r)
- if err != nil {
- t.Fatal(err)
- }
- t.Log(r, *r.UUser)
- }
|