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

     1  package octopus
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  func TestPackSelect(t *testing.T) {
     9  	namespace := []byte{0x02, 0x00, 0x00, 0x00}
    10  	indexNum := []byte{0x01, 0x00, 0x00, 0x00}
    11  	offset := []byte{0x00, 0x00, 0x00, 0x00}
    12  	limit := []byte{0x0A, 0x00, 0x00, 0x00}
    13  	tuples := []byte{0x02, 0x00, 0x00, 0x00}
    14  	tuple1 := []byte{0x02, 0x00, 0x00, 0x00, 0x03, 97, 97, 97, 0x02, 0x10, 0x00}
    15  	tuple2 := []byte{0x02, 0x00, 0x00, 0x00, 0x03, 98, 98, 98, 0x02, 0x20, 0x00}
    16  
    17  	selectReq := append(namespace, indexNum...)
    18  	selectReq = append(selectReq, offset...)
    19  	selectReq = append(selectReq, limit...)
    20  	selectReq = append(selectReq, tuples...)
    21  	selectReq = append(selectReq, tuple1...)
    22  	selectReq = append(selectReq, tuple2...)
    23  
    24  	type args struct {
    25  		ns       uint32
    26  		indexnum uint32
    27  		offset   uint32
    28  		limit    uint32
    29  		keys     [][][]byte
    30  	}
    31  	tests := []struct {
    32  		name string
    33  		args args
    34  		want []byte
    35  	}{
    36  		{
    37  			name: "select",
    38  			args: args{
    39  				ns:       2,
    40  				indexnum: 1,
    41  				offset:   0,
    42  				limit:    10,
    43  				keys: [][][]byte{
    44  					{
    45  						[]byte("aaa"),
    46  						{0x10, 0x00},
    47  					},
    48  					{
    49  						[]byte("bbb"),
    50  						{0x20, 0x00},
    51  					},
    52  				},
    53  			},
    54  			want: selectReq,
    55  		},
    56  	}
    57  	for _, tt := range tests {
    58  		t.Run(tt.name, func(t *testing.T) {
    59  			if got := PackSelect(tt.args.ns, tt.args.indexnum, tt.args.offset, tt.args.limit, tt.args.keys); !reflect.DeepEqual(got, tt.want) {
    60  				t.Errorf("PackSelect() = %v, want %v", got, tt.want)
    61  			}
    62  		})
    63  	}
    64  }
    65  
    66  func TestPackInsertReplace(t *testing.T) {
    67  	fieldValue := []byte{0x0A, 0x00, 0x00, 0x00}
    68  	namespace := []byte{0x02, 0x00, 0x00, 0x00}
    69  	insertreplaceFlags := []byte{0x01, 0x00, 0x00, 0x00}
    70  	insertFlags := []byte{0x03, 0x00, 0x00, 0x00}
    71  	replaceFlags := []byte{0x05, 0x00, 0x00, 0x00}
    72  	insertTupleCardinality := []byte{0x02, 0x00, 0x00, 0x00}
    73  	insertTupleFields := append([]byte{0x04}, fieldValue...) //len + Field1
    74  	insertTupleFields = append(insertTupleFields, []byte{0x00}...)
    75  
    76  	insertReq := append(namespace, insertFlags...)
    77  	insertReq = append(insertReq, insertTupleCardinality...)
    78  	insertReq = append(insertReq, insertTupleFields...)
    79  
    80  	replaceReq := append(namespace, replaceFlags...)
    81  	replaceReq = append(replaceReq, insertTupleCardinality...)
    82  	replaceReq = append(replaceReq, insertTupleFields...)
    83  
    84  	insertreplaceReq := append(namespace, insertreplaceFlags...)
    85  	insertreplaceReq = append(insertreplaceReq, insertTupleCardinality...)
    86  	insertreplaceReq = append(insertreplaceReq, insertTupleFields...)
    87  
    88  	type args struct {
    89  		ns         uint32
    90  		insertMode InsertMode
    91  		tuple      [][]byte
    92  	}
    93  	tests := []struct {
    94  		name string
    95  		args args
    96  		want []byte
    97  	}{
    98  		{
    99  			name: "insert",
   100  			args: args{
   101  				ns:         2,
   102  				insertMode: 1,
   103  				tuple: [][]byte{
   104  					fieldValue,
   105  					{},
   106  				},
   107  			},
   108  			want: insertReq,
   109  		},
   110  		{
   111  			name: "replace",
   112  			args: args{
   113  				ns:         2,
   114  				insertMode: 2,
   115  				tuple: [][]byte{
   116  					fieldValue,
   117  					{},
   118  				},
   119  			},
   120  			want: replaceReq,
   121  		},
   122  		{
   123  			name: "insertreplace",
   124  			args: args{
   125  				ns:         2,
   126  				insertMode: 0,
   127  				tuple: [][]byte{
   128  					fieldValue,
   129  					{},
   130  				},
   131  			},
   132  			want: insertreplaceReq,
   133  		},
   134  	}
   135  	for _, tt := range tests {
   136  		t.Run(tt.name, func(t *testing.T) {
   137  			if got := PackInsertReplace(tt.args.ns, tt.args.insertMode, tt.args.tuple); !reflect.DeepEqual(got, tt.want) {
   138  				t.Errorf("PackInsertReplace() = %v, want %v", got, tt.want)
   139  			}
   140  		})
   141  	}
   142  }