From 9e30c9621e8411185b6f846ec3824555feb627d5 Mon Sep 17 00:00:00 2001 From: Tolik Zinovyev Date: Wed, 3 Nov 2021 23:03:59 -0400 Subject: [PATCH] Merge tests. --- idb/postgres/internal/writer/writer_test.go | 209 +++++++++----------- 1 file changed, 97 insertions(+), 112 deletions(-) diff --git a/idb/postgres/internal/writer/writer_test.go b/idb/postgres/internal/writer/writer_test.go index 51b7c1d8d..34630aa96 100644 --- a/idb/postgres/internal/writer/writer_test.go +++ b/idb/postgres/internal/writer/writer_test.go @@ -341,136 +341,121 @@ func TestWriterTxnTableAssetCloseAmount(t *testing.T) { assert.NoError(t, rows.Err()) } -func TestWriterTxnParticipationTableBasic(t *testing.T) { - db, shutdownFunc := setupPostgres(t) - defer shutdownFunc() - - block := bookkeeping.Block{ - BlockHeader: bookkeeping.BlockHeader{ - Round: basics.Round(2), - GenesisID: test.MakeGenesis().ID(), - GenesisHash: test.GenesisHash, - UpgradeState: bookkeeping.UpgradeState{ - CurrentProtocol: test.Proto, +func TestWriterTxnParticipationTable(t *testing.T) { + type testtype struct { + name string + payset transactions.Payset + expected []txnParticipationRow + } + + makeBlockFunc := func() bookkeeping.Block { + return bookkeeping.Block{ + BlockHeader: bookkeeping.BlockHeader{ + Round: basics.Round(2), + GenesisID: test.MakeGenesis().ID(), + GenesisHash: test.GenesisHash, + UpgradeState: bookkeeping.UpgradeState{ + CurrentProtocol: test.Proto, + }, }, - }, - Payset: make(transactions.Payset, 2), + } } - stxnad0 := test.MakePaymentTxn( - 1000, 1, 0, 0, 0, 0, test.AccountA, test.AccountB, basics.Address{}, - basics.Address{}) - var err error - block.Payset[0], err = block.EncodeSignedTxn(stxnad0.SignedTxn, stxnad0.ApplyData) - require.NoError(t, err) - - stxnad1 := test.MakeAssetConfigTxn( - 0, 100, 1, false, "ma", "myasset", "myasset.com", test.AccountC) - block.Payset[1], err = block.EncodeSignedTxn(stxnad1.SignedTxn, stxnad1.ApplyData) - require.NoError(t, err) - - f := func(tx pgx.Tx) error { - w, err := writer.MakeWriter(tx) + var tests []testtype + { + stxnad0 := test.MakePaymentTxn( + 1000, 1, 0, 0, 0, 0, test.AccountA, test.AccountB, basics.Address{}, + basics.Address{}) + stib0, err := makeBlockFunc().EncodeSignedTxn(stxnad0.SignedTxn, stxnad0.ApplyData) require.NoError(t, err) - err = w.AddBlock(&block, block.Payset, ledgercore.StateDelta{}) + stxnad1 := test.MakeAssetConfigTxn( + 0, 100, 1, false, "ma", "myasset", "myasset.com", test.AccountC) + stib1, err := makeBlockFunc().EncodeSignedTxn(stxnad1.SignedTxn, stxnad1.ApplyData) require.NoError(t, err) - w.Close() - return nil - } - err = pgutil.TxWithRetry(db, serializable, f, nil) - require.NoError(t, err) - - results, err := txnParticipationQuery(db, `SELECT * FROM txn_participation ORDER BY round, intra, addr`) - assert.NoError(t, err) - - expected := []txnParticipationRow{ - { - addr: test.AccountA, - round: 2, - intra: 0, - }, - { - addr: test.AccountB, - round: 2, - intra: 0, - }, - { - addr: test.AccountC, - round: 2, - intra: 1, - }, - } - - // Verify expected participation - assert.Len(t, results, len(expected)) - for i := range results { - assert.Equal(t, expected[i], results[i]) + testcase := testtype{ + name: "basic", + payset: []transactions.SignedTxnInBlock{stib0, stib1}, + expected: []txnParticipationRow{ + { + addr: test.AccountA, + round: 2, + intra: 0, + }, + { + addr: test.AccountB, + round: 2, + intra: 0, + }, + { + addr: test.AccountC, + round: 2, + intra: 1, + }, + }, + } + tests = append(tests, testcase) } -} - -func TestWriterTxnParticipationTableAppCallAddresses(t *testing.T) { - db, shutdownFunc := setupPostgres(t) - defer shutdownFunc() + { + stxnad := test.MakeCreateAppTxn(test.AccountA) + stxnad.Txn.ApplicationCallTxnFields.Accounts = + []basics.Address{test.AccountB, test.AccountC} + stib, err := makeBlockFunc().EncodeSignedTxn(stxnad.SignedTxn, stxnad.ApplyData) + require.NoError(t, err) - block := bookkeeping.Block{ - BlockHeader: bookkeeping.BlockHeader{ - Round: basics.Round(2), - GenesisID: test.MakeGenesis().ID(), - GenesisHash: test.GenesisHash, - UpgradeState: bookkeeping.UpgradeState{ - CurrentProtocol: test.Proto, + testcase := testtype{ + name: "app_call_addresses", + payset: []transactions.SignedTxnInBlock{stib}, + expected: []txnParticipationRow{ + { + addr: test.AccountA, + round: 2, + intra: 0, + }, + { + addr: test.AccountB, + round: 2, + intra: 0, + }, + { + addr: test.AccountC, + round: 2, + intra: 0, + }, }, - }, - Payset: make(transactions.Payset, 1), + } + tests = append(tests, testcase) } - stxnad := test.MakeCreateAppTxn(test.AccountA) - stxnad.Txn.ApplicationCallTxnFields.Accounts = - []basics.Address{test.AccountB, test.AccountC} - var err error - block.Payset[0], err = block.EncodeSignedTxn(stxnad.SignedTxn, stxnad.ApplyData) - require.NoError(t, err) + for _, testcase := range tests { + t.Run(testcase.name, func(t *testing.T) { + db, shutdownFunc := setupPostgres(t) + defer shutdownFunc() - f := func(tx pgx.Tx) error { - w, err := writer.MakeWriter(tx) - require.NoError(t, err) + block := makeBlockFunc() + block.Payset = testcase.payset - err = w.AddBlock(&block, block.Payset, ledgercore.StateDelta{}) - require.NoError(t, err) + f := func(tx pgx.Tx) error { + w, err := writer.MakeWriter(tx) + require.NoError(t, err) - w.Close() - return nil - } - err = pgutil.TxWithRetry(db, serializable, f, nil) - require.NoError(t, err) + err = w.AddBlock(&block, block.Payset, ledgercore.StateDelta{}) + require.NoError(t, err) - results, err := txnParticipationQuery(db, `SELECT * FROM txn_participation ORDER BY round, intra, addr`) - assert.NoError(t, err) + w.Close() + return nil + } + err := pgutil.TxWithRetry(db, serializable, f, nil) + require.NoError(t, err) - expected := []txnParticipationRow{ - { - addr: test.AccountA, - round: 2, - intra: 0, - }, - { - addr: test.AccountB, - round: 2, - intra: 0, - }, - { - addr: test.AccountC, - round: 2, - intra: 0, - }, - } + results, err := txnParticipationQuery( + db, `SELECT * FROM txn_participation ORDER BY round, intra, addr`) + assert.NoError(t, err) - // Verify expected participation - assert.Len(t, results, len(expected)) - for i := range results { - assert.Equal(t, expected[i], results[i]) + // Verify expected participation + assert.Equal(t, testcase.expected, results) + }) } }