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", ]
def export(self, **kwargs: Any) -> Response: """Export saved queries --- get: description: >- Exports multiple saved queries and downloads them as YAML files parameters: - in: query name: q content: application/json: schema: $ref: '#/components/schemas/get_export_ids_schema' responses: 200: description: A zip file with saved query(ies) and database(s) as YAML content: application/zip: schema: type: string format: binary 400: $ref: '#/components/responses/400' 401: $ref: '#/components/responses/401' 404: $ref: '#/components/responses/404' 500: $ref: '#/components/responses/500' """ token = request.args.get("token") requested_ids = kwargs["rison"] timestamp = datetime.now().strftime("%Y%m%dT%H%M%S") root = f"saved_query_export_{timestamp}" filename = f"{root}.zip" buf = BytesIO() with ZipFile(buf, "w") as bundle: try: for file_name, file_content in ExportSavedQueriesCommand( requested_ids ).run(): with bundle.open(f"{root}/{file_name}", "w") as fp: fp.write(file_content.encode()) except SavedQueryNotFoundError: return self.response_404() buf.seek(0) response = send_file( buf, mimetype="application/zip", as_attachment=True, attachment_filename=filename, ) if token: response.set_cookie(token, "done", max_age=600) return response