-
Notifications
You must be signed in to change notification settings - Fork 0
/
migration_job_manual_test.go
154 lines (138 loc) · 5.22 KB
/
migration_job_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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package anser
import (
"context"
"testing"
"github.com/cdr/amboy/registry"
"github.com/deciduosity/anser/client"
"github.com/deciduosity/anser/mock"
"github.com/deciduosity/anser/model"
"github.com/deciduosity/birch"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestManualMigration(t *testing.T) {
env := mock.NewEnvironment()
mh := &MigrationHelperMock{Environment: env}
ctx := context.Background()
const jobTypeName = "manual-migration"
factory, err := registry.GetJobFactory(jobTypeName)
require.NoError(t, err)
job, ok := factory().(*manualMigrationJob)
require.True(t, ok)
require.Equal(t, jobTypeName, job.Type().Name)
t.Run("Factory", func(t *testing.T) {
// verify that the factory doesn't share state.
jone := factory().(*manualMigrationJob)
jone.SetID("foo")
jtwo := factory().(*manualMigrationJob)
jtwo.SetID("bar")
assert.NotEqual(t, jone, jtwo)
})
t.Run("Constructor", func(t *testing.T) {
// verify that the public constructor returns the correct type
migration := NewManualMigration(env, model.Manual{})
assert.NotNil(t, migration)
assert.Equal(t, jobTypeName, migration.Type().Name)
})
t.Run("UnregisteredJob", func(t *testing.T) {
job := factory().(*manualMigrationJob)
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(), "could not find migration named")
})
t.Run("Client", func(t *testing.T) {
assert.NoError(t, env.RegisterManualMigrationOperation("passing", func(c client.Client, d *birch.Document) error { return nil }))
assert.NoError(t, env.RegisterManualMigrationOperation("failing", func(c client.Client, d *birch.Document) error { return errors.New("manual fail") }))
defer func() { env.MigrationRegistry = make(map[string]client.MigrationOperation) }()
t.Run("NoClient", func(t *testing.T) {
// reset, define a job but have the session acquisition fail
job = factory().(*manualMigrationJob)
job.MigrationHelper = mh
job.Definition.OperationName = "passing"
env.ClientError = errors.New("no client, sorry")
defer func() { env.ClientError = nil }()
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(), "no client, sorry")
})
t.Run("Passing", func(t *testing.T) {
env.Client = mock.NewClient()
job = factory().(*manualMigrationJob)
job.MigrationHelper = mh
job.Definition.OperationName = "passing"
job.Run(ctx)
assert.True(t, job.Status().Completed)
assert.False(t, job.HasErrors())
})
t.Run("Failing", func(t *testing.T) {
// reset and have a job that always fails and make sure the error propagates
job = factory().(*manualMigrationJob)
job.MigrationHelper = mh
job.Definition.OperationName = "failing"
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(), "manual fail")
})
t.Run("DBErrors", func(t *testing.T) {
t.Run("FindOne", func(t *testing.T) {
env.Client = mock.NewClient()
res := mock.NewSingleResult()
res.ErrorValue = errors.New("not found")
env.Client.Databases["foo"] = &mock.Database{DBName: "foo", Collections: map[string]*mock.Collection{"bar": &mock.Collection{SingleResult: res}}}
job = factory().(*manualMigrationJob)
job.Definition.Namespace = model.Namespace{DB: "foo", Collection: "bar"}
job.MigrationHelper = mh
job.Definition.OperationName = "passing"
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(), "not found")
})
t.Run("DecodeError", func(t *testing.T) {
env.Client = mock.NewClient()
res := mock.NewSingleResult()
res.DecodeBytesError = errors.New("could not decode")
env.Client.Databases["foo"] = &mock.Database{DBName: "foo", Collections: map[string]*mock.Collection{"bar": &mock.Collection{SingleResult: res}}}
job = factory().(*manualMigrationJob)
job.Definition.Namespace = model.Namespace{DB: "foo", Collection: "bar"}
job.MigrationHelper = mh
job.Definition.OperationName = "passing"
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(), "could not decode")
})
t.Run("InvalidBSON", func(t *testing.T) {
env.Client = mock.NewClient()
res := mock.NewSingleResult()
res.DecodeBytesValue = []byte{}
env.Client.Databases["foo"] = &mock.Database{DBName: "foo", Collections: map[string]*mock.Collection{"bar": &mock.Collection{SingleResult: res}}}
job = factory().(*manualMigrationJob)
job.Definition.Namespace = model.Namespace{DB: "foo", Collection: "bar"}
job.MigrationHelper = mh
job.Definition.OperationName = "passing"
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(), "error: too small")
})
})
})
}