Exemple #1
0
def test_utils_url_read(mock_urlopen):  # mock_ul_urlopen, mock_ul_request, mock_ul):
    

    def side_effect(argss):
        return StringIO(argss)
    
    mockread = mock.Mock()
    class mystringio(object):

        def __init__(self, url, **kwargs):
            mockread.reset_mock()
            if isinstance(url, Exception):
                self.a = url
            else:
                self.a = StringIO(url)

        def read(self, *a, **kw):
            if isinstance(self.a, Exception):
                raise self.a
            mockread(*a, **kw)
            return self.a.read(*a, **kw)

        def close(self, *a, **kw):
            if not isinstance(self.a, Exception):
                self.a.close(*a, **kw)

    mock_urlopen.side_effect = lambda url, **kw: mystringio(url, **kw)
    with pytest.raises(TypeError):
        url_read('', "name")

    val = 'url'
    blockSize = 1024*1024
    assert url_read(val, blockSize) == val
    mock_urlopen.assert_called_with(val)
    assert mockread.call_count == 2
    mockread.assert_called_with(blockSize)

    mock_urlopen.side_effect = lambda url, **kw: mystringio(url, **kw)
    defBlockSize = -1
    assert url_read(val, arg_to_read=56) == val
    mock_urlopen.assert_called_with(val, arg_to_read=56)
    assert mockread.call_count == 1  # because blocksize is -1

    mock_urlopen.side_effect = lambda url, **kw: mystringio(URLError('wat?'))
    with pytest.raises(URLError):
        url_read(val)

    mock_urlopen.side_effect = lambda url, **kw: mystringio(socket.timeout())
    with pytest.raises(socket.error):
        url_read(val)
Exemple #2
0
def get_inventory(segment, session=None, **kwargs):
    """raises tons of exceptions (see main). FIXME: write doc
    :param session: if **not** None but a valid sqlalchemy session object, then
    the inventory, if downloaded because not present, will be saveed to the db (compressed)
    """
    data = segment.channel.station.inventory_xml
    if not data:
        query_url = get_inventory_query(segment.channel.station)
        data = url_read(query_url, **kwargs)
        if session and data:
            segment.channel.station.inventory_xml = dumps_inv(data)
            session.commit()
        elif not data:
            raise ValueError("No data from server")
    return loads_inv(data)