github.com/stellar/stellar-etl@v1.0.1-0.20240312145900-4874b6bf2b89/internal/transform/offer_normalized_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 TestTransformOfferNormalized(t *testing.T) { 14 type testInput struct { 15 change ingest.Change 16 ledger uint32 17 } 18 type transformTest struct { 19 input testInput 20 wantOutput NormalizedOfferOutput 21 wantErr error 22 } 23 24 hardCodedInput, err := makeOfferNormalizedTestInput() 25 assert.NoError(t, err) 26 hardCodedOutput := makeOfferNormalizedTestOutput() 27 28 tests := []transformTest{ 29 { 30 input: testInput{ingest.Change{ 31 Type: xdr.LedgerEntryTypeOffer, 32 Pre: &xdr.LedgerEntry{ 33 LastModifiedLedgerSeq: xdr.Uint32(100), 34 Data: xdr.LedgerEntryData{ 35 Type: xdr.LedgerEntryTypeOffer, 36 Offer: &xdr.OfferEntry{ 37 SellerId: genericAccountID, 38 Price: xdr.Price{ 39 N: 5, 40 D: 34, 41 }, 42 }, 43 }, 44 }, 45 Post: nil, 46 }, 100}, 47 wantOutput: NormalizedOfferOutput{}, 48 wantErr: fmt.Errorf("offer 0 is deleted"), 49 }, 50 { 51 input: testInput{hardCodedInput, 100}, 52 wantOutput: hardCodedOutput, 53 wantErr: nil, 54 }, 55 } 56 57 for _, test := range tests { 58 actualOutput, actualError := TransformOfferNormalized(test.input.change, test.input.ledger) 59 assert.Equal(t, test.wantErr, actualError) 60 assert.Equal(t, test.wantOutput, actualOutput) 61 } 62 } 63 64 func makeOfferNormalizedTestInput() (ledgerChange ingest.Change, err error) { 65 ledgerChange = ingest.Change{ 66 Type: xdr.LedgerEntryTypeOffer, 67 Pre: nil, 68 Post: &xdr.LedgerEntry{ 69 LastModifiedLedgerSeq: xdr.Uint32(30715263), 70 Data: xdr.LedgerEntryData{ 71 Type: xdr.LedgerEntryTypeOffer, 72 Offer: &xdr.OfferEntry{ 73 SellerId: testAccount1ID, 74 OfferId: 260678439, 75 Selling: nativeAsset, 76 Buying: ethAsset, 77 Amount: 2628450327, 78 Price: xdr.Price{ 79 N: 920936891, 80 D: 1790879058, 81 }, 82 Flags: 2, 83 }, 84 }, 85 }, 86 } 87 return 88 } 89 90 func makeOfferNormalizedTestOutput() NormalizedOfferOutput { 91 var dimOfferID, marketID, accountID uint64 92 dimOfferID = 16030420496366177311 93 marketID = 10357275879248593505 94 accountID = 4268167189990212240 95 return NormalizedOfferOutput{ 96 Market: DimMarket{ 97 ID: marketID, 98 BaseCode: "ETH", 99 BaseIssuer: testAccount3Address, 100 CounterCode: "native", 101 CounterIssuer: "", 102 }, 103 Offer: DimOffer{ 104 HorizonID: 260678439, 105 DimOfferID: dimOfferID, 106 MarketID: marketID, 107 MakerID: accountID, 108 Action: "b", 109 BaseAmount: 262.8450327, 110 CounterAmount: 135.16473161502083, 111 Price: 0.5142373444404865, 112 }, 113 Account: DimAccount{ 114 Address: testAccount1Address, 115 ID: accountID, 116 }, 117 Event: FactOfferEvent{ 118 LedgerSeq: 100, 119 OfferInstanceID: dimOfferID, 120 }, 121 } 122 }