def test_fork_identical_new_trial(self): lineage = LineageNode(TrialStub(id="my-id", working_dir="same_folder")) with pytest.raises( RuntimeError, match="The new trial new-id has the same working directory" ): lineage.fork(TrialStub(id="new-id", working_dir="same_folder")) assert lineage.children == []
def test_register(self): item = [0] lineage = LineageNode(item) assert lineage.item == item assert lineage.item is not item item = [1] lineage.register(item) assert lineage.item == item assert lineage.item is not item
def test_set_jump(self): parent_lineage = LineageNode(1) child_lineage = LineageNode(2) parent_lineage.set_jump(child_lineage) assert child_lineage.parent is None assert child_lineage.jumps == [] assert child_lineage.base is parent_lineage assert parent_lineage.children == [] assert parent_lineage.jumps == [child_lineage] assert parent_lineage.base is None
def test_fork_to_existing_path(self, tmp_path): trial = TrialStub(id="stub", working_dir=os.path.join(tmp_path, "stub")) os.makedirs(trial.working_dir) lineage = LineageNode(trial) new_trial = TrialStub(id="fork", working_dir=os.path.join(tmp_path, "fork")) os.makedirs(new_trial.working_dir) with pytest.raises( FileExistsError, match="Folder already exists for trial fork." ): lineage.fork(new_trial) assert lineage.children == []
def test_fork(self, mocker): path = "/some_path" trial = TrialStub(path) lineage = LineageNode(trial) new_path = "/another_path" new_trial = TrialStub(new_path) mocker.patch("shutil.copytree") new_lineage = lineage.fork(new_trial) shutil.copytree.assert_called_once_with(path, new_path) assert new_lineage.item.working_dir == new_trial.working_dir assert new_lineage.parent is lineage assert lineage.children[0] is new_lineage
def test_get_true_ancestor_with_jump(self): lineage = LineageNode(1) child_lineage = LineageNode(2, parent=lineage) true_lineage = LineageNode(3) true_lineage.set_jump(child_lineage) assert child_lineage.parent is lineage assert child_lineage.base is true_lineage assert child_lineage.get_true_ancestor() is true_lineage
def test_set_jump_to_old_node(self): parent_lineage = LineageNode(1) child_lineage = LineageNode(2) parent_lineage.set_jump(child_lineage) another_child_lineage = LineageNode(3) with pytest.raises(RuntimeError, match="Trying to jump to an existing node"): another_child_lineage.set_jump(child_lineage) assert child_lineage.parent is None assert child_lineage.jumps == [] assert child_lineage.base is parent_lineage assert another_child_lineage.parent is None assert another_child_lineage.jumps == [] assert another_child_lineage.base is None assert parent_lineage.children == [] assert parent_lineage.jumps == [child_lineage] assert parent_lineage.base is None
def test_set_jump_twice(self): parent_lineage = LineageNode(1) child_lineage = LineageNode(2) parent_lineage.set_jump(child_lineage) another_child_lineage = LineageNode(3) parent_lineage.set_jump(another_child_lineage) assert child_lineage.parent is None assert child_lineage.jumps == [] assert child_lineage.base is parent_lineage assert another_child_lineage.parent is None assert another_child_lineage.jumps == [] assert another_child_lineage.base is parent_lineage assert parent_lineage.children == [] assert parent_lineage.jumps == [child_lineage, another_child_lineage] assert parent_lineage.base is None
def test_get_best_trial_non_completed_root(self): lineage = LineageNode(TrialStub(id="my-id")) assert lineage.get_best_trial() is None
def test_get_best_trial_empty(self): trial = TrialStub(id="id-1", objective=1) lineage = LineageNode(trial) assert lineage.get_best_trial().id == "id-1"
def test_get_true_ancestor_parent_no_jump(self): lineage = LineageNode(1) child_lineage = LineageNode(2, parent=lineage) assert child_lineage.get_true_ancestor() is lineage
def test_get_true_ancestor_no_parent(self): lineage = LineageNode(1) assert lineage.get_true_ancestor() is None