-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigration_job_simple_test.go
86 lines (79 loc) · 2.88 KB
/
migration_job_simple_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
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/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestSimpleMigrationJob(t *testing.T) {
env := mock.NewEnvironment()
mh := &MigrationHelperMock{Environment: env}
ctx := context.Background()
const jobTypeName = "simple-migration"
// first test the factory and registry
factory, err := registry.GetJobFactory(jobTypeName)
require.NoError(t, err)
job, ok := factory().(*simpleMigrationJob)
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, ok := factory().(*simpleMigrationJob)
require.True(t, ok)
jone.SetID("foo")
jtwo, ok := factory().(*simpleMigrationJob)
require.True(t, ok)
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 := NewSimpleMigration(env, model.Simple{})
assert.NotNil(t, migration)
assert.Equal(t, jobTypeName, migration.Type().Name)
})
t.Run("Client", func(t *testing.T) {
env.ShouldPreferClient = true
t.Run("SuccessfulOperation", func(t *testing.T) {
env.Client = mock.NewClient()
env.Client.Databases["foo"] = &mock.Database{DBName: "foo", Collections: map[string]*mock.Collection{"bar": &mock.Collection{UpdateResult: client.UpdateResult{ModifiedCount: 1}}}}
// run a test were nothing happens so it's not an error
job = factory().(*simpleMigrationJob)
job.Definition.Namespace = model.Namespace{DB: "foo", Collection: "bar"}
job.MigrationHelper = mh
job.Run(ctx)
assert.True(t, job.Status().Completed)
assert.NoError(t, job.Error())
})
t.Run("FailedOperation", func(t *testing.T) {
env.Client = mock.NewClient()
env.Client.Databases["foo"] = &mock.Database{DBName: "foo", Collections: map[string]*mock.Collection{"bar": &mock.Collection{UpdateResult: client.UpdateResult{ModifiedCount: 0}}}}
// run a test were nothing happens so it's not an error
job = factory().(*simpleMigrationJob)
job.Definition.Namespace = model.Namespace{DB: "foo", Collection: "bar"}
job.MigrationHelper = mh
job.Run(ctx)
assert.True(t, job.Status().Completed)
err = job.Error()
require.Error(t, err)
assert.Contains(t, err.Error(), "could not update")
})
t.Run("NoClient", func(t *testing.T) {
job = factory().(*simpleMigrationJob)
// run a test where we can't get a db session
job.MigrationHelper = mh
env.ClientError = errors.New("no client")
job.Run(ctx)
assert.True(t, job.Status().Completed)
require.True(t, job.HasErrors())
err = job.Error()
require.Error(t, err)
assert.Contains(t, err.Error(), "no client")
})
})
}