Skip to content

refactor: enhance model validation and loading logic in drawers - #7839

Merged
wxg0103 merged 1 commit into
v3from
pr@v3@refactor_get_model_list
Nov 11, 2026
Merged

wxg0103 merged 1 commit into
v3from
pr@v3@refactor_get_model_list

Conversation

@shaohuzhang1

Copy link
Copy Markdown
Contributor

refactor: enhance model validation and loading logic in drawers

credential={"api_base": "https://api.example.com/v1", "api_key": "secret"},
allow_builtin_fallback=False,
)
cache_model_list.assert_called_once()

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's a review of the added test code:

Issues / Observations

  1. Whitespace-only credential handling is implicit but unverified.
    test_model_list_post_requires_provider_credential_fields passes "api_base": " " (a space) and expects it to be treated as missing. This is fine if the production code strips values — but the test relies on that undocumented behavior. Consider also asserting a whitespace-only api_key to lock in stripping for both fields, or add a separate test if strip() isn't guaranteed.

  2. Magic strings duplicated across tests.
    "test_provider", field names, and labels are repeated. Extract constants or a shared setUp/helper to avoid drift between the two tests.

  3. Patch target string duplication.
    "models_provider.views.provide.ModelProvideConstants" appears twice. A module-level constant like PATCH_PROVIDER_CONSTANTS = "..." improves maintainability and typo safety.

  4. Weak assertion on cache call.
    cache_model_list.assert_called_once() doesn't verify arguments. Assert the payload (model list, provider, type, credentials) to ensure caching keyed correctly — a bug could cache under the wrong provider otherwise.

  5. provider.get_model_type_list.return_value is never asserted.
    It's set up but not verified. Either assert it was called with expected args, or remove the setup if the endpoint doesn't require validation here. If validation of model_type against this list exists, add a negative test for an invalid model_type.

  6. Mocking external constants vs. injectable dependency.
    Patching a module-global dict (ModelProvideConstants) couples the test to implementation detail. Consider injecting the provider registry into ModelList (constructor/class attr) for cleaner, less brittle tests.

  7. SimpleNamespace(data=...) for request.
    Works, but using Django's RequestFactory (or the actual framework's test client) would exercise middleware/serialization and catch real integration issues (e.g., multi-value form fields, non-string types).

  8. Only success/validation paths covered.
    Missing tests worth adding:

    • Unknown provider → appropriate error (404/BadRequest).
    • Invalid/missing model_type.
    • Exception raised inside provider.get_model_list → how is it propagated/wrapped?
    • Empty model list result → still cached? correct shape returned?
  9. Assertion style inconsistency.
    Mixes assertEqual(200, response.status_code) with assertIn(..., str(...)). Prefer consistent self.assertEqual(response.status_code, 200) ordering; also asserting the body structure (not just str(context.exception.message)) would be stronger (e.g., structured error codes/fields).

  10. allow_builtin_fallback=False is asserted but not tested in the alternative branch.
    Add a test where allow_builtin_fallback is passed as True in the request to cover that path.

  11. Potential naming confusion.
    test_model_list_returns_provider_models (existing) suggests GET-like behavior; new tests cover POST. Confirm the method overload is intentional, or split into separate endpoints/tests for clarity.

  12. No assertion that get_model_credential().to_form_list() is called once.
    Adding provider.get_model_credential.assert_called_once() guards against redundant instantiation.

Suggested Minimal Refactor

PROVIDER_PATCH = "models_provider.views.provide.ModelProvideConstants"

def _make_provider(self):
    provider = MagicMock()
    provider.get_model_type_list.return_value = [{"value": "EMBEDDING"}]
    provider.get_model_credential.return_value.to_form_list.return_value = [
        {"field": "api_base", "label": "API URL", "required": True},
        {"field": "api_key", "label": "API Key", "required": True},
    ]
    return provider

Then use patch(PROVIDER_PATCH, ...) consistently and assert cached payload:

cache_model_list.assert_called_once_with(
    provider="test_provider",
    model_type="EMBEDDING",
    model_list=[{"name": "text-embedding-3-small"}],
)

Overall the tests are reasonably focused; main improvements are stronger assertions on cache arguments, reducing duplication, and adding coverage for error/fallback branches.

@wxg0103
wxg0103 merged commit bc20faf into v3 Nov 11, 2026
3 of 4 checks passed
@wxg0103
wxg0103 deleted the pr@v3@refactor_get_model_list branch November 11, 2026 10:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants