github.com/voedger/voedger@v0.0.0-20240520144910-273e84102129/pkg/state/impl_result_storage_test.go (about)

     1  /*
     2   * Copyright (c) 2023-present unTill Pro, Ltd.
     3   */
     4  
     5  package state
     6  
     7  import (
     8  	"context"
     9  	"fmt"
    10  	"testing"
    11  
    12  	"github.com/stretchr/testify/require"
    13  
    14  	"github.com/voedger/voedger/pkg/appdef"
    15  	"github.com/voedger/voedger/pkg/istructs"
    16  )
    17  
    18  func TestCmdResultStorage_InsertInValue(t *testing.T) {
    19  	cmdResBuilder := istructs.NewNullObjectBuilder()
    20  	s := ProvideCommandProcessorStateFactory()(context.Background(), nil, nil, SimpleWSIDFunc(istructs.NullWSID),
    21  		nil, nil, nil, nil, 1, func() istructs.IObjectBuilder { return cmdResBuilder }, nil, nil, nil)
    22  
    23  	kb, err := s.KeyBuilder(Result, testRecordQName1)
    24  	require.NoError(t, err)
    25  
    26  	vb, err := s.NewValue(kb)
    27  	require.NoError(t, err)
    28  
    29  	fieldName := "name"
    30  	value := "value"
    31  
    32  	vb.PutString(fieldName, value)
    33  }
    34  
    35  func TestResultStorage_InsertInKey(t *testing.T) {
    36  	defer func() {
    37  		r := fmt.Sprint(recover())
    38  		require.Equal(t, "assignment to entry in nil map", r)
    39  	}()
    40  
    41  	cmdResBuilder := istructs.NewNullObjectBuilder()
    42  	s := ProvideCommandProcessorStateFactory()(context.Background(), nil, nil, SimpleWSIDFunc(istructs.NullWSID),
    43  		nil, nil, nil, nil, 1, func() istructs.IObjectBuilder { return cmdResBuilder }, nil, nil, nil)
    44  
    45  	kb, err := s.KeyBuilder(Result, testRecordQName1)
    46  	require.NoError(t, err)
    47  
    48  	fieldName := "name"
    49  	value := "value"
    50  
    51  	kb.PutString(fieldName, value)
    52  }
    53  
    54  func TestResultStorage_QueryProcessor(t *testing.T) {
    55  
    56  	sentObjects := make([]istructs.IObject, 0)
    57  
    58  	cmdResBuilder := istructs.NewNullObjectBuilder()
    59  	execQueryCallback := func() istructs.ExecQueryCallback {
    60  		return func(object istructs.IObject) error {
    61  			sentObjects = append(sentObjects, object)
    62  			return nil
    63  		}
    64  	}
    65  	s := ProvideQueryProcessorStateFactory()(context.Background(), nil, nil, SimpleWSIDFunc(istructs.NullWSID),
    66  		nil, nil, nil, nil, nil, func() istructs.IObjectBuilder { return cmdResBuilder }, nil, execQueryCallback)
    67  
    68  	kb, err := s.KeyBuilder(Result, appdef.NullQName)
    69  	require.NoError(t, err)
    70  
    71  	intent, err := s.NewValue(kb)
    72  	require.NoError(t, err)
    73  	require.NotNil(t, intent)
    74  
    75  	intent, err = s.NewValue(kb)
    76  	require.NoError(t, err)
    77  	require.NotNil(t, intent)
    78  
    79  	require.NoError(t, s.ApplyIntents())
    80  	require.Len(t, sentObjects, 2)
    81  
    82  }