-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator_manual_test.go
139 lines (127 loc) · 4.07 KB
/
generator_manual_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
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
134
135
136
137
138
139
package anser
import (
"context"
"fmt"
"strings"
"testing"
"github.com/cdr/amboy/registry"
"github.com/deciduosity/anser/mock"
"github.com/deciduosity/anser/model"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestManualMigrationGenerator(t *testing.T) {
ctx := context.Background()
env := mock.NewEnvironment()
mh := &MigrationHelperMock{Environment: env}
const jobTypeName = "manual-migration-generator"
factory, err := registry.GetJobFactory(jobTypeName)
require.NoError(t, err)
job, ok := factory().(*manualMigrationGenerator)
require.True(t, ok)
require.Equal(t, job.Type().Name, jobTypeName)
ns := model.Namespace{DB: "foo", Collection: "bar"}
opts := model.GeneratorOptions{}
t.Run("Interface", func(t *testing.T) {
assert.Implements(t, (*Generator)(nil), &manualMigrationGenerator{})
})
t.Run("Constructor", func(t *testing.T) {
// check that the public method produces a reasonable object
// of the correct type, without shared state
generator := NewManualMigrationGenerator(env, opts, "").(*manualMigrationGenerator)
require.NotNil(t, generator)
assert.Equal(t, generator.Type().Name, jobTypeName)
assert.NotEqual(t, generator, job)
})
t.Run("DependencyCheck", func(t *testing.T) {
// check that the run method returns an error if it can't get a dependency error
env.NetworkError = errors.New("injected network error")
job.MigrationHelper = mh
job.Run(ctx)
assert.True(t, job.Status().Completed)
require.True(t, job.HasErrors())
err = job.Error()
assert.Error(t, err)
assert.Contains(t, err.Error(), "injected network error")
env.NetworkError = nil
})
t.Run("Client", func(t *testing.T) {
env.Client = mock.NewClient()
env.ShouldPreferClient = true
defer func() {
env.ShouldPreferClient = false
env.Client = nil
}()
t.Run("ClientError", func(t *testing.T) {
// make sure that client acquisition errors propagate
job = factory().(*manualMigrationGenerator)
env.ClientError = errors.New("injected client error")
job.MigrationHelper = mh
job.Run(ctx)
assert.True(t, job.Status().Completed)
if assert.True(t, job.HasErrors()) {
err = job.Error()
assert.Error(t, err)
assert.Contains(t, err.Error(), "injected client error")
}
env.ClientError = nil
})
t.Run("FindError", func(t *testing.T) {
// make sure that client acquisition errors propagate
job = factory().(*manualMigrationGenerator)
job.NS.DB = "foo"
job.NS.Collection = "bar"
assert.NotNil(t, env.Client.Database("foo").Collection("bar"))
env.Client.Databases["foo"].Collections["bar"].FindError = errors.New("injected query error")
job.MigrationHelper = mh
job.Run(ctx)
assert.True(t, job.Status().Completed)
if assert.True(t, job.HasErrors()) {
err = job.Error()
assert.Error(t, err)
assert.Contains(t, err.Error(), "injected query error")
}
env.ClientError = nil
})
t.Run("Generation", func(t *testing.T) {
defer func() { env.Network = mock.NewDependencyNetwork() }()
env.Network = mock.NewDependencyNetwork()
// make sure that we generate the jobs we would expect to:
job = factory().(*manualMigrationGenerator)
job.NS = ns
job.MigrationHelper = mh
job.Limit = 3
job.SetID("manual")
cursor := &mock.Cursor{
Results: []interface{}{
&doc{"one"}, &doc{"two"}, &doc{"three"}, &doc{"four"},
},
ShouldIter: true,
MaxNextCalls: 4,
}
ids := job.generateJobs(ctx, env, cursor)
for idx, id := range ids {
require.True(t, strings.HasPrefix(id, "manual."))
require.True(t, strings.HasSuffix(id, fmt.Sprintf(".%d", idx)))
switch idx {
case 0:
assert.Contains(t, id, ".one.")
case 1:
assert.Contains(t, id, ".two.")
case 2:
assert.Contains(t, id, ".three.")
case 3:
assert.Contains(t, id, ".four.")
}
}
assert.Len(t, ids, 3)
assert.Len(t, job.Migrations, 3)
network, err := env.GetDependencyNetwork()
require.NoError(t, err)
network.AddGroup(job.ID(), ids)
networkMap := network.Network()
assert.Len(t, networkMap[job.ID()], 3)
})
})
}