已合并
test(hub): add test cases for torch.hub.help and torch.hub._get_torch_home APIs #34080
xiemingda1002创建于 4月21日
test(hub): add test cases for torch.hub.help and torch.hub._get_torch_home APIs #34080
已合并
xiemingda1002创建于 4月21日
已删除 :add-test-hub-help-api-v2.10.0合入到Ascend/pytorchv2.10.0
1 个文件变更+96-0
Atest/test_hub_api.py+96-0
@@ -0,0 +1,96 @@
1+"""
2+Add validation cases for torch.hub APIs on NPU:
3+1. test/test_hub.py from PyTorch community lacks sufficient API validations for torch.hub.help and torch.hub._get_torch_home, so this file is added.
4+2. This file validates torch.hub.help, torch.hub._get_torch_home (extendable).
5+"""
6+ 
7+import os
8+import sys
9+import tempfile
10+import unittest
11+from unittest.mock import patch
12+ 
13+import torch.hub as hub
14+from torch.testing._internal.common_utils import TestCase, run_tests
15+ 
16+ 
17+class TestHubHelp(TestCase):
18+ """Test torch.hub.help API with zero external dependency."""
19+ 
20+ def setUp(self):
21+ self.mock_repo_obj = tempfile.TemporaryDirectory()
22+ self.mock_repo = self.mock_repo_obj.name
23+ self.addCleanup(self.mock_repo_obj.cleanup)
24+ 
25+ # Clean up sys.path pollution after each test
26+ self.addCleanup(lambda: sys.path.remove(self.mock_repo) if self.mock_repo in sys.path else None)
27+ 
28+ hubconf_path = os.path.join(self.mock_repo, "hubconf.py")
29+ with open(hubconf_path, "w", encoding="utf-8") as f:
30+ f.write(
31+ 'def entry_with_docstring():\n'
32+ ' """This is a mock docstring containing EfficientNet info."""\n'
33+ ' pass\n\n'
34+ 'def entry_without_docstring():\n'
35+ ' pass\n'
36+ )
37+ os.makedirs(os.path.join(self.mock_repo, ".git"), exist_ok=True)
38+ 
39+ def test_help_function_callable(self):
40+ """Verify help function exists and is callable."""
41+ self.assertTrue(hasattr(hub, "help"))
42+ self.assertTrue(callable(hub.help))
43+ 
44+ @patch("torch.hub._get_cache_or_reload")
45+ def test_help_returns_none_without_docstring(self, mock_get_repo):
46+ """Verify help returns None when entrypoint has no docstring."""
47+ mock_get_repo.return_value = self.mock_repo
48+ docstring = hub.help(
49+ "mock/local_repo",
50+ "entry_without_docstring",
51+ force_reload=False,
52+ trust_repo=True,
53+ )
54+ self.assertIsNone(docstring)
55+ 
56+ @patch("torch.hub._get_cache_or_reload")
57+ def test_help_returns_docstring_with_content(self, mock_get_repo):
58+ """Verify help returns valid docstring when entrypoint has docstring."""
59+ mock_get_repo.return_value = self.mock_repo
60+ docstring = hub.help(
61+ "mock/local_repo",
62+ "entry_with_docstring",
63+ force_reload=False,
64+ trust_repo=True,
65+ )
66+ self.assertIsInstance(docstring, str)
67+ self.assertTrue(len(docstring) > 0)
68+ self.assertIn("EfficientNet", docstring)
69+ 
70+ 
71+class TestHubGetTorchHome(TestCase):
72+ """Test torch.hub._get_torch_home API"""
73+ 
74+ def test_get_torch_home_returns_path(self):
75+ """Verify _get_torch_home returns a valid path string."""
76+ torch_home = hub._get_torch_home()
77+ self.assertIsInstance(torch_home, str)
78+ self.assertTrue(len(torch_home) > 0)
79+ 
80+ def test_get_torch_home_with_env_variable(self):
81+ """Verify _get_torch_home respects TORCH_HOME environment variable."""
82+ original = os.environ.get("TORCH_HOME")
83+ self.addCleanup(
84+ lambda: os.environ.pop("TORCH_HOME", None) if original is None
85+ else os.environ.__setitem__("TORCH_HOME", original)
86+ )
87+ 
88+ with tempfile.TemporaryDirectory() as tmpdir:
89+ os.environ["TORCH_HOME"] = tmpdir
90+ if hasattr(hub._get_torch_home, "cache_clear"):
91+ hub._get_torch_home.cache_clear()
92+ self.assertEqual(hub._get_torch_home(), tmpdir)
93+ 
94+ 
95+if __name__ == "__main__":
96+ run_tests()