Skip to content

Commit

Permalink
Merge tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
tolikzinovyev committed Nov 4, 2021
1 parent eda1a3a commit 9e30c96
Showing 1 changed file with 97 additions and 112 deletions.
209 changes: 97 additions & 112 deletions idb/postgres/internal/writer/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}

Expand Down

0 comments on commit 9e30c96

Please sign in to comment.