| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- """Model downloads are stubbed: assert asset scope and failed validation behavior."""
- import importlib.util
- from pathlib import Path
- import sys
- import tempfile
- from types import SimpleNamespace
- import unittest
- from unittest.mock import Mock, patch
- SPEC = importlib.util.spec_from_file_location('deploy_models', Path(__file__).parents[1] / 'deploy_models.py')
- models = importlib.util.module_from_spec(SPEC)
- SPEC.loader.exec_module(models)
- class DownloadTests(unittest.TestCase):
- def setUp(self):
- self.temp = tempfile.TemporaryDirectory()
- self.addCleanup(self.temp.cleanup)
- self.enterContext(patch.object(models, 'ROOT', Path(self.temp.name)))
- self.hub = SimpleNamespace(snapshot_download=Mock(), hf_hub_download=Mock())
- self.enterContext(patch.dict(sys.modules, huggingface_hub=self.hub))
- def test_valid_local_assets_skip_network(self):
- with patch.object(models, 'bge_valid', return_value=True), \
- patch.object(models, 'tokenizer_valid', return_value=True):
- models.download()
- self.hub.snapshot_download.assert_not_called()
- self.hub.hf_hub_download.assert_not_called()
- def test_fresh_server_downloads_only_bge_and_deepseek_tokenizer(self):
- with patch.object(models, 'bge_valid', side_effect=[False, True]), \
- patch.object(models, 'tokenizer_valid', side_effect=[False, True]):
- models.download()
- self.assertEqual(self.hub.snapshot_download.call_args.args, ('BAAI/bge-large-zh-v1.5',))
- patterns = self.hub.snapshot_download.call_args.kwargs['allow_patterns']
- self.assertIn('pytorch_model.bin', patterns)
- self.assertNotIn('*.py', patterns)
- self.assertEqual(self.hub.hf_hub_download.call_args.args,
- ('deepseek-ai/DeepSeek-V3', 'tokenizer.json'))
- def test_broken_download_is_not_success(self):
- with patch.object(models, 'bge_valid', return_value=False):
- with self.assertRaisesRegex(ValueError, 'bge_model_validation_failed'):
- models.download()
- self.hub.hf_hub_download.assert_not_called()
- def test_broken_tokenizer_is_not_success(self):
- with patch.object(models, 'bge_valid', return_value=True), \
- patch.object(models, 'tokenizer_valid', return_value=False):
- with self.assertRaisesRegex(ValueError, 'summary_tokenizer_validation_failed'):
- models.download()
- def test_active_model_validation_never_repairs_or_downloads(self):
- with patch.object(models, 'bge_valid', return_value=False):
- with self.assertRaisesRegex(ValueError, 'stop_before_repair'):
- models.download(check_only=True)
- self.hub.snapshot_download.assert_not_called()
- self.hub.hf_hub_download.assert_not_called()
- if __name__ == '__main__':
- unittest.main()
|