feat: Update test mocks for google-genai SDK (#231) #235

Closed
opened 2026-02-20 15:11:12 +00:00 by egullickson · 2 comments
Owner

Relates to #231

Update ocr/tests/test_gemini_engine.py to match new SDK internals:

  • Replace engine._model / engine._generation_config mocks with engine._client / engine._model_name
  • Mock engine._client.models.generate_content instead of mock_model.generate_content
  • Update sys.modules patches: replace vertexai/google.cloud.aiplatform with google.genai
  • Update test_missing_sdk_raises_unavailable to test google.genai import failure

Acceptance Criteria

  • All tests pass with new mock patterns
  • No references to old SDK module names in test patches
  • test_vin_decode.py and test_manual_extractor.py confirmed unchanged (they mock at router/engine-instance level)

File

ocr/tests/test_gemini_engine.py

Relates to #231 Update `ocr/tests/test_gemini_engine.py` to match new SDK internals: - Replace `engine._model` / `engine._generation_config` mocks with `engine._client` / `engine._model_name` - Mock `engine._client.models.generate_content` instead of `mock_model.generate_content` - Update `sys.modules` patches: replace `vertexai`/`google.cloud.aiplatform` with `google.genai` - Update `test_missing_sdk_raises_unavailable` to test `google.genai` import failure ## Acceptance Criteria - [ ] All tests pass with new mock patterns - [ ] No references to old SDK module names in test patches - [ ] `test_vin_decode.py` and `test_manual_extractor.py` confirmed unchanged (they mock at router/engine-instance level) ## File `ocr/tests/test_gemini_engine.py`
egullickson added the
status
in-progress
type
feature
labels 2026-02-20 15:11:21 +00:00
egullickson added this to the Sprint 2026-02-02 milestone 2026-02-20 15:11:24 +00:00
Author
Owner

Plan: M4 -- Update tests (#235)

Phase: Planning | Agent: Planner | Status: APPROVED
Parent: #231 | Revision: v4


Context

The test file ocr/tests/test_gemini_engine.py mocks the old vertexai.generative_models SDK internals. After M2 (#233) and M3 (#234) change the production code, these mocks must be updated to match the new google.genai SDK patterns.

Codebase Analysis

File SDK References Action
ocr/tests/test_gemini_engine.py 2 tests use sys.modules patches with old module names; others set engine._model directly Update mock patterns
ocr/tests/test_vin_decode.py None (mocks at router level) No changes
ocr/tests/test_manual_extractor.py None (mocks at engine instance level) No changes
ocr/tests/test_resolve_vin_year.py Imports resolve_vin_year only (pure function, no SDK) No changes -- file is currently untracked; commit before beginning M1 to avoid loss during branch operations

Internal State Changes (old -> new mock targets)

Tests that set engine._model and engine._generation_config must change to:

engine._client = MagicMock()  # with .models.generate_content return value
engine._model_name = "gemini-2.5-flash"

Implementation

  • File: ocr/tests/test_gemini_engine.py
  • Update module docstring (L5): replace "All Vertex AI SDK calls are mocked" with "All google-genai SDK calls are mocked"
  • All tests that set engine._model and engine._generation_config -> set engine._client (MagicMock with .models.generate_content return value) and engine._model_name = "gemini-2.5-flash"
  • test_valid_pdf_returns_structured_schedules: update BOTH the sys.modules patch (replace vertexai/google.cloud.aiplatform entries with google.genai entries) AND the engine._model -> engine._client / engine._model_name field reassignments. Clean up dead if False branch (L162).
  • test_missing_sdk_raises_unavailable: change patch to patch.dict("sys.modules", {"google.genai": None}) to simulate missing new SDK
  • Verify test_vin_decode.py, test_manual_extractor.py, and test_resolve_vin_year.py pass unchanged

Review Findings

QR plan-completeness:

  • [RULE 1] HIGH: Untracked test_resolve_vin_year.py acknowledged -- commit before M1 branch operations

TW plan-scrub:

  • PRECISION: test_valid_pdf_returns_structured_schedules needs BOTH sys.modules patch update AND engine._model -> engine._client field reassignments -- clarified
  • PRECISION: test_missing_sdk_raises_unavailable exact patch: patch.dict("sys.modules", {"google.genai": None}) -- specified

QR plan-code:

  • [RULE 2] SHOULD_FIX: Dead code if False branch at L162 -- clean up during this milestone

QR plan-docs:

  • [RULE 2] SHOULD_FIX: Test module docstring (L5) -- included in implementation

Verdict: APPROVED | Next: Execute (depends on M2 #233 and M3 #234)

## Plan: M4 -- Update tests (#235) **Phase**: Planning | **Agent**: Planner | **Status**: APPROVED **Parent**: #231 | **Revision**: v4 --- ### Context The test file `ocr/tests/test_gemini_engine.py` mocks the old `vertexai.generative_models` SDK internals. After M2 (#233) and M3 (#234) change the production code, these mocks must be updated to match the new `google.genai` SDK patterns. ### Codebase Analysis | File | SDK References | Action | |------|---------------|--------| | `ocr/tests/test_gemini_engine.py` | 2 tests use `sys.modules` patches with old module names; others set `engine._model` directly | Update mock patterns | | `ocr/tests/test_vin_decode.py` | None (mocks at router level) | No changes | | `ocr/tests/test_manual_extractor.py` | None (mocks at engine instance level) | No changes | | `ocr/tests/test_resolve_vin_year.py` | Imports `resolve_vin_year` only (pure function, no SDK) | No changes -- file is currently untracked; commit before beginning M1 to avoid loss during branch operations | ### Internal State Changes (old -> new mock targets) Tests that set `engine._model` and `engine._generation_config` must change to: ```python engine._client = MagicMock() # with .models.generate_content return value engine._model_name = "gemini-2.5-flash" ``` ### Implementation - File: `ocr/tests/test_gemini_engine.py` - Update module docstring (L5): replace "All Vertex AI SDK calls are mocked" with "All google-genai SDK calls are mocked" - All tests that set `engine._model` and `engine._generation_config` -> set `engine._client` (MagicMock with `.models.generate_content` return value) and `engine._model_name = "gemini-2.5-flash"` - `test_valid_pdf_returns_structured_schedules`: update BOTH the `sys.modules` patch (replace `vertexai`/`google.cloud.aiplatform` entries with `google.genai` entries) AND the `engine._model` -> `engine._client` / `engine._model_name` field reassignments. Clean up dead `if False` branch (L162). - `test_missing_sdk_raises_unavailable`: change patch to `patch.dict("sys.modules", {"google.genai": None})` to simulate missing new SDK - Verify `test_vin_decode.py`, `test_manual_extractor.py`, and `test_resolve_vin_year.py` pass unchanged ### Review Findings **QR plan-completeness:** - [RULE 1] HIGH: Untracked `test_resolve_vin_year.py` acknowledged -- commit before M1 branch operations **TW plan-scrub:** - PRECISION: `test_valid_pdf_returns_structured_schedules` needs BOTH `sys.modules` patch update AND `engine._model` -> `engine._client` field reassignments -- clarified - PRECISION: `test_missing_sdk_raises_unavailable` exact patch: `patch.dict("sys.modules", {"google.genai": None})` -- specified **QR plan-code:** - [RULE 2] SHOULD_FIX: Dead code `if False` branch at L162 -- clean up during this milestone **QR plan-docs:** - [RULE 2] SHOULD_FIX: Test module docstring (L5) -- included in implementation --- *Verdict*: APPROVED | *Next*: Execute (depends on M2 #233 and M3 #234)
Author
Owner

Milestone: M4 Complete -- Update test mocks

Phase: Execution | Agent: Developer | Status: PASS


Changes

  • ocr/tests/test_gemini_engine.py: Updated all mock patterns for new SDK
    • All engine._model -> engine._client (MagicMock with .models.generate_content)
    • All engine._generation_config -> engine._model_name = "gemini-2.5-flash"
    • test_valid_pdf_returns_structured_schedules: Replaced old sys.modules patch with google.genai entries, removed dead if False branch
    • test_missing_sdk_raises_unavailable: Patches google and google.genai as None
    • test_model_is_none_after_construction -> test_client_is_none_after_construction
    • test_model_reused_on_second_call -> test_client_reused_on_second_call
    • Updated module docstring
    • Removed unused PropertyMock import

Test Results

All 18 gemini engine tests pass. All VIN decode and manual extractor tests pass unchanged (they mock at router/engine-instance level).

Acceptance Criteria

  • All tests pass with new mock patterns
  • No references to old SDK module names in test patches
  • test_vin_decode.py and test_manual_extractor.py confirmed unchanged

Verdict: PASS | Next: M5 -- Doc-sync

## Milestone: M4 Complete -- Update test mocks **Phase**: Execution | **Agent**: Developer | **Status**: PASS --- ### Changes - `ocr/tests/test_gemini_engine.py`: Updated all mock patterns for new SDK - All `engine._model` -> `engine._client` (MagicMock with `.models.generate_content`) - All `engine._generation_config` -> `engine._model_name = "gemini-2.5-flash"` - `test_valid_pdf_returns_structured_schedules`: Replaced old sys.modules patch with `google.genai` entries, removed dead `if False` branch - `test_missing_sdk_raises_unavailable`: Patches `google` and `google.genai` as None - `test_model_is_none_after_construction` -> `test_client_is_none_after_construction` - `test_model_reused_on_second_call` -> `test_client_reused_on_second_call` - Updated module docstring - Removed unused `PropertyMock` import ### Test Results All 18 gemini engine tests pass. All VIN decode and manual extractor tests pass unchanged (they mock at router/engine-instance level). ### Acceptance Criteria - [x] All tests pass with new mock patterns - [x] No references to old SDK module names in test patches - [x] `test_vin_decode.py` and `test_manual_extractor.py` confirmed unchanged --- *Verdict*: PASS | *Next*: M5 -- Doc-sync
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: egullickson/motovaultpro#235