github.com/stellar/stellar-etl@v1.0.1-0.20240312145900-4874b6bf2b89/internal/transform/ttl_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 TestTransformTtl(t *testing.T) { 15 type transformTest struct { 16 input ingest.Change 17 wantOutput TtlOutput 18 wantErr error 19 } 20 21 hardCodedInput := makeTtlTestInput() 22 hardCodedOutput := makeTtlTestOutput() 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 TtlOutput{}, fmt.Errorf("Could not extract ttl 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 := TransformTtl(test.input, header) 56 assert.Equal(t, test.wantErr, actualError) 57 assert.Equal(t, test.wantOutput, actualOutput) 58 } 59 } 60 61 func makeTtlTestInput() []ingest.Change { 62 var hash xdr.Hash 63 64 preTtlLedgerEntry := xdr.LedgerEntry{ 65 LastModifiedLedgerSeq: 0, 66 Data: xdr.LedgerEntryData{ 67 Type: xdr.LedgerEntryTypeTtl, 68 Ttl: &xdr.TtlEntry{ 69 KeyHash: hash, 70 LiveUntilLedgerSeq: 0, 71 }, 72 }, 73 } 74 75 TtlLedgerEntry := xdr.LedgerEntry{ 76 LastModifiedLedgerSeq: 1, 77 Data: xdr.LedgerEntryData{ 78 Type: xdr.LedgerEntryTypeTtl, 79 Ttl: &xdr.TtlEntry{ 80 KeyHash: hash, 81 LiveUntilLedgerSeq: 123, 82 }, 83 }, 84 } 85 86 return []ingest.Change{ 87 { 88 Type: xdr.LedgerEntryTypeTtl, 89 Pre: &preTtlLedgerEntry, 90 Post: &TtlLedgerEntry, 91 }, 92 } 93 } 94 95 func makeTtlTestOutput() []TtlOutput { 96 return []TtlOutput{ 97 { 98 KeyHash: "0000000000000000000000000000000000000000000000000000000000000000", 99 LiveUntilLedgerSeq: 123, 100 LastModifiedLedger: 1, 101 LedgerEntryChange: 1, 102 Deleted: false, 103 LedgerSequence: 10, 104 ClosedAt: time.Date(1970, time.January, 1, 0, 16, 40, 0, time.UTC), 105 }, 106 } 107 }