github.com/stellar/stellar-etl@v1.0.1-0.20240312145900-4874b6bf2b89/internal/transform/asset_test.go (about) 1 package transform 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 9 "github.com/stellar/go/ingest" 10 "github.com/stellar/go/xdr" 11 ) 12 13 func TestTransformAsset(t *testing.T) { 14 15 type assetInput struct { 16 operation xdr.Operation 17 index int32 18 txnIndex int32 19 // transaction xdr.TransactionEnvelope 20 } 21 22 type transformTest struct { 23 input assetInput 24 wantOutput AssetOutput 25 wantErr error 26 } 27 28 nonPaymentInput := assetInput{ 29 operation: genericBumpOperation, 30 txnIndex: 0, 31 index: 0, 32 } 33 34 tests := []transformTest{ 35 { 36 input: nonPaymentInput, 37 wantOutput: AssetOutput{}, 38 wantErr: fmt.Errorf("operation of type 11 cannot issue an asset (id 0)"), 39 }, 40 } 41 42 hardCodedInputTransaction, err := makeAssetTestInput() 43 assert.NoError(t, err) 44 hardCodedOutputArray := makeAssetTestOutput() 45 46 for i, op := range hardCodedInputTransaction.Envelope.Operations() { 47 tests = append(tests, transformTest{ 48 input: assetInput{ 49 operation: op, 50 index: int32(i), 51 txnIndex: int32(i)}, 52 wantOutput: hardCodedOutputArray[i], 53 wantErr: nil, 54 }) 55 } 56 57 for _, test := range tests { 58 actualOutput, actualError := TransformAsset(test.input.operation, test.input.index, test.input.txnIndex, 0) 59 assert.Equal(t, test.wantErr, actualError) 60 assert.Equal(t, test.wantOutput, actualOutput) 61 } 62 } 63 64 func makeAssetTestInput() (inputTransaction ingest.LedgerTransaction, err error) { 65 inputTransaction = genericLedgerTransaction 66 inputEnvelope := genericBumpOperationEnvelope 67 68 inputEnvelope.Tx.SourceAccount = testAccount1 69 70 inputOperations := []xdr.Operation{ 71 xdr.Operation{ 72 SourceAccount: nil, 73 Body: xdr.OperationBody{ 74 Type: xdr.OperationTypePayment, 75 PaymentOp: &xdr.PaymentOp{ 76 Destination: testAccount2, 77 Asset: usdtAsset, 78 Amount: 350000000, 79 }, 80 }, 81 }, 82 xdr.Operation{ 83 SourceAccount: nil, 84 Body: xdr.OperationBody{ 85 Type: xdr.OperationTypePayment, 86 PaymentOp: &xdr.PaymentOp{ 87 Destination: testAccount3, 88 Asset: nativeAsset, 89 Amount: 350000000, 90 }, 91 }, 92 }, 93 } 94 95 inputEnvelope.Tx.Operations = inputOperations 96 inputTransaction.Envelope.V1 = &inputEnvelope 97 return 98 } 99 100 func makeAssetTestOutput() (transformedAssets []AssetOutput) { 101 transformedAssets = []AssetOutput{ 102 AssetOutput{ 103 AssetCode: "USDT", 104 AssetIssuer: "GBVVRXLMNCJQW3IDDXC3X6XCH35B5Q7QXNMMFPENSOGUPQO7WO7HGZPA", 105 AssetType: "credit_alphanum4", 106 AssetID: 1229977787683536144, 107 108 ID: -8205667356306085451, 109 }, 110 AssetOutput{ 111 AssetCode: "", 112 AssetIssuer: "", 113 AssetType: "native", 114 AssetID: 12638146518625398189, 115 ID: -5706705804583548011, 116 }, 117 } 118 return 119 }