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_handler.py 1.8 kB

6 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. """Test file_handler.py."""
  16. from unittest import mock, TestCase
  17. from mindinsight.lineagemgr.summary.file_handler import FileHandler
  18. class TestFileHandler(TestCase):
  19. """Test file_handler.py"""
  20. @mock.patch("os.path.getsize", return_value=12)
  21. @mock.patch("builtins.open")
  22. def setUp(self, *args):
  23. args[0].return_value.__enter__.return_value.read.return_value = b'\x0a\x0b\x0c' * 4
  24. self.file_handler = FileHandler("fake_path.log")
  25. def test_seek(self):
  26. """Test seek method."""
  27. self.file_handler.seek(5)
  28. cur_pos = self.file_handler.tell()
  29. self.assertEqual(cur_pos, 5)
  30. def test_read(self):
  31. """Test read method."""
  32. res = self.file_handler.read(3)
  33. self.assertEqual(res, b'\x0a\x0b\x0c')
  34. def test_read_with_pos(self):
  35. """Test read method with specific position."""
  36. res = self.file_handler.read(3, 1)
  37. self.assertEqual(res, b'\x0b\x0c\x0a')
  38. def test_size(self):
  39. """Test size property."""
  40. size = self.file_handler.size
  41. self.assertEqual(size, 12)