-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathconfiguration_test.dart
216 lines (208 loc) · 6.99 KB
/
configuration_test.dart
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import 'package:test/test.dart';
import 'package:typesense/typesense.dart';
import 'test_utils.dart';
void main() {
final config = Configuration(
'abc123',
nodes: {
Node(
Protocol.https,
'localhost',
path: pathToService,
),
},
nearestNode: Node(
Protocol.http,
'localhost',
path: pathToService,
),
connectionTimeout: Duration(seconds: 10),
healthcheckInterval: Duration(seconds: 5),
numRetries: 5,
retryInterval: Duration(seconds: 3),
sendApiKeyAsQueryParam: true,
cachedSearchResultsTTL: Duration(seconds: 30),
);
group('Configuration', () {
test('has an apiKey field', () {
expect(config.apiKey, equals('abc123'));
});
test('has a nodes field', () {
expect(config.nodes, isNotEmpty);
expect(config.nodes!.length, equals(1));
expect(config.nodes!.first.uri.port, equals(443));
expect(config.nodes!.first.uri.toString(),
equals('https://localhost/path/to/service'));
});
test('has a nearestNode field', () {
expect(config.nearestNode!.port, equals(80));
expect(config.nearestNode!.uri.toString(),
equals('http://localhost/path/to/service'));
});
test('has a numRetries field', () {
expect(config.numRetries, equals(5));
});
test('has a retryInterval field', () {
expect(config.retryInterval, equals(Duration(seconds: 3)));
});
test('has a connectionTimeout field', () {
expect(config.connectionTimeout, equals(Duration(seconds: 10)));
});
test('has a healthcheckInterval field', () {
expect(config.healthcheckInterval, equals(Duration(seconds: 5)));
});
test('has a cacheSearchResults field', () {
expect(config.cachedSearchResultsTTL, equals(Duration(seconds: 30)));
});
test('has a sendApiKeyAsQueryParam field', () {
expect(config.sendApiKeyAsQueryParam, isTrue);
});
test('has a copyWith method', () {
var updatedConfig = config.copyWith(
apiKey: 'newKey',
healthcheckInterval: Duration(minutes: 1),
sendApiKeyAsQueryParam: false);
expect(config.apiKey == updatedConfig.apiKey, isFalse);
expect(updatedConfig.apiKey, equals('newKey'));
expect(config.healthcheckInterval == updatedConfig.healthcheckInterval,
isFalse);
expect(
config.sendApiKeyAsQueryParam == updatedConfig.sendApiKeyAsQueryParam,
isFalse);
expect(updatedConfig.sendApiKeyAsQueryParam, isFalse);
expect(updatedConfig.connectionTimeout, equals(config.connectionTimeout));
expect(updatedConfig.nearestNode, equals(config.nearestNode));
expect(updatedConfig.nodes, equals(config.nodes));
expect(updatedConfig.numRetries, equals(config.numRetries));
expect(updatedConfig.retryInterval, equals(config.retryInterval));
expect(updatedConfig.cachedSearchResultsTTL,
equals(config.cachedSearchResultsTTL));
});
});
group('Configuration initialization', () {
test('with an empty apiKey throws', () {
expect(
() => Configuration(
'',
connectionTimeout: Duration(seconds: 10),
healthcheckInterval: Duration(seconds: 5),
nearestNode: NearestNode,
nodes: {MockNode},
numRetries: 5,
retryInterval: Duration(seconds: 3),
sendApiKeyAsQueryParam: true,
),
throwsA(
isA<MissingConfiguration>().having(
(e) => e.message,
'message',
'Ensure that Configuration.apiKey is not empty',
),
),
);
});
test('with null nodes and nearestNode throws', () {
expect(
() => Configuration(
'abc123',
connectionTimeout: Duration(seconds: 10),
healthcheckInterval: Duration(seconds: 5),
numRetries: 5,
retryInterval: Duration(seconds: 3),
sendApiKeyAsQueryParam: true,
),
throwsA(
isA<MissingConfiguration>().having(
(e) => e.message,
'message',
'Ensure that at least one node is present in Configuration',
),
),
);
});
test(
'with missing numRetries, sets numRetries to number of nodes in Configuration.nodes + number of nearest node',
() {
final config = Configuration(
'abc123',
connectionTimeout: Duration(seconds: 10),
healthcheckInterval: Duration(seconds: 5),
nearestNode: NearestNode,
nodes: {MockNode},
retryInterval: Duration(seconds: 3),
sendApiKeyAsQueryParam: true,
);
expect(config.numRetries, equals(config.nodes!.length + 1));
});
test(
'with missing retryIntervalSeconds, sets retryIntervalSeconds to 100 milliseconds',
() {
final config = Configuration(
'abc123',
connectionTimeout: Duration(seconds: 10),
healthcheckInterval: Duration(seconds: 5),
nearestNode: NearestNode,
nodes: {MockNode},
numRetries: 5,
sendApiKeyAsQueryParam: true,
);
expect(config.retryInterval, equals(Duration(milliseconds: 100)));
});
test('with missing connectionTimeout, sets connectionTimeout to 5 seconds',
() {
final config = Configuration(
'abc123',
healthcheckInterval: Duration(seconds: 5),
nearestNode: NearestNode,
nodes: {MockNode},
numRetries: 5,
retryInterval: Duration(seconds: 3),
sendApiKeyAsQueryParam: true,
);
expect(config.connectionTimeout, equals(Duration(seconds: 10)));
});
test(
'with missing healthcheckInterval, sets healthcheckInterval to 15 seconds',
() {
final config = Configuration(
'abc123',
connectionTimeout: Duration(seconds: 10),
nearestNode: NearestNode,
nodes: {MockNode},
numRetries: 5,
retryInterval: Duration(seconds: 3),
sendApiKeyAsQueryParam: true,
);
expect(config.healthcheckInterval, equals(Duration(seconds: 15)));
});
test(
'with missing cachedSearchResultsTTL, sets cachedSearchResultsTTL to 0',
() {
final config = Configuration(
'abc123',
nearestNode: NearestNode,
nodes: {MockNode},
connectionTimeout: Duration(seconds: 10),
healthcheckInterval: Duration(seconds: 5),
numRetries: 5,
retryInterval: Duration(seconds: 3),
sendApiKeyAsQueryParam: true,
);
expect(config.cachedSearchResultsTTL, equals(Duration.zero));
});
test(
'with missing sendApiKeyAsQueryParam, sets sendApiKeyAsQueryParam to false',
() {
final config = Configuration(
'abc123',
connectionTimeout: Duration(seconds: 10),
healthcheckInterval: Duration(seconds: 5),
nearestNode: NearestNode,
nodes: {MockNode},
numRetries: 5,
retryInterval: Duration(seconds: 3),
);
expect(config.sendApiKeyAsQueryParam, isFalse);
});
});
}