MCPcopy
hub / github.com/optuna/optuna / test_studies_command

Function test_studies_command

tests/test_cli.py:375–438  ·  view source on GitHub ↗
(output_format: str | None)

Source from the content-addressed store, hash-verified

373
374@output_formats
375def test_studies_command(output_format: str | None) -> None:
376 with NamedTemporaryFilePool() as fp, StorageSupplier("journal", file=fp) as storage:
377 assert isinstance(storage, JournalStorage)
378 storage_url = fp.name
379
380 # First study.
381 study_1 = optuna.create_study(storage=storage)
382
383 # Run command.
384 command = ["optuna", "studies", "--storage", storage_url]
385 if output_format is not None:
386 command += ["--format", output_format]
387
388 studies = _get_output(command, output_format or "table")
389
390 expected_keys = ["name", "direction", "n_trials", "datetime_start"]
391
392 # Check user_attrs are not printed.
393 if output_format in (None, "table", "value"):
394 assert list(studies[0].keys()) == expected_keys
395 else:
396 assert set(studies[0].keys()) == set(expected_keys)
397
398 # Add a second study.
399 study_2 = optuna.create_study(
400 storage=storage, study_name="study_2", directions=["minimize", "maximize"]
401 )
402 study_2.optimize(objective_func_multi_objective, n_trials=10)
403 study_2.set_user_attr("key_1", "value_1")
404 study_2.set_user_attr("key_2", "value_2")
405
406 # Run command again to include second study.
407 studies = _get_output(command, output_format or "table")
408
409 expected_keys = ["name", "direction", "n_trials", "datetime_start", "user_attrs"]
410
411 assert len(studies) == 2
412 for study in studies:
413 if output_format in (None, "table", "value"):
414 assert list(study.keys()) == expected_keys
415 else:
416 assert set(study.keys()) == set(expected_keys)
417
418 # Check study_name, direction, n_trials and user_attrs for the first study.
419 assert studies[0]["name"] == study_1.study_name
420 if output_format in (None, "table", "value"):
421 assert studies[0]["n_trials"] == "0"
422 assert eval(studies[0]["direction"]) == ("MINIMIZE",)
423 assert eval(studies[0]["user_attrs"]) == {}
424 else:
425 assert studies[0]["n_trials"] == 0
426 assert studies[0]["direction"] == ["MINIMIZE"]
427 assert studies[0]["user_attrs"] == {}
428
429 # Check study_name, direction, n_trials and user_attrs for the second study.
430 assert studies[1]["name"] == study_2.study_name
431 if output_format in (None, "table", "value"):
432 assert studies[1]["n_trials"] == "10"

Callers

nothing calls this directly

Calls 5

StorageSupplierClass · 0.90
_get_outputFunction · 0.85
optimizeMethod · 0.80
set_user_attrMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…