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

     1  // Copyright © 2021 Kaleido, Inc.
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  
    17  package fftypes
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  )
    24  
    25  func TestSubscriptionOptionsDatabaseSerialization(t *testing.T) {
    26  
    27  	firstEvent := SubOptsFirstEventNewest
    28  	readAhead := uint16(50)
    29  	sub1 := &Subscription{
    30  		Options: SubscriptionOptions{
    31  			FirstEvent: &firstEvent,
    32  			ReadAhead:  &readAhead,
    33  		},
    34  	}
    35  
    36  	// Verify it serializes as bytes to the database
    37  	b1, err := sub1.Options.Value()
    38  	assert.NoError(t, err)
    39  	assert.Equal(t, `{"firstEvent":"newest","readAhead":50}`, string(b1.([]byte)))
    40  
    41  	// Verify it restores ok
    42  	sub2 := &Subscription{}
    43  	err = sub2.Options.Scan(b1)
    44  	assert.NoError(t, err)
    45  	b2, err := sub1.Options.Value()
    46  	assert.NoError(t, err)
    47  	assert.Equal(t, string(b1.([]byte)), string(b2.([]byte)))
    48  
    49  	// Verify it can also scan as a string
    50  	err = sub2.Options.Scan(string(b1.([]byte)))
    51  	assert.NoError(t, err)
    52  
    53  	// Out of luck with anything else
    54  	err = sub2.Options.Scan(false)
    55  	assert.Regexp(t, "FF10125", err)
    56  
    57  }