def test_export_chart_command_invalid_dataset(self, mock_g): """Test that an error is raised when exporting an invalid dataset""" mock_g.user = security_manager.find_user("admin") command = ExportChartsCommand([-1]) contents = command.run() with self.assertRaises(ChartNotFoundError): next(contents)
def test_export_chart_command(self, mock_g): mock_g.user = security_manager.find_user("admin") example_chart = db.session.query(Slice).all()[0] command = ExportChartsCommand([example_chart.id]) contents = dict(command.run()) expected = [ "metadata.yaml", "charts/energy_sankey.yaml", "datasets/examples/energy_usage.yaml", "databases/examples.yaml", ] assert expected == list(contents.keys()) metadata = yaml.safe_load(contents["charts/energy_sankey.yaml"]) assert metadata == { "slice_name": "Energy Sankey", "viz_type": "sankey", "params": { "collapsed_fieldsets": "", "groupby": ["source", "target",], "metric": "sum__value", "row_limit": "5000", "slice_name": "Energy Sankey", "viz_type": "sankey", }, "cache_timeout": None, "dataset_uuid": str(example_chart.table.uuid), "uuid": str(example_chart.uuid), "version": "1.0.0", }
def test_export_chart_command_no_access(self, mock_g): """Test that users can't export datasets they don't have access to""" mock_g.user = security_manager.find_user("gamma") example_chart = db.session.query(Slice).all()[0] command = ExportChartsCommand([example_chart.id]) contents = command.run() with self.assertRaises(ChartNotFoundError): next(contents)
def test_export_chart_command_no_related(self, mock_g): """ Test that only the chart is exported when export_related=False. """ mock_g.user = security_manager.find_user("admin") example_chart = (db.session.query(Slice).filter_by( slice_name="Energy Sankey").one()) command = ExportChartsCommand([example_chart.id], export_related=False) contents = dict(command.run()) expected = [ "metadata.yaml", f"charts/Energy_Sankey_{example_chart.id}.yaml", ] assert expected == list(contents.keys())
def test_export_chart_command_key_order(self, mock_g): """Test that they keys in the YAML have the same order as export_fields""" mock_g.user = security_manager.find_user("admin") example_chart = db.session.query(Slice).all()[0] command = ExportChartsCommand([example_chart.id]) contents = dict(command.run()) metadata = yaml.safe_load(contents["charts/energy_sankey.yaml"]) assert list(metadata.keys()) == [ "slice_name", "viz_type", "params", "cache_timeout", "uuid", "version", "dataset_uuid", ]
def test_export_chart_with_query_context(self, mock_g): """Test that charts that have a query_context are exported correctly""" mock_g.user = security_manager.find_user("alpha") example_chart = db.session.query(Slice).filter_by( slice_name="Heatmap").one() command = ExportChartsCommand([example_chart.id]) contents = dict(command.run()) expected = [ "metadata.yaml", f"charts/Heatmap_{example_chart.id}.yaml", "datasets/examples/energy_usage.yaml", "databases/examples.yaml", ] assert expected == list(contents.keys()) metadata = yaml.safe_load( contents[f"charts/Heatmap_{example_chart.id}.yaml"]) assert metadata == { "slice_name": "Heatmap", "viz_type": "heatmap", "params": { "all_columns_x": "source", "all_columns_y": "target", "canvas_image_rendering": "pixelated", "collapsed_fieldsets": "", "linear_color_scheme": "blue_white_yellow", "metric": "sum__value", "normalize_across": "heatmap", "slice_name": "Heatmap", "viz_type": "heatmap", "xscale_interval": "1", "yscale_interval": "1", }, "cache_timeout": None, "dataset_uuid": str(example_chart.table.uuid), "uuid": str(example_chart.uuid), "version": "1.0.0", }