def test_hdf5_to_file_good_extension(path_type: Union[Type[pathlib.Path], Type[str]], dummy_model: DummyModel, tmp_path: pathlib.Path) -> None: path = path_type(tmp_path / 'test.h5') dummy_model.to_file(path) new_model = dummy_model.from_file(open(path, 'rb'), 'test.h5', content_type='application/x-hdf5') assert_models_equal(dummy_model, new_model)
def test_eq_hash() -> None: # model1 and model2 have the same checksum, model3 a different checksum model1 = DummyModel(None) model2 = DummyModel(None) model3 = DummyModel(None) model4 = DummyModel(None) model1.checksum = hashlib.sha256(b'foo').hexdigest() model2.checksum = model1.checksum model3.checksum = hashlib.sha256(b'bar').hexdigest() assert model1 == model1 assert model1 == model2 assert model1 != model3 assert model3 == model3 assert model3 != model4 assert model4 == model4 assert hash(model1) == hash(model2) assert hash(model1) != hash(model3) assert hash(model3) != hash(model4) assert model1 != 1 assert model4 != 1
def dummy_model() -> DummyModel: ranges = np.array( [(1, 4.5), (2, -5.5)], dtype=[('a', 'i4'), ('b', 'f8')] ) model = DummyModel(ranges) model.author = 'Test author' model.comment = 'Test comment' model.target = 'Test target' model.version = 1 model.created = datetime(2020, 6, 15, 14, 11, tzinfo=timezone.utc) return model
def test_hdf5_to_file(clear_metadata: bool, dummy_model: DummyModel) -> None: if clear_metadata: dummy_model.comment = None dummy_model.author = None dummy_model.target = None dummy_model.created = None fh = io.BytesIO() dummy_model.to_file(fh, content_type='application/x-hdf5') fh.seek(0) new_model = DummyModel.from_file(fh, 'http://test.invalid/dummy.h5', content_type='application/x-hdf5') assert_models_equal(dummy_model, new_model)
def test_hdf5_to_file_bad_extension(path_type: Union[Type[pathlib.Path], Type[str]], dummy_model: DummyModel, tmp_path: pathlib.Path) -> None: with pytest.raises(models.FileTypeError, match=r'Expected extension of \.h5 or \.hdf5, not \.foo'): dummy_model.to_file(path_type(tmp_path / 'test.foo'))
def test_hdf5_to_file_bad_content_type(dummy_model: DummyModel) -> None: with pytest.raises(models.FileTypeError, match='Expected application/x-hdf5, not image/png'): dummy_model.to_file(io.BytesIO(), content_type='image/png')
def test_hdf5_to_file_no_content_type_or_filename(dummy_model: DummyModel) -> None: with pytest.raises(AttributeError): dummy_model.to_file(io.BytesIO())
def test_hdf5_to_file_no_version(dummy_model: DummyModel) -> None: dummy_model.version = None with pytest.raises(ValueError): dummy_model.to_file(io.BytesIO(), content_type='application/x-hdf5')