github.com/blend/go-sdk@v1.20220411.3/grpcutil/calculator/server_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package calculator
     9  
    10  import (
    11  	"context"
    12  	"io"
    13  	"testing"
    14  
    15  	"google.golang.org/grpc/metadata"
    16  
    17  	"github.com/blend/go-sdk/assert"
    18  	v1 "github.com/blend/go-sdk/grpcutil/calculator/v1"
    19  )
    20  
    21  func numberStream(values ...float64) *numberStreamServer {
    22  	return &numberStreamServer{
    23  		Input: values,
    24  	}
    25  }
    26  
    27  var (
    28  	_ v1.Calculator_AddStreamServer      = (*numberStreamServer)(nil)
    29  	_ v1.Calculator_SubtractStreamServer = (*numberStreamServer)(nil)
    30  )
    31  
    32  type mockServerStream struct{}
    33  
    34  func (mss mockServerStream) SetHeader(metadata.MD) error  { return nil }
    35  func (mss mockServerStream) SetTrailer(metadata.MD)       {}
    36  func (mss mockServerStream) SendHeader(metadata.MD) error { return nil }
    37  func (mss mockServerStream) Context() context.Context     { return context.Background() }
    38  func (mss mockServerStream) SendMsg(m interface{}) error  { return nil }
    39  func (mss mockServerStream) RecvMsg(m interface{}) error  { return nil }
    40  
    41  type numberStreamServer struct {
    42  	mockServerStream
    43  	Output     float64
    44  	Input      []float64
    45  	InputIndex int
    46  }
    47  
    48  func (nss *numberStreamServer) Recv() (*v1.Number, error) {
    49  	defer func() {
    50  		nss.InputIndex++
    51  	}()
    52  
    53  	if nss.InputIndex < len(nss.Input) {
    54  		return &v1.Number{
    55  			Value: nss.Input[nss.InputIndex],
    56  		}, nil
    57  	}
    58  	return nil, io.EOF
    59  }
    60  
    61  func (nss *numberStreamServer) SendAndClose(value *v1.Number) error {
    62  	nss.Output = value.Value
    63  	return nil
    64  }
    65  
    66  func Test_CalculatorServer_Add(t *testing.T) {
    67  	assert := assert.New(t)
    68  
    69  	res, err := new(Server).Add(context.TODO(), &v1.Numbers{
    70  		Values: []float64{1, 2, 3, 4},
    71  	})
    72  	assert.Nil(err)
    73  	assert.Equal(10, res.Value)
    74  }
    75  
    76  func Test_CalculatorServer_AddStream(t *testing.T) {
    77  	assert := assert.New(t)
    78  
    79  	stream := numberStream(1, 2, 3, 4)
    80  	err := new(Server).AddStream(stream)
    81  	assert.Nil(err)
    82  	assert.Equal(10, stream.Output)
    83  }
    84  
    85  func Test_CalculatorServer_Subtract(t *testing.T) {
    86  	assert := assert.New(t)
    87  
    88  	res, err := new(Server).Subtract(context.TODO(), &v1.Numbers{
    89  		Values: []float64{1, 2, 3, 4},
    90  	})
    91  	assert.Nil(err)
    92  	assert.Equal(-8, res.Value)
    93  }
    94  
    95  func Test_CalculatorServer_SubtractStream(t *testing.T) {
    96  	assert := assert.New(t)
    97  
    98  	stream := numberStream(1, 2, 3, 4)
    99  	err := new(Server).SubtractStream(stream)
   100  	assert.Nil(err)
   101  	assert.Equal(-8, stream.Output)
   102  }
   103  
   104  func Test_CalculatorServer_Multiply(t *testing.T) {
   105  	assert := assert.New(t)
   106  
   107  	res, err := new(Server).Multiply(context.TODO(), &v1.Numbers{
   108  		Values: []float64{1, 2, 3, 4},
   109  	})
   110  	assert.Nil(err)
   111  	assert.Equal(24, res.Value)
   112  }
   113  
   114  func Test_CalculatorServer_MultiplyStream(t *testing.T) {
   115  	assert := assert.New(t)
   116  
   117  	stream := numberStream(1, 2, 3, 4)
   118  	err := new(Server).MultiplyStream(stream)
   119  	assert.Nil(err)
   120  	assert.Equal(24, stream.Output)
   121  }
   122  
   123  func Test_CalculatorServer_Divide(t *testing.T) {
   124  	assert := assert.New(t)
   125  
   126  	res, err := new(Server).Divide(context.TODO(), &v1.Numbers{
   127  		Values: []float64{1, 2, 3, 4},
   128  	})
   129  	assert.Nil(err)
   130  	assert.InDelta(0.04167, res.Value, 0.0001)
   131  }
   132  
   133  func Test_CalculatorServer_DivideStream(t *testing.T) {
   134  	assert := assert.New(t)
   135  
   136  	stream := numberStream(1, 2, 3, 4)
   137  	err := new(Server).DivideStream(stream)
   138  	assert.Nil(err)
   139  	assert.InDelta(0.04167, stream.Output, 0.0001)
   140  }