docstr: Tests basic validation of SpecDict.
docstr_tok: ["Tests","basic","validation","of","SpecDict","."]
code: def test_basic_validation(self):
      """Tests basic validation of SpecDict."""
  
      h1, h2 = 3, 4
      spec_1 = SpecDict(
          {
              "out_tensor_1": TensorSpec("b, h", h=h1, framework="np"),
              "out_tensor_2": TensorSpec("b, h", h=h2, framework="np"),
              "out_class_1": TypeClass1,
          }
      )
  
      # test validation.
      tensor_1 = {
          "out_tensor_1": np.random.randn(2, h1),
          "out_tensor_2": np.random.randn(2, h2),
          "out_class_1": TypeClass1(),
      }
  
      spec_1.validate(tensor_1)
  
      # test missing key in tensor
      tensor_2 = {
          "out_tensor_1": np.random.randn(2, h1),
          "out_tensor_2": np.random.randn(2, h2),
      }
  
      self.assertRaises(ValueError, lambda: spec_1.validate(tensor_2))
  
      # test additional key in tensor (not mentioned in spec)
      tensor_3 = {
          "out_tensor_1": np.random.randn(2, h1),
          "out_tensor_2": np.random.randn(2, h2),
          "out_class_1": TypeClass1(),
          "out_class_2": TypeClass1(),
      }
  
      # this should pass because exact_match is False
      spec_1.validate(tensor_3, exact_match=False)
  
      # this should fail because exact_match is True
      self.assertRaises(
          ValueError,
          lambda: spec_1.validate(tensor_3, exact_match=True),
      )
  
      # raise type mismatch
      tensor_4 = {
          "out_tensor_1": np.random.randn(2, h1),
          "out_tensor_2": np.random.randn(2, h2),
          "out_class_1": TypeClass2(),
      }
  
      self.assertRaises(ValueError, lambda: spec_1.validate(tensor_4))
  
      # test nested specs
      spec_2 = SpecDict(
          {
              "encoder": {
                  "input": TensorSpec("b, h", h=h1, framework="np"),
                  "output": TensorSpec("b, h", h=h2, framework="np"),
              },
              "decoder": {
                  "input": TensorSpec("b, h", h=h2, framework="np"),
                  "output": TensorSpec("b, h", h=h1, framework="np"),
              },
          }
      )
  
      tensor_5 = {
          "encoder": {
              "input": np.random.randn(2, h1),
              "output": np.random.randn(2, h2),
          },
          "decoder": {
              "input": np.random.randn(2, h2),
              "output": np.random.randn(2, h1),
          },
      }
  
      spec_2.validate(tensor_5)
  
file_path: ray/rllib/core/models/specs/tests/test_spec_dict.py
imports: import unittest
  import numpy
  from ray.rllib.core.models.specs.specs_base import TensorSpec
  from ray.rllib.core.models.specs.specs_dict import SpecDict
  from ray.rllib.core.models.specs.checker import check_input_specs, convert_to_canonical_format
  from ray.rllib.core.models.specs.checker import SpecCheckingError
label_desc: Use image node_id 1 for calling the TestSpecDict obj's underlying member method code with example usage: obj.test_basic_validation() without return types