/
main.go
125 lines (106 loc) · 3.57 KB
/
main.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
// Package test contains simple test helpers that should not
// have any dependencies on horizon's packages. think constants,
// custom matchers, generic helpers etc.
package test
import (
"bytes"
"context"
"net/http"
"net/http/httptest"
"testing"
"github.com/jmoiron/sqlx"
"github.com/sirupsen/logrus"
"github.com/AnneNamuli/go-stellar/services/horizon/internal/logmetrics"
tdb "github.com/AnneNamuli/go-stellar/services/horizon/internal/test/db"
"github.com/AnneNamuli/go-stellar/support/log"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// StaticMockServer is a test helper that records it's last request
type StaticMockServer struct {
*httptest.Server
LastRequest *http.Request
}
// T provides a common set of functionality for each test in horizon
type T struct {
T *testing.T
Assert *assert.Assertions
Require *require.Assertions
Ctx context.Context
HorizonDB *sqlx.DB
CoreDB *sqlx.DB
Logger *log.Entry
LogMetrics *logmetrics.Metrics
LogBuffer *bytes.Buffer
}
// Context provides a context suitable for testing in tests that do not create
// a full App instance (in which case your tests should be using the app's
// context). This context has a logger bound to it suitable for testing.
func Context() context.Context {
return log.Set(context.Background(), testLogger)
}
// Database returns a connection to the horizon test database
//
// DEPRECATED: use `Horizon()` from test/db package
func Database(t *testing.T) *sqlx.DB {
return tdb.Horizon(t)
}
// DatabaseURL returns the database connection the url any test
// use when connecting to the history/horizon database
//
// DEPRECATED: use `HorizonURL()` from test/db package
func DatabaseURL() string {
return tdb.HorizonURL()
}
// OverrideLogger sets the default logger used by horizon to `l`. This is used
// by the testing system so that we can collect output from logs during test
// runs. Panics if the logger is already overridden.
func OverrideLogger(l *log.Entry) {
if oldDefault != nil {
panic("logger already overridden")
}
oldDefault = log.DefaultLogger
log.DefaultLogger = l
}
// RestoreLogger restores the default horizon logger after it is overridden
// using a call to `OverrideLogger`. Panics if the default logger is not
// presently overridden.
func RestoreLogger() {
if oldDefault == nil {
panic("logger not overridden, cannot restore")
}
log.DefaultLogger = oldDefault
oldDefault = nil
}
// Start initializes a new test helper object and conceptually "starts" a new
// test
func Start(t *testing.T) *T {
result := &T{}
result.T = t
result.LogBuffer = new(bytes.Buffer)
result.Logger, result.LogMetrics = logmetrics.New()
result.Logger.Logger.Out = result.LogBuffer
result.Logger.Logger.Formatter.(*logrus.TextFormatter).DisableColors = true
result.Logger.Logger.Level = logrus.DebugLevel
OverrideLogger(result.Logger)
result.Ctx = log.Set(context.Background(), result.Logger)
result.HorizonDB = Database(t)
result.CoreDB = StellarCoreDatabase(t)
result.Assert = assert.New(t)
result.Require = require.New(t)
return result
}
// StellarCoreDatabase returns a connection to the stellar core test database
//
// DEPRECATED: use `StellarCore()` from test/db package
func StellarCoreDatabase(t *testing.T) *sqlx.DB {
return tdb.StellarCore(t)
}
// StellarCoreDatabaseURL returns the database connection the url any test
// use when connecting to the stellar-core database
//
// DEPRECATED: use `StellarCoreURL()` from test/db package
func StellarCoreDatabaseURL() string {
return tdb.StellarCoreURL()
}
var oldDefault *log.Entry = nil