github.com/turingchain2020/turingchain@v1.1.21/types/executor_test.go (about)

     1  // Copyright Turing Corp. 2018 All Rights Reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package types
     6  
     7  import (
     8  	"encoding/json"
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestLoadExecutorType(t *testing.T) {
    15  	NewTuringchainConfig(GetDefaultCfgstring())
    16  	exec := LoadExecutorType("manage")
    17  	assert.NotEqual(t, exec, nil)
    18  	assert.Equal(t, exec.GetName(), "manage")
    19  
    20  	exec = LoadExecutorType("coins")
    21  	assert.NotEqual(t, exec, nil)
    22  	assert.Equal(t, exec.GetName(), "coins")
    23  
    24  	exec = LoadExecutorType("xxxx")
    25  	assert.Equal(t, exec, nil)
    26  }
    27  
    28  func TestFormatTx(t *testing.T) {
    29  	cfg := NewTuringchainConfig(GetDefaultCfgstring())
    30  	tx := &Transaction{
    31  		Payload: []byte("this is  a test."),
    32  	}
    33  	tx, err := FormatTx(cfg, "user.p.none", tx)
    34  	assert.Equal(t, err, nil)
    35  	assert.Equal(t, tx.Execer, []byte("user.p.none"))
    36  	fee, _ := tx.GetRealFee(cfg.GetMinTxFeeRate())
    37  	assert.Equal(t, tx.Fee, fee)
    38  }
    39  
    40  func TestFormatTxEncode(t *testing.T) {
    41  	cfg := NewTuringchainConfig(GetDefaultCfgstring())
    42  	data, err := FormatTxEncode(cfg, "coins", &Transaction{
    43  		Payload: []byte("this is  a test."),
    44  	})
    45  	assert.Equal(t, err, nil)
    46  	var tx Transaction
    47  	err = Decode(data, &tx)
    48  	assert.Equal(t, err, nil)
    49  	assert.Equal(t, tx.Execer, []byte("coins"))
    50  }
    51  
    52  func TestCallCreateTxJSON(t *testing.T) {
    53  	cfg := NewTuringchainConfig(GetDefaultCfgstring())
    54  	modify := &ModifyConfig{
    55  		Key:   "token-finisher",
    56  		Value: "xxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    57  		Op:    "add",
    58  		Addr:  "",
    59  	}
    60  	data, err := json.Marshal(modify)
    61  	assert.Equal(t, err, nil)
    62  
    63  	result, err := CallCreateTxJSON(cfg, "manage", "Modify", data)
    64  	assert.Equal(t, err, nil)
    65  	assert.NotEqual(t, result, nil)
    66  	var tx Transaction
    67  	err = Decode(result, &tx)
    68  	assert.Equal(t, err, nil)
    69  	assert.Equal(t, tx.Execer, []byte("manage"))
    70  	fee, _ := tx.GetRealFee(cfg.GetMinTxFeeRate())
    71  	assert.Equal(t, tx.Fee, fee)
    72  
    73  	_, err = CallCreateTxJSON(cfg, "coins", "Modify", data)
    74  	assert.NotEqual(t, err, nil)
    75  
    76  	_, err = CallCreateTxJSON(cfg, "xxxx", "xxx", data)
    77  	assert.NotEqual(t, err, nil)
    78  
    79  	modify = &ModifyConfig{
    80  		Key:   "token-finisher",
    81  		Value: "xxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    82  		Op:    "delete",
    83  		Addr:  "",
    84  	}
    85  	data, err = json.Marshal(modify)
    86  	assert.Equal(t, err, nil)
    87  
    88  	result, err = CallCreateTxJSON(cfg, "manage", "Modify", data)
    89  	assert.Equal(t, err, nil)
    90  	assert.NotEqual(t, result, nil)
    91  	err = Decode(result, &tx)
    92  	assert.Equal(t, err, nil)
    93  	assert.Equal(t, tx.Execer, []byte("manage"))
    94  	fee, _ = tx.GetRealFee(cfg.GetMinTxFeeRate())
    95  	assert.Equal(t, tx.Fee, fee)
    96  
    97  }
    98  
    99  func TestCallCreateTx(t *testing.T) {
   100  	cfg := NewTuringchainConfig(GetDefaultCfgstring())
   101  	modify := &ModifyConfig{
   102  		Key:   "token-finisher",
   103  		Value: "xxxxxxxxxxxxxxxxxxxxxxxxxxxx",
   104  		Op:    "add",
   105  		Addr:  "",
   106  	}
   107  
   108  	result, err := CallCreateTx(cfg, "manage", "Modify", modify)
   109  	assert.Equal(t, err, nil)
   110  	assert.NotEqual(t, result, nil)
   111  	var tx Transaction
   112  	err = Decode(result, &tx)
   113  	assert.Equal(t, err, nil)
   114  	assert.Equal(t, tx.Execer, []byte("manage"))
   115  	fee, _ := tx.GetRealFee(cfg.GetMinTxFeeRate())
   116  	assert.Equal(t, tx.Fee, fee)
   117  
   118  	_, err = CallCreateTx(cfg, "coins", "Modify", modify)
   119  	assert.NotEqual(t, err, nil)
   120  
   121  	_, err = CallCreateTx(cfg, "xxxx", "xxx", modify)
   122  	assert.NotEqual(t, err, nil)
   123  
   124  	modify = &ModifyConfig{
   125  		Key:   "token-finisher",
   126  		Value: "xxxxxxxxxxxxxxxxxxxxxxxxxxxx",
   127  		Op:    "delete",
   128  		Addr:  "",
   129  	}
   130  
   131  	result, err = CallCreateTx(cfg, "manage", "Modify", modify)
   132  	assert.Equal(t, err, nil)
   133  	assert.NotEqual(t, result, nil)
   134  	err = Decode(result, &tx)
   135  	assert.Equal(t, err, nil)
   136  	assert.Equal(t, tx.Execer, []byte("manage"))
   137  	fee, _ = tx.GetRealFee(cfg.GetMinTxFeeRate())
   138  	assert.Equal(t, tx.Fee, fee)
   139  }
   140  
   141  func TestIsAssetsTransfer(t *testing.T) {
   142  	assert.Equal(t, true, IsAssetsTransfer(&AssetsTransfer{}))
   143  	assert.Equal(t, true, IsAssetsTransfer(&AssetsWithdraw{}))
   144  	assert.Equal(t, true, IsAssetsTransfer(&AssetsTransferToExec{}))
   145  	assert.Equal(t, false, IsAssetsTransfer(&Transaction{}))
   146  }