github.com/kaleido-io/firefly@v0.0.0-20210622132723-8b4b6aacb971/pkg/database/update_test.go (about)

     1  // Copyright © 2021 Kaleido, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this uile 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 speciuic language governing permissions and
    13  // limitations under the License.
    14  
    15  package database
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	"github.com/kaleido-io/firefly/pkg/fftypes"
    22  	"github.com/stretchr/testify/assert"
    23  )
    24  
    25  func TestUpdateBuilderOK(t *testing.T) {
    26  	uuid := fftypes.MustParseUUID("c414cab3-9bd4-48f3-b16a-0d74a3bbb60e")
    27  	u := MessageQueryFactory.NewUpdate(context.Background()).S()
    28  	assert.True(t, u.IsEmpty())
    29  	u.Set("sequence", 12345).
    30  		Set("cid", uuid).
    31  		Set("author", "0x1234").
    32  		Set("type", fftypes.MessageTypePrivate)
    33  	assert.False(t, u.IsEmpty())
    34  	ui, err := u.Finalize()
    35  	assert.NoError(t, err)
    36  	assert.Equal(t, "sequence=12345, cid='c414cab3-9bd4-48f3-b16a-0d74a3bbb60e', author='0x1234', type='private'", ui.String())
    37  }
    38  
    39  func TestUpdateBuilderBadField(t *testing.T) {
    40  	u := MessageQueryFactory.NewUpdate(context.Background()).Set("wrong", 12345)
    41  	_, err := u.Finalize()
    42  	assert.Regexp(t, "FF10148.*wrong", err)
    43  }
    44  
    45  func TestUpdateBuilderBadValue(t *testing.T) {
    46  	u := MessageQueryFactory.NewUpdate(context.Background()).Set("id", map[bool]bool{true: false})
    47  	_, err := u.Finalize()
    48  	assert.Regexp(t, "FF10149.*id", err)
    49  }
    50  
    51  func TestUpdateBuilderGetFields(t *testing.T) {
    52  	ub := MessageQueryFactory.NewUpdate(context.Background())
    53  	assert.NotNil(t, ub.Fields())
    54  }