test_base.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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, make_name
  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_anonymous(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. def test_object_parent_named(self):
  44. # Named objects
  45. name = make_name()
  46. grp = self.f.create_group(name)
  47. sub_grp = grp.create_group("foo")
  48. parent = sub_grp.parent.name
  49. self.assertEqual(parent, "/" + name)
  50. class TestMapping(BaseTest):
  51. """
  52. Test if the registration of Group as a
  53. Mapping behaves as expected
  54. """
  55. def setUp(self):
  56. super().setUp()
  57. data = ('a', 'b')
  58. self.grp = self.f.create_group('bar')
  59. self.attr = self.f.attrs.create('x', data)
  60. def test_keys(self):
  61. key_1 = self.f.keys()
  62. self.assertIsInstance(repr(key_1), str)
  63. key_2 = self.grp.keys()
  64. self.assertIsInstance(repr(key_2), str)
  65. def test_values(self):
  66. value_1 = self.f.values()
  67. self.assertIsInstance(repr(value_1), str)
  68. value_2 = self.grp.values()
  69. self.assertIsInstance(repr(value_2), str)
  70. def test_items(self):
  71. item_1 = self.f.items()
  72. self.assertIsInstance(repr(item_1), str)
  73. item_2 = self.grp.items()
  74. self.assertIsInstance(repr(item_1), str)
  75. class TestRepr(BaseTest):
  76. """
  77. repr() works correctly with Unicode names
  78. """
  79. USTRING = chr(0xfc) + chr(0xdf)
  80. def _check_type(self, obj):
  81. self.assertIsInstance(repr(obj), str)
  82. def test_group(self):
  83. """ Group repr() with unicode """
  84. grp = self.f.create_group(make_name(self.USTRING))
  85. self._check_type(grp)
  86. def test_dataset(self):
  87. """ Dataset repr() with unicode """
  88. dset = self.f.create_dataset(make_name(self.USTRING), (1,), "i4")
  89. self._check_type(dset)
  90. def test_namedtype(self):
  91. """ Named type repr() with unicode """
  92. name = make_name(self.USTRING)
  93. self.f[name] = np.dtype('f')
  94. typ = self.f[name]
  95. self._check_type(typ)
  96. def test_empty(self):
  97. data = Empty(dtype='f')
  98. self.assertNotEqual(Empty(dtype='i'), data)
  99. self._check_type(data)
  100. @ut.skipIf(not UNICODE_FILENAMES, "Filesystem unicode support required")
  101. def test_file(self):
  102. """ File object repr() with unicode """
  103. fname = tempfile.mktemp(self.USTRING+'.hdf5')
  104. try:
  105. with File(fname,'w') as f:
  106. self._check_type(f)
  107. finally:
  108. try:
  109. os.unlink(fname)
  110. except Exception:
  111. pass
  112. def test_is_hdf5():
  113. filename = File(tempfile.mktemp(), "w").filename
  114. assert is_hdf5(filename)
  115. # non-existing HDF5 file
  116. filename = tempfile.mktemp()
  117. assert not is_hdf5(filename)