github.com/stellar/stellar-etl@v1.0.1-0.20240312145900-4874b6bf2b89/internal/transform/account_test.go (about) 1 package transform 2 3 import ( 4 "fmt" 5 "testing" 6 "time" 7 8 "github.com/guregu/null" 9 "github.com/stretchr/testify/assert" 10 11 "github.com/stellar/go/ingest" 12 "github.com/stellar/go/xdr" 13 ) 14 15 func TestTransformAccount(t *testing.T) { 16 type inputStruct struct { 17 ledgerChange ingest.Change 18 } 19 20 type transformTest struct { 21 input inputStruct 22 wantOutput AccountOutput 23 wantErr error 24 } 25 26 hardCodedInput := makeAccountTestInput() 27 hardCodedOutput := makeAccountTestOutput() 28 29 tests := []transformTest{ 30 { 31 inputStruct{ingest.Change{ 32 Type: xdr.LedgerEntryTypeOffer, 33 Pre: nil, 34 Post: &xdr.LedgerEntry{ 35 Data: xdr.LedgerEntryData{ 36 Type: xdr.LedgerEntryTypeOffer, 37 }, 38 }, 39 }, 40 }, 41 AccountOutput{}, fmt.Errorf("Could not extract account data from ledger entry; actual type is LedgerEntryTypeOffer"), 42 }, 43 { 44 inputStruct{wrapAccountEntry(xdr.AccountEntry{ 45 AccountId: genericAccountID, 46 Balance: -1, 47 }, 0), 48 }, 49 AccountOutput{}, fmt.Errorf("Balance is negative (-1) for account: %s", genericAccountAddress), 50 }, 51 { 52 inputStruct{wrapAccountEntry(xdr.AccountEntry{ 53 AccountId: genericAccountID, 54 Ext: xdr.AccountEntryExt{ 55 V: 1, 56 V1: &xdr.AccountEntryExtensionV1{ 57 Liabilities: xdr.Liabilities{ 58 Buying: -1, 59 }, 60 }, 61 }, 62 }, 0), 63 }, 64 AccountOutput{}, fmt.Errorf("The buying liabilities count is negative (-1) for account: %s", genericAccountAddress), 65 }, 66 { 67 inputStruct{wrapAccountEntry(xdr.AccountEntry{ 68 AccountId: genericAccountID, 69 Ext: xdr.AccountEntryExt{ 70 V: 1, 71 V1: &xdr.AccountEntryExtensionV1{ 72 Liabilities: xdr.Liabilities{ 73 Selling: -2, 74 }, 75 }, 76 }, 77 }, 0), 78 }, 79 AccountOutput{}, fmt.Errorf("The selling liabilities count is negative (-2) for account: %s", genericAccountAddress), 80 }, 81 { 82 inputStruct{wrapAccountEntry(xdr.AccountEntry{ 83 AccountId: genericAccountID, 84 SeqNum: -3, 85 }, 0), 86 }, 87 AccountOutput{}, fmt.Errorf("Account sequence number is negative (-3) for account: %s", genericAccountAddress), 88 }, 89 { 90 inputStruct{ 91 hardCodedInput, 92 }, 93 hardCodedOutput, nil, 94 }, 95 } 96 97 for _, test := range tests { 98 header := xdr.LedgerHeaderHistoryEntry{ 99 Header: xdr.LedgerHeader{ 100 ScpValue: xdr.StellarValue{ 101 CloseTime: 1000, 102 }, 103 LedgerSeq: 10, 104 }, 105 } 106 actualOutput, actualError := TransformAccount(test.input.ledgerChange, header) 107 assert.Equal(t, test.wantErr, actualError) 108 assert.Equal(t, test.wantOutput, actualOutput) 109 } 110 } 111 112 func wrapAccountEntry(accountEntry xdr.AccountEntry, lastModified int) ingest.Change { 113 return ingest.Change{ 114 Type: xdr.LedgerEntryTypeAccount, 115 Pre: &xdr.LedgerEntry{ 116 LastModifiedLedgerSeq: xdr.Uint32(lastModified), 117 Data: xdr.LedgerEntryData{ 118 Type: xdr.LedgerEntryTypeAccount, 119 Account: &accountEntry, 120 }, 121 }, 122 } 123 } 124 125 func makeAccountTestInput() ingest.Change { 126 127 ledgerEntry := xdr.LedgerEntry{ 128 LastModifiedLedgerSeq: 30705278, 129 Data: xdr.LedgerEntryData{ 130 Type: xdr.LedgerEntryTypeAccount, 131 Account: &xdr.AccountEntry{ 132 AccountId: testAccount1ID, 133 Balance: 10959979, 134 SeqNum: 117801117454198833, 135 NumSubEntries: 141, 136 InflationDest: &testAccount2ID, 137 Flags: 4, 138 HomeDomain: "examplehome.com", 139 Thresholds: xdr.Thresholds([4]byte{2, 1, 3, 5}), 140 Ext: xdr.AccountEntryExt{ 141 V: 1, 142 V1: &xdr.AccountEntryExtensionV1{ 143 Liabilities: xdr.Liabilities{ 144 Buying: 1000, 145 Selling: 1500, 146 }, 147 Ext: xdr.AccountEntryExtensionV1Ext{ 148 V: 2, 149 V2: &xdr.AccountEntryExtensionV2{ 150 NumSponsored: 3, 151 NumSponsoring: 1, 152 }, 153 }, 154 }, 155 }, 156 }, 157 }, 158 Ext: xdr.LedgerEntryExt{ 159 V: 1, 160 V1: &xdr.LedgerEntryExtensionV1{ 161 SponsoringId: &testAccount3ID, 162 }, 163 }, 164 } 165 return ingest.Change{ 166 Type: xdr.LedgerEntryTypeAccount, 167 Pre: &ledgerEntry, 168 Post: nil, 169 } 170 } 171 172 func makeAccountTestOutput() AccountOutput { 173 return AccountOutput{ 174 AccountID: testAccount1Address, 175 Balance: 1.0959979, 176 BuyingLiabilities: 0.0001, 177 SellingLiabilities: 0.00015, 178 SequenceNumber: 117801117454198833, 179 NumSubentries: 141, 180 InflationDestination: testAccount2Address, 181 Flags: 4, 182 HomeDomain: "examplehome.com", 183 MasterWeight: 2, 184 ThresholdLow: 1, 185 ThresholdMedium: 3, 186 ThresholdHigh: 5, 187 Sponsor: null.StringFrom(testAccount3Address), 188 NumSponsored: 3, 189 NumSponsoring: 1, 190 LastModifiedLedger: 30705278, 191 LedgerEntryChange: 2, 192 Deleted: true, 193 LedgerSequence: 10, 194 ClosedAt: time.Date(1970, time.January, 1, 0, 16, 40, 0, time.UTC), 195 } 196 }