20
20
from pkg .apis .manager .v1beta1 .python import api_pb2
21
21
from pkg .suggestion .v1beta1 .hyperopt .service import HyperoptService
22
22
23
+ import utils
24
+
23
25
24
26
class TestHyperopt (unittest .TestCase ):
25
27
def setUp (self ):
26
28
servicers = {
27
- api_pb2 .DESCRIPTOR .services_by_name [' Suggestion' ]: HyperoptService (
29
+ api_pb2 .DESCRIPTOR .services_by_name [" Suggestion" ]: HyperoptService (
28
30
)
29
31
}
30
32
@@ -189,8 +191,8 @@ def test_get_suggestion(self):
189
191
190
192
get_suggestion = self .test_server .invoke_unary_unary (
191
193
method_descriptor = (api_pb2 .DESCRIPTOR
192
- .services_by_name [' Suggestion' ]
193
- .methods_by_name [' GetSuggestions' ]),
194
+ .services_by_name [" Suggestion" ]
195
+ .methods_by_name [" GetSuggestions" ]),
194
196
invocation_metadata = {},
195
197
request = request , timeout = 1 )
196
198
@@ -200,103 +202,116 @@ def test_get_suggestion(self):
200
202
self .assertEqual (2 , len (response .parameter_assignments ))
201
203
202
204
def test_validate_algorithm_settings (self ):
203
- experiment_spec = [None ]
204
-
205
- def call_validate ():
206
- experiment = api_pb2 .Experiment (name = "test" , spec = experiment_spec [0 ])
207
- request = api_pb2 .ValidateAlgorithmSettingsRequest (experiment = experiment )
208
-
209
- validate_algorithm_settings = self .test_server .invoke_unary_unary (
210
- method_descriptor = (api_pb2 .DESCRIPTOR
211
- .services_by_name ['Suggestion' ]
212
- .methods_by_name ['ValidateAlgorithmSettings' ]),
213
- invocation_metadata = {},
214
- request = request , timeout = 1 )
205
+ # Valid cases.
206
+ experiment_spec = api_pb2 .ExperimentSpec (
207
+ algorithm = api_pb2 .AlgorithmSpec (
208
+ algorithm_name = "tpe" ,
209
+ algorithm_settings = [
210
+ api_pb2 .AlgorithmSetting (
211
+ name = "random_state" ,
212
+ value = "10"
213
+ ),
214
+ api_pb2 .AlgorithmSetting (
215
+ name = "gamma" ,
216
+ value = "0.25"
217
+ ),
218
+ api_pb2 .AlgorithmSetting (
219
+ name = "prior_weight" ,
220
+ value = "1.0"
221
+ ),
222
+ api_pb2 .AlgorithmSetting (
223
+ name = "n_EI_candidates" ,
224
+ value = "24"
225
+ ),
226
+ ]
227
+ )
228
+ )
215
229
216
- return validate_algorithm_settings .termination ()
230
+ _ , _ , code , _ = utils .call_validate (self .test_server , experiment_spec )
231
+ self .assertEqual (code , grpc .StatusCode .OK )
217
232
218
- # valid cases
219
- algorithm_spec = api_pb2 .AlgorithmSpec (
220
- algorithm_name = "tpe" ,
221
- algorithm_settings = [
222
- api_pb2 .AlgorithmSetting (
223
- name = "random_state" ,
224
- value = "10"
225
- ),
226
- api_pb2 .AlgorithmSetting (
227
- name = "gamma" ,
228
- value = "0.25"
229
- ),
230
- api_pb2 .AlgorithmSetting (
231
- name = "prior_weight" ,
232
- value = "1.0"
233
- ),
234
- api_pb2 .AlgorithmSetting (
235
- name = "n_EI_candidates" ,
236
- value = "24"
237
- ),
238
- ],
233
+ # Invalid cases.
234
+ # Unknown algorithm name.
235
+ experiment_spec = api_pb2 .ExperimentSpec (
236
+ algorithm = api_pb2 .AlgorithmSpec (
237
+ algorithm_name = "unknown"
238
+ )
239
239
)
240
- experiment_spec [0 ] = api_pb2 .ExperimentSpec (algorithm = algorithm_spec )
241
- self .assertEqual (call_validate ()[2 ], grpc .StatusCode .OK )
242
240
243
- # invalid cases
244
- experiment_spec [0 ] = api_pb2 .ExperimentSpec (
245
- algorithm = api_pb2 .AlgorithmSpec (algorithm_name = "unknown" ))
246
- _ , _ , code , details = call_validate ()
241
+ _ , _ , code , details = utils .call_validate (self .test_server , experiment_spec )
247
242
self .assertEqual (code , grpc .StatusCode .INVALID_ARGUMENT )
248
- self .assertEqual (details , ' unknown algorithm name unknown' )
243
+ self .assertEqual (details , " unknown algorithm name unknown" )
249
244
250
- experiment_spec [0 ] = api_pb2 .ExperimentSpec (
245
+ # Unknown algorithm setting name.
246
+ experiment_spec = api_pb2 .ExperimentSpec (
251
247
algorithm = api_pb2 .AlgorithmSpec (
252
248
algorithm_name = "random" ,
253
249
algorithm_settings = [
254
- api_pb2 .AlgorithmSetting (name = "unknown_conf" , value = "1111" )]
255
- ))
256
- _ , _ , code , details = call_validate ()
250
+ api_pb2 .AlgorithmSetting (name = "unknown_conf" , value = "1111" )
251
+ ]
252
+ )
253
+ )
254
+
255
+ _ , _ , code , details = utils .call_validate (self .test_server , experiment_spec )
257
256
self .assertEqual (code , grpc .StatusCode .INVALID_ARGUMENT )
258
- self .assertEqual (details , ' unknown setting unknown_conf for algorithm random' )
257
+ self .assertEqual (details , " unknown setting unknown_conf for algorithm random" )
259
258
260
- experiment_spec [0 ] = api_pb2 .ExperimentSpec (
259
+ # Invalid gamma value.
260
+ experiment_spec = api_pb2 .ExperimentSpec (
261
261
algorithm = api_pb2 .AlgorithmSpec (
262
262
algorithm_name = "tpe" ,
263
263
algorithm_settings = [
264
- api_pb2 .AlgorithmSetting (name = "gamma" , value = "1.5" )]
265
- ))
266
- _ , _ , code , details = call_validate ()
264
+ api_pb2 .AlgorithmSetting (name = "gamma" , value = "1.5" )
265
+ ]
266
+ )
267
+ )
268
+
269
+ _ , _ , code , details = utils .call_validate (self .test_server , experiment_spec )
267
270
self .assertEqual (code , grpc .StatusCode .INVALID_ARGUMENT )
268
- self .assertEqual (details , ' gamma should be in the range of (0, 1)' )
271
+ self .assertEqual (details , " gamma should be in the range of (0, 1)" )
269
272
270
- experiment_spec [0 ] = api_pb2 .ExperimentSpec (
273
+ # Invalid n_EI_candidates value.
274
+ experiment_spec = api_pb2 .ExperimentSpec (
271
275
algorithm = api_pb2 .AlgorithmSpec (
272
276
algorithm_name = "tpe" ,
273
277
algorithm_settings = [
274
- api_pb2 .AlgorithmSetting (name = "n_EI_candidates" , value = "0" )]
275
- ))
276
- _ , _ , code , details = call_validate ()
278
+ api_pb2 .AlgorithmSetting (name = "n_EI_candidates" , value = "0" )
279
+ ]
280
+ )
281
+ )
282
+
283
+ _ , _ , code , details = utils .call_validate (self .test_server , experiment_spec )
277
284
self .assertEqual (code , grpc .StatusCode .INVALID_ARGUMENT )
278
- self .assertEqual (details , ' n_EI_candidates should be great than zero' )
285
+ self .assertEqual (details , " n_EI_candidates should be great than zero" )
279
286
280
- experiment_spec [0 ] = api_pb2 .ExperimentSpec (
287
+ # Invalid random_state value.
288
+ experiment_spec = api_pb2 .ExperimentSpec (
281
289
algorithm = api_pb2 .AlgorithmSpec (
282
290
algorithm_name = "tpe" ,
283
291
algorithm_settings = [
284
- api_pb2 .AlgorithmSetting (name = "random_state" , value = "-1" )]
285
- ))
286
- _ , _ , code , details = call_validate ()
292
+ api_pb2 .AlgorithmSetting (name = "random_state" , value = "-1" )
293
+ ]
294
+ )
295
+ )
296
+
297
+ _ , _ , code , details = utils .call_validate (self .test_server , experiment_spec )
287
298
self .assertEqual (code , grpc .StatusCode .INVALID_ARGUMENT )
288
- self .assertEqual (details , ' random_state should be great or equal than zero' )
299
+ self .assertEqual (details , " random_state should be great or equal than zero" )
289
300
290
- experiment_spec [0 ] = api_pb2 .ExperimentSpec (
301
+ # Invalid prior_weight value.
302
+ experiment_spec = api_pb2 .ExperimentSpec (
291
303
algorithm = api_pb2 .AlgorithmSpec (
292
304
algorithm_name = "tpe" ,
293
305
algorithm_settings = [
294
- api_pb2 .AlgorithmSetting (name = "prior_weight" , value = "aaa" )]
295
- ))
296
- _ , _ , code , details = call_validate ()
306
+ api_pb2 .AlgorithmSetting (name = "prior_weight" , value = "aaa" )
307
+ ]
308
+ )
309
+ )
310
+
311
+ _ , _ , code , details = utils .call_validate (self .test_server , experiment_spec )
297
312
self .assertEqual (code , grpc .StatusCode .INVALID_ARGUMENT )
298
- self .assertTrue (details .startswith (' failed to validate prior_weight(aaa)' ))
313
+ self .assertTrue (details .startswith (" failed to validate prior_weight(aaa)" ))
299
314
300
315
301
- if __name__ == ' __main__' :
316
+ if __name__ == " __main__" :
302
317
unittest .main ()
0 commit comments