Skip to content

Commit

Permalink
Added a proper unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Feb 26, 2020
1 parent 1082237 commit 54aa31f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ def get_long_description():
"python-multipart",
"sqlite-utils",
],
extras_require={"test": ["pytest", "pytest-asyncio", "asgiref==3.1.2", "requests"]},
extras_require={"test": ["pytest", "pytest-asyncio", "asgiref==3.1.2", "httpx"]},
package_data={"datasette_upload_csvs": ["templates/*.html"]},
)
31 changes: 29 additions & 2 deletions tests/test_datasette_upload_csvs.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,29 @@
def test_blah():
assert True
from datasette.app import Datasette
import pytest
import httpx
import sqlite_utils


@pytest.mark.asyncio
async def test_upload(tmpdir):
path = str(tmpdir / "data.db")
db = sqlite_utils.Database(path)

db["hello"].insert({"hello": "world"})

datasette = Datasette([path])

# First test the upload page exists
async with httpx.AsyncClient(app=datasette.app()) as client:
response = await client.get("http://localhost/-/upload-csv")
assert 200 == response.status_code
assert b'<form action="/-/upload-csv" method="post"' in response.content

# Now try uploading a file
files = {"csv": ("dogs.csv", "name,age\nCleo,5\nPancakes,4", "text/csv")}
response = await client.post("http://localhost/-/upload-csv", files=files)
assert b"<h1>Upload complete</h1>" in response.content
assert b"Imported 2 rows into" in response.content

dogs = list(db["dogs"].rows)
assert [{"name": "Cleo", "age": "5"}, {"name": "Pancakes", "age": "4"}] == dogs

0 comments on commit 54aa31f

Please sign in to comment.