def test_session_assets_data(): store = AssetStore() store.add_shared_data('ww', b'wwww') s = SessionAssets(store) s._send_command = lambda x: None assert s.id # Add data s.add_data('xx', b'xxxx') s.add_data('yy', b'yyyy') assert len(s.get_data_names()) == 2 assert 'xx' in s.get_data_names() assert 'yy' in s.get_data_names() # get_data() assert s.get_data('xx') == b'xxxx' assert s.get_data('zz') is None assert s.get_data('ww') is b'wwww' # Add url data s.add_data('readme', 'https://github.com/zoofIO/flexx/blob/master/README.md') assert 'Flexx is' in s.get_data('readme').decode() # Add data with same name with raises(ValueError): s.add_data('xx', b'zzzz') # Add BS data with raises(TypeError): s.add_data('dd') # no data with raises(TypeError): s.add_data('dd', 4) # not an asset if sys.version_info > (3, ): with raises(TypeError): s.add_data('dd', 'not bytes') with raises(TypeError): s.add_data(b'dd', b'yes, bytes') # name not str with raises(TypeError): s.add_data(4, b'zzzz') # name not a str # get_data() assert s.get_data('xx') is b'xxxx' assert s.get_data('ww') is store.get_data('ww') assert s.get_data('ww') == b'wwww' assert s.get_data('bla') is None
def test_asset_store_export(): from flexx import ui dir = os.path.join(tempfile.gettempdir(), 'flexx_export') if os.path.isdir(dir): shutil.rmtree(dir) # os.mkdir(dir) -> No, export can create this dir! store = AssetStore() store.update_modules() # Getting an asset marks them as used store.get_asset('flexx.ui.js') store.get_asset('flexx.app.js') store.get_asset('flexx.js') store.get_asset('reset.css') store.add_shared_data('foo.png', b'x') s = SessionAssets(store) s.add_data('bar.png', b'x') store.export(dir) s._export(dir) assert len(os.listdir(dir)) == 2 assert os.path.isfile(os.path.join(dir, '_assets', 'shared', 'reset.css')) assert os.path.isfile(os.path.join(dir, '_assets', 'shared', 'flexx.ui.js')) assert os.path.isfile( os.path.join(dir, '_assets', 'shared', 'flexx.app.js')) assert os.path.isfile(os.path.join(dir, '_assets', 'shared', 'flexx.js')) assert not os.path.isfile( os.path.join(dir, '_assets', 'shared', 'flexx.ui._widget.js')) assert os.path.isfile(os.path.join(dir, '_data', 'shared', 'foo.png')) assert os.path.isfile(os.path.join(dir, '_data', s.id, 'bar.png')) # Will only create a dir that is one level deep with raises(ValueError): store.export(os.path.join(dir, 'not', 'exist'))
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