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_io.py 909 B

1234567891011121314151617181920212223242526272829303132
  1. # Copyright (c) Alibaba, Inc. and its affiliates.
  2. import tempfile
  3. import unittest
  4. from modelscope.fileio.io import dump, dumps, load
  5. class FileIOTest(unittest.TestCase):
  6. def test_format(self, format='json'):
  7. obj = [1, 2, 3, 'str', {'model': 'resnet'}]
  8. result_str = dumps(obj, format)
  9. temp_name = tempfile.gettempdir() + '/' + next(
  10. tempfile._get_candidate_names()) + '.' + format
  11. dump(obj, temp_name)
  12. obj_load = load(temp_name)
  13. self.assertEqual(obj_load, obj)
  14. with open(temp_name, 'r') as infile:
  15. self.assertEqual(result_str, infile.read())
  16. with self.assertRaises(TypeError):
  17. obj_load = load(temp_name + 's')
  18. with self.assertRaises(TypeError):
  19. dump(obj, temp_name + 's')
  20. def test_yaml(self):
  21. self.test_format('yaml')
  22. if __name__ == '__main__':
  23. unittest.main()