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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. 'text_generation_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(list(set(imports.keys()) - set(['torch'])), [])
  35. self.assertEqual(len(from_imports.keys()), 10)
  36. self.assertTrue(from_imports['modelscope.metainfo'] is not None)
  37. self.assertEqual(from_imports['modelscope.metainfo'], ['Pipelines'])
  38. self.assertEqual(decorators,
  39. [('PIPELINES', 'text-generation', 'text-generation')])
  40. def test_files_scaning_method(self):
  41. fileScaner = FilesAstScaning()
  42. output = fileScaner.get_files_scan_results()
  43. self.assertTrue(output['index'] is not None)
  44. self.assertTrue(output['requirements'] is not None)
  45. index, requirements = output['index'], output['requirements']
  46. self.assertIsInstance(index, dict)
  47. self.assertIsInstance(requirements, dict)
  48. self.assertIsInstance(list(index.keys())[0], tuple)
  49. index_0 = list(index.keys())[0]
  50. self.assertIsInstance(index[index_0], dict)
  51. self.assertTrue(index[index_0]['imports'] is not None)
  52. self.assertIsInstance(index[index_0]['imports'], list)
  53. self.assertTrue(index[index_0]['module'] is not None)
  54. self.assertIsInstance(index[index_0]['module'], str)
  55. index_0 = list(requirements.keys())[0]
  56. self.assertIsInstance(requirements[index_0], list)
  57. def test_file_mtime_md5_method(self):
  58. fileScaner = FilesAstScaning()
  59. # create first file
  60. with open(self.test_file, 'w', encoding='utf-8') as f:
  61. f.write('This is the new test!')
  62. md5_1 = fileScaner.files_mtime_md5(self.tmp_dir, [])
  63. md5_2 = fileScaner.files_mtime_md5(self.tmp_dir, [])
  64. self.assertEqual(md5_1, md5_2)
  65. time.sleep(2)
  66. # case of revise
  67. with open(self.test_file, 'w', encoding='utf-8') as f:
  68. f.write('test again')
  69. md5_3 = fileScaner.files_mtime_md5(self.tmp_dir, [])
  70. self.assertNotEqual(md5_1, md5_3)
  71. # case of create
  72. self.test_file_new = os.path.join(self.tmp_dir, 'test_1.py')
  73. time.sleep(2)
  74. with open(self.test_file_new, 'w', encoding='utf-8') as f:
  75. f.write('test again')
  76. md5_4 = fileScaner.files_mtime_md5(self.tmp_dir, [])
  77. self.assertNotEqual(md5_1, md5_4)
  78. self.assertNotEqual(md5_3, md5_4)
  79. if __name__ == '__main__':
  80. unittest.main()