genkit icon indicating copy to clipboard operation
genkit copied to clipboard

[PY] Some unit tests are conditional upon the existence of `GEMINI_API_KEY`.

Open yesudeep opened this issue 1 year ago • 1 comments

Describe the bug Tests fail with:

=================================== short test summary info ===================================
FAILED plugins/google-genai/test/test_google_plugin.py::TestGoogleAIInit::test_init_raises_value_error_no_api_key - AssertionError: ValueError not raised
FAILED plugins/google-genai/test/test_google_plugin.py::TestGoogleAIInit::test_init_with_credentials - AssertionError: expected call not found.
Expected: Client(vertexai=False, api_key=None, credentials=<MagicMock spec='Credentials' id='NNNNNNNNN'>, debug_config=None, http_options=HttpOptions(base_url=None, api_version=None, headers={'x-goog-api-client': 'genkit-python/0.3.2', 'user-agent': 'genkit-python/0.3.2'}, timeout=None, client_args=None, async_client_args=None))
Actual: Client(vertexai=False, api_key='XXXXXXXXXXXXXXXXXXXXXX', credentials=<MagicMock spec='Credentials' id='NNNNNNNNN'>, debug_config=None, http_options=HttpOptions(base_url=None, api_version=None, headers={'x-goog-api-client': 'genkit-python/0.3.2', 'user-agent': 'genkit-python/0.3.2'}, timeout=None, client_args=None, async_client_args=None))

pytest introspection follows:

Kwargs:
assert {'vertexai': False, 'api_key': 'XXXXXXXXXXXXXXXXXXXXXX', 'credentials': <MagicMock spec='Credentials' id='NNNNNNNNN'>, 'debug_config': None, 'http_options': HttpOptions(base_url=None, api_version=None, headers={'x-goog-api-client': 'genkit-python/0.3.2', 'user-agent': 'genkit-python/0.3.2'}, timeout=None, client_args=None, async_client_args=None)} == {'vertexai': False, 'api_key': None, 'credentials': <MagicMock spec='Credentials' id='NNNNNNNNN'>, 'debug_config': None, 'http_options': HttpOptions(base_url=None, api_version=None, headers={'x-goog-api-client': 'genkit-python/0.3.2', 'user-agent': 'genkit-python/0.3.2'}, timeout=None, client_args=None, async_client_args=None)}

  Common items:
  {'credentials': <MagicMock spec='Credentials' id='NNNNNNNNN'>,
   'debug_config': None,
   'http_options': HttpOptions(base_url=None, api_version=None, headers={'x-goog-api-client': 'genkit-python/0.3.2', 'user-agent': 'genkit-python/0.3.2'}, timeout=None, client_args=None, async_client_args=None),
   'vertexai': False}
  Differing items:
  {'api_key': 'XXXXXXXXXXXXXXXXXXXXXX'} != {'api_key': None}

  Full diff:
    {
  -     'api_key': None,
  +     'api_key': 'XXXXXXXXXXXXXXXXXXXXXX',
        'credentials': <MagicMock spec='Credentials' id='NNNNNNNNN'>,
        'debug_config': None,
        'http_options': HttpOptions(base_url=None, api_version=None, headers={'x-goog-api-client': 'genkit-python/0.3.2', 'user-agent': 'genkit-python/0.3.2'}, timeout=None, client_args=None, async_client_args=None),
        'vertexai': False,
    }
========================= 2 failed, 346 passed, 13 warnings in 12.50s =========================

To Reproduce

  1. Define the GEMINI_API_KEY environment variable with an API key value.

  2. Tests fail.

  3. Unset the GEMINI_API_KEY environment variable.

  4. Tests pass.

Expected behavior Tests should cover both cases (environment variable defined and undefined).

Screenshots If applicable, add screenshots to help explain your problem.

Runtime (please complete the following information):

  • OS: macOS
  • Genkit Version HEAD

Python version

zsh❯ uv run python --version
Python 3.12.8

Additional context Add any other context about the problem here.

yesudeep avatar May 29 '25 01:05 yesudeep

Fixed in #2972. The two affected tests are now hermetic: test_init_raises_value_error_no_api_key clears GEMINI_API_KEY with patch.dict(clear=True), and test_init_with_credentials matches the api key with mock.ANY, so they pass whether or not the variable is exported. Both cases are covered explicitly now, with test_init_from_env_var handling the defined case, and the tests live in py/packages/genkit-google-genai/test/google_plugin_test.py after the package restructure. Closing this

hilariie avatar Jul 23 '26 11:07 hilariie