-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_helpers.go
64 lines (50 loc) · 1.38 KB
/
test_helpers.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
package migrate
import (
"context"
"testing"
"github.com/clinia/x/elasticx"
"github.com/elastic/go-elasticsearch/v8"
"github.com/stretchr/testify/assert"
)
type testFixture struct {
ctx context.Context
client elasticx.Client
es *elasticsearch.TypedClient
}
func newTestFixture(t *testing.T) *testFixture {
ctx := context.Background()
config := elasticsearch.Config{}
client, err := elasticx.NewClient(config)
assert.NoError(t, err)
err = client.Init(ctx)
assert.NoError(t, err)
es, err := elasticsearch.NewTypedClient(config)
assert.NoError(t, err)
return &testFixture{
ctx: ctx,
client: client,
es: es,
}
}
// setupEngine creates an engine with the given name and returns it.
// It also deletes the engine when it exist.
func (f *testFixture) setupEngine(t *testing.T, name string) elasticx.Engine {
if exists, err := f.client.EngineExists(f.ctx, name); err != nil {
t.Fatal(err)
} else if exists {
engine, err := f.client.Engine(f.ctx, name)
assert.NoError(t, err)
err = engine.Remove(f.ctx)
assert.NoError(t, err)
}
engine, err := f.client.CreateEngine(f.ctx, name)
assert.NoError(t, err)
return engine
}
// cleanEngine deletes the engine with the given name.
func (f *testFixture) cleanEngine(t *testing.T, name string) {
engine, err := f.client.Engine(f.ctx, name)
assert.NoError(t, err)
err = engine.Remove(f.ctx)
assert.NoError(t, err)
}