github.com/NpoolPlatform/chain-middleware@v0.0.0-20240228100535-eb1bcf896eb9/pkg/client/coin/usedfor/client_test.go (about) 1 package coinusedfor 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 coin1 "github.com/NpoolPlatform/chain-middleware/pkg/mw/coin" 20 types "github.com/NpoolPlatform/message/npool/basetypes/chain/v1" 21 basetypes "github.com/NpoolPlatform/message/npool/basetypes/v1" 22 npool "github.com/NpoolPlatform/message/npool/chain/mw/v1/coin/usedfor" 23 "github.com/stretchr/testify/assert" 24 25 cruder "github.com/NpoolPlatform/libent-cruder/pkg/cruder" 26 27 "github.com/google/uuid" 28 ) 29 30 func init() { 31 if runByGithubAction, err := strconv.ParseBool(os.Getenv("RUN_BY_GITHUB_ACTION")); err == nil && runByGithubAction { 32 return 33 } 34 if err := testinit.Init(); err != nil { 35 fmt.Printf("cannot init test stub: %v\n", err) 36 } 37 } 38 39 var ret = &npool.CoinUsedFor{ 40 CoinName: uuid.NewString(), 41 CoinUnit: uuid.NewString(), 42 CoinENV: "test", 43 UsedFor: types.CoinUsedFor_CoinUsedForCouponCash, 44 UsedForStr: types.CoinUsedFor_CoinUsedForCouponCash.String(), 45 Priority: 1, 46 } 47 48 var req = &npool.CoinUsedForReq{ 49 UsedFor: &ret.UsedFor, 50 } 51 52 func setupCoinUsedFor(t *testing.T) func(*testing.T) { 53 ret.CoinTypeID = uuid.NewString() 54 req.CoinTypeID = &ret.CoinTypeID 55 56 h1, err := coin1.NewHandler( 57 context.Background(), 58 coin1.WithEntID(&ret.CoinTypeID, true), 59 coin1.WithName(&ret.CoinName, true), 60 coin1.WithUnit(&ret.CoinUnit, true), 61 coin1.WithLogo(&ret.CoinLogo, true), 62 coin1.WithENV(&ret.CoinENV, true), 63 ) 64 assert.Nil(t, err) 65 66 _, err = h1.CreateCoin(context.Background()) 67 assert.Nil(t, err) 68 69 return func(*testing.T) { 70 _, _ = h1.DeleteCoin(context.Background()) 71 } 72 } 73 74 func createCoinUsedFor(t *testing.T) { 75 info, err := CreateCoinUsedFor(context.Background(), req) 76 fmt.Println(err) 77 if assert.Nil(t, err) { 78 ret.CreatedAt = info.CreatedAt 79 ret.UpdatedAt = info.UpdatedAt 80 ret.ID = info.ID 81 ret.EntID = info.EntID 82 assert.Equal(t, ret, info) 83 } 84 } 85 86 func getCoinUsedFors(t *testing.T) { 87 infos, total, err := GetCoinUsedFors(context.Background(), &npool.Conds{ 88 CoinTypeID: &basetypes.StringVal{Op: cruder.EQ, Value: ret.CoinTypeID}, 89 }, 0, 100) 90 if assert.Nil(t, err) { 91 assert.Equal(t, 1, len(infos)) 92 assert.Equal(t, uint32(1), total) 93 assert.Equal(t, ret, infos[0]) 94 } 95 } 96 97 func deleteCoinUsedFor(t *testing.T) { 98 info, err := DeleteCoinUsedFor(context.Background(), ret.ID) 99 if assert.Nil(t, err) { 100 assert.Equal(t, ret, info) 101 } 102 } 103 104 func TestClient(t *testing.T) { 105 if runByGithubAction, err := strconv.ParseBool(os.Getenv("RUN_BY_GITHUB_ACTION")); err == nil && runByGithubAction { 106 return 107 } 108 // Here won't pass test due to we always test with localhost 109 110 teardown := setupCoinUsedFor(t) 111 defer teardown(t) 112 113 gport := config.GetIntValueWithNameSpace("", config.KeyGRPCPort) 114 115 monkey.Patch(grpc2.GetGRPCConn, func(service string, tags ...string) (*grpc.ClientConn, error) { 116 return grpc.Dial(fmt.Sprintf("localhost:%v", gport), grpc.WithTransportCredentials(insecure.NewCredentials())) 117 }) 118 monkey.Patch(grpc2.GetGRPCConnV1, func(service string, recvMsgBytes int, tags ...string) (*grpc.ClientConn, error) { 119 return grpc.Dial(fmt.Sprintf("localhost:%v", gport), grpc.WithTransportCredentials(insecure.NewCredentials())) 120 }) 121 122 t.Run("createCoinUsedFor", createCoinUsedFor) 123 t.Run("getCoinUsedFors", getCoinUsedFors) 124 t.Run("deleteCoinUsedFor", deleteCoinUsedFor) 125 }