github.com/matrixorigin/matrixone@v0.7.0/cmd/mo-dump/main_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  package main
    15  
    16  import (
    17  	"bytes"
    18  	"database/sql"
    19  	"fmt"
    20  	"github.com/stretchr/testify/require"
    21  	"os"
    22  	"testing"
    23  )
    24  
    25  func TestConvertValue(t *testing.T) {
    26  	kase := []struct {
    27  		val string
    28  		typ string
    29  	}{
    30  		{"1", "int"},
    31  		{"1", "tinyint"},
    32  		{"1", "smallint"},
    33  		{"1", "bigint"},
    34  		{"1", "unsigned bigint"},
    35  		{"1", "unsigned int"},
    36  		{"1", "unsigned tinyint"},
    37  		{"1", "unsigned smallint"},
    38  		{"1.1", "float"},
    39  		{"1.1", "double"},
    40  		{"1.1", "decimal"},
    41  		{"asa", "varchar"},
    42  		{"asa", "char"},
    43  		{"asa", "text"},
    44  		{"asa", "blob"},
    45  		{"asa", "uuid"},
    46  		{"asa", "json"},
    47  		{"2021-01-01", "date"},
    48  		{"2021-01-01 00:00:00", "datetime"},
    49  		{"2021-01-01 00:00:00", "timestamp"},
    50  	}
    51  	for _, v := range kase {
    52  		s := convertValue(makeValue(v.val), v.typ)
    53  		switch v.typ {
    54  		case "int", "tinyint", "smallint", "bigint", "unsigned bigint", "unsigned int", "unsigned tinyint", "unsigned smallint", "float", "double":
    55  			require.Equal(t, v.val, s)
    56  		default:
    57  
    58  			require.Equal(t, fmt.Sprintf("'%v'", v.val), s)
    59  		}
    60  	}
    61  }
    62  
    63  func makeValue(val string) interface{} {
    64  	tmp := sql.RawBytes(val)
    65  	return &tmp
    66  }
    67  
    68  func TestShowCreateTable(t *testing.T) {
    69  	kases := []struct {
    70  		sql          string
    71  		withNextLine bool
    72  		res          string
    73  	}{
    74  		{
    75  			sql:          "create table t1 (a int, b int)",
    76  			withNextLine: false,
    77  			res:          "create table t1 (a int, b int);\n",
    78  		},
    79  		{
    80  			sql:          "create table t1 (a int, b int)",
    81  			withNextLine: true,
    82  			res:          "create table t1 (a int, b int);\n\n\n",
    83  		},
    84  		{
    85  			sql:          "create table t1 (a int, b int);",
    86  			withNextLine: false,
    87  			res:          "create table t1 (a int, b int);\n",
    88  		},
    89  		{
    90  			sql:          "create table t1 (a int, b int);",
    91  			withNextLine: true,
    92  			res:          "create table t1 (a int, b int);\n\n\n",
    93  		},
    94  	}
    95  	old := os.Stdout
    96  	for _, v := range kases {
    97  		r, w, _ := os.Pipe()
    98  		os.Stdout = w
    99  		showCreateTable(v.sql, v.withNextLine)
   100  
   101  		e := w.Close()
   102  		require.Nil(t, e)
   103  		var buf bytes.Buffer
   104  		_, e = buf.ReadFrom(r)
   105  		require.Nil(t, e)
   106  		require.Equal(t, v.res, buf.String())
   107  	}
   108  	os.Stdout = old
   109  }