def test_session_assets(): store = AssetStore() s = SessionAssets(store) s._send_command = lambda x: None assert not s.get_used_asset_names() with open(test_filename, 'wb') as f: f.write(b'bar\n') # Add assets, check mangles name a1 = s.add_asset('foo.css', b'foo\n') a2 = s.add_asset('foo.js', test_filename) assert 'foo' in a1 and s.id in a1 and a1.endswith('.css') assert 'foo' in a2 and s.id in a2 and a2.endswith('.js') assert s.get_used_asset_names() == [a1, a2] # order in which it came # Get the asset raises(IndexError, store.load_asset, 'foo.css') raises(IndexError, store.load_asset, 'foo.js') assert store.load_asset(a1) == b'foo\n' assert store.load_asset(a2) == b'bar\n' # Use asset store.add_asset('spam.js', b'1234\x00') s.use_global_asset('spam.js') assert s.get_used_asset_names()[-1] == 'spam.js' raises(IndexError, s.use_global_asset, 'unknown-asset.js') raises(ValueError, s.add_asset, 3, b'a\n') # Add assets after loading page s.get_page() s.use_global_asset('spam.js') # prints a warning, but it does work # Global assets s.add_global_asset('eggs.js', b'12345\x00') assert s.get_used_asset_names()[-1] == 'eggs.js' assert store.load_asset('eggs.js') == b'12345\x00' raises(ValueError, s.use_global_asset, 3) # Remote assets s.use_remote_asset('http://linked.com/not/verified.js') s.use_remote_asset('http://linked.com/not/verified.css') s.use_remote_asset('http://linked.com/not/verified.css') # twice is ok raises(ValueError, s.use_remote_asset, 3) page = s.get_page() assert 'not/verified.js' in page assert 'not/verified.css' in page
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')
def test_asset_store_export(): dir = os.path.join(tempfile.gettempdir(), 'flexx_export') if os.path.isdir(dir): shutil.rmtree(dir) os.mkdir(dir) s = AssetStore() s.export(dir) assert not os.listdir(dir) s.add_asset('foo.js', b'xx') s.add_asset('foo.css', b'xx') s.export(dir) assert len(os.listdir(dir)) == 2 # Fail raises(ValueError, s.export, os.path.join(dir, 'doesnotexist'))
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')