mirror of
https://github.com/wassname/optuna-dashboard.git
synced 2026-09-09 11:28:14 +08:00
Tests, formatting
This commit is contained in:
+53
-24
@@ -56,41 +56,70 @@ class NoteTestCase(TestCase):
|
||||
|
||||
def test_delete_notes_trial(self) -> None:
|
||||
study = optuna.create_study()
|
||||
trial_1 = study.ask({"x1": optuna.distributions.FloatDistribution(0, 10)})
|
||||
trial_2 = study.ask({"x1": optuna.distributions.FloatDistribution(0, 10)})
|
||||
trials = [
|
||||
study.ask({"x1": optuna.distributions.FloatDistribution(0, 10)}) for _ in range(2)
|
||||
]
|
||||
storage = study._storage
|
||||
|
||||
for trial, body in [(trial_1, "version 1"), (trial_2, "version 2")]:
|
||||
save_note(trial, body)
|
||||
notes = ["trial 0", "trial 1"]
|
||||
for trial, body in zip(trials, notes):
|
||||
with self.subTest(body):
|
||||
save_note(trial, body)
|
||||
|
||||
# first assert existence
|
||||
actual = get_note(trial)
|
||||
self.assertEqual(actual, body)
|
||||
self.assertEqual(get_note(trial), body)
|
||||
|
||||
# delete
|
||||
note.delete_notes(storage, study._study_id, trial._trial_id)
|
||||
note.delete_notes(storage, study._study_id, trial._trial_id)
|
||||
|
||||
# assert deletion
|
||||
actual = get_note(trial)
|
||||
self.assertEqual(actual, "")
|
||||
self.assertEqual(get_note(trial), "")
|
||||
|
||||
def test_delete_notes_study(self) -> None:
|
||||
study = optuna.create_study()
|
||||
trials = [
|
||||
study.ask({"x1": optuna.distributions.FloatDistribution(0, 10)}) for _ in range(2)
|
||||
]
|
||||
storage = study._storage
|
||||
|
||||
notes = ["trial 0", "trial 1"]
|
||||
for trial, body in zip(trials, notes):
|
||||
with self.subTest(body):
|
||||
save_note(trial, body)
|
||||
|
||||
actual = get_note(trial)
|
||||
self.assertEqual(actual, body)
|
||||
|
||||
save_note(study, "Study note")
|
||||
actual = get_note(study)
|
||||
self.assertEqual(actual, "Study note")
|
||||
|
||||
note.delete_study_notes(storage, study._study_id)
|
||||
|
||||
def test_delete_notes_study(self) -> None:
|
||||
pass
|
||||
for trial in trials:
|
||||
self.assertEqual(get_note(trial), "")
|
||||
self.assertEqual(get_note(study), "")
|
||||
|
||||
def test_transfer_notes(self) -> None:
|
||||
study = optuna.create_study()
|
||||
trial_1 = study.ask({"x1": optuna.distributions.FloatDistribution(0, 10)})
|
||||
trial_2 = study.ask({"x2": optuna.distributions.FloatDistribution(0, 10)})
|
||||
storage = study._storage
|
||||
old_study = optuna.create_study()
|
||||
old_trials = [
|
||||
old_study.ask({"x1": optuna.distributions.FloatDistribution(0, 10)}) for _ in range(2)
|
||||
]
|
||||
storage = old_study._storage
|
||||
|
||||
save_note(trial_1, "trial 1")
|
||||
save_note(trial_2, "trial 2")
|
||||
notes = ["trial 0", "trial 1"]
|
||||
for trial, body in zip(old_trials, notes):
|
||||
save_note(trial, body)
|
||||
save_note(old_study, "Study")
|
||||
|
||||
new_study = optuna.create_study(
|
||||
storage=storage, directions=study.directions
|
||||
)
|
||||
note.transfer_notes(storage, study, new_study)
|
||||
new_study = optuna.create_study(storage=storage, directions=old_study.directions)
|
||||
new_study.add_trials(old_study.get_trials(deepcopy=False))
|
||||
|
||||
note.transfer_notes(storage, old_study, new_study)
|
||||
|
||||
for old_trial in old_trials:
|
||||
self.assertEqual(get_note(old_trial), "")
|
||||
self.assertEqual(get_note(old_study), "")
|
||||
|
||||
system_attrs = new_study._storage.get_study_system_attrs(new_study._study_id)
|
||||
for new_trial, body in zip(new_study.get_trials(), notes):
|
||||
actual = note.get_note_from_system_attrs(system_attrs, new_trial._trial_id)
|
||||
self.assertEqual(actual["body"], body)
|
||||
self.assertEqual(get_note(new_study), "Study")
|
||||
|
||||
Reference in New Issue
Block a user