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_file.py 2.3 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # Copyright (c) Alibaba, Inc. and its affiliates.
  2. import os
  3. import tempfile
  4. import unittest
  5. from requests import HTTPError
  6. from maas_lib.fileio.file import File, HTTPStorage, LocalStorage
  7. class FileTest(unittest.TestCase):
  8. def test_local_storage(self):
  9. storage = LocalStorage()
  10. temp_name = tempfile.gettempdir() + '/' + next(
  11. tempfile._get_candidate_names())
  12. binary_content = b'12345'
  13. storage.write(binary_content, temp_name)
  14. self.assertEqual(binary_content, storage.read(temp_name))
  15. content = '12345'
  16. storage.write_text(content, temp_name)
  17. self.assertEqual(content, storage.read_text(temp_name))
  18. os.remove(temp_name)
  19. def test_http_storage(self):
  20. storage = HTTPStorage()
  21. url = 'http://pai-vision-data-hz.oss-cn-zhangjiakou.aliyuncs.com' \
  22. '/data/test/data.txt'
  23. content = 'this is test data'
  24. self.assertEqual(content.encode('utf8'), storage.read(url))
  25. self.assertEqual(content, storage.read_text(url))
  26. with storage.as_local_path(url) as local_file:
  27. with open(local_file, 'r') as infile:
  28. self.assertEqual(content, infile.read())
  29. with self.assertRaises(NotImplementedError):
  30. storage.write('dfad', url)
  31. with self.assertRaises(HTTPError):
  32. storage.read(url + 'df')
  33. def test_file(self):
  34. url = 'http://pai-vision-data-hz.oss-cn-zhangjiakou.aliyuncs.com'\
  35. '/data/test/data.txt'
  36. content = 'this is test data'
  37. self.assertEqual(content.encode('utf8'), File.read(url))
  38. with File.as_local_path(url) as local_file:
  39. with open(local_file, 'r') as infile:
  40. self.assertEqual(content, infile.read())
  41. with self.assertRaises(NotImplementedError):
  42. File.write('dfad', url)
  43. with self.assertRaises(HTTPError):
  44. File.read(url + 'df')
  45. temp_name = tempfile.gettempdir() + '/' + next(
  46. tempfile._get_candidate_names())
  47. binary_content = b'12345'
  48. File.write(binary_content, temp_name)
  49. self.assertEqual(binary_content, File.read(temp_name))
  50. os.remove(temp_name)
  51. if __name__ == '__main__':
  52. unittest.main()

致力于通过开放的社区合作,开源AI模型以及相关创新技术,推动基于模型即服务的生态繁荣发展