github.com/mailru/activerecord@v1.12.2/pkg/octopus/mock_server_b_test.go (about)

     1  package octopus
     2  
     3  import (
     4  	"log"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"github.com/mailru/activerecord/pkg/iproto/iproto"
     9  )
    10  
    11  func TestMockServer_ProcessRequest(t *testing.T) {
    12  	logger := NewMockMockServerLogger(t)
    13  
    14  	pk := append([][]byte{}, PackString([]byte{}, "pk", iproto.ModeDefault))
    15  
    16  	var keysPacked [][][]byte
    17  
    18  	for _, key := range []string{"key1"} {
    19  		var keysField [][]byte
    20  		keysField = append(keysField, PackString([]byte{}, key, iproto.ModeDefault))
    21  		keysPacked = append(keysPacked, keysField)
    22  	}
    23  
    24  	type fields struct {
    25  		oft []FixtureType
    26  	}
    27  	type args struct {
    28  		msg uint8
    29  		req []byte
    30  	}
    31  	tests := []struct {
    32  		name   string
    33  		fields fields
    34  		args   args
    35  		want   []byte
    36  		wantEx bool
    37  		mocks  func(t *testing.T)
    38  	}{
    39  		{
    40  			name: "simple check ProcessRequest",
    41  			fields: fields{
    42  				oft: []FixtureType{
    43  					{
    44  						ID:       1,
    45  						Msg:      RequestTypeSelect,
    46  						Request:  []byte("aassdd"),
    47  						Response: []byte("zzxxcc"),
    48  					},
    49  				},
    50  			},
    51  			args: args{
    52  				msg: uint8(RequestTypeSelect),
    53  				req: []byte("aassdd"),
    54  			},
    55  			want:   []byte("zzxxcc"),
    56  			wantEx: true,
    57  		},
    58  		{
    59  			name: "Select message type",
    60  			fields: fields{
    61  				oft: []FixtureType{
    62  					{
    63  						ID:       1,
    64  						Msg:      RequestTypeSelect,
    65  						Request:  PackSelect(1, 0, 0, 10, keysPacked),
    66  						Response: []byte("select ok"),
    67  					},
    68  				},
    69  			},
    70  			args: args{
    71  				msg: uint8(RequestTypeSelect),
    72  				req: PackSelect(1, 0, 0, 10, keysPacked),
    73  			},
    74  			want:   []byte("select ok"),
    75  			wantEx: true,
    76  		},
    77  		{
    78  			name: "Select message type (not found)",
    79  			fields: fields{
    80  				oft: []FixtureType{
    81  					{
    82  						ID:      1,
    83  						Msg:     RequestTypeSelect,
    84  						Request: PackSelect(1, 0, 0, 10, keysPacked),
    85  						Response: PackResponse([][]byte{
    86  							[]byte("f1"),
    87  							[]byte("f2")},
    88  						),
    89  					},
    90  				},
    91  			},
    92  			args: args{
    93  				msg: uint8(RequestTypeSelect),
    94  				req: PackSelect(1, 0, 0, 1, keysPacked),
    95  			},
    96  			want:   nil,
    97  			wantEx: false,
    98  			mocks: func(t *testing.T) {
    99  				logger.EXPECT().DebugSelectRequest(uint32(1), uint32(0), uint32(0), uint32(1), keysPacked, SelectMockFixture{
   100  					Indexnum:   0,
   101  					Offset:     0,
   102  					Limit:      10,
   103  					Keys:       keysPacked,
   104  					RespTuples: []TupleData{{Cnt: uint32(2), Data: [][]byte{[]byte("f1"), []byte("f2")}}},
   105  				})
   106  			},
   107  		},
   108  		{
   109  			name: "Call procedure message type (not found)",
   110  			fields: fields{
   111  				oft: []FixtureType{
   112  					{
   113  						ID:      1,
   114  						Msg:     RequestTypeCall,
   115  						Request: PackLua("foo", "a1", "a2"),
   116  						Response: PackResponse([][]byte{
   117  							[]byte("status"),
   118  							[]byte("data")},
   119  						),
   120  					},
   121  				},
   122  			},
   123  			args: args{
   124  				msg: uint8(RequestTypeCall),
   125  				req: PackLua("foo"),
   126  			},
   127  			want:   nil,
   128  			wantEx: false,
   129  			mocks: func(t *testing.T) {
   130  				logger.EXPECT().DebugCallRequest(
   131  					"foo",
   132  					[][]byte{},
   133  					CallMockFixture{
   134  						ProcName:   "foo",
   135  						Args:       [][]byte{[]byte("a1"), []byte("a2")},
   136  						RespTuples: []TupleData{{Cnt: uint32(2), Data: [][]byte{[]byte("status"), []byte("data")}}},
   137  					},
   138  				)
   139  			},
   140  		},
   141  		{
   142  			name: "Insert message type (not found)",
   143  			fields: fields{
   144  				oft: []FixtureType{
   145  					{
   146  						ID:       2,
   147  						Msg:      RequestTypeInsert,
   148  						Request:  PackInsertReplace(2, InsertModeInserOrReplace, [][]byte{[]byte("i1")}),
   149  						Response: nil,
   150  					},
   151  				},
   152  			},
   153  			args: args{
   154  				msg: uint8(RequestTypeInsert),
   155  				req: PackInsertReplace(2, InsertModeInserOrReplace, [][]byte{[]byte("i2")}),
   156  			},
   157  			wantEx: false,
   158  			mocks: func(t *testing.T) {
   159  				logger.EXPECT().DebugInsertRequest(uint32(2), true, InsertModeInserOrReplace, TupleData{Cnt: uint32(1), Data: [][]byte{[]byte("i2")}}, InsertMockFixture{
   160  					NeedRetVal: true,
   161  					InsertMode: InsertModeInserOrReplace,
   162  					Tuple:      TupleData{Cnt: uint32(1), Data: [][]byte{[]byte("i1")}},
   163  				})
   164  			},
   165  		},
   166  		{
   167  			name: "Update message type (not found)",
   168  			fields: fields{
   169  				oft: []FixtureType{
   170  					{
   171  						ID:  1,
   172  						Msg: RequestTypeUpdate,
   173  						Request: PackUpdate(42, pk, []Ops{
   174  							{
   175  								Field: 1,
   176  								Op:    OpSet,
   177  								Value: []byte("u1"),
   178  							},
   179  							{
   180  								Field: 2,
   181  								Op:    OpSet,
   182  								Value: []byte("u2"),
   183  							},
   184  						}),
   185  						Response: PackResponse([][]byte{}),
   186  					},
   187  				},
   188  			},
   189  			args: args{
   190  				msg: uint8(RequestTypeUpdate),
   191  				req: PackUpdate(42, pk, []Ops{
   192  					{
   193  						Field: 1,
   194  						Op:    OpSet,
   195  						Value: []byte("u1"),
   196  					},
   197  				}),
   198  			},
   199  			wantEx: false,
   200  			mocks: func(t *testing.T) {
   201  				logger.EXPECT().DebugUpdateRequest(
   202  					uint32(42),
   203  					pk,
   204  					[]Ops{
   205  						{
   206  							Field: 1,
   207  							Op:    OpSet,
   208  							Value: []byte("u1"),
   209  						},
   210  					},
   211  					UpdateMockFixture{
   212  						PrimaryKey: pk,
   213  						UpdateOps: []Ops{
   214  							{
   215  								Field: 1,
   216  								Op:    OpSet,
   217  								Value: []byte("u1"),
   218  							},
   219  							{
   220  								Field: 2,
   221  								Op:    OpSet,
   222  								Value: []byte("u2"),
   223  							},
   224  						},
   225  					})
   226  			},
   227  		},
   228  	}
   229  	for _, tt := range tests {
   230  		t.Run(tt.name, func(t *testing.T) {
   231  			if tt.mocks != nil {
   232  				tt.mocks(t)
   233  			}
   234  
   235  			oms := &MockServer{
   236  				oft:    tt.fields.oft,
   237  				logger: logger,
   238  			}
   239  
   240  			got, ex := oms.ProcessRequest(tt.args.msg, tt.args.req)
   241  			if !reflect.DeepEqual(got, tt.want) {
   242  				t.Errorf("MockServer.ProcessRequest() got = %v, want %v", got, tt.want)
   243  			}
   244  
   245  			if ex != tt.wantEx {
   246  				t.Errorf("MockServer.ProcessRequest() exist = %v, want %v", ex, tt.wantEx)
   247  			}
   248  		})
   249  	}
   250  }
   251  
   252  func PackResponse(data [][]byte) []byte {
   253  	var tuples [][][]byte
   254  
   255  	tuples = append(tuples, data)
   256  
   257  	resp, err := PackResopnseStatus(RcOK, tuples)
   258  	if err != nil {
   259  		log.Fatal(err)
   260  	}
   261  
   262  	return resp
   263  }