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

     1  package input
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stellar/go/ingest"
     7  	"github.com/stellar/go/ingest/ledgerbackend"
     8  	"github.com/stellar/go/support/log"
     9  	"github.com/stellar/stellar-etl/internal/utils"
    10  	"github.com/stretchr/testify/assert"
    11  
    12  	"github.com/stellar/go/xdr"
    13  )
    14  
    15  func TestSendBatchToChannel(t *testing.T) {
    16  	type functionInput struct {
    17  		entry          ChangeBatch
    18  		changesChannel chan ChangeBatch
    19  	}
    20  	type functionOutput struct {
    21  		entry *ChangeBatch
    22  	}
    23  
    24  	changesChannel := make(chan ChangeBatch)
    25  
    26  	accountTestBatch := wrapLedgerEntry(
    27  		xdr.LedgerEntryTypeAccount,
    28  		xdr.LedgerEntry{
    29  			Data: xdr.LedgerEntryData{
    30  				Type:    xdr.LedgerEntryTypeAccount,
    31  				Account: &xdr.AccountEntry{},
    32  			},
    33  		})
    34  	offerTestBatch := wrapLedgerEntry(
    35  		xdr.LedgerEntryTypeOffer,
    36  		xdr.LedgerEntry{
    37  			Data: xdr.LedgerEntryData{
    38  				Type:  xdr.LedgerEntryTypeOffer,
    39  				Offer: &xdr.OfferEntry{},
    40  			},
    41  		})
    42  	trustTestBatch := wrapLedgerEntry(
    43  		xdr.LedgerEntryTypeTrustline,
    44  		xdr.LedgerEntry{
    45  			Data: xdr.LedgerEntryData{
    46  				Type:      xdr.LedgerEntryTypeTrustline,
    47  				TrustLine: &xdr.TrustLineEntry{},
    48  			},
    49  		})
    50  	poolTestBatch := wrapLedgerEntry(
    51  		xdr.LedgerEntryTypeLiquidityPool,
    52  		xdr.LedgerEntry{
    53  			Data: xdr.LedgerEntryData{
    54  				Type:          xdr.LedgerEntryTypeLiquidityPool,
    55  				LiquidityPool: &xdr.LiquidityPoolEntry{},
    56  			},
    57  		})
    58  
    59  	tests := []struct {
    60  		name string
    61  		args functionInput
    62  		out  functionOutput
    63  	}{
    64  		{
    65  			name: "accounts",
    66  			args: functionInput{
    67  				entry:          accountTestBatch,
    68  				changesChannel: changesChannel,
    69  			},
    70  			out: functionOutput{
    71  				entry: &accountTestBatch,
    72  			},
    73  		},
    74  		{
    75  			name: "offer",
    76  			args: functionInput{
    77  				entry:          offerTestBatch,
    78  				changesChannel: changesChannel,
    79  			},
    80  			out: functionOutput{
    81  				entry: &offerTestBatch,
    82  			},
    83  		},
    84  		{
    85  			name: "trustline",
    86  			args: functionInput{
    87  				entry:          trustTestBatch,
    88  				changesChannel: changesChannel,
    89  			},
    90  			out: functionOutput{
    91  				entry: &trustTestBatch,
    92  			},
    93  		},
    94  		{
    95  			name: "pool",
    96  			args: functionInput{
    97  				entry:          poolTestBatch,
    98  				changesChannel: changesChannel,
    99  			},
   100  			out: functionOutput{
   101  				entry: &poolTestBatch,
   102  			},
   103  		},
   104  	}
   105  	for _, tt := range tests {
   106  		t.Run(tt.name, func(t *testing.T) {
   107  			go func() {
   108  				tt.args.changesChannel <- tt.args.entry
   109  			}()
   110  			read := <-tt.args.changesChannel
   111  			assert.Equal(t, *tt.out.entry, read)
   112  		})
   113  	}
   114  }
   115  
   116  func wrapLedgerEntry(entryType xdr.LedgerEntryType, entry xdr.LedgerEntry) ChangeBatch {
   117  	changes := map[xdr.LedgerEntryType]LedgerChanges{
   118  		entryType: {
   119  			Changes: []ingest.Change{
   120  				{Type: entry.Data.Type, Post: &entry},
   121  			},
   122  			LedgerHeaders: []xdr.LedgerHeaderHistoryEntry{},
   123  		},
   124  	}
   125  	return ChangeBatch{
   126  		Changes: changes,
   127  	}
   128  }
   129  
   130  func mockExtractBatch(
   131  	batchStart, batchEnd uint32,
   132  	core *ledgerbackend.CaptiveStellarCore,
   133  	env utils.EnvironmentDetails, logger *utils.EtlLogger) ChangeBatch {
   134  	log.Errorf("mock called")
   135  	return ChangeBatch{
   136  		Changes:    map[xdr.LedgerEntryType]LedgerChanges{},
   137  		BatchStart: batchStart,
   138  		BatchEnd:   batchEnd,
   139  	}
   140  }
   141  
   142  func TestStreamChangesBatchNumbers(t *testing.T) {
   143  	type batchRange struct {
   144  		batchStart uint32
   145  		batchEnd   uint32
   146  	}
   147  	type input struct {
   148  		batchStart uint32
   149  		batchEnd   uint32
   150  	}
   151  	type output struct {
   152  		batchRanges []batchRange
   153  	}
   154  	tests := []struct {
   155  		name string
   156  		args input
   157  		out  output
   158  	}{
   159  		{
   160  			name: "single",
   161  			args: input{batchStart: 1, batchEnd: 65},
   162  			out: output{
   163  				batchRanges: []batchRange{
   164  					batchRange{
   165  						batchStart: 1, batchEnd: 65,
   166  					},
   167  				},
   168  			},
   169  		}, {
   170  			name: "one extra",
   171  			args: input{batchStart: 1, batchEnd: 66},
   172  			out: output{
   173  				batchRanges: []batchRange{
   174  					batchRange{
   175  						batchStart: 1, batchEnd: 64,
   176  					}, batchRange{
   177  						batchStart: 65, batchEnd: 66,
   178  					},
   179  				},
   180  			},
   181  		}, {
   182  			name: "multiple",
   183  			args: input{batchStart: 1, batchEnd: 128},
   184  			out: output{
   185  				batchRanges: []batchRange{
   186  					batchRange{
   187  						batchStart: 1, batchEnd: 64,
   188  					},
   189  					batchRange{
   190  						batchStart: 65, batchEnd: 128,
   191  					},
   192  				},
   193  			},
   194  		}, {
   195  			name: "partial",
   196  			args: input{batchStart: 1, batchEnd: 32},
   197  			out: output{
   198  				batchRanges: []batchRange{
   199  					batchRange{
   200  						batchStart: 1, batchEnd: 32,
   201  					},
   202  				},
   203  			},
   204  		},
   205  	}
   206  	for _, tt := range tests {
   207  		t.Run(tt.name, func(t *testing.T) {
   208  			batchSize := uint32(64)
   209  			changeChan := make(chan ChangeBatch, 10)
   210  			closeChan := make(chan int)
   211  			env := utils.EnvironmentDetails{
   212  				NetworkPassphrase: "",
   213  				ArchiveURLs:       nil,
   214  				BinaryPath:        "",
   215  				CoreConfig:        "",
   216  			}
   217  			logger := utils.NewEtlLogger()
   218  			ExtractBatch = mockExtractBatch
   219  			go StreamChanges(nil, tt.args.batchStart, tt.args.batchEnd, batchSize, changeChan, closeChan, env, logger)
   220  			var got []batchRange
   221  			for b := range changeChan {
   222  				got = append(got, batchRange{
   223  					b.BatchStart,
   224  					b.BatchEnd,
   225  				})
   226  			}
   227  			assert.Equal(t, tt.out.batchRanges, got)
   228  		})
   229  	}
   230  }