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

     1  // Copyright 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 aggregation
    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 TestJsonArrayAgg_Name(t *testing.T) {
    28  	assert := require.New(t)
    29  
    30  	m := NewJsonArray(expression.NewGetField(0, types.Int32, "field", true))
    31  	assert.Equal("JSON_ARRAYAGG(field)", m.String())
    32  }
    33  
    34  func TestJsonArrayAgg_SimpleIntField(t *testing.T) {
    35  	assert := require.New(t)
    36  	ctx := sql.NewEmptyContext()
    37  
    38  	j := NewJsonArray(expression.NewGetField(0, types.Int32, "field", true))
    39  	b, _ := j.NewBuffer()
    40  
    41  	b.Update(ctx, sql.NewRow(float64(7)))
    42  	b.Update(ctx, sql.NewRow(float64(2)))
    43  
    44  	v, err := b.Eval(ctx)
    45  	assert.NoError(err)
    46  	assert.Equal(types.MustJSON(`[7, 2]`), v)
    47  }
    48  
    49  func TestJsonArrayAgg_Strings(t *testing.T) {
    50  	assert := require.New(t)
    51  	ctx := sql.NewEmptyContext()
    52  
    53  	j := NewJsonArray(expression.NewGetField(0, types.Int32, "field", true))
    54  	b, _ := j.NewBuffer()
    55  
    56  	b.Update(ctx, sql.NewRow("hi"))
    57  	b.Update(ctx, sql.NewRow("hello"))
    58  
    59  	v, err := b.Eval(ctx)
    60  	assert.NoError(err)
    61  	assert.Equal(types.MustJSON(`["hi","hello"]`), v)
    62  }
    63  
    64  func TestJsonArrayAgg_Empty(t *testing.T) {
    65  	assert := require.New(t)
    66  	ctx := sql.NewEmptyContext()
    67  
    68  	j := NewJsonArray(expression.NewGetField(0, types.Int32, "field", true))
    69  	b, _ := j.NewBuffer()
    70  
    71  	v, err := b.Eval(ctx)
    72  	assert.NoError(err)
    73  	assert.Equal(types.JSONDocument{Val: []interface{}(nil)}, v)
    74  }
    75  
    76  func TestJsonArrayAgg_JSON(t *testing.T) {
    77  	assert := require.New(t)
    78  	ctx := sql.NewEmptyContext()
    79  
    80  	j := NewJsonArray(expression.NewGetField(0, types.JSON, "field", true))
    81  	b, _ := j.NewBuffer()
    82  	b.Update(ctx, sql.NewRow(types.MustJSON(`{"key1": "value1", "key2": "value2"}`)))
    83  
    84  	v, err := b.Eval(ctx)
    85  	assert.NoError(err)
    86  	assert.Equal(types.MustJSON(`[{"key1": "value1", "key2": "value2"}]`), v)
    87  }