forked from stellar/go
/
t.go
99 lines (81 loc) · 2.13 KB
/
t.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package test
import (
"io"
"encoding/json"
"github.com/stellar/go/support/db"
"github.com/stellar/go/services/horizon/internal/ledger"
)
// CoreSession returns a db.Session instance pointing at the stellar core test database
func (t *T) CoreSession() *db.Session {
return &db.Session{
DB: t.CoreDB,
Ctx: t.Ctx,
}
}
// Finish finishes the test, logging any accumulated horizon logs to the logs
// output
func (t *T) Finish() {
RestoreLogger()
// Reset cached ledger state
ledger.SetState(ledger.State{})
if t.LogBuffer.Len() > 0 {
t.T.Log("\n" + t.LogBuffer.String())
}
}
// HorizonSession returns a db.Session instance pointing at the horizon test
// database
func (t *T) HorizonSession() *db.Session {
return &db.Session{
DB: t.HorizonDB,
Ctx: t.Ctx,
}
}
// Scenario loads the named sql scenario into the database
func (t *T) Scenario(name string) *T {
LoadScenario(name)
t.UpdateLedgerState()
return t
}
// ScenarioWithoutHorizon loads the named sql scenario into the database
func (t *T) ScenarioWithoutHorizon(name string) *T {
LoadScenarioWithoutHorizon(name)
t.UpdateLedgerState()
return t
}
// UnmarshalPage populates dest with the records contained in the json-encoded
// page in r.
func (t *T) UnmarshalPage(r io.Reader, dest interface{}) {
var env struct {
Embedded struct {
Records json.RawMessage `json:"records"`
} `json:"_embedded"`
}
err := json.NewDecoder(r).Decode(&env)
t.Require.NoError(err, "failed to decode page")
err = json.Unmarshal(env.Embedded.Records, dest)
t.Require.NoError(err, "failed to decode records")
}
// UpdateLedgerState updates the cached ledger state (or panicing on failure).
func (t *T) UpdateLedgerState() {
var next ledger.State
err := t.CoreSession().GetRaw(&next, `
SELECT
COALESCE(MIN(ledgerseq), 0) as core_elder,
COALESCE(MAX(ledgerseq), 0) as core_latest
FROM ledgerheaders
`)
if err != nil {
panic(err)
}
err = t.HorizonSession().GetRaw(&next, `
SELECT
COALESCE(MIN(sequence), 0) as history_elder,
COALESCE(MAX(sequence), 0) as history_latest
FROM history_ledgers
`)
if err != nil {
panic(err)
}
ledger.SetState(next)
return
}