def test_export_query_command(self, mock_g): mock_g.user = security_manager.find_user("admin") command = ExportSavedQueriesCommand([self.example_query.id]) contents = dict(command.run()) expected = [ "metadata.yaml", "queries/examples/schema1/The_answer.yaml", "databases/examples.yaml", ] assert expected == list(contents.keys()) metadata = yaml.safe_load( contents["queries/examples/schema1/The_answer.yaml"]) assert metadata == { "schema": "schema1", "label": "The answer", "description": "Answer to the Ultimate Question of Life, the Universe, and Everything", "sql": "SELECT 42", "uuid": str(self.example_query.uuid), "version": "1.0.0", "database_uuid": str(self.example_database.uuid), }
def test_export_query_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 = ExportSavedQueriesCommand([-1]) contents = command.run() with self.assertRaises(SavedQueryNotFoundError): next(contents)
def test_export_query_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") command = ExportSavedQueriesCommand([self.example_query.id]) contents = command.run() with self.assertRaises(SavedQueryNotFoundError): next(contents)
def test_export_query_command_no_related(self, mock_g): """ Test that only the query is exported when export_related=False. """ mock_g.user = security_manager.find_user("admin") command = ExportSavedQueriesCommand([self.example_query.id], export_related=False) contents = dict(command.run()) expected = [ "metadata.yaml", "queries/examples/schema1/The_answer.yaml", ] assert expected == list(contents.keys())
def test_export_query_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") command = ExportSavedQueriesCommand([self.example_query.id]) contents = dict(command.run()) metadata = yaml.safe_load(contents["queries/examples/schema1/The_answer.yaml"]) assert list(metadata.keys()) == [ "schema", "label", "description", "sql", "uuid", "version", "database_uuid", ]