-
Notifications
You must be signed in to change notification settings - Fork 127
/
config_db_test.go
153 lines (125 loc) · 5.8 KB
/
config_db_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
package evergreen
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)
func TestGetSectionsBSON(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
defer func(client *mongo.Client) {
GetEnvironment().(*envState).sharedDBClient = client
}(GetEnvironment().(*envState).sharedDBClient)
GetEnvironment().(*envState).sharedDBClient = GetEnvironment().(*envState).client
t.Run("IncludeOverrides", func(t *testing.T) {
require.NoError(t, GetEnvironment().DB().Collection(ConfigCollection).Drop(ctx))
defer func() { require.NoError(t, GetEnvironment().DB().Collection(ConfigCollection).Drop(ctx)) }()
config := NotifyConfig{BufferTargetPerInterval: 1}
_, err := GetEnvironment().DB().Collection(ConfigCollection).InsertOne(ctx, struct {
ID string `bson:"_id"`
NotifyConfig `bson:",inline"`
}{ID: config.SectionId(), NotifyConfig: config})
require.NoError(t, err)
sections, err := getSectionsBSON(ctx, []string{config.SectionId()}, true)
assert.NoError(t, err)
assert.Len(t, sections, 1)
})
t.Run("OnlyShared", func(t *testing.T) {
require.NoError(t, GetEnvironment().SharedDB().Collection(ConfigCollection).Drop(ctx))
defer func() { require.NoError(t, GetEnvironment().SharedDB().Collection(ConfigCollection).Drop(ctx)) }()
config := NotifyConfig{BufferTargetPerInterval: 1}
_, err := GetEnvironment().SharedDB().Collection(ConfigCollection).InsertOne(ctx, struct {
ID string `bson:"_id"`
NotifyConfig `bson:",inline"`
}{ID: config.SectionId(), NotifyConfig: config})
require.NoError(t, err)
sections, err := getSectionsBSON(ctx, []string{config.SectionId()}, false)
assert.NoError(t, err)
assert.Len(t, sections, 1)
})
}
func TestGetConfigSection(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
t.Run("NilSharedDB", func(t *testing.T) {
t.Run("ConfigDocumentExists", func(t *testing.T) {
require.NoError(t, GetEnvironment().DB().Collection(ConfigCollection).Drop(ctx))
defer func() { require.NoError(t, GetEnvironment().DB().Collection(ConfigCollection).Drop(ctx)) }()
config := NotifyConfig{BufferTargetPerInterval: 1}
_, err := GetEnvironment().DB().Collection(ConfigCollection).InsertOne(ctx, struct {
ID string `bson:"_id"`
NotifyConfig `bson:",inline"`
}{ID: config.SectionId(), NotifyConfig: config})
require.NoError(t, err)
var fetchedConfig NotifyConfig
assert.NoError(t, getConfigSection(ctx, &fetchedConfig))
assert.Equal(t, config, fetchedConfig)
})
t.Run("DocumentMissing", func(t *testing.T) {
require.NoError(t, GetEnvironment().DB().Collection(ConfigCollection).Drop(ctx))
fetchedConfig := NotifyConfig{BufferTargetPerInterval: 1}
assert.NoError(t, getConfigSection(ctx, &fetchedConfig))
assert.Zero(t, fetchedConfig)
})
})
t.Run("SharedDBSet", func(t *testing.T) {
defer func(client *mongo.Client) {
GetEnvironment().(*envState).sharedDBClient = client
}(GetEnvironment().(*envState).sharedDBClient)
GetEnvironment().(*envState).sharedDBClient = GetEnvironment().(*envState).client
t.Run("ConfigDocumentExists", func(t *testing.T) {
require.NoError(t, GetEnvironment().DB().Collection(ConfigCollection).Drop(ctx))
defer func() { require.NoError(t, GetEnvironment().DB().Collection(ConfigCollection).Drop(ctx)) }()
config := NotifyConfig{BufferTargetPerInterval: 1}
_, err := GetEnvironment().DB().Collection(ConfigCollection).InsertOne(ctx, struct {
ID string `bson:"_id"`
NotifyConfig `bson:",inline"`
}{ID: config.SectionId(), NotifyConfig: config})
require.NoError(t, err)
var fetchedConfig NotifyConfig
assert.NoError(t, getConfigSection(ctx, &fetchedConfig))
assert.Equal(t, config, fetchedConfig)
})
t.Run("DocumentMissing", func(t *testing.T) {
require.NoError(t, GetEnvironment().SharedDB().Collection(ConfigCollection).Drop(ctx))
fetchedConfig := NotifyConfig{BufferTargetPerInterval: 1}
assert.NoError(t, getConfigSection(ctx, &fetchedConfig))
assert.Zero(t, fetchedConfig)
})
})
}
func TestSetConfigSection(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
t.Run("AlreadyExists", func(t *testing.T) {
require.NoError(t, GetEnvironment().DB().Collection(ConfigCollection).Drop(ctx))
defer func() { require.NoError(t, GetEnvironment().DB().Collection(ConfigCollection).Drop(ctx)) }()
targetPerInterval := 1
config := NotifyConfig{BufferTargetPerInterval: targetPerInterval}
_, err := GetEnvironment().DB().Collection(ConfigCollection).InsertOne(ctx, struct {
ID string `bson:"_id"`
NotifyConfig `bson:",inline"`
}{ID: config.SectionId(), NotifyConfig: config})
require.NoError(t, err)
intervalSecs := 2
assert.NoError(t, setConfigSection(ctx, config.SectionId(), bson.M{"$set": bson.M{"buffer_interval_seconds": intervalSecs}}))
fetchedConfig := NotifyConfig{}
assert.NoError(t, getConfigSection(ctx, &fetchedConfig))
assert.Equal(t, targetPerInterval, fetchedConfig.BufferTargetPerInterval)
assert.Equal(t, intervalSecs, fetchedConfig.BufferIntervalSeconds)
})
t.Run("New", func(t *testing.T) {
require.NoError(t, GetEnvironment().DB().Collection(ConfigCollection).Drop(ctx))
defer func() { require.NoError(t, GetEnvironment().DB().Collection(ConfigCollection).Drop(ctx)) }()
intervalSecs := 1
config := NotifyConfig{}
assert.NoError(t, setConfigSection(ctx, config.SectionId(), bson.M{"$set": bson.M{"buffer_interval_seconds": intervalSecs}}))
fetchedConfig := NotifyConfig{}
assert.NoError(t, getConfigSection(ctx, &fetchedConfig))
assert.Equal(t, intervalSecs, fetchedConfig.BufferIntervalSeconds)
})
}