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

     1  package description
     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/app/coin/description"
    21  	"github.com/stretchr/testify/assert"
    22  
    23  	appcoin1 "github.com/NpoolPlatform/chain-middleware/pkg/mw/app/coin"
    24  	coin1 "github.com/NpoolPlatform/chain-middleware/pkg/mw/coin"
    25  
    26  	cruder "github.com/NpoolPlatform/libent-cruder/pkg/cruder"
    27  
    28  	"github.com/google/uuid"
    29  )
    30  
    31  func init() {
    32  	if runByGithubAction, err := strconv.ParseBool(os.Getenv("RUN_BY_GITHUB_ACTION")); err == nil && runByGithubAction {
    33  		return
    34  	}
    35  	if err := testinit.Init(); err != nil {
    36  		fmt.Printf("cannot init test stub: %v\n", err)
    37  	}
    38  }
    39  
    40  var ret = &npool.CoinDescription{
    41  	EntID:      uuid.NewString(),
    42  	AppID:      uuid.NewString(),
    43  	CoinLogo:   uuid.NewString(),
    44  	CoinUnit:   "BTC",
    45  	CoinENV:    "test",
    46  	UsedForStr: basetypes.UsedFor_ProductPage.String(),
    47  	UsedFor:    basetypes.UsedFor_ProductPage,
    48  	Title:      uuid.NewString(),
    49  	Message:    uuid.NewString(),
    50  }
    51  
    52  var req = &npool.CoinDescriptionReq{
    53  	AppID:   &ret.AppID,
    54  	UsedFor: &ret.UsedFor,
    55  	Title:   &ret.Title,
    56  	Message: &ret.Message,
    57  }
    58  
    59  func setupAppCoinDescription(t *testing.T) func(*testing.T) {
    60  	ret.CoinTypeID = uuid.NewString()
    61  	req.CoinTypeID = &ret.CoinTypeID
    62  	ret.CoinName = uuid.NewString()
    63  
    64  	h1, err := coin1.NewHandler(
    65  		context.Background(),
    66  		coin1.WithEntID(&ret.CoinTypeID, true),
    67  		coin1.WithName(&ret.CoinName, true),
    68  		coin1.WithLogo(&ret.CoinLogo, true),
    69  		coin1.WithUnit(&ret.CoinUnit, true),
    70  		coin1.WithENV(&ret.CoinENV, true),
    71  	)
    72  	assert.Nil(t, err)
    73  
    74  	info1, err := h1.CreateCoin(context.Background())
    75  	assert.Nil(t, err)
    76  	h1.ID = &info1.ID
    77  
    78  	h2, err := appcoin1.NewHandler(
    79  		context.Background(),
    80  		appcoin1.WithAppID(&ret.AppID, true),
    81  		appcoin1.WithCoinTypeID(&ret.CoinTypeID, true),
    82  	)
    83  	assert.Nil(t, err)
    84  
    85  	info2, err := h2.CreateCoin(context.Background())
    86  	assert.Nil(t, err)
    87  	h2.ID = &info2.ID
    88  
    89  	return func(*testing.T) {
    90  		_, _ = h1.DeleteCoin(context.Background())
    91  		_, _ = h2.DeleteCoin(context.Background())
    92  	}
    93  }
    94  
    95  func createCoinDescription(t *testing.T) {
    96  	info, err := CreateCoinDescription(context.Background(), req)
    97  	if assert.Nil(t, err) {
    98  		ret.CreatedAt = info.CreatedAt
    99  		ret.UpdatedAt = info.UpdatedAt
   100  		ret.ID = info.ID
   101  		ret.EntID = info.EntID
   102  		assert.Equal(t, ret, info)
   103  	}
   104  }
   105  
   106  func updateCoinDescription(t *testing.T) {
   107  	ret.Title = uuid.NewString()
   108  	ret.Message = uuid.NewString()
   109  
   110  	req.ID = &ret.ID
   111  	req.Title = &ret.Title
   112  	req.Message = &ret.Message
   113  
   114  	info, err := UpdateCoinDescription(context.Background(), req)
   115  	if assert.Nil(t, err) {
   116  		ret.UpdatedAt = info.UpdatedAt
   117  		assert.Equal(t, info, ret)
   118  	}
   119  }
   120  
   121  func getCoinDescription(t *testing.T) {
   122  	info, err := GetCoinDescription(context.Background(), ret.EntID)
   123  	if assert.Nil(t, err) {
   124  		assert.Equal(t, info, ret)
   125  	}
   126  }
   127  
   128  func getCoinDescriptions(t *testing.T) {
   129  	infos, total, err := GetCoinDescriptions(context.Background(), &npool.Conds{
   130  		EntID: &basetypes.StringVal{Op: cruder.EQ, Value: ret.EntID},
   131  	}, 0, 100)
   132  	if assert.Nil(t, err) {
   133  		assert.Equal(t, len(infos), 1)
   134  		assert.Equal(t, total, uint32(1))
   135  		if assert.Equal(t, len(infos), 1) {
   136  			assert.Equal(t, infos[0], ret)
   137  		}
   138  	}
   139  }
   140  
   141  func TestClient(t *testing.T) {
   142  	if runByGithubAction, err := strconv.ParseBool(os.Getenv("RUN_BY_GITHUB_ACTION")); err == nil && runByGithubAction {
   143  		return
   144  	}
   145  	// Here won't pass test due to we always test with localhost
   146  
   147  	teardown := setupAppCoinDescription(t)
   148  	defer teardown(t)
   149  
   150  	gport := config.GetIntValueWithNameSpace("", config.KeyGRPCPort)
   151  
   152  	monkey.Patch(grpc2.GetGRPCConn, func(service string, tags ...string) (*grpc.ClientConn, error) {
   153  		return grpc.Dial(fmt.Sprintf("localhost:%v", gport), grpc.WithTransportCredentials(insecure.NewCredentials()))
   154  	})
   155  	monkey.Patch(grpc2.GetGRPCConnV1, func(service string, recvMsgBytes int, tags ...string) (*grpc.ClientConn, error) {
   156  		return grpc.Dial(fmt.Sprintf("localhost:%v", gport), grpc.WithTransportCredentials(insecure.NewCredentials()))
   157  	})
   158  
   159  	t.Run("createCoinDescription", createCoinDescription)
   160  	t.Run("updateCoinDescription", updateCoinDescription)
   161  	t.Run("getCoinDescription", getCoinDescription)
   162  	t.Run("getCoinDescriptions", getCoinDescriptions)
   163  }