async def test_refresh_cached_credentials_success(
      self, mock_request_class, manager, mock_tool_context
  ):
    """Test successful refresh of expired credentials retrieved from cache.

    This tests the interaction between caching and refresh functionality,
    ensuring that expired cached credentials can be refreshed properly.
    """
    # Manager starts with no default credentials
    manager.credentials_config.credentials = None

    # Create mock cached credentials JSON
    mock_cached_creds_json = json.dumps({
        "token": "expired_token",
        "refresh_token": "valid_refresh_token",
        "client_id": "test_client_id",
        "client_secret": "test_client_secret",
    })

    mock_refreshed_creds_json = json.dumps({
        "token": "new_token",
        "refresh_token": "valid_refresh_token",
        "client_id": "test_client_id",
        "client_secret": "test_client_secret",
    })

    # Set up the tool context state to contain cached credentials
    mock_tool_context.state[BIGQUERY_TOKEN_CACHE_KEY] = mock_cached_creds_json

    # Create expired cached credentials with refresh token
    mock_cached_creds = Mock(spec=Credentials)
    mock_cached_creds.valid = False
    mock_cached_creds.expired = True
    mock_cached_creds.refresh_token = "valid_refresh_token"
    mock_cached_creds.to_json.return_value = mock_refreshed_creds_json

    # Mock successful refresh
    def mock_refresh(request):
      mock_cached_creds.valid = True

    mock_cached_creds.refresh = Mock(side_effect=mock_refresh)

    # Mock the Credentials.from_authorized_user_info method
    with patch(
        "google.oauth2.credentials.Credentials.from_authorized_user_info"
    ) as mock_from_json:
      mock_from_json.return_value = mock_cached_creds

      result = await manager.get_valid_credentials(mock_tool_context)

      # Verify credentials were created from cached JSON
      mock_from_json.assert_called_once_with(
          json.loads(mock_cached_creds_json), manager.credentials_config.scopes
      )
      # Verify refresh was attempted and succeeded
      mock_cached_creds.refresh.assert_called_once()
      # Verify refreshed credentials were not cached into manager
      assert manager.credentials_config.credentials is None
      # Verify refreshed credentials were cached
      assert (
          "new_token"
          == json.loads(mock_tool_context.state[BIGQUERY_TOKEN_CACHE_KEY])[
              "token"
          ]
      )
      assert result == mock_cached_creds
Test successful refresh of expired credentials retrieved from cache.

    This tests the interaction between caching and refresh functionality,
    ensuring that expired cached credentials can be refreshed properly.
    
tests/unittests/tools/bigquery/test_bigquery_credentials_manager.py