code: def test_n_components(estimator, build_dataset):
      """Check that estimators that have a n_components parameters can use it
      and that it actually works as expected"""
      input_data, labels, _, X = build_dataset()
      model = clone(estimator)
  
      if hasattr(model, "n_components"):
          set_random_state(model)
          model.set_params(n_components=None)
          model.fit(*remove_y(model, input_data, labels))
          assert model.components_.shape == (X.shape[1], X.shape[1])
  
          model = clone(estimator)
          set_random_state(model)
          model.set_params(n_components=X.shape[1] - 1)
          model.fit(*remove_y(model, input_data, labels))
          assert model.components_.shape == (X.shape[1] - 1, X.shape[1])
  
          model = clone(estimator)
          set_random_state(model)
          model.set_params(n_components=X.shape[1] + 1)
          with pytest.raises(ValueError) as expected_err:
              model.fit(*remove_y(model, input_data, labels))
          assert str(
              expected_err.value
          ) == "Invalid n_components, must be in [1, {}]".format(
              X.shape[1]
          )
  
          model = clone(estimator)
          set_random_state(model)
          model.set_params(n_components=0)
          with pytest.raises(ValueError) as expected_err:
              model.fit(*remove_y(model, input_data, labels))
          assert str(
              expected_err.value
          ) == "Invalid n_components, must be in [1, {}]".format(
              X.shape[1]
          )
  
imports: from numpy.core.numeric import array_equal
  import pytest
  import re
  import unittest
  import metric_learn
  import numpy
  from sklearn import clone
  from test.test_utils import ids_metric_learners, metric_learners, remove_y
  from metric_learn.sklearn_shims import set_random_state, SKLEARN_AT_LEAST_0_22
label_desc: Use image node_id 6 for calling a global function with example usage: test_n_components(estimator, build_dataset) without return types