-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
90 lines (77 loc) · 2.07 KB
/
example_test.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
package anser
import (
"context"
"testing"
"time"
"github.com/cdr/amboy/queue"
"github.com/deciduosity/anser/client"
"github.com/deciduosity/anser/model"
"github.com/stretchr/testify/assert"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
// proofOfConcept is a simple mock "main" to demonstrate how you could
// build a simple migration utility.
func proofOfConcept(shouldUseClient bool) error {
env := GetEnvironment()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cl, err := mongo.NewClient(options.Client().ApplyURI("mongodb://localhost:27017").SetConnectTimeout(100 * time.Millisecond))
if err != nil {
return err
}
if err := cl.Connect(ctx); err != nil {
return err
}
client := client.WrapClient(cl)
q := queue.NewAdaptiveOrderedLocalQueue(3, 3)
if err := q.Start(ctx); err != nil {
return err
}
if err := env.Setup(q, client, "anser_test"); err != nil {
return err
}
ns := model.Namespace{DB: "mci", Collection: "test"}
app := &Application{
Generators: []Generator{
NewSimpleMigrationGenerator(env,
model.GeneratorOptions{
JobID: "first",
DependsOn: []string{},
NS: ns,
Query: map[string]interface{}{
"time": map[string]interface{}{"$gt": time.Now().Add(-time.Hour)},
},
},
// update:
map[string]interface{}{
"$rename": map[string]string{"time": "timeSince"},
}),
NewStreamMigrationGenerator(env,
model.GeneratorOptions{
JobID: "second",
DependsOn: []string{"first"},
NS: ns,
Query: map[string]interface{}{
"time": map[string]interface{}{"$gt": time.Now().Add(-time.Hour)},
},
},
// the name of a registered aggregate operation
"op-name"),
},
}
if err := app.Setup(env); err != nil {
return err
}
return app.Run(ctx)
}
func TestExampleApp(t *testing.T) {
ResetEnvironment()
t.Run("Client", func(t *testing.T) {
assert.NoError(t, proofOfConcept(true))
})
ResetEnvironment()
t.Run("Session", func(t *testing.T) {
assert.NoError(t, proofOfConcept(false))
})
}