I have an endpoint that takes a csv file.
Now, I want to write a test that makes a post request with this file.
I am trying to generate this csv file on the fly (rather than manually create and store it)
I tried this:
def csv_fixture(rows, type):
headers = None
if type == "merchant_upload":
headers = MerchantCSV.ordered_columns()
elif type == "invoice_upload":
headers = InvoiceCSV.ordered_columns()
assert headers is not None
rows = [headers] + rows
with open("file.csv", "w+") as f:
writer = csv.writer(f)
writer.writerows(rows)
yield f
my_file = csv_fixture(merchants, type="merchant_upload")
request = rf.post("/invoice_admin/upload_organisations/",
{"onboarding_file": my_file})
My endpoint does something like this:
if filename not in request.FILES:
raise Exception("Upload Failed: No file submitted.")
file = TextIOWrapper(
request.FILES[filename].file, encoding=request.encoding)
headers = peek_first_row(file)
missing = required_cols - set(headers)
if missing:
raise Exception(f"Columns missing in csv: {str(missing)})")
return csv.DictReader(file)
My endpoint works if I manually upload the file. However, if I try doing it programatically with the first snipper, I get an error:
def peek_first_row(file):
rows = csv.reader(file)
> headers = next(rows)
E StopIteration
app/invoice_admin/csv_parser.py:11: StopIteration
Please could someone guide me? I have looked at lots of tutorials, but I'm lost at this point.