github.com/mailru/activerecord@v1.12.2/internal/pkg/generator/generator_b_test.go (about)

     1  package generator
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/mailru/activerecord/internal/pkg/ds"
     8  	"github.com/mailru/activerecord/internal/pkg/testutil"
     9  )
    10  
    11  func TestGenerate(t *testing.T) {
    12  	type args struct {
    13  		appInfo      string
    14  		cl           ds.RecordPackage
    15  		linkedObject map[string]ds.RecordPackage
    16  	}
    17  	tests := []struct {
    18  		name    string
    19  		args    args
    20  		wantRet []GenerateFile
    21  		wantErr bool
    22  	}{
    23  		{
    24  			name: "Filename",
    25  			args: args{
    26  				appInfo: testutil.TestAppInfo.String(),
    27  				cl: ds.RecordPackage{
    28  					Server: ds.ServerDeclaration{
    29  						Host:    "127.0.0.1",
    30  						Port:    "11011",
    31  						Timeout: 500,
    32  					},
    33  					Namespace: ds.NamespaceDeclaration{
    34  						ObjectName:  "5",
    35  						PublicName:  "Bar",
    36  						PackageName: "bar",
    37  					},
    38  					Backends: []string{"octopus"},
    39  					Fields: []ds.FieldDeclaration{
    40  						{Name: "Field1", Format: "int", PrimaryKey: true, Mutators: []string{}, Size: 5, Serializer: []string{}},
    41  					},
    42  					FieldsMap:       map[string]int{"Field1": 0},
    43  					FieldsObjectMap: map[string]ds.FieldObject{},
    44  					Indexes: []ds.IndexDeclaration{
    45  						{
    46  							Name:     "Field1",
    47  							Num:      0,
    48  							Selector: "SelectByField1",
    49  							Fields:   []int{0},
    50  							FieldsMap: map[string]ds.IndexField{
    51  								"Field1": {IndField: 0, Order: 0},
    52  							},
    53  							Primary: true,
    54  							Unique:  true,
    55  							Type:    "int",
    56  						},
    57  					},
    58  					IndexMap:      map[string]int{"Field1": 0},
    59  					SelectorMap:   map[string]int{"SelectByField1": 0},
    60  					ImportPackage: ds.NewImportPackage(),
    61  					SerializerMap: map[string]ds.SerializerDeclaration{},
    62  					TriggerMap:    map[string]ds.TriggerDeclaration{},
    63  					FlagMap:       map[string]ds.FlagDeclaration{},
    64  				},
    65  				linkedObject: map[string]ds.RecordPackage{},
    66  			},
    67  			wantRet: []GenerateFile{
    68  				{
    69  					Dir:     "bar",
    70  					Name:    "octopus.go",
    71  					Backend: "octopus",
    72  					Data:    []byte{},
    73  				},
    74  				{
    75  					Dir:     "bar",
    76  					Name:    "mock.go",
    77  					Backend: "octopus",
    78  					Data:    []byte{},
    79  				},
    80  				{
    81  					Dir:     "bar",
    82  					Name:    "fixture.go",
    83  					Backend: "octopus",
    84  					Data:    []byte{},
    85  				},
    86  			},
    87  			wantErr: false,
    88  		},
    89  	}
    90  	for _, tt := range tests {
    91  		t.Run(tt.name, func(t *testing.T) {
    92  			gotRet, err := Generate(tt.args.appInfo, tt.args.cl, tt.args.linkedObject)
    93  			if (err != nil) != tt.wantErr {
    94  				t.Errorf("Generate() error = %v, wantErr %v", err, tt.wantErr)
    95  				return
    96  			}
    97  
    98  			// Testing in backend specific tests
    99  			for iGotRet := range gotRet {
   100  				gotRet[iGotRet].Data = []byte{}
   101  			}
   102  
   103  			got := filesByName(gotRet)
   104  
   105  			for name, file := range filesByName(tt.wantRet) {
   106  				if !reflect.DeepEqual(got[name], file) {
   107  					t.Errorf("Generate() = %v, want %v", gotRet, tt.wantRet)
   108  				}
   109  			}
   110  		})
   111  	}
   112  }
   113  
   114  func filesByName(files []GenerateFile) map[string]GenerateFile {
   115  	ret := make(map[string]GenerateFile, len(files))
   116  	for _, file := range files {
   117  		ret[file.Name] = file
   118  	}
   119  	return ret
   120  }