class_docstr: Tests for the KnowledgeBase class.
class_docstr_tok: ["Tests","for","the","KnowledgeBase","class","."]
docstr: Test that the knowledge base retrieves at most `max_trials` trials in total.
docstr_tok: ["Test","that","the","knowledge","base","retrieves","at","most","`","max_trials","`","trials","in","total","."]
code: def test_max_trials_is_respected(
      self, storage: BaseStorageProtocol, tmp_path: Path
  ):
      """Test that the knowledge base retrieves at most `max_trials` trials in total."""
      n_previous_experiments = 2
      n_trials_per_experiment = 10
      max_trials = 15
  
      n_trials_in_kb = n_previous_experiments * n_trials_per_experiment
  
      source_experiments = create_experiments(
          n_previous_experiments=n_previous_experiments,
          n_trials_per_experiment=n_trials_per_experiment,
          storage=storage,
      )
      kb = KnowledgeBase(storage)
  
      target_experiment = build_experiment(
          name="foo",
          space={"x": "uniform(0, 1)"},
          storage={
              "type": "legacy",
              "database": {
                  "type": "pickleddb",
                  "host": f"{tmp_path}/foobar.pkl",
              },
          },
      )
  
      related_trials = kb.get_related_trials(
          target_experiment,
          max_trials=max_trials,
      )
  
      assert len(related_trials) == n_previous_experiments
      for i, (exp_config, trials) in enumerate(related_trials):
          # NOTE: There is a slight mismatch between what comes back from the Storage
          # and the configuration property of the actual Experiment object.
          # For instance, the `working_dir` property goes from None to "", and such.
          # Hence we can't do the full comparison like this:
          # assert exp_config == source_experiments[i].configuration
  
          assert (
              exp_config["_id"]
              == source_experiments[i].configuration["_id"]
          )
          assert (
              exp_config["name"]
              == source_experiments[i].configuration["name"]
          )
  
      total_fetched_trials = sum(
          len(trials) for _, trials in related_trials
      )
      assert total_fetched_trials == min(n_trials_in_kb, max_trials)
  
file_path: orion/tests/unittests/core/worker/warm_start/test_knowledge_base.py
imports: from __future__ import annotations
  from pathlib import Path
  import pytest
  from orion.benchmark.task import RosenBrock
  from orion.client import build_experiment
  from orion.client.experiment import ExperimentClient
  from orion.core.io.database.pickleddb import PickledDB
  from orion.core.worker.experiment_config import ExperimentConfig
  from orion.core.worker.warm_start import KnowledgeBase
  from orion.storage.base import BaseStorageProtocol
  from orion.storage.legacy import Legacy
label_desc: Use image node_id 2 for calling the TestKnowledgeBase obj's underlying member method code with example usage: obj.test_max_trials_is_respected(storage, tmp_path) without return types