test_base.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. # This file is part of h5py, a Python interface to the HDF5 library.
  2. #
  3. # http://www.h5py.org
  4. #
  5. # Copyright 2008-2013 Andrew Collette and contributors
  6. #
  7. # License: Standard 3-clause BSD; see "license.txt" for full license terms
  8. # and contributor agreement.
  9. """
  10. Common high-level operations test
  11. Tests features common to all high-level objects, like the .name property.
  12. """
  13. from h5py import File
  14. from h5py._hl.base import is_hdf5, Empty
  15. from .common import ut, TestCase, UNICODE_FILENAMES
  16. import numpy as np
  17. import os
  18. import tempfile
  19. class BaseTest(TestCase):
  20. def setUp(self):
  21. self.f = File(self.mktemp(), 'w')
  22. def tearDown(self):
  23. if self.f:
  24. self.f.close()
  25. class TestName(BaseTest):
  26. """
  27. Feature: .name attribute returns the object name
  28. """
  29. def test_anonymous(self):
  30. """ Anonymous objects have name None """
  31. grp = self.f.create_group(None)
  32. self.assertIs(grp.name, None)
  33. class TestParent(BaseTest):
  34. """
  35. test the parent group of the high-level interface objects
  36. """
  37. def test_object_parent(self):
  38. # Anonymous objects
  39. grp = self.f.create_group(None)
  40. # Parent of an anonymous object is undefined
  41. with self.assertRaises(ValueError):
  42. grp.parent
  43. # Named objects
  44. grp = self.f.create_group("bar")
  45. sub_grp = grp.create_group("foo")
  46. parent = sub_grp.parent.name
  47. self.assertEqual(parent, "/bar")
  48. class TestMapping(BaseTest):
  49. """
  50. Test if the registration of Group as a
  51. Mapping behaves as expected
  52. """
  53. def setUp(self):
  54. super().setUp()
  55. data = ('a', 'b')
  56. self.grp = self.f.create_group('bar')
  57. self.attr = self.f.attrs.create('x', data)
  58. def test_keys(self):
  59. key_1 = self.f.keys()
  60. self.assertIsInstance(repr(key_1), str)
  61. key_2 = self.grp.keys()
  62. self.assertIsInstance(repr(key_2), str)
  63. def test_values(self):
  64. value_1 = self.f.values()
  65. self.assertIsInstance(repr(value_1), str)
  66. value_2 = self.grp.values()
  67. self.assertIsInstance(repr(value_2), str)
  68. def test_items(self):
  69. item_1 = self.f.items()
  70. self.assertIsInstance(repr(item_1), str)
  71. item_2 = self.grp.items()
  72. self.assertIsInstance(repr(item_1), str)
  73. class TestRepr(BaseTest):
  74. """
  75. repr() works correctly with Unicode names
  76. """
  77. USTRING = chr(0xfc) + chr(0xdf)
  78. def _check_type(self, obj):
  79. self.assertIsInstance(repr(obj), str)
  80. def test_group(self):
  81. """ Group repr() with unicode """
  82. grp = self.f.create_group(self.USTRING)
  83. self._check_type(grp)
  84. def test_dataset(self):
  85. """ Dataset repr() with unicode """
  86. dset = self.f.create_dataset(self.USTRING, (1,))
  87. self._check_type(dset)
  88. def test_namedtype(self):
  89. """ Named type repr() with unicode """
  90. self.f['type'] = np.dtype('f')
  91. typ = self.f['type']
  92. self._check_type(typ)
  93. def test_empty(self):
  94. data = Empty(dtype='f')
  95. self.assertNotEqual(Empty(dtype='i'), data)
  96. self._check_type(data)
  97. @ut.skipIf(not UNICODE_FILENAMES, "Filesystem unicode support required")
  98. def test_file(self):
  99. """ File object repr() with unicode """
  100. fname = tempfile.mktemp(self.USTRING+'.hdf5')
  101. try:
  102. with File(fname,'w') as f:
  103. self._check_type(f)
  104. finally:
  105. try:
  106. os.unlink(fname)
  107. except Exception:
  108. pass
  109. def test_is_hdf5():
  110. filename = File(tempfile.mktemp(), "w").filename
  111. assert is_hdf5(filename)
  112. # non-existing HDF5 file
  113. filename = tempfile.mktemp()
  114. assert not is_hdf5(filename)