def test_fetch_model_bad_extension(web_server, lazy) -> None:
    url = web_server('wrong_extension.blah')
    with pytest.raises(models.FileTypeError) as exc_info:
        with fetch_requests.Fetcher() as fetcher:
            fetcher.get(url, DummyModel, lazy=lazy)
    assert exc_info.value.url == url
    assert exc_info.value.original_url == url
def test_lazy(web_server) -> None:
    with fetch_requests.Fetcher() as fetcher:
        model = fetcher.get(web_server('rfi_mask_ranges.h5'),
                            DummyModel,
                            lazy=True)
        assert len(model.ranges) == 2
        assert not model.is_closed
    assert model.is_closed
def test_fetcher_resolve(web_server) -> None:
    url = web_server('indirect.alias')
    with fetch_requests.Fetcher() as fetcher:
        urls = fetcher.resolve(url)
    assert urls == [
        web_server('indirect.alias'),
        web_server('direct.alias'),
        web_server('rfi_mask_ranges.h5')
    ]
def test_fetcher_custom_session(web_server) -> None:
    session = DummySession()
    with session:
        with fetch_requests.Fetcher(session=session) as fetcher:
            assert fetcher.session is session
            fetcher.get(web_server('direct.alias'), DummyModel)
        assert session.calls == 2
        assert not session.closed
    assert session.closed
def test_fetcher_caching(mock_responses) -> None:
    with fetch_requests.Fetcher() as fetcher:
        model1 = fetcher.get(get_data_url('rfi_mask_ranges.h5'), DummyModel)
        model2 = fetcher.get(get_data_url('indirect.alias'), DummyModel)
        model3 = fetcher.get(get_data_url('direct.alias'), DummyModel)
        assert model1 is model2
        assert model1 is model3
        assert not model1.is_closed
    assert len(mock_responses.calls) == 3
    assert model1.is_closed
def test_fetch_model_bad_content_type(mock_responses, lazy) -> None:
    data = get_data('rfi_mask_ranges.h5')
    url = get_data_url('bad_content_type.h5')
    mock_responses.add(responses.HEAD,
                       url,
                       content_type='image/png',
                       headers={
                           'Accept-Ranges': 'bytes',
                           'Content-Length': str(len(data))
                       })
    mock_responses.add(responses.GET, url, content_type='image/png', body=data)
    with pytest.raises(
            models.FileTypeError,
            match='Expected application/x-hdf5, not image/png') as exc_info:
        with fetch_requests.Fetcher() as fetcher:
            fetcher.get(url, DummyModel, lazy=lazy)
    assert exc_info.value.url == url
    assert exc_info.value.original_url == url
def test_fetch_model_cached_model_type_error(web_server) -> None:
    class OtherModel(models.SimpleHDF5Model):
        model_type = 'other'

        @classmethod
        def from_hdf5(cls, hdf5: h5py.File) -> 'OtherModel':
            return cls()

        def to_hdf5(self, hdf5: h5py.File) -> None:
            pass

    url = web_server('rfi_mask_ranges.h5')
    with fetch_requests.Fetcher() as fetcher:
        fetcher.get(url, DummyModel)
        with pytest.raises(models.ModelTypeError) as exc_info:
            fetcher.get(url, OtherModel)
        assert exc_info.value.url == url
        assert exc_info.value.original_url == url
def test_lazy_local() -> None:
    with fetch_requests.Fetcher() as fetcher:
        model = fetcher.get(get_file_url('rfi_mask_ranges.h5'),
                            DummyModel,
                            lazy=True)
        assert len(model.ranges) == 2