github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/tobase64_frombase64_test.go (about)

     1  // Copyright 2020-2021 Dolthub, Inc.
     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 function
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/stretchr/testify/require"
    21  
    22  	"github.com/dolthub/go-mysql-server/sql"
    23  	"github.com/dolthub/go-mysql-server/sql/expression"
    24  	"github.com/dolthub/go-mysql-server/sql/types"
    25  )
    26  
    27  func TestBase64(t *testing.T) {
    28  	fTo := NewToBase64(expression.NewGetField(0, types.LongText, "", false))
    29  	fFrom := NewFromBase64(expression.NewGetField(0, types.LongText, "", false))
    30  
    31  	testCases := []struct {
    32  		name     string
    33  		row      sql.Row
    34  		expected interface{}
    35  		err      bool
    36  	}{
    37  		// Use a MySQL server to get expected values if updating/adding to this!
    38  		{"null input", sql.NewRow(nil), nil, false},
    39  		{"single_line", sql.NewRow([]byte("foo")), string("Zm9v"), false},
    40  		{"multi_line", sql.NewRow(
    41  			[]byte("Gallia est omnis divisa in partes tres, quarum unam " +
    42  				"incolunt Belgae, aliam Aquitani, tertiam qui ipsorum lingua Celtae, " +
    43  				"nostra Galli appellantur")),
    44  			"R2FsbGlhIGVzdCBvbW5pcyBkaXZpc2EgaW4gcGFydGVzIHRyZXMsIHF1YXJ1bSB1bmFtIGluY29s\n" +
    45  				"dW50IEJlbGdhZSwgYWxpYW0gQXF1aXRhbmksIHRlcnRpYW0gcXVpIGlwc29ydW0gbGluZ3VhIENl\n" +
    46  				"bHRhZSwgbm9zdHJhIEdhbGxpIGFwcGVsbGFudHVy", false},
    47  		{"empty_input", sql.NewRow([]byte{}), string(""), false},
    48  		{"symbols", sql.NewRow([]byte("!@#$% %^&*()_+\r\n\t{};")), string("IUAjJCUgJV4mKigpXysNCgl7fTs="),
    49  			false},
    50  	}
    51  
    52  	for _, tt := range testCases {
    53  		t.Run(tt.name, func(t *testing.T) {
    54  			t.Helper()
    55  			require := require.New(t)
    56  			ctx := sql.NewEmptyContext()
    57  			v, err := fTo.Eval(ctx, tt.row)
    58  
    59  			if tt.err {
    60  				require.Error(err)
    61  			} else {
    62  				require.NoError(err)
    63  				require.Equal(tt.expected, v)
    64  
    65  				ctx = sql.NewEmptyContext()
    66  				v2, err := fFrom.Eval(ctx, sql.NewRow(v))
    67  				require.NoError(err)
    68  				require.Equal(sql.NewRow(v2), tt.row)
    69  			}
    70  		})
    71  	}
    72  }