-
Notifications
You must be signed in to change notification settings - Fork 1
/
t.go
133 lines (109 loc) · 2.98 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
121
122
123
124
125
126
127
128
129
130
131
132
133
package test
import (
"io"
"encoding/json"
"github.com/diamnet/go/services/aurora/internal/ledger"
"github.com/diamnet/go/services/aurora/internal/operationfeestats"
"github.com/diamnet/go/support/db"
"github.com/diamnet/go/support/render/hal"
)
// CoreSession returns a db.Session instance pointing at the diamnet 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 aurora logs to the logs
// output
func (t *T) Finish() {
RestoreLogger()
// Reset cached ledger state
ledger.SetState(ledger.State{})
operationfeestats.ResetState()
if t.LogBuffer.Len() > 0 {
t.T.Log("\n" + t.LogBuffer.String())
}
}
// AuroraSession returns a db.Session instance pointing at the aurora test
// database
func (t *T) AuroraSession() *db.Session {
return &db.Session{
DB: t.AuroraDB,
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
}
// ScenarioWithoutAurora loads the named sql scenario into the database
func (t *T) ScenarioWithoutAurora(name string) *T {
LoadScenarioWithoutAurora(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
}
// UnmarshalExtras extracts and returns extras content
func (t *T) UnmarshalExtras(r io.Reader) map[string]string {
var resp struct {
Extras map[string]string `json:"extras"`
}
err := json.NewDecoder(r).Decode(&resp)
t.Require.NoError(err, "failed to decode page")
return resp.Extras
}
// 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.AuroraSession().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)
}