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

     1  package checker
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/mailru/activerecord/internal/pkg/ds"
     8  	"github.com/mailru/activerecord/pkg/octopus"
     9  )
    10  
    11  func TestCheck(t *testing.T) {
    12  	rpFoo := ds.NewRecordPackage()
    13  	rpFoo.Backends = []string{"octopus"}
    14  	rpFoo.Namespace = ds.NamespaceDeclaration{ObjectName: "0", PackageName: "foo", PublicName: "Foo"}
    15  	rpFoo.Server = ds.ServerDeclaration{Host: "127.0.0.1", Port: "11011"}
    16  
    17  	err := rpFoo.AddField(ds.FieldDeclaration{
    18  		Name:       "ID",
    19  		Format:     octopus.Int,
    20  		PrimaryKey: true,
    21  		Mutators:   []string{},
    22  		Size:       0,
    23  		Serializer: []string{},
    24  		ObjectLink: "",
    25  	})
    26  	if err != nil {
    27  		t.Errorf("can't prepare test data: %s", err)
    28  		return
    29  	}
    30  
    31  	err = rpFoo.AddField(ds.FieldDeclaration{
    32  		Name:       "BarID",
    33  		Format:     octopus.Int,
    34  		PrimaryKey: false,
    35  		Mutators:   []string{},
    36  		Size:       0,
    37  		Serializer: []string{},
    38  		ObjectLink: "Bar",
    39  	})
    40  	if err != nil {
    41  		t.Errorf("can't prepare test data: %s", err)
    42  		return
    43  	}
    44  
    45  	err = rpFoo.AddFieldObject(ds.FieldObject{
    46  		Name:       "Foo",
    47  		Key:        "ID",
    48  		ObjectName: "bar",
    49  		Field:      "BarID",
    50  		Unique:     true,
    51  	})
    52  	if err != nil {
    53  		t.Errorf("can't prepare test data: %s", err)
    54  		return
    55  	}
    56  
    57  	rpInvalidFormat := ds.NewRecordPackage()
    58  	rpInvalidFormat.Backends = []string{"octopus"}
    59  	rpInvalidFormat.Namespace = ds.NamespaceDeclaration{ObjectName: "0", PackageName: "invform", PublicName: "InvalidFormat"}
    60  	rpInvalidFormat.Server = ds.ServerDeclaration{Host: "127.0.0.1", Port: "11011"}
    61  
    62  	err = rpInvalidFormat.AddField(ds.FieldDeclaration{
    63  		Name:       "ID",
    64  		Format:     "byte",
    65  		PrimaryKey: true,
    66  		Mutators:   []string{},
    67  		Size:       0,
    68  		Serializer: []string{},
    69  		ObjectLink: "",
    70  	})
    71  	if err != nil {
    72  		t.Errorf("can't prepare test data: %s", err)
    73  		return
    74  	}
    75  
    76  	onInvalidFormat := ds.NewRecordPackage()
    77  	onInvalidFormat.Backends = []string{"octopus"}
    78  	onInvalidFormat.Namespace = ds.NamespaceDeclaration{ObjectName: "invalid", PackageName: "invform", PublicName: "InvalidFormat"}
    79  	onInvalidFormat.Server = ds.ServerDeclaration{Host: "127.0.0.1", Port: "11011", Conf: "box"}
    80  
    81  	err = onInvalidFormat.AddField(ds.FieldDeclaration{
    82  		Name:       "ID",
    83  		Format:     "byte",
    84  		PrimaryKey: true,
    85  		Mutators:   []string{},
    86  		Size:       0,
    87  		Serializer: []string{},
    88  		ObjectLink: "",
    89  	})
    90  	if err != nil {
    91  		t.Errorf("can't prepare test data: %s", err)
    92  		return
    93  	}
    94  
    95  	type args struct {
    96  		files         map[string]*ds.RecordPackage
    97  		linkedObjects map[string]string
    98  	}
    99  	tests := []struct {
   100  		name    string
   101  		args    args
   102  		wantErr bool
   103  	}{
   104  		{
   105  			name: "octopus empty",
   106  			args: args{
   107  				files:         map[string]*ds.RecordPackage{},
   108  				linkedObjects: map[string]string{},
   109  			},
   110  			wantErr: false,
   111  		},
   112  		{
   113  			name: "linked objs",
   114  			args: args{
   115  				files:         map[string]*ds.RecordPackage{"foo": rpFoo},
   116  				linkedObjects: map[string]string{"bar": "bar"},
   117  			},
   118  			wantErr: false,
   119  		},
   120  		{
   121  			name: "wrong octopus format",
   122  			args: args{
   123  				files:         map[string]*ds.RecordPackage{"invalid": rpInvalidFormat},
   124  				linkedObjects: map[string]string{},
   125  			},
   126  			wantErr: true,
   127  		},
   128  		{
   129  			name: "wrong octopus namespace objectname format",
   130  			args: args{
   131  				files:         map[string]*ds.RecordPackage{"invalid": onInvalidFormat},
   132  				linkedObjects: map[string]string{},
   133  			},
   134  			wantErr: true,
   135  		},
   136  	}
   137  	for _, tt := range tests {
   138  		t.Run(tt.name, func(t *testing.T) {
   139  			if err := Check(tt.args.files, tt.args.linkedObjects); (err != nil) != tt.wantErr {
   140  				t.Errorf("Check() error = %v, wantErr %v", err, tt.wantErr)
   141  			}
   142  		})
   143  	}
   144  }
   145  
   146  func TestInit(t *testing.T) {
   147  	type args struct {
   148  		files map[string]*ds.RecordPackage
   149  	}
   150  	tests := []struct {
   151  		name string
   152  		args args
   153  		want *Checker
   154  	}{
   155  		{
   156  			name: "simple init",
   157  			args: args{
   158  				files: map[string]*ds.RecordPackage{},
   159  			},
   160  			want: &Checker{
   161  				files: map[string]*ds.RecordPackage{},
   162  			},
   163  		},
   164  	}
   165  	for _, tt := range tests {
   166  		t.Run(tt.name, func(t *testing.T) {
   167  			if got := Init(tt.args.files); !reflect.DeepEqual(got, tt.want) {
   168  				t.Errorf("Init() = %v, want %v", got, tt.want)
   169  			}
   170  		})
   171  	}
   172  }