github.com/NpoolPlatform/chain-middleware@v0.0.0-20240228100535-eb1bcf896eb9/pkg/mw/app/coin/description/description_test.go (about) 1 package description 2 3 import ( 4 "context" 5 "fmt" 6 "os" 7 "strconv" 8 "testing" 9 10 testinit "github.com/NpoolPlatform/chain-middleware/pkg/testinit" 11 basetypes "github.com/NpoolPlatform/message/npool/basetypes/v1" 12 npool "github.com/NpoolPlatform/message/npool/chain/mw/v1/app/coin/description" 13 14 appcoin1 "github.com/NpoolPlatform/chain-middleware/pkg/mw/app/coin" 15 coin1 "github.com/NpoolPlatform/chain-middleware/pkg/mw/coin" 16 17 "github.com/google/uuid" 18 "github.com/stretchr/testify/assert" 19 ) 20 21 func init() { 22 if runByGithubAction, err := strconv.ParseBool(os.Getenv("RUN_BY_GITHUB_ACTION")); err == nil && runByGithubAction { 23 return 24 } 25 if err := testinit.Init(); err != nil { 26 fmt.Printf("cannot init test stub: %v\n", err) 27 } 28 } 29 30 var ret = &npool.CoinDescription{ 31 EntID: uuid.NewString(), 32 AppID: uuid.NewString(), 33 CoinLogo: uuid.NewString(), 34 CoinUnit: "BTC", 35 CoinENV: "test", 36 UsedForStr: basetypes.UsedFor_ProductPage.String(), 37 UsedFor: basetypes.UsedFor_ProductPage, 38 Title: uuid.NewString(), 39 Message: uuid.NewString(), 40 } 41 42 var req = &npool.CoinDescriptionReq{ 43 AppID: &ret.AppID, 44 UsedFor: &ret.UsedFor, 45 Title: &ret.Title, 46 Message: &ret.Message, 47 } 48 49 func setupCoin(t *testing.T) func(*testing.T) { 50 ret.CoinTypeID = uuid.NewString() 51 req.CoinTypeID = &ret.CoinTypeID 52 ret.CoinName = uuid.NewString() 53 54 h1, err := coin1.NewHandler( 55 context.Background(), 56 coin1.WithEntID(&ret.CoinTypeID, true), 57 coin1.WithName(&ret.CoinName, true), 58 coin1.WithLogo(&ret.CoinLogo, true), 59 coin1.WithUnit(&ret.CoinUnit, true), 60 coin1.WithENV(&ret.CoinENV, true), 61 ) 62 assert.Nil(t, err) 63 64 info1, err := h1.CreateCoin(context.Background()) 65 assert.Nil(t, err) 66 h1.ID = &info1.ID 67 68 h2, err := appcoin1.NewHandler( 69 context.Background(), 70 appcoin1.WithAppID(&ret.AppID, true), 71 appcoin1.WithCoinTypeID(&ret.CoinTypeID, true), 72 ) 73 assert.Nil(t, err) 74 75 info2, err := h2.CreateCoin(context.Background()) 76 assert.Nil(t, err) 77 h2.ID = &info2.ID 78 79 return func(*testing.T) { 80 _, _ = h1.DeleteCoin(context.Background()) 81 _, _ = h2.DeleteCoin(context.Background()) 82 } 83 } 84 85 func create(t *testing.T) { 86 handler, err := NewHandler( 87 context.Background(), 88 WithEntID(req.EntID, false), 89 WithAppID(req.AppID, true), 90 WithCoinTypeID(req.CoinTypeID, true), 91 WithTitle(req.Title, true), 92 WithMessage(req.Message, true), 93 WithUsedFor(req.UsedFor, true), 94 ) 95 assert.Nil(t, err) 96 97 info, err := handler.CreateCoinDescription(context.Background()) 98 if assert.Nil(t, err) { 99 ret.UpdatedAt = info.UpdatedAt 100 ret.CreatedAt = info.CreatedAt 101 ret.ID = info.ID 102 ret.EntID = info.EntID 103 assert.Equal(t, info, ret) 104 } 105 } 106 107 func update(t *testing.T) { 108 ret.Title = uuid.NewString() 109 ret.Message = uuid.NewString() 110 111 req.Title = &ret.Title 112 req.Message = &ret.Message 113 114 handler, err := NewHandler( 115 context.Background(), 116 WithID(&ret.ID, true), 117 WithTitle(req.Title, true), 118 WithMessage(req.Message, true), 119 ) 120 assert.Nil(t, err) 121 122 info, err := handler.UpdateCoinDescription(context.Background()) 123 if assert.Nil(t, err) { 124 ret.UpdatedAt = info.UpdatedAt 125 assert.Equal(t, info, ret) 126 } 127 } 128 129 func TestCoin(t *testing.T) { 130 if runByGithubAction, err := strconv.ParseBool(os.Getenv("RUN_BY_GITHUB_ACTION")); err == nil && runByGithubAction { 131 return 132 } 133 134 teardown := setupCoin(t) 135 defer teardown(t) 136 137 t.Run("create", create) 138 t.Run("update", update) 139 }