github.com/matrixorigin/matrixone@v1.2.0/pkg/frontend/variables_test.go (about)

     1  // Copyright 2022 Matrix Origin
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  // http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package frontend
    16  
    17  import (
    18  	"fmt"
    19  	"testing"
    20  
    21  	"github.com/stretchr/testify/assert"
    22  
    23  	"github.com/smartystreets/goconvey/convey"
    24  )
    25  
    26  func TestScope(t *testing.T) {
    27  	convey.Convey("test scope", t, func() {
    28  		wanted := make(map[Scope]string)
    29  		for i := ScopeGlobal; i <= ScopeResetPersist; i++ {
    30  			wanted[i] = i.String()
    31  		}
    32  
    33  		convey.So(wanted[ScopeBoth], convey.ShouldEqual, ScopeBoth.String())
    34  	})
    35  }
    36  
    37  func TestSystemVariable(t *testing.T) {
    38  	convey.Convey("all", t, func() {
    39  		bt := SystemVariableBoolType{}
    40  		it := SystemVariableIntType{}
    41  		ut := SystemVariableUintType{}
    42  		dt := SystemVariableDoubleType{}
    43  		et := SystemVariableEnumType{}
    44  		sett := SystemVariableSetType{}
    45  		st := SystemVariableStringType{}
    46  		nt := SystemVariableNullType{}
    47  		svs := []SystemVariableType{
    48  			bt,
    49  			it,
    50  			ut,
    51  			dt,
    52  			et,
    53  			sett,
    54  			st,
    55  			nt,
    56  		}
    57  
    58  		for _, sv := range svs {
    59  			fmt.Sprintln(sv.String(), sv.Type(), sv.MysqlType(), sv.Zero())
    60  		}
    61  
    62  		btrt, err := nt.Convert(nil)
    63  		convey.So(err, convey.ShouldBeNil)
    64  		convey.So(btrt, convey.ShouldBeNil)
    65  
    66  		_, err = nt.Convert("string")
    67  		convey.So(err, convey.ShouldNotBeNil)
    68  
    69  		_, err = bt.Convert(0)
    70  		convey.So(err, convey.ShouldBeNil)
    71  
    72  		_, err = bt.Convert(1)
    73  		convey.So(err, convey.ShouldBeNil)
    74  
    75  	})
    76  }
    77  
    78  func Test_valueIsBoolTrue(t *testing.T) {
    79  	type args struct {
    80  		value interface{}
    81  	}
    82  	dumpWantErr := func(t assert.TestingT, err error, msgAndArgs ...interface{}) bool {
    83  		return false
    84  	}
    85  	tests := []struct {
    86  		name    string
    87  		args    args
    88  		want    bool
    89  		wantErr assert.ErrorAssertionFunc
    90  	}{
    91  		{
    92  			name: "bool/true",
    93  			args: args{
    94  				value: true,
    95  			},
    96  			want:    true,
    97  			wantErr: dumpWantErr,
    98  		},
    99  		{
   100  			name: "bool/false",
   101  			args: args{
   102  				value: false,
   103  			},
   104  			want:    false,
   105  			wantErr: dumpWantErr,
   106  		},
   107  		{
   108  			name: "int/1",
   109  			args: args{
   110  				value: 1,
   111  			},
   112  			want:    true,
   113  			wantErr: dumpWantErr,
   114  		},
   115  		{
   116  			name: "int/0",
   117  			args: args{
   118  				value: 0,
   119  			},
   120  			want:    false,
   121  			wantErr: dumpWantErr,
   122  		},
   123  		{
   124  			name: "int8/1",
   125  			args: args{
   126  				value: int8(1),
   127  			},
   128  			want:    true,
   129  			wantErr: dumpWantErr,
   130  		},
   131  		{
   132  			name: "int8/0",
   133  			args: args{
   134  				value: int8(0),
   135  			},
   136  			want:    false,
   137  			wantErr: dumpWantErr,
   138  		},
   139  		{
   140  			name: "int16/1",
   141  			args: args{
   142  				value: int16(1),
   143  			},
   144  			want:    true,
   145  			wantErr: dumpWantErr,
   146  		},
   147  		{
   148  			name: "int16/0",
   149  			args: args{
   150  				value: int16(0),
   151  			},
   152  			want:    false,
   153  			wantErr: dumpWantErr,
   154  		},
   155  		{
   156  			name: "int32/1",
   157  			args: args{
   158  				value: int32(1),
   159  			},
   160  			want:    true,
   161  			wantErr: dumpWantErr,
   162  		},
   163  		{
   164  			name: "int32/0",
   165  			args: args{
   166  				value: int32(0),
   167  			},
   168  			want:    false,
   169  			wantErr: dumpWantErr,
   170  		},
   171  		{
   172  			name: "int64/1",
   173  			args: args{
   174  				value: int64(1),
   175  			},
   176  			want:    true,
   177  			wantErr: dumpWantErr,
   178  		},
   179  		{
   180  			name: "int64/0",
   181  			args: args{
   182  				value: int64(0),
   183  			},
   184  			want:    false,
   185  			wantErr: dumpWantErr,
   186  		},
   187  		{
   188  			name: "uint8/1",
   189  			args: args{
   190  				value: uint8(1),
   191  			},
   192  			want:    true,
   193  			wantErr: dumpWantErr,
   194  		},
   195  		{
   196  			name: "uint8/0",
   197  			args: args{
   198  				value: uint8(0),
   199  			},
   200  			want:    false,
   201  			wantErr: dumpWantErr,
   202  		},
   203  		{
   204  			name: "uint16/1",
   205  			args: args{
   206  				value: uint16(1),
   207  			},
   208  			want:    true,
   209  			wantErr: dumpWantErr,
   210  		},
   211  		{
   212  			name: "uint16/0",
   213  			args: args{
   214  				value: uint16(0),
   215  			},
   216  			want:    false,
   217  			wantErr: dumpWantErr,
   218  		},
   219  		{
   220  			name: "uint32/1",
   221  			args: args{
   222  				value: uint32(1),
   223  			},
   224  			want:    true,
   225  			wantErr: dumpWantErr,
   226  		},
   227  		{
   228  			name: "uint32/0",
   229  			args: args{
   230  				value: uint32(0),
   231  			},
   232  			want:    false,
   233  			wantErr: dumpWantErr,
   234  		},
   235  		{
   236  			name: "uint64/1",
   237  			args: args{
   238  				value: uint64(1),
   239  			},
   240  			want:    true,
   241  			wantErr: dumpWantErr,
   242  		},
   243  		{
   244  			name: "uint64/0",
   245  			args: args{
   246  				value: uint64(0),
   247  			},
   248  			want:    false,
   249  			wantErr: dumpWantErr,
   250  		},
   251  		{
   252  			name: "string/on",
   253  			args: args{
   254  				value: "ON",
   255  			},
   256  			want:    true,
   257  			wantErr: dumpWantErr,
   258  		},
   259  		{
   260  			name: "string/off",
   261  			args: args{
   262  				value: "OFF",
   263  			},
   264  			want:    false,
   265  			wantErr: dumpWantErr,
   266  		},
   267  	}
   268  	for _, tt := range tests {
   269  		t.Run(tt.name, func(t *testing.T) {
   270  			got, err := valueIsBoolTrue(tt.args.value)
   271  			if !tt.wantErr(t, err, fmt.Sprintf("valueIsBoolTrue(%v)", tt.args.value)) {
   272  				return
   273  			}
   274  			assert.Equalf(t, tt.want, got, "valueIsBoolTrue(%v)", tt.args.value)
   275  		})
   276  	}
   277  }