github.com/NpoolPlatform/chain-middleware@v0.0.0-20240228100535-eb1bcf896eb9/pkg/client/fiat/client_test.go (about)

     1  package fiat
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"os"
     7  	"strconv"
     8  	"testing"
     9  
    10  	"github.com/NpoolPlatform/chain-middleware/pkg/testinit"
    11  
    12  	"github.com/NpoolPlatform/go-service-framework/pkg/config"
    13  
    14  	"bou.ke/monkey"
    15  	grpc2 "github.com/NpoolPlatform/go-service-framework/pkg/grpc"
    16  	"google.golang.org/grpc"
    17  	"google.golang.org/grpc/credentials/insecure"
    18  
    19  	basetypes "github.com/NpoolPlatform/message/npool/basetypes/v1"
    20  	npool "github.com/NpoolPlatform/message/npool/chain/mw/v1/fiat"
    21  	"github.com/stretchr/testify/assert"
    22  
    23  	cruder "github.com/NpoolPlatform/libent-cruder/pkg/cruder"
    24  
    25  	"github.com/google/uuid"
    26  )
    27  
    28  func init() {
    29  	if runByGithubAction, err := strconv.ParseBool(os.Getenv("RUN_BY_GITHUB_ACTION")); err == nil && runByGithubAction {
    30  		return
    31  	}
    32  	if err := testinit.Init(); err != nil {
    33  		fmt.Printf("cannot init test stub: %v\n", err)
    34  	}
    35  }
    36  
    37  var ret = &npool.Fiat{
    38  	Name: uuid.NewString(),
    39  	Logo: uuid.NewString(),
    40  	Unit: "USD",
    41  }
    42  
    43  var req = &npool.FiatReq{
    44  	Name: &ret.Name,
    45  	Logo: &ret.Logo,
    46  	Unit: &ret.Unit,
    47  }
    48  
    49  func createFiat(t *testing.T) {
    50  	info, err := CreateFiat(context.Background(), req)
    51  	if assert.Nil(t, err) {
    52  		ret.CreatedAt = info.CreatedAt
    53  		ret.UpdatedAt = info.UpdatedAt
    54  		ret.ID = info.ID
    55  		ret.EntID = info.EntID
    56  		assert.Equal(t, ret, info)
    57  	}
    58  }
    59  
    60  func updateFiat(t *testing.T) {
    61  	ret.Logo = uuid.NewString()
    62  	req.ID = &ret.ID
    63  	req.Logo = &ret.Logo
    64  
    65  	info, err := UpdateFiat(context.Background(), req)
    66  	if assert.Nil(t, err) {
    67  		ret.UpdatedAt = info.UpdatedAt
    68  		assert.Equal(t, info, ret)
    69  	}
    70  }
    71  
    72  func getFiat(t *testing.T) {
    73  	info, err := GetFiat(context.Background(), ret.EntID)
    74  	if assert.Nil(t, err) {
    75  		assert.Equal(t, info, ret)
    76  	}
    77  }
    78  
    79  func getFiats(t *testing.T) {
    80  	infos, total, err := GetFiats(context.Background(), &npool.Conds{
    81  		EntID: &basetypes.StringVal{Op: cruder.EQ, Value: ret.EntID},
    82  	}, 0, 2)
    83  	if assert.Nil(t, err) {
    84  		assert.Equal(t, len(infos), 1)
    85  		assert.Equal(t, total, uint32(1))
    86  		assert.Equal(t, infos[0], ret)
    87  	}
    88  }
    89  
    90  func TestClient(t *testing.T) {
    91  	if runByGithubAction, err := strconv.ParseBool(os.Getenv("RUN_BY_GITHUB_ACTION")); err == nil && runByGithubAction {
    92  		return
    93  	}
    94  	// Here won't pass test due to we always test with localhost
    95  
    96  	gport := config.GetIntValueWithNameSpace("", config.KeyGRPCPort)
    97  
    98  	monkey.Patch(grpc2.GetGRPCConn, func(service string, tags ...string) (*grpc.ClientConn, error) {
    99  		return grpc.Dial(fmt.Sprintf("localhost:%v", gport), grpc.WithTransportCredentials(insecure.NewCredentials()))
   100  	})
   101  	monkey.Patch(grpc2.GetGRPCConnV1, func(service string, recvMsgBytes int, tags ...string) (*grpc.ClientConn, error) {
   102  		return grpc.Dial(fmt.Sprintf("localhost:%v", gport), grpc.WithTransportCredentials(insecure.NewCredentials()))
   103  	})
   104  
   105  	t.Run("createFiat", createFiat)
   106  	t.Run("updateFiat", updateFiat)
   107  	t.Run("getFiat", getFiat)
   108  	t.Run("getFiats", getFiats)
   109  }