You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_ast.py 3.8 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # Copyright (c) Alibaba, Inc. and its affiliates.
  2. import os
  3. import shutil
  4. import tempfile
  5. import time
  6. import unittest
  7. from pathlib import Path
  8. from modelscope.utils.ast_utils import AstScaning, FilesAstScaning, load_index
  9. p = Path(__file__)
  10. MODELSCOPE_PATH = p.resolve().parents[2].joinpath('modelscope')
  11. class AstScaningTest(unittest.TestCase):
  12. def setUp(self):
  13. print(('Testing %s.%s' % (type(self).__name__, self._testMethodName)))
  14. self.tmp_dir = tempfile.TemporaryDirectory().name
  15. self.test_file = os.path.join(self.tmp_dir, 'test.py')
  16. if not os.path.exists(self.tmp_dir):
  17. os.makedirs(self.tmp_dir)
  18. def tearDown(self):
  19. super().tearDown()
  20. shutil.rmtree(self.tmp_dir)
  21. def test_ast_scaning_class(self):
  22. astScaner = AstScaning()
  23. pipeline_file = os.path.join(MODELSCOPE_PATH, 'pipelines', 'nlp',
  24. 'sequence_classification_pipeline.py')
  25. output = astScaner.generate_ast(pipeline_file)
  26. self.assertTrue(output['imports'] is not None)
  27. self.assertTrue(output['from_imports'] is not None)
  28. self.assertTrue(output['decorators'] is not None)
  29. imports, from_imports, decorators = output['imports'], output[
  30. 'from_imports'], output['decorators']
  31. self.assertIsInstance(imports, dict)
  32. self.assertIsInstance(from_imports, dict)
  33. self.assertIsInstance(decorators, list)
  34. self.assertListEqual(
  35. list(set(imports.keys()) - set(['typing', 'numpy'])), [])
  36. self.assertEqual(len(from_imports.keys()), 9)
  37. self.assertTrue(from_imports['modelscope.metainfo'] is not None)
  38. self.assertEqual(from_imports['modelscope.metainfo'], ['Pipelines'])
  39. self.assertEqual(
  40. decorators,
  41. [('PIPELINES', 'text-classification', 'sentiment-analysis')])
  42. def test_files_scaning_method(self):
  43. fileScaner = FilesAstScaning()
  44. output = fileScaner.get_files_scan_results()
  45. self.assertTrue(output['index'] is not None)
  46. self.assertTrue(output['requirements'] is not None)
  47. index, requirements = output['index'], output['requirements']
  48. self.assertIsInstance(index, dict)
  49. self.assertIsInstance(requirements, dict)
  50. self.assertIsInstance(list(index.keys())[0], tuple)
  51. index_0 = list(index.keys())[0]
  52. self.assertIsInstance(index[index_0], dict)
  53. self.assertTrue(index[index_0]['imports'] is not None)
  54. self.assertIsInstance(index[index_0]['imports'], list)
  55. self.assertTrue(index[index_0]['module'] is not None)
  56. self.assertIsInstance(index[index_0]['module'], str)
  57. index_0 = list(requirements.keys())[0]
  58. self.assertIsInstance(requirements[index_0], list)
  59. def test_file_mtime_md5_method(self):
  60. fileScaner = FilesAstScaning()
  61. # create first file
  62. with open(self.test_file, 'w', encoding='utf-8') as f:
  63. f.write('This is the new test!')
  64. md5_1 = fileScaner.files_mtime_md5(self.tmp_dir, [])
  65. md5_2 = fileScaner.files_mtime_md5(self.tmp_dir, [])
  66. self.assertEqual(md5_1, md5_2)
  67. time.sleep(2)
  68. # case of revise
  69. with open(self.test_file, 'w', encoding='utf-8') as f:
  70. f.write('test again')
  71. md5_3 = fileScaner.files_mtime_md5(self.tmp_dir, [])
  72. self.assertNotEqual(md5_1, md5_3)
  73. # case of create
  74. self.test_file_new = os.path.join(self.tmp_dir, 'test_1.py')
  75. time.sleep(2)
  76. with open(self.test_file_new, 'w', encoding='utf-8') as f:
  77. f.write('test again')
  78. md5_4 = fileScaner.files_mtime_md5(self.tmp_dir, [])
  79. self.assertNotEqual(md5_1, md5_4)
  80. self.assertNotEqual(md5_3, md5_4)
  81. if __name__ == '__main__':
  82. unittest.main()