github.com/matrixorigin/matrixone@v0.7.0/pkg/vectorize/json_unquote/json_unquote_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 json_unquote
    16  
    17  import (
    18  	"github.com/matrixorigin/matrixone/pkg/container/nulls"
    19  	"github.com/matrixorigin/matrixone/pkg/container/types"
    20  	"github.com/stretchr/testify/require"
    21  	"testing"
    22  )
    23  
    24  func TestStringBatch(t *testing.T) {
    25  	xs := [][]byte{
    26  		[]byte(`"hello"`),
    27  		[]byte(`"world"`),
    28  	}
    29  	rs := make([]string, len(xs))
    30  	nsp := nulls.NewWithSize(2)
    31  	rs, err := StringBatch(xs, rs, nsp)
    32  	require.NoError(t, err)
    33  	require.Equal(t, "hello", rs[0])
    34  	require.Equal(t, "world", rs[1])
    35  	nsp.Set(0)
    36  	_, err = StringBatch(xs, rs, nsp)
    37  	require.NoError(t, err)
    38  	xs = append(xs, []byte(`hello`))
    39  	rs = make([]string, len(xs))
    40  	nsp = nulls.NewWithSize(3)
    41  	_, err = StringBatch(xs, rs, nsp)
    42  	require.Error(t, err)
    43  }
    44  func TestJsonBatch(t *testing.T) {
    45  	xs := [][]byte{
    46  		[]byte(`"hello"`),
    47  		[]byte(`"world"`),
    48  	}
    49  	for i := range xs {
    50  		bj, _ := types.ParseSliceToByteJson(xs[i])
    51  		xs[i], _ = bj.Marshal()
    52  	}
    53  	rs := make([]string, len(xs))
    54  	nsp := nulls.NewWithSize(2)
    55  	rs, err := JsonBatch(xs, rs, nsp)
    56  	require.NoError(t, err)
    57  	require.Equal(t, "hello", rs[0])
    58  	require.Equal(t, "world", rs[1])
    59  
    60  	nsp.Set(0)
    61  	_, err = JsonBatch(xs, rs, nsp)
    62  	require.NoError(t, err)
    63  
    64  }