Repository navigation
fix: enhance model list retrieval to fallback on built-in models when dynamic list is empty - #7929
Conversation
… dynamic list is empty
| credential = self.provider.get_model_credential("STT", "wand-asr-v1") | ||
| self.assertIsInstance(credential, TencentTokenhubSTTModelCredential) | ||
| fields = {item.get("field") for item in credential.to_form_list()} | ||
| self.assertIn("api_key", fields) |
There was a problem hiding this comment.
Overall, the added tests are syntactically fine and cover the intended Tencent STT credential paths. Main concerns are test isolation, strictness of assertions, and Django test class choice.
Potential issues / suggestions
-
SimpleTestCasemay be too restrictiveSimpleTestCasedisallows database access.- If
TencentModelProvider()orcredential.to_form_list()touches the DB, cache, settings requiring DB, or network, these tests may fail or behave inconsistently. - If no DB access is involved,
SimpleTestCaseis appropriate. Otherwise useTestCaseor mock external dependencies.
-
Shared provider instance across tests
cls.provider = TencentModelProvider()insetUpClassmeans all tests share one instance.- If the provider caches credentials or has mutable state, tests may become order-dependent.
- Safer:
def setUp(self): self.provider = TencentModelProvider()
- If the provider is guaranteed stateless, the current approach is acceptable for speed.
-
Default-credential test is too permissive
- This only checks that the credential is not
Noneand that its form list is non-empty:self.assertIsNotNone(credential) self.assertTrue(credential.to_form_list())
- This could pass even if the wrong default credential is returned.
- Better to assert the expected credential type and/or expected fields:
self.assertIsNotNone(credential) self.assertIsInstance(credential, ExpectedDefaultSTTCredential) fields = {item["field"] for item in credential.to_form_list()} self.assertIn("api_key", fields)
- Adjust to the actual default credential class.
- This only checks that the credential is not
-
Clarify empty string vs. missing model name
- The test name says
without_model_name, but it passes"", notNone. - Confirm the real contract. If both
""andNoneare valid “no model selected” inputs, test both. If only one is valid, rename the test to match, e.g.test_stt_default_credential_with_empty_model_name.
- The test name says
-
Local import inside test method
- This import:
is fine functionally, but normally belongs at module level.
from models_provider.impl.tencent_model_provider.credential.tokenhub_stt import ( TencentTokenhubSTTModelCredential, )
- Move it to the top unless it is intentionally local to avoid a circular import. If so, add a short comment explaining why.
- This import:
-
Clearer failure for
assertIsInstanceassertIsInstance(credential, ...)will fail clearly ifcredentialisNone, but adding:before it can make the failure reason clearer.self.assertIsNotNone(credential)
-
Missing negative coverage
- Consider adding tests for:
- unsupported model type,
- unsupported model name,
- empty model name for non-STT types.
- This depends on the expected behavior of
get_model_credential.
- Consider adding tests for:
-
Minor robustness in form-field extraction
{item.get("field") for item in ...}may silently includeNoneif malformed data is returned.- Using
item["field"]makes malformed entries fail explicitly. This is optional.
Suggested refactor sketch
# -*- coding: utf-8 -*-
from django.test import SimpleTestCase
from models_provider.impl.tencent_model_provider.tencent_model_provider import TencentModelProvider
from models_provider.impl.tencent_model_provider.credential.tokenhub_stt import (
TencentTokenhubSTTModelCredential,
)
class TencentModelProviderTests(SimpleTestCase):
def setUp(self):
self.provider = TencentModelProvider()
def test_stt_model_type_is_exposed(self):
model_types = {item.get("value") for item in self.provider.get_model_type_list()}
self.assertIn("STT", model_types)
def test_stt_default_credential_with_empty_model_name(self):
credential = self.provider.get_model_credential("STT", "")
self.assertIsNotNone(credential)
# Assert the expected default credential type as well.
self.assertTrue(credential.to_form_list())
def test_wand_asr_v1_uses_tokenhub_credential(self):
credential = self.provider.get_model_credential("STT", "wand-asr-v1")
self.assertIsNotNone(credential)
self.assertIsInstance(credential, TencentTokenhubSTTModelCredential)
fields = {item["field"] for item in credential.to_form_list()}
self.assertIn("api_key", fields)No blocking syntax issues were found. The most important checks are whether SimpleTestCase is safe for this provider and whether the default-credential assertion is strong enough.
| elif not allow_builtin_fallback: | ||
| return [] | ||
| return self.get_model_info_manage().get_model_list_by_model_type(model_type) | ||
|
|
There was a problem hiding this comment.
Looking at this diff, here are the observations:
Potential Issues
-
Behavior change / semantic shift: Previously, when
manage is not None, the function always returned the dynamic list (possibly empty) immediately. Now it falls through to the builtin list whendynamic_listis empty andallow_builtin_fallbackis True. This changes behavior for existing callers and should be verified against intended contract. -
Control flow asymmetry between branches:
if manage is not None:branch does NOT return[]whennot allow_builtin_fallback— instead it returnsdynamic_list(which may be empty), effectively matching. That's fine.elif not allow_builtin_fallback:returns[]only whenmanage is None. But ifmanage is Noneandallow_builtin_fallbackis True, it silently falls through to builtin. Confirm that's intended — a failing/absent dynamic manage will now silently use builtins even when the caller asked for "no fallback" indirectly via the credential path. Slight inconsistency:not allow_builtin_fallbacksemantics differ depending on whethermanagewas constructed.
-
Comment language/style: The inline comment is in Chinese while the file/answers convention appears consistent — verify project style guide (some repos require English or i18n comments). Also the comment says "交由视图继续走缓存/内置兜底" which describes caller behavior — this couples the helper to view-layer decisions; consider documenting in docstring instead.
-
Redundant nested check:
if dynamic_list or not allow_builtin_fallback: return dynamic_listfollowed by falling out of theif manage is not Noneblock is correct, but the readability could be improved with an earlyreturn []explicitly for the fallthrough-non-fallback case to make intent obvious.
Optimization / Readability Suggestions
if api_key or credential:
manage = self._get_dynamic_manage_cached(api_key, api_base, credential)
if manage is not None:
dynamic_list = manage.get_model_list_by_model_type(model_type)
if dynamic_list or not allow_builtin_fallback:
return dynamic_list
# else: fall through to builtin list below
elif not allow_builtin_fallback:
return []
return self.get_model_info_manage().get_model_list_by_model_type(model_type)- Consider extracting
builtin = self.get_model_info_manage().get_model_list_by_model_type(model_type)once for reuse. - Add/update docstring to describe
allow_builtin_fallbackprecisely (does it apply whenmanage is None? when dynamic list is empty?). - Add unit tests for the four combinations:
manage present/absent×allow_builtin_fallback True/False, plusdynamic_list empty/non-empty.
Minor
- No null-check on the return value of
get_model_list_by_model_type— ensure it's guaranteed to be list-like (orshort-circuit depends on truthiness;Nonewould behave as empty, likely fine but worth confirming). - The diff header shows
credential=Nonetruncated; confirm signature unchanged elsewhere.
fix: enhance model list retrieval to fallback on built-in models when dynamic list is empty