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

     1  package fiat
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"os"
     7  	"strconv"
     8  	"testing"
     9  
    10  	testinit "github.com/NpoolPlatform/chain-middleware/pkg/testinit"
    11  	npool "github.com/NpoolPlatform/message/npool/chain/mw/v1/fiat"
    12  
    13  	"github.com/google/uuid"
    14  	"github.com/stretchr/testify/assert"
    15  )
    16  
    17  func init() {
    18  	if runByGithubAction, err := strconv.ParseBool(os.Getenv("RUN_BY_GITHUB_ACTION")); err == nil && runByGithubAction {
    19  		return
    20  	}
    21  	if err := testinit.Init(); err != nil {
    22  		fmt.Printf("cannot init test stub: %v\n", err)
    23  	}
    24  }
    25  
    26  var ret = &npool.Fiat{
    27  	Name: uuid.NewString(),
    28  	Logo: uuid.NewString(),
    29  	Unit: "USD",
    30  }
    31  
    32  var req = &npool.FiatReq{
    33  	Name: &ret.Name,
    34  	Logo: &ret.Logo,
    35  	Unit: &ret.Unit,
    36  }
    37  
    38  func setupFiat(t *testing.T) func(*testing.T) {
    39  	return func(*testing.T) {}
    40  }
    41  
    42  func create(t *testing.T) {
    43  	h1, err := NewHandler(
    44  		context.Background(),
    45  		WithName(&ret.Name, true),
    46  		WithLogo(&ret.Logo, true),
    47  		WithUnit(&ret.Unit, true),
    48  	)
    49  	assert.Nil(t, err)
    50  
    51  	info, err := h1.CreateFiat(context.Background())
    52  	if assert.Nil(t, err) {
    53  		ret.UpdatedAt = info.UpdatedAt
    54  		ret.CreatedAt = info.CreatedAt
    55  		ret.ID = info.ID
    56  		ret.EntID = info.EntID
    57  		assert.Equal(t, info.String(), ret.String())
    58  	}
    59  }
    60  
    61  func update(t *testing.T) {
    62  	ret.Logo = "ABCED"
    63  	ret.Name = uuid.NewString()
    64  
    65  	req.Logo = &ret.Logo
    66  	req.Name = &ret.Name
    67  
    68  	h1, err := NewHandler(
    69  		context.Background(),
    70  		WithID(&ret.ID, true),
    71  		WithName(req.Name, true),
    72  		WithUnit(req.Unit, true),
    73  		WithLogo(req.Logo, true),
    74  	)
    75  	assert.Nil(t, err)
    76  
    77  	info, err := h1.UpdateFiat(context.Background())
    78  	if assert.Nil(t, err) {
    79  		ret.UpdatedAt = info.UpdatedAt
    80  		assert.Equal(t, info.String(), ret.String())
    81  	}
    82  }
    83  
    84  func TestFiat(t *testing.T) {
    85  	if runByGithubAction, err := strconv.ParseBool(os.Getenv("RUN_BY_GITHUB_ACTION")); err == nil && runByGithubAction {
    86  		return
    87  	}
    88  
    89  	teardown := setupFiat(t)
    90  	defer teardown(t)
    91  
    92  	t.Run("create", create)
    93  	t.Run("update", update)
    94  }