-
Notifications
You must be signed in to change notification settings - Fork 2
/
t.go
120 lines (99 loc) · 2.66 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package test
import (
"io"
"encoding/json"
"github.com/digitalbitsorg/go/services/frontier/internal/ledger"
"github.com/digitalbitsorg/go/services/frontier/internal/render/hal"
"github.com/digitalbitsorg/go/support/db"
)
// CoreSession returns a db.Session instance pointing at the digitalbits 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 frontier 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 frontier 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{}) hal.Links {
var env struct {
Embedded struct {
Records json.RawMessage `json:"records"`
} `json:"_embedded"`
Links struct {
Self hal.Link `json:"self"`
Next hal.Link `json:"next"`
Prev hal.Link `json:"prev"`
} `json:"_links"`
}
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")
return env.Links
}
// UnmarshalNext extracts and returns the next link
func (t *T) UnmarshalNext(r io.Reader) string {
var env struct {
Links struct {
Next struct {
Href string `json:"href"`
} `json:"next"`
} `json:"_links"`
}
err := json.NewDecoder(r).Decode(&env)
t.Require.NoError(err, "failed to decode page")
return env.Links.Next.Href
}
// 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(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
}