Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chris124567 committed Jan 12, 2025
1 parent 40f78cb commit de870ee
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 5 deletions.
64 changes: 59 additions & 5 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,25 @@ func TestAPI(t *testing.T) {
t.Fatal(err)
}

// Unconfirmed transaction
txn3 := types.Transaction{
SiacoinInputs: []types.SiacoinInput{{
ParentID: txn1.SiacoinOutputID(0),
UnlockConditions: unlockConditions,
}},
SiacoinOutputs: []types.SiacoinOutput{{
Address: types.VoidAddress,
Value: txn1.SiacoinOutputs[0].Value,
}},
}
testutil.SignTransaction(cm.TipState(), pk1, &txn3)

// Other unconfirmed transctions

Check failure on line 216 in api/api_test.go

View workflow job for this annotation

GitHub Actions / test / test (1.23, ubuntu-latest)

`transctions` is a misspelling of `transitions` (misspell)
txn4 := types.Transaction{}
if _, err := cm.AddPoolTransactions([]types.Transaction{txn3, txn4}); err != nil {
t.Fatal(err)
}

// Ensure explorer has time to add blocks
time.Sleep(2 * time.Second)

Expand Down Expand Up @@ -412,16 +431,51 @@ func TestAPI(t *testing.T) {
testutil.Equal(t, "immature siacoins", types.ZeroCurrency, resp.ImmatureSiacoins)
testutil.Equal(t, "unspent siafunds", txn2.SiafundOutputs[1].Value, resp.UnspentSiafunds)
}},
// There is an issue with JSON unmarshaling of events.
// TODO: fix when explorer.Events are replaced with wallet.Events
{"Events", func(t *testing.T) {
{"AddressEvents", func(t *testing.T) {
resp, err := client.AddressEvents(addr1, 0, 500)
if err != nil {
t.Fatal(err)
}
if len(resp) == 0 {
t.Fatal("no events for addr1")
testutil.Equal(t, "events", 3, len(resp))

ev0 := resp[0].Data.(explorer.EventV1Transaction)
testutil.CheckTransaction(t, txn2, ev0.Transaction)

ev1 := resp[1].Data.(explorer.EventV1Transaction)
testutil.CheckTransaction(t, txn1, ev1.Transaction)

ev2 := resp[2].Data.(explorer.EventV1Transaction)
testutil.CheckTransaction(t, genesisBlock.Transactions[0], ev2.Transaction)
}},
{"Event", func(t *testing.T) {
resp, err := client.Event(types.Hash256(txn2.ID()))
if err != nil {
t.Fatal(err)
}

ev := resp.Data.(explorer.EventV1Transaction)
testutil.CheckTransaction(t, txn2, ev.Transaction)
}},
{"Event unconfirmed", func(t *testing.T) {
resp, err := client.Event(types.Hash256(txn3.ID()))
if err != nil {
t.Fatal(err)
}

ev := resp.Data.(explorer.EventV1Transaction)
ev.Transaction.SiacoinOutputs[0].Source = explorer.SourceTransaction
testutil.CheckTransaction(t, txn3, ev.Transaction)
}},
{"Address event unconfirmed", func(t *testing.T) {
resp, err := client.AddressUnconfirmedEvents(addr1)
if err != nil {
t.Fatal(err)
}
testutil.Equal(t, "events", 1, len(resp))

ev := resp[0].Data.(explorer.EventV1Transaction)
ev.Transaction.SiacoinOutputs[0].Source = explorer.SourceTransaction
testutil.CheckTransaction(t, txn3, ev.Transaction)
}},
{"Contract", func(t *testing.T) {
resp, err := client.Contract(txn1.FileContractID(0))
Expand Down
3 changes: 3 additions & 0 deletions explorer/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,9 @@ func CoreToExplorerV1Transaction(txn types.Transaction) (result Transaction) {
for _, arb := range txn.ArbitraryData {
result.ArbitraryData = append(result.ArbitraryData, arb)
}
for _, sig := range txn.Signatures {
result.Signatures = append(result.Signatures, sig)
}

return
}
Expand Down
14 changes: 14 additions & 0 deletions persist/sqlite/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,20 @@ func TestFileContract(t *testing.T) {
testutil.CheckTransaction(t, txn, ev2.Transaction)
}

{
events, err := db.Events([]types.Hash256{types.Hash256(reviseTxn.ID()), types.Hash256(txn.ID())})
if err != nil {
t.Fatal(err)
}
testutil.Equal(t, "events", 2, len(events))

ev0 := events[0].Data.(explorer.EventV1Transaction)
testutil.CheckTransaction(t, reviseTxn, ev0.Transaction)

ev1 := events[1].Data.(explorer.EventV1Transaction)
testutil.CheckTransaction(t, txn, ev1.Transaction)
}

{
dbFCs, err := db.Contracts([]types.FileContractID{fcID})
if err != nil {
Expand Down
20 changes: 20 additions & 0 deletions persist/sqlite/v2consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1234,6 +1234,26 @@ func TestV2FileContractResolution(t *testing.T) {
testutil.CheckV2Transaction(t, txn1, explorer.V2Transaction(ev6))
}

{
events, err := db.Events([]types.Hash256{types.Hash256(txn4.ID()), types.Hash256(txn3.ID()), types.Hash256(txn2.ID()), types.Hash256(txn1.ID())})
if err != nil {
t.Fatal(err)
}
testutil.Equal(t, "events", 4, len(events))

ev0 := events[0].Data.(explorer.EventV2Transaction)
testutil.CheckV2Transaction(t, txn4, explorer.V2Transaction(ev0))

ev1 := events[1].Data.(explorer.EventV2Transaction)
testutil.CheckV2Transaction(t, txn3, explorer.V2Transaction(ev1))

ev2 := events[2].Data.(explorer.EventV2Transaction)
testutil.CheckV2Transaction(t, txn2, explorer.V2Transaction(ev2))

ev3 := events[3].Data.(explorer.EventV2Transaction)
testutil.CheckV2Transaction(t, txn1, explorer.V2Transaction(ev3))
}

// revert the block
{
state := prevState
Expand Down

0 comments on commit de870ee

Please sign in to comment.