github.com/mailru/activerecord@v1.12.2/internal/pkg/parser/field_b_test.go (about) 1 package parser 2 3 import ( 4 "go/ast" 5 "reflect" 6 "testing" 7 8 "github.com/mailru/activerecord/internal/pkg/ds" 9 ) 10 11 func TestParseFields(t *testing.T) { 12 type args struct { 13 fields []*ast.Field 14 } 15 tests := []struct { 16 name string 17 args args 18 wantErr bool 19 want ds.RecordPackage 20 }{ 21 { 22 name: "simple fields", 23 args: args{ 24 fields: []*ast.Field{ 25 { 26 Names: []*ast.Ident{{Name: "ID"}}, 27 Type: &ast.Ident{Name: "int"}, 28 Tag: &ast.BasicLit{Value: "`" + `ar:"primary_key"` + "`"}, 29 }, 30 { 31 Names: []*ast.Ident{{Name: "BarID"}}, 32 Type: &ast.Ident{Name: "int"}, 33 Tag: &ast.BasicLit{Value: "`" + `ar:""` + "`"}, 34 }, 35 }, 36 }, 37 wantErr: false, 38 want: ds.RecordPackage{ 39 Server: ds.ServerDeclaration{}, 40 Namespace: ds.NamespaceDeclaration{}, 41 Fields: []ds.FieldDeclaration{ 42 {Name: "ID", Format: "int", PrimaryKey: true, Mutators: []string{}, Serializer: []string{}}, 43 {Name: "BarID", Format: "int", PrimaryKey: false, Mutators: []string{}, Serializer: []string{}}, 44 }, 45 FieldsMap: map[string]int{"ID": 0, "BarID": 1}, 46 FieldsObjectMap: map[string]ds.FieldObject{}, 47 Indexes: []ds.IndexDeclaration{ 48 { 49 Name: "ID", 50 Num: 0, 51 Selector: "SelectByID", 52 Fields: []int{0}, 53 FieldsMap: map[string]ds.IndexField{ 54 "ID": {IndField: 0, Order: 0}, 55 }, 56 Primary: true, 57 Unique: true, 58 }, 59 }, 60 IndexMap: map[string]int{"ID": 0}, 61 SelectorMap: map[string]int{"SelectByID": 0}, 62 ImportPackage: ds.NewImportPackage(), 63 Backends: []string{}, 64 SerializerMap: map[string]ds.SerializerDeclaration{}, 65 TriggerMap: map[string]ds.TriggerDeclaration{}, 66 FlagMap: map[string]ds.FlagDeclaration{}, 67 }, 68 }, 69 } 70 71 rp := ds.NewRecordPackage() 72 73 for _, tt := range tests { 74 t.Run(tt.name, func(t *testing.T) { 75 if err := ParseFields(rp, tt.args.fields); (err != nil) != tt.wantErr { 76 t.Errorf("ParseFields() error = %v, wantErr %v", err, tt.wantErr) 77 return 78 } 79 80 if !reflect.DeepEqual(rp.Indexes, tt.want.Indexes) { 81 t.Errorf("ParseFields() = %+v, want %+v", rp, tt.want) 82 } 83 }) 84 } 85 }