github.com/stellar/stellar-etl@v1.0.1-0.20240312145900-4874b6bf2b89/internal/transform/contract_code_test.go (about)

     1  package transform
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  
    10  	"github.com/stellar/go/ingest"
    11  	"github.com/stellar/go/xdr"
    12  )
    13  
    14  func TestTransformContractCode(t *testing.T) {
    15  	type transformTest struct {
    16  		input      ingest.Change
    17  		wantOutput ContractCodeOutput
    18  		wantErr    error
    19  	}
    20  
    21  	hardCodedInput := makeContractCodeTestInput()
    22  	hardCodedOutput := makeContractCodeTestOutput()
    23  	tests := []transformTest{
    24  		{
    25  			ingest.Change{
    26  				Type: xdr.LedgerEntryTypeOffer,
    27  				Pre:  nil,
    28  				Post: &xdr.LedgerEntry{
    29  					Data: xdr.LedgerEntryData{
    30  						Type: xdr.LedgerEntryTypeOffer,
    31  					},
    32  				},
    33  			},
    34  			ContractCodeOutput{}, fmt.Errorf("Could not extract contract code from ledger entry; actual type is LedgerEntryTypeOffer"),
    35  		},
    36  	}
    37  
    38  	for i := range hardCodedInput {
    39  		tests = append(tests, transformTest{
    40  			input:      hardCodedInput[i],
    41  			wantOutput: hardCodedOutput[i],
    42  			wantErr:    nil,
    43  		})
    44  	}
    45  
    46  	for _, test := range tests {
    47  		header := xdr.LedgerHeaderHistoryEntry{
    48  			Header: xdr.LedgerHeader{
    49  				ScpValue: xdr.StellarValue{
    50  					CloseTime: 1000,
    51  				},
    52  				LedgerSeq: 10,
    53  			},
    54  		}
    55  		actualOutput, actualError := TransformContractCode(test.input, header)
    56  		assert.Equal(t, test.wantErr, actualError)
    57  		assert.Equal(t, test.wantOutput, actualOutput)
    58  	}
    59  }
    60  
    61  func makeContractCodeTestInput() []ingest.Change {
    62  	var hash [32]byte
    63  
    64  	contractCodeLedgerEntry := xdr.LedgerEntry{
    65  		LastModifiedLedgerSeq: 24229503,
    66  		Data: xdr.LedgerEntryData{
    67  			Type: xdr.LedgerEntryTypeContractCode,
    68  			ContractCode: &xdr.ContractCodeEntry{
    69  				Hash: hash,
    70  				Ext: xdr.ExtensionPoint{
    71  					V: 1,
    72  				},
    73  			},
    74  		},
    75  	}
    76  
    77  	return []ingest.Change{
    78  		{
    79  			Type: xdr.LedgerEntryTypeContractCode,
    80  			Pre:  &xdr.LedgerEntry{},
    81  			Post: &contractCodeLedgerEntry,
    82  		},
    83  	}
    84  }
    85  
    86  func makeContractCodeTestOutput() []ContractCodeOutput {
    87  	return []ContractCodeOutput{
    88  		{
    89  			ContractCodeHash:   "0000000000000000000000000000000000000000000000000000000000000000",
    90  			ContractCodeExtV:   1,
    91  			LastModifiedLedger: 24229503,
    92  			LedgerEntryChange:  1,
    93  			Deleted:            false,
    94  			LedgerSequence:     10,
    95  			ClosedAt:           time.Date(1970, time.January, 1, 0, 16, 40, 0, time.UTC),
    96  			LedgerKeyHash:      "dfed061dbe464e0ff320744fcd604ac08b39daa74fa24110936654cbcb915ccc",
    97  		},
    98  	}
    99  }