Repository navigation
refactor: enhance model validation and loading logic in drawers - #7839
Conversation
| credential={"api_base": "https://api.example.com/v1", "api_key": "secret"}, | ||
| allow_builtin_fallback=False, | ||
| ) | ||
| cache_model_list.assert_called_once() |
There was a problem hiding this comment.
Here's a review of the added test code:
Issues / Observations
-
Whitespace-only credential handling is implicit but unverified.
test_model_list_post_requires_provider_credential_fieldspasses"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-onlyapi_keyto lock in stripping for both fields, or add a separate test ifstrip()isn't guaranteed. -
Magic strings duplicated across tests.
"test_provider", field names, and labels are repeated. Extract constants or a sharedsetUp/helper to avoid drift between the two tests. -
Patch target string duplication.
"models_provider.views.provide.ModelProvideConstants"appears twice. A module-level constant likePATCH_PROVIDER_CONSTANTS = "..."improves maintainability and typo safety. -
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. -
provider.get_model_type_list.return_valueis 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 ofmodel_typeagainst this list exists, add a negative test for an invalidmodel_type. -
Mocking external constants vs. injectable dependency.
Patching a module-global dict (ModelProvideConstants) couples the test to implementation detail. Consider injecting the provider registry intoModelList(constructor/class attr) for cleaner, less brittle tests. -
SimpleNamespace(data=...)for request.
Works, but using Django'sRequestFactory(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). -
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?
- Unknown
-
Assertion style inconsistency.
MixesassertEqual(200, response.status_code)withassertIn(..., str(...)). Prefer consistentself.assertEqual(response.status_code, 200)ordering; also asserting the body structure (not juststr(context.exception.message)) would be stronger (e.g., structured error codes/fields). -
allow_builtin_fallback=Falseis asserted but not tested in the alternative branch.
Add a test whereallow_builtin_fallbackis passed asTruein the request to cover that path. -
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. -
No assertion that
get_model_credential().to_form_list()is called once.
Addingprovider.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 providerThen 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.
refactor: enhance model validation and loading logic in drawers