github.com/mailru/activerecord@v1.12.2/internal/pkg/parser/import_w_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 TestParseImport(t *testing.T) {
    12  	rp := ds.NewRecordPackage()
    13  	type args struct {
    14  		dst        *ds.RecordPackage
    15  		importSpec *ast.ImportSpec
    16  	}
    17  	tests := []struct {
    18  		name    string
    19  		args    args
    20  		wantErr bool
    21  		want    *ds.RecordPackage
    22  	}{
    23  		{
    24  			name: "simple import",
    25  			args: args{
    26  				dst: rp,
    27  				importSpec: &ast.ImportSpec{
    28  					Path: &ast.BasicLit{
    29  						Value: `"github.com/mailru/activerecord-cookbook.git/example/model/dictionary"`,
    30  					},
    31  				},
    32  			},
    33  			want: &ds.RecordPackage{
    34  				Server: ds.ServerDeclaration{
    35  					Host:    "",
    36  					Port:    "",
    37  					Timeout: 0,
    38  				},
    39  				Namespace: ds.NamespaceDeclaration{
    40  					ObjectName:  "",
    41  					PublicName:  "",
    42  					PackageName: "",
    43  				},
    44  				Backends:        []string{},
    45  				ProcFieldsMap:   map[string]int{},
    46  				ProcOutFields:   map[int]ds.ProcFieldDeclaration{},
    47  				Fields:          []ds.FieldDeclaration{},
    48  				FieldsMap:       map[string]int{},
    49  				FieldsObjectMap: map[string]ds.FieldObject{},
    50  				Indexes:         []ds.IndexDeclaration{},
    51  				IndexMap:        map[string]int{},
    52  				SelectorMap:     map[string]int{},
    53  				ImportPackage: ds.ImportPackage{
    54  					Imports: []ds.ImportDeclaration{
    55  						{
    56  							Path: "github.com/mailru/activerecord-cookbook.git/example/model/dictionary",
    57  						},
    58  					},
    59  					ImportMap:    map[string]int{"github.com/mailru/activerecord-cookbook.git/example/model/dictionary": 0},
    60  					ImportPkgMap: map[string]int{"dictionary": 0},
    61  				},
    62  				SerializerMap:         map[string]ds.SerializerDeclaration{},
    63  				TriggerMap:            map[string]ds.TriggerDeclaration{},
    64  				FlagMap:               map[string]ds.FlagDeclaration{},
    65  				MutatorMap:            map[string]ds.MutatorDeclaration{},
    66  				ImportStructFieldsMap: map[string][]ds.PartialFieldDeclaration{},
    67  				LinkedStructsMap:      map[string]ds.LinkedPackageDeclaration{},
    68  			},
    69  			wantErr: false,
    70  		},
    71  	}
    72  	for _, tt := range tests {
    73  		t.Run(tt.name, func(t *testing.T) {
    74  			if err := ParseImport(&tt.args.dst.ImportPackage, tt.args.importSpec); (err != nil) != tt.wantErr {
    75  				t.Errorf("ParseImport() error = %v, wantErr %v", err, tt.wantErr)
    76  				return
    77  			}
    78  
    79  			if !reflect.DeepEqual(tt.args.dst, tt.want) {
    80  				t.Errorf("ParseImport() = %+v, wantErr %+v", tt.args.dst, tt.want)
    81  			}
    82  		})
    83  	}
    84  }