Skip to content

Commit

Permalink
Internals tests for datasette.client methods
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Oct 9, 2020
1 parent 5d73cc0 commit a168735
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
9 changes: 9 additions & 0 deletions datasette/views/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@ def database_color(self, database):
async def options(self, request, *args, **kwargs):
return Response.text("Method not allowed", status=405)

async def put(self, request, *args, **kwargs):
return Response.text("Method not allowed", status=405)

async def patch(self, request, *args, **kwargs):
return Response.text("Method not allowed", status=405)

async def delete(self, request, *args, **kwargs):
return Response.text("Method not allowed", status=405)

async def dispatch_request(self, request, *args, **kwargs):
handler = getattr(self, request.method.lower(), None)
return await handler(request, *args, **kwargs)
Expand Down
44 changes: 44 additions & 0 deletions tests/test_internals_datasette_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from .fixtures import app_client
import httpx
import pytest


@pytest.fixture
def datasette(app_client):
return app_client.ds


@pytest.mark.asyncio
@pytest.mark.parametrize(
"method,path,expected_status",
[
("get", "/", 200),
("options", "/", 405),
("head", "/", 200),
("put", "/", 405),
("patch", "/", 405),
("delete", "/", 405),
],
)
async def test_client_methods(datasette, method, path, expected_status):
client_method = getattr(datasette.client, method)
response = await client_method(path)
assert isinstance(response, httpx.Response)
assert response.status_code == expected_status
# Try that again using datasette.client.request
response2 = await datasette.client.request(method, path)
assert response2.status_code == expected_status


@pytest.mark.asyncio
async def test_client_post(datasette):
response = await datasette.client.post(
"/-/messages",
data={
"message": "A message",
},
allow_redirects=False,
)
assert isinstance(response, httpx.Response)
assert response.status_code == 302
assert "ds_messages" in response.cookies

0 comments on commit a168735

Please sign in to comment.