github.com/mailru/activerecord@v1.12.2/internal/pkg/checker/checker_w_test.go (about)

     1  package checker
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/mailru/activerecord/internal/pkg/ds"
     7  )
     8  
     9  func Test_checkBackend(t *testing.T) {
    10  	rcOctopus := ds.NewRecordPackage()
    11  	rcOctopus.Backends = []string{"octopus"}
    12  	rcMany := ds.NewRecordPackage()
    13  	rcMany.Backends = []string{"octopus", "postgres"}
    14  
    15  	type args struct {
    16  		cl *ds.RecordPackage
    17  	}
    18  	tests := []struct {
    19  		name    string
    20  		args    args
    21  		wantErr bool
    22  	}{
    23  		{name: "emptyBack", args: args{cl: ds.NewRecordPackage()}, wantErr: true},
    24  		{name: "oneBack", args: args{cl: rcOctopus}, wantErr: false},
    25  		{name: "manyBack", args: args{cl: rcMany}, wantErr: true},
    26  	}
    27  	for _, tt := range tests {
    28  		t.Run(tt.name, func(t *testing.T) {
    29  			if err := checkBackend(tt.args.cl); (err != nil) != tt.wantErr {
    30  				t.Errorf("CheckBackend() error = %v, wantErr %v", err, tt.wantErr)
    31  			}
    32  		})
    33  	}
    34  }
    35  
    36  func Test_checkLinkedObject(t *testing.T) {
    37  	rp := ds.NewRecordPackage()
    38  	rpLinked := ds.NewRecordPackage()
    39  
    40  	err := rpLinked.AddFieldObject(ds.FieldObject{
    41  		Name:       "Foo",
    42  		Key:        "ID",
    43  		ObjectName: "bar",
    44  		Field:      "barID",
    45  		Unique:     false,
    46  	})
    47  	if err != nil {
    48  		t.Errorf("can't prepare test data: %s", err)
    49  		return
    50  	}
    51  
    52  	type args struct {
    53  		cl            *ds.RecordPackage
    54  		linkedObjects map[string]string
    55  	}
    56  
    57  	tests := []struct {
    58  		name    string
    59  		args    args
    60  		wantErr bool
    61  	}{
    62  		{name: "without linked obj", args: args{cl: rp, linkedObjects: map[string]string{}}, wantErr: false},
    63  		{name: "no linked obj", args: args{cl: rpLinked, linkedObjects: map[string]string{}}, wantErr: true},
    64  		{name: "normal linked obj", args: args{cl: rpLinked, linkedObjects: map[string]string{"bar": "bar"}}, wantErr: false},
    65  	}
    66  	for _, tt := range tests {
    67  		t.Run(tt.name, func(t *testing.T) {
    68  			if err := checkLinkedObject(tt.args.cl, tt.args.linkedObjects); (err != nil) != tt.wantErr {
    69  				t.Errorf("checkLinkedObject() error = %v, wantErr %v", err, tt.wantErr)
    70  			}
    71  		})
    72  	}
    73  }
    74  
    75  func Test_checkNamespace(t *testing.T) {
    76  	type args struct {
    77  		ns ds.NamespaceDeclaration
    78  	}
    79  	tests := []struct {
    80  		name    string
    81  		args    args
    82  		wantErr bool
    83  	}{
    84  		{
    85  			name: "normal namespace",
    86  			args: args{
    87  				ns: ds.NamespaceDeclaration{
    88  					ObjectName:  "0",
    89  					PublicName:  "Foo",
    90  					PackageName: "foo",
    91  				},
    92  			},
    93  			wantErr: false,
    94  		},
    95  		{
    96  			name: "empty name",
    97  			args: args{
    98  				ns: ds.NamespaceDeclaration{
    99  					ObjectName:  "0",
   100  					PublicName:  "",
   101  					PackageName: "foo",
   102  				},
   103  			},
   104  			wantErr: true,
   105  		},
   106  		{
   107  			name: "empty package",
   108  			args: args{
   109  				ns: ds.NamespaceDeclaration{
   110  					ObjectName:  "0",
   111  					PublicName:  "Foo",
   112  					PackageName: "",
   113  				},
   114  			},
   115  			wantErr: true,
   116  		},
   117  	}
   118  	for _, tt := range tests {
   119  		t.Run(tt.name, func(t *testing.T) {
   120  			if err := checkNamespace(tt.args.ns); (err != nil) != tt.wantErr {
   121  				t.Errorf("checkNamespace() error = %v, wantErr %v", err, tt.wantErr)
   122  			}
   123  		})
   124  	}
   125  }
   126  
   127  func Test_checkFields(t *testing.T) {
   128  	type args struct {
   129  		cl ds.RecordPackage
   130  	}
   131  	tests := []struct {
   132  		name    string
   133  		args    args
   134  		wantErr bool
   135  	}{
   136  		{
   137  			name: "empty fields",
   138  			args: args{
   139  				cl: ds.RecordPackage{
   140  					Fields: []ds.FieldDeclaration{},
   141  				},
   142  			},
   143  			wantErr: true,
   144  		},
   145  		{
   146  			name: "empty format",
   147  			args: args{
   148  				cl: ds.RecordPackage{
   149  					Fields: []ds.FieldDeclaration{
   150  						{
   151  							Name: "Foo",
   152  						},
   153  					},
   154  				},
   155  			},
   156  			wantErr: true,
   157  		},
   158  		{
   159  			name: "invalid format",
   160  			args: args{
   161  				cl: ds.RecordPackage{
   162  					Fields: []ds.FieldDeclaration{
   163  						{
   164  							Name:   "Foo",
   165  							Format: "[]int",
   166  						},
   167  					},
   168  				},
   169  			},
   170  			wantErr: true,
   171  		},
   172  		{
   173  			name: "no primary",
   174  			args: args{
   175  				cl: ds.RecordPackage{
   176  					Fields: []ds.FieldDeclaration{
   177  						{
   178  							Name:   "Foo",
   179  							Format: "int",
   180  						},
   181  					},
   182  				},
   183  			},
   184  			wantErr: true,
   185  		},
   186  		{
   187  			name: "normal field",
   188  			args: args{
   189  				cl: ds.RecordPackage{
   190  					Fields: []ds.FieldDeclaration{
   191  						{
   192  							Name:       "Foo",
   193  							Format:     "int",
   194  							PrimaryKey: true,
   195  						},
   196  					},
   197  				},
   198  			},
   199  			wantErr: false,
   200  		},
   201  		{
   202  			name: "fields conflict with links",
   203  			args: args{
   204  				cl: ds.RecordPackage{
   205  					Fields: []ds.FieldDeclaration{
   206  						{
   207  							Name:       "Foo",
   208  							Format:     "int",
   209  							PrimaryKey: true,
   210  						},
   211  					},
   212  					FieldsObjectMap: map[string]ds.FieldObject{
   213  						"Foo": {},
   214  					},
   215  				},
   216  			},
   217  			wantErr: true,
   218  		},
   219  		{
   220  			name: "mutators and primary",
   221  			args: args{
   222  				cl: ds.RecordPackage{
   223  					Fields: []ds.FieldDeclaration{
   224  						{
   225  							Name:       "Foo",
   226  							Format:     "int",
   227  							PrimaryKey: true,
   228  							Mutators: []string{
   229  								"fmut",
   230  							},
   231  						},
   232  					},
   233  				},
   234  			},
   235  			wantErr: true,
   236  		},
   237  		{
   238  			name: "serializer not declared",
   239  			args: args{
   240  				cl: ds.RecordPackage{
   241  					Fields: []ds.FieldDeclaration{
   242  						{
   243  							Name:       "Foo",
   244  							Format:     "int",
   245  							PrimaryKey: true,
   246  						},
   247  						{
   248  							Name:   "Foo",
   249  							Format: "int",
   250  							Mutators: []string{
   251  								"fmut",
   252  							},
   253  							Serializer: []string{
   254  								"fser",
   255  							},
   256  						},
   257  					},
   258  					SerializerMap: map[string]ds.SerializerDeclaration{},
   259  				},
   260  			},
   261  			wantErr: true,
   262  		},
   263  		{
   264  			name: "mutators and links",
   265  			args: args{
   266  				cl: ds.RecordPackage{
   267  					Fields: []ds.FieldDeclaration{
   268  						{
   269  							Name:       "Foo",
   270  							Format:     "int",
   271  							PrimaryKey: true,
   272  						},
   273  						{
   274  							Name:   "Foo",
   275  							Format: "int",
   276  							Mutators: []string{
   277  								"fmut",
   278  							},
   279  							ObjectLink: "Bar",
   280  						},
   281  					},
   282  				},
   283  			},
   284  			wantErr: true,
   285  		},
   286  		{
   287  			name: "serializer and links",
   288  			args: args{
   289  				cl: ds.RecordPackage{
   290  					Fields: []ds.FieldDeclaration{
   291  						{
   292  							Name:       "Foo",
   293  							Format:     "int",
   294  							PrimaryKey: true,
   295  						},
   296  						{
   297  							Name:   "Foo",
   298  							Format: "int",
   299  							Serializer: []string{
   300  								"fser",
   301  							},
   302  							ObjectLink: "Bar",
   303  						},
   304  					},
   305  					SerializerMap: map[string]ds.SerializerDeclaration{
   306  						"fser": {},
   307  					},
   308  				},
   309  			},
   310  			wantErr: true,
   311  		},
   312  		{
   313  			name: "custom mutator without serializer",
   314  			args: args{
   315  				cl: ds.RecordPackage{
   316  					Fields: []ds.FieldDeclaration{
   317  						{
   318  							Name:       "Pk",
   319  							Format:     "int",
   320  							PrimaryKey: true,
   321  						},
   322  						{
   323  							Name:   "Foo",
   324  							Format: "string",
   325  							Mutators: []string{
   326  								"cmut",
   327  							},
   328  						},
   329  					},
   330  					MutatorMap: map[string]ds.MutatorDeclaration{
   331  						"cmut": {
   332  							Name:          "cmut",
   333  							Type:          "pkg.Bar",
   334  							PartialFields: make([]ds.PartialFieldDeclaration, 1),
   335  						},
   336  					},
   337  				},
   338  			},
   339  			wantErr: true,
   340  		},
   341  		{
   342  			name: "few custom mutator on field",
   343  			args: args{
   344  				cl: ds.RecordPackage{
   345  					Fields: []ds.FieldDeclaration{
   346  						{
   347  							Name:       "Pk",
   348  							Format:     "int",
   349  							PrimaryKey: true,
   350  						},
   351  						{
   352  							Name:   "Foo",
   353  							Format: "string",
   354  							Mutators: []string{
   355  								"dec", "cmut", "cmut2",
   356  							},
   357  						},
   358  					},
   359  					MutatorMap: map[string]ds.MutatorDeclaration{
   360  						"cmut": {
   361  							Name: "cmut",
   362  							Type: "string",
   363  						},
   364  						"cmut2": {
   365  							Name: "cmut2",
   366  							Type: "string",
   367  						},
   368  					},
   369  				},
   370  			},
   371  			wantErr: true,
   372  		},
   373  	}
   374  	for _, tt := range tests {
   375  		t.Run(tt.name, func(t *testing.T) {
   376  			if err := checkFields(&tt.args.cl); (err != nil) != tt.wantErr {
   377  				t.Errorf("checkFields() error = %v, wantErr %v", err, tt.wantErr)
   378  			}
   379  		})
   380  	}
   381  }
   382  
   383  func Test_checkProcFields(t *testing.T) {
   384  	type args struct {
   385  		cl ds.RecordPackage
   386  	}
   387  	tests := []struct {
   388  		name    string
   389  		args    args
   390  		wantErr bool
   391  	}{
   392  		{
   393  			name: "empty fields",
   394  			args: args{
   395  				cl: ds.RecordPackage{
   396  					ProcOutFields: ds.ProcFieldDeclarations{},
   397  				},
   398  			},
   399  			wantErr: true,
   400  		},
   401  		{
   402  			name: "2 fields declaration",
   403  			args: args{
   404  				cl: ds.RecordPackage{
   405  					Fields: []ds.FieldDeclaration{
   406  						{
   407  							Name:       "Foo",
   408  							Format:     "int",
   409  							PrimaryKey: true,
   410  						},
   411  					},
   412  					ProcOutFields: ds.ProcFieldDeclarations{
   413  						0: {
   414  							Name:   "Foo",
   415  							Format: "int",
   416  							Type:   ds.INOUT,
   417  						},
   418  					},
   419  				},
   420  			},
   421  			wantErr: true,
   422  		},
   423  		{
   424  			name: "empty format",
   425  			args: args{
   426  				cl: ds.RecordPackage{
   427  					ProcOutFields: ds.ProcFieldDeclarations{
   428  						0: {
   429  							Name: "Foo",
   430  							Type: ds.OUT,
   431  						},
   432  					},
   433  				},
   434  			},
   435  			wantErr: true,
   436  		},
   437  		{
   438  			name: "invalid input format",
   439  			args: args{
   440  				cl: ds.RecordPackage{
   441  					ProcOutFields: ds.ProcFieldDeclarations{
   442  						0: {
   443  							Name:   "Foo",
   444  							Format: "int",
   445  							Type:   ds.OUT,
   446  						},
   447  					},
   448  					ProcInFields: []ds.ProcFieldDeclaration{
   449  						{
   450  							Name:   "Foo",
   451  							Format: "[]int",
   452  							Type:   ds.IN,
   453  						},
   454  					},
   455  				},
   456  			},
   457  			wantErr: true,
   458  		},
   459  		{
   460  			name: "invalid output format",
   461  			args: args{
   462  				cl: ds.RecordPackage{
   463  					ProcOutFields: ds.ProcFieldDeclarations{
   464  						0: {
   465  							Name:   "Foo",
   466  							Format: "[]int",
   467  							Type:   ds.OUT,
   468  						},
   469  					},
   470  				},
   471  			},
   472  			wantErr: true,
   473  		},
   474  		{
   475  			name: "type not found",
   476  			args: args{
   477  				cl: ds.RecordPackage{
   478  					ProcOutFields: ds.ProcFieldDeclarations{
   479  						0: {
   480  							Name:   "Foo",
   481  							Format: "int",
   482  						},
   483  					},
   484  				},
   485  			},
   486  			wantErr: true,
   487  		},
   488  		{
   489  			name: "incorrect fields order",
   490  			args: args{
   491  				cl: ds.RecordPackage{
   492  					ProcOutFields: ds.ProcFieldDeclarations{
   493  						0: {
   494  							Name:   "Foo",
   495  							Format: "int",
   496  						},
   497  						2: {
   498  							Name:   "Bar",
   499  							Format: "int",
   500  						},
   501  					},
   502  				},
   503  			},
   504  			wantErr: true,
   505  		},
   506  		{
   507  			name: "normal field",
   508  			args: args{
   509  				cl: ds.RecordPackage{
   510  					ProcOutFields: ds.ProcFieldDeclarations{
   511  						0: {
   512  							Name:   "Foo",
   513  							Format: "int",
   514  							Type:   ds.OUT,
   515  						},
   516  					},
   517  				},
   518  			},
   519  			wantErr: false,
   520  		},
   521  		{
   522  			name: "normal input field",
   523  			args: args{
   524  				cl: ds.RecordPackage{
   525  					ProcInFields: []ds.ProcFieldDeclaration{
   526  						{
   527  							Name:   "Foo",
   528  							Format: "[]string",
   529  							Type:   ds.IN,
   530  						},
   531  					},
   532  					ProcOutFields: ds.ProcFieldDeclarations{
   533  						0: {
   534  							Name:   "Foo",
   535  							Format: "int",
   536  							Type:   ds.OUT,
   537  						},
   538  					},
   539  				},
   540  			},
   541  			wantErr: false,
   542  		},
   543  		{
   544  			name: "serializer not declared",
   545  			args: args{
   546  				cl: ds.RecordPackage{
   547  					ProcOutFields: ds.ProcFieldDeclarations{
   548  						0: {
   549  							Name:   "Foo",
   550  							Format: "int",
   551  							Type:   ds.OUT,
   552  						},
   553  						1: {
   554  							Name:   "Foo",
   555  							Format: "int",
   556  							Type:   ds.OUT,
   557  							Serializer: []string{
   558  								"fser",
   559  							},
   560  						},
   561  					},
   562  				},
   563  			},
   564  			wantErr: true,
   565  		},
   566  	}
   567  	for _, tt := range tests {
   568  		t.Run(tt.name, func(t *testing.T) {
   569  			if err := checkFields(&tt.args.cl); (err != nil) != tt.wantErr {
   570  				t.Errorf("checkFields() error = %v, wantErr %v", err, tt.wantErr)
   571  			}
   572  		})
   573  	}
   574  }