Пример #1
0
def test_asset_store_simple():

    s = AssetStore()
    assert len(s.get_asset_names()) == 1  # reset.css
    assert not s._cache

    raises(IndexError, s.load_asset, 'foo.js')

    with open(test_filename, 'wb') as f:
        f.write(b'bar\n')

    s.add_asset('foo.css', b'foo\n')
    s.add_asset('foo.js', test_filename)

    assert s.get_asset_names() == ['foo.css', 'foo.js',
                                   'reset.css']  # alphabetically
    assert s.load_asset('foo.css') == b'foo\n'
    assert s.load_asset('foo.js') == b'bar\n'

    # Check caching
    with open(test_filename, 'wb') as f:
        f.write(b'foo\n')
    assert s.load_asset('foo.js') == b'bar\n'

    # Setting same asset
    s.add_asset('foo.css', b'foo\n')
    s.add_asset('foo.js', test_filename)
    raises(ValueError, s.add_asset, 'foo.css', b'fooo\n')
    raises(ValueError, s.add_asset, 'foo.js', b'foo\n')
    raises(ValueError, s.add_asset, 'foo.js', b'bar\n')

    # Fail add_asset
    raises(ValueError, s.add_asset, 'xxx', 3)  # value must be str or bytes
    raises(ValueError, s.add_asset, 'xxx',
           'some file that does not exist')  # str means filename
    raises(
        RuntimeError, s._cache_get,
        'nonexistent.ever')  # Runtime error, because this should never happen

    # Assets from http
    s.add_asset('webresource.js', 'http://code.jquery.com/jquery.min.js')
    assert len(s.load_asset('webresource.js')) > 0

    # Fail load_asset
    raises(IndexError, s.load_asset, 'nonexistent.js')
Пример #2
0
def test_asset_store_simple():
    
    s = AssetStore()
    assert len(s.get_asset_names()) == 1  # reset.css
    assert not s._cache
    
    raises(IndexError, s.load_asset, 'foo.js')
    
    with open(test_filename, 'wb') as f:
        f.write(b'bar\n')
    
    s.add_asset('foo.css', b'foo\n')
    s.add_asset('foo.js', test_filename)
    
    assert s.get_asset_names() == ['foo.css', 'foo.js', 'reset.css']  # alphabetically
    assert s.load_asset('foo.css') == b'foo\n'
    assert s.load_asset('foo.js') == b'bar\n'
    
    # Check caching
    with open(test_filename, 'wb') as f:
        f.write(b'foo\n')
    assert s.load_asset('foo.js') == b'bar\n'
    
    # Setting same asset
    s.add_asset('foo.css', b'foo\n')
    s.add_asset('foo.js', test_filename)
    raises(ValueError, s.add_asset, 'foo.css', b'fooo\n')
    raises(ValueError, s.add_asset, 'foo.js', b'foo\n')
    raises(ValueError, s.add_asset, 'foo.js', b'bar\n')
    
    # Fail add_asset
    raises(ValueError, s.add_asset, 'xxx', 3)  # value must be str or bytes
    raises(ValueError, s.add_asset, 'xxx', 'some file that does not exist')  # str means filename
    raises(RuntimeError, s._cache_get, 'nonexistent.ever')  # Runtime error, because this should never happen
    
    # Assets from http 
    s.add_asset('webresource.js', 'http://code.jquery.com/jquery.min.js')
    assert len(s.load_asset('webresource.js')) > 0
    
    # Fail load_asset
    raises(IndexError, s.load_asset, 'nonexistent.js')
Пример #3
0
def test_asset_store_data():

    s = AssetStore()
    assert len(s.get_asset_names()) == N_STANDARD_ASSETS
    assert len(s.get_data_names()) == 0

    # Add data
    s.add_shared_data('xx', b'xxxx')
    s.add_shared_data('yy', b'yyyy')
    assert len(s.get_asset_names()) == N_STANDARD_ASSETS
    assert len(s.get_data_names()) == 2
    assert 'xx' in s.get_data_names()
    assert 'yy' in s.get_data_names()
    assert '2 data' in repr(s)

    # get_data()
    assert s.get_data('xx') == b'xxxx'
    assert s.get_data('zz') is None

    # Add data with same name
    with raises(ValueError):
        s.add_shared_data('xx', b'zzzz')

    # Add url data
    s.add_shared_data('readme',
                      'https://github.com/zoofIO/flexx/blob/master/README.md')
    assert 'Flexx is' in s.get_data('readme').decode()

    # Add BS data
    with raises(TypeError):
        s.add_shared_data('dd')  # no data
    with raises(TypeError):
        s.add_shared_data('dd', 4)  # not an asset
    if sys.version_info > (3, ):
        with raises(TypeError):
            s.add_shared_data('dd', 'not bytes')
        with raises(TypeError):
            s.add_shared_data(b'dd', b'yes, bytes')  # name not str
    with raises(TypeError):
        s.add_shared_data(4, b'zzzz')  # name not a str