To check that the filename used is as expected you could check that the Content-Disposition
header is as expected. For example:
assert response.headers['Content-Disposition'] == 'attachment; filename=123.xlsx'
To check "the existance of the file" you could for example check that for some test data it lies within an expected range of size. For example:
assert 3000 <= response.content_length <= 5000 assert 3000 <= len(response.data) <= 5000
Another level of verifying that the Excel file works would be attempting to load the data back into openpyxl
and checking if it reports any problems. For example:
from io
import BytesIO
from openpyxl
import load_workbook
load_workbook(filename = BytesIO(response.data))
To check that the filename used is as anycodings_flask expected you could check that the anycodings_flask Content-Disposition header is as anycodings_flask expected. For example:,To check "the existance of the file" you anycodings_flask could for example check that for some anycodings_flask test data it lies within an expected anycodings_flask range of size. For example:,Which passes, but I'd like to test that the anycodings_openpyxl (a) the filename used is as expected and (b) anycodings_openpyxl that the file... exists? Is an Excel file?,Which would indicate that the data anycodings_flask contents of the file are invalid as a anycodings_flask Excel file.
I have a Flask view that generates an Excel anycodings_openpyxl file (using openpyxl) from some data and anycodings_openpyxl it's returned to the user using send_file(). anycodings_openpyxl A very simplified version:
import io
from flask import send_file
from openpyxl.workbook import Workbook
@app.route("/download/<int:id>")
def file_download(id):
wb = Workbook()
# Add sheets and data to the workbook here.
file = io.BytesIO()
wb.save(file)
file.seek(0)
return send_file(file, attachment_filename=f"{id}.xlsx", as_attachment=True)
This works fine -- the file downloads and is anycodings_openpyxl a valid Excel file. But I'm not sure how to anycodings_openpyxl test the file download. So far I have anycodings_openpyxl something like this (using pytest):
def test_file_download(test_client):
response = test_client.get("/download/123")
assert response.status_code == 200
assert response.content_type == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
To check that the filename used is as anycodings_flask expected you could check that the anycodings_flask Content-Disposition header is as anycodings_flask expected. For example:
assert response.headers['Content-Disposition'] == 'attachment; filename=123.xlsx'
To check "the existance of the file" you anycodings_flask could for example check that for some anycodings_flask test data it lies within an expected anycodings_flask range of size. For example:
assert 3000 <= response.content_length <= 5000 assert 3000 <= len(response.data) <= 5000
Another level of verifying that the anycodings_flask Excel file works would be attempting to anycodings_flask load the data back into openpyxl and anycodings_flask checking if it reports any problems. For anycodings_flask example:
from io
import BytesIO
from openpyxl
import load_workbook
load_workbook(filename = BytesIO(response.data))
To check "the existance of the file" you could for example check that for some test data it lies within an expected range of size. For example:,To check that the filename used is as expected you could check that the Content-Disposition header is as expected. For example:,Another level of verifying that the Excel file works would be attempting to load the data back into openpyxl and checking if it reports any problems. For example:
assert response.headers['Content-Disposition'] == 'attachment; filename=123.xlsx'
name – the optional name of the test, otherwise the function name will be used.,name – the optional name of the filter, otherwise the function name will be used.,name – the optional name of the global, otherwise the function name will be used.,name – the optional name of the global function, otherwise the function name will be used.
from flask
import Flask
app = Flask(__name__)
app = Flask('yourapplication')
app = Flask(__name__.split('.')[0])
@app.route('/')
def index():
pass
def index():
pass
app.add_url_rule('/', 'index', index)
app.view_functions['index'] = index
with app.app_context():
...
We can see that we're sending the person to /return-files/, which doesn't exist yet, let's make that.,In this Flask Web development tutorial, we're going to be discussing how to return files rather than templates.,In this case, if a person goes to /file-downloads/, and clicks the download button, they will get an image returned. How about a pdf?,Simple enough, but what if you want to protect the files in some way. On PythonProgramming.net, for example, I let subscribers just download the videos, but you have to be a subscriber. Most downloads need to be in the static directory, which is totally public, so how would one go about protecting a file from the general public? That's what we'll be talking about in the next tutorial.
__init__.py
from flask
import send_file
#...other code....
@app.route('/file-downloads/')
def file_downloads():
try:
return render_template('downloads.html')
except Exception as e:
return str(e)
templates/downloads.html
{% extends "header.html" %}
{% block body %}
<body class="body">
<div class="container" align="left">
<a href="/return-files/" target="blank"><button class='btn btn-default'>Download!</button></a>
</div>
</body>
{% endblock %}
In this case, if a person goes to /file-downloads/, and clicks the download button, they will get an image returned. How about a pdf?
@app.route('/return-files/')
def return_files_tut():
try:
return send_file('/var/www/PythonProgramming/PythonProgramming/static/ohhey.pdf', attachment_filename = 'ohhey.pdf')
except Exception as e:
return str(e)