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""" 11 Common high-level operations test 12 13 Tests features common to all high-level objects, like the .name property. 14""" 15 16from h5py import File 17from h5py._hl.base import is_hdf5, Empty 18from .common import ut, TestCase, UNICODE_FILENAMES 19 20import numpy as np 21import os 22import tempfile 23 24class BaseTest(TestCase): 25 26 def setUp(self): 27 self.f = File(self.mktemp(), 'w') 28 29 def tearDown(self): 30 if self.f: 31 self.f.close() 32 33 34class TestName(BaseTest): 35 36 """ 37 Feature: .name attribute returns the object name 38 """ 39 40 def test_anonymous(self): 41 """ Anonymous objects have name None """ 42 grp = self.f.create_group(None) 43 self.assertIs(grp.name, None) 44 45class TestParent(BaseTest): 46 47 """ 48 test the parent group of the high-level interface objects 49 """ 50 51 def test_object_parent(self): 52 # Anonymous objects 53 grp = self.f.create_group(None) 54 # Parent of an anonymous object is undefined 55 with self.assertRaises(ValueError): 56 grp.parent 57 58 # Named objects 59 grp = self.f.create_group("bar") 60 sub_grp = grp.create_group("foo") 61 parent = sub_grp.parent.name 62 self.assertEqual(parent, "/bar") 63 64class TestMapping(BaseTest): 65 66 """ 67 Test if the registration of Group as a 68 Mapping behaves as expected 69 """ 70 71 def setUp(self): 72 super().setUp() 73 data = ('a', 'b') 74 self.grp = self.f.create_group('bar') 75 self.attr = self.f.attrs.create('x', data) 76 77 def test_keys(self): 78 key_1 = self.f.keys() 79 self.assertIsInstance(repr(key_1), str) 80 key_2 = self.grp.keys() 81 self.assertIsInstance(repr(key_2), str) 82 83 def test_values(self): 84 value_1 = self.f.values() 85 self.assertIsInstance(repr(value_1), str) 86 value_2 = self.grp.values() 87 self.assertIsInstance(repr(value_2), str) 88 89 def test_items(self): 90 item_1 = self.f.items() 91 self.assertIsInstance(repr(item_1), str) 92 item_2 = self.grp.items() 93 self.assertIsInstance(repr(item_1), str) 94 95 96class TestRepr(BaseTest): 97 98 """ 99 repr() works correctly with Unicode names 100 """ 101 102 USTRING = chr(0xfc) + chr(0xdf) 103 104 def _check_type(self, obj): 105 self.assertIsInstance(repr(obj), str) 106 107 def test_group(self): 108 """ Group repr() with unicode """ 109 grp = self.f.create_group(self.USTRING) 110 self._check_type(grp) 111 112 def test_dataset(self): 113 """ Dataset repr() with unicode """ 114 dset = self.f.create_dataset(self.USTRING, (1,)) 115 self._check_type(dset) 116 117 def test_namedtype(self): 118 """ Named type repr() with unicode """ 119 self.f['type'] = np.dtype('f') 120 typ = self.f['type'] 121 self._check_type(typ) 122 123 def test_empty(self): 124 data = Empty(dtype='f') 125 self.assertNotEqual(Empty(dtype='i'), data) 126 self._check_type(data) 127 128 @ut.skipIf(not UNICODE_FILENAMES, "Filesystem unicode support required") 129 def test_file(self): 130 """ File object repr() with unicode """ 131 fname = tempfile.mktemp(self.USTRING+'.hdf5') 132 try: 133 with File(fname,'w') as f: 134 self._check_type(f) 135 finally: 136 try: 137 os.unlink(fname) 138 except Exception: 139 pass 140 141def test_is_hdf5(): 142 filename = File(tempfile.mktemp(), "w").filename 143 assert is_hdf5(filename) 144 # non-existing HDF5 file 145 filename = tempfile.mktemp() 146 assert not is_hdf5(filename) 147