|
| 1 | +import unittest |
| 2 | + |
| 3 | +import h5py |
| 4 | +import numpy as np |
| 5 | + |
| 6 | +import bald |
| 7 | +from bald.tests import BaldTestCase |
| 8 | + |
| 9 | +def _fattrs(f): |
| 10 | + f.attrs['rdf__type'] = 'bald__Container' |
| 11 | + group_alias = f.create_group('bald__alias_list') |
| 12 | + group_pref.attrs['bald__'] = 'http://binary-array-ld.net/latest/' |
| 13 | + group_pref.attrs['rdf__'] = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#' |
| 14 | + f.attrs['bald__aliases'] = group_alias.ref |
| 15 | + return f |
| 16 | + |
| 17 | +def _create_parent_child(f, pshape, cshape): |
| 18 | + dsetp = f.create_dataset("parent_dataset", pshape, dtype='i') |
| 19 | + dsetc = f.create_dataset("child_dataset", cshape, dtype='i') |
| 20 | + dsetp.attrs['rdf__type'] = 'bald__Array' |
| 21 | + dsetp.attrs['bald__references'] = dsetc.ref |
| 22 | + dsetc.attrs['rdf__type'] = 'bald__Array' |
| 23 | + dsetc.attrs['rdf__type'] = 'bald__Reference' |
| 24 | + dsetc.attrs['bald__array'] = dsetc.ref |
| 25 | + return f |
| 26 | + |
| 27 | + |
| 28 | +class Test(BaldTestCase): |
| 29 | + |
| 30 | + def test_valid_uri(self): |
| 31 | + with self.temp_filename('.hdf') as tfile: |
| 32 | + f = h5py.File(tfile, "w") |
| 33 | + f = _fattrs(f) |
| 34 | + f = _create_parent_child(f, (11, 17), (11, 17)) |
| 35 | + f.close() |
| 36 | + validation = bald.validate_hdf5(tfile) |
| 37 | + self.assertTrue(validation.is_valid()) |
| 38 | + |
| 39 | + def test_invalid_uri(self): |
| 40 | + with self.temp_filename('.hdf') as tfile: |
| 41 | + f = h5py.File(tfile, "w") |
| 42 | + f = _fattrs(f) |
| 43 | + f = _create_parent_child(f, (11, 17), (11, 17)) |
| 44 | + f.attrs['bald__turtle'] = 'bald__walnut' |
| 45 | + f.close() |
| 46 | + validation = bald.validate_hdf5(tfile) |
| 47 | + self.assertFalse(validation.is_valid()) |
| 48 | + |
| 49 | +class TestArrayReference(BaldTestCase): |
| 50 | + def test_match(self): |
| 51 | + with self.temp_filename('.hdf') as tfile: |
| 52 | + f = h5py.File(tfile, "w") |
| 53 | + f = _fattrs(f) |
| 54 | + f = _create_parent_child(f, (11, 17), (11, 17)) |
| 55 | + f.close() |
| 56 | + validation = bald.validate_hdf5(tfile) |
| 57 | + self.assertTrue(validation.is_valid()) |
| 58 | + |
| 59 | + def test_mismatch_zeroth(self): |
| 60 | + with self.temp_filename('.hdf') as tfile: |
| 61 | + f = h5py.File(tfile, "w") |
| 62 | + f = _fattrs(f) |
| 63 | + f = _create_parent_child(f, (11, 17), (11, 13)) |
| 64 | + f.close() |
| 65 | + validation = bald.validate_hdf5(tfile) |
| 66 | + self.assertFalse(validation.is_valid()) |
| 67 | + |
| 68 | + def test_mismatch_oneth(self): |
| 69 | + with self.temp_filename('.hdf') as tfile: |
| 70 | + f = h5py.File(tfile, "w") |
| 71 | + f = _fattrs(f) |
| 72 | + f = _create_parent_child(f, (11, 17), (13, 17)) |
| 73 | + f.close() |
| 74 | + validation = bald.validate_hdf5(tfile) |
| 75 | + self.assertFalse(validation.is_valid()) |
| 76 | + |
| 77 | + def test_match_plead_dim(self): |
| 78 | + with self.temp_filename('.hdf') as tfile: |
| 79 | + f = h5py.File(tfile, "w") |
| 80 | + f = _fattrs(f) |
| 81 | + # parent has leading dimension wrt child |
| 82 | + f = _create_parent_child(f, (4, 13, 17), (13, 17)) |
| 83 | + f.close() |
| 84 | + validation = bald.validate_hdf5(tfile) |
| 85 | + self.assertTrue(validation.is_valid()) |
| 86 | + |
| 87 | + def test_match_clead_dim(self): |
| 88 | + with self.temp_filename('.hdf') as tfile: |
| 89 | + f = h5py.File(tfile, "w") |
| 90 | + f = _fattrs(f) |
| 91 | + # child has leading dimension wrt parent |
| 92 | + f = _create_parent_child(f, (13, 17), (7, 13, 17)) |
| 93 | + f.close() |
| 94 | + validation = bald.validate_hdf5(tfile) |
| 95 | + self.assertTrue(validation.is_valid()) |
| 96 | + |
| 97 | + def test_mismatch_pdisjc_lead_dim(self): |
| 98 | + with self.temp_filename('.hdf') as tfile: |
| 99 | + f = h5py.File(tfile, "w") |
| 100 | + f = _fattrs(f) |
| 101 | + # child and parent have disjoint leading dimensions |
| 102 | + f = _create_parent_child(f, (4, 13, 17), (7, 13, 17)) |
| 103 | + |
| 104 | + f.close() |
| 105 | + validation = bald.validate_hdf5(tfile) |
| 106 | + self.assertFalse(validation.is_valid()) |
| 107 | + |
| 108 | + def test_mismatch_pdisjc_trail_dim(self): |
| 109 | + with self.temp_filename('.hdf') as tfile: |
| 110 | + f = h5py.File(tfile, "w") |
| 111 | + f = _fattrs(f) |
| 112 | + # child and parent have disjoint trailing dimensions |
| 113 | + f = _create_parent_child(f, (13, 17, 2), (13, 17, 9)) |
| 114 | + f.close() |
| 115 | + validation = bald.validate_hdf5(tfile) |
| 116 | + self.assertFalse(validation.is_valid()) |
| 117 | + |
| 118 | + |
| 119 | + # def test_match_(self): |
| 120 | + # with self.temp_filename('.hdf') as tfile: |
| 121 | + # f = h5py.File(tfile, "w") |
| 122 | + # f = _fattrs(f) |
| 123 | + # # |
| 124 | + # f = _create_parent_child(f, (), ()) |
| 125 | + # f.close() |
| 126 | + # self.assert(bald.validate_hdf5(tfile).is_valid()) |
| 127 | + |
| 128 | +if __name__ == '__main__': |
| 129 | + unittest.main() |
0 commit comments