Skip to content

Commit

Permalink
Overload TestClient.delete to accept JSON data again (#107)
Browse files Browse the repository at this point in the history
Starting from version 0.21 the `TestClient` in starlette
is based on `httpx`, which does not accept a `json` parameter
for its `delete` method.

Motivation: AIMAAS expects a request body for some DELETE requests.

Co-authored-by: frozenIceage <[email protected]>
  • Loading branch information
crazyscientist and frozenIceage authored Nov 22, 2022
1 parent abd87ec commit 5ddf705
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/default.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Tests

on: [push, pull_request]
on: [push, pull_request, workflow_dispatch]

jobs:
test:
Expand Down
2 changes: 2 additions & 0 deletions backend/requirements_test.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# See: https://github.com/encode/starlette/issues/1943
httpx
pytest
pytest-cov
pytest-mock==3.6.1
Expand Down
22 changes: 21 additions & 1 deletion backend/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from alembic import command
from alembic.config import Config
from httpx._client import USE_CLIENT_DEFAULT
import pytest
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
Expand Down Expand Up @@ -333,11 +334,30 @@ def testuser(dbsession) -> User:
return get_user(db=dbsession, username=TEST_USER.username)


class OldStyleTestClient(TestClient):
def delete(self, url, *, params=None, headers=None, cookies=None, auth=USE_CLIENT_DEFAULT,
follow_redirects=None, allow_redirects=None, timeout=USE_CLIENT_DEFAULT,
extensions=None, json=None):
# Note: Since starlette 0.21 `TestClient.delete` no longer accepts the `json` parameter.
return self.request(
"DELETE",
url,
params=params,
headers=headers,
cookies=cookies,
auth=auth,
follow_redirects=self._choose_redirect_arg(follow_redirects, allow_redirects),
timeout=timeout,
extensions=extensions,
json=json
)


@pytest.fixture
def client(dbsession):
app = create_app(session=dbsession)
app.dependency_overrides[get_db] = lambda: dbsession
client = TestClient(app)
client = OldStyleTestClient(app)
yield client


Expand Down
6 changes: 1 addition & 5 deletions backend/tests/test_dynamic_routes.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from datetime import datetime, timezone, timedelta

import pytest
from sqlalchemy import update

from ..models import *
from ..dynamic_routes import *
Expand All @@ -13,10 +12,7 @@
class TestRouteBasics:
def test_load_schema_data(self, dbsession, client):
schemas = load_schemas(db=dbsession)
assert len(schemas) == 2

s = schemas[0]
assert s.name == 'Person'
assert {s.name for s in schemas} == {'Person', 'UnPerson'}

def test_routes_were_generated(self, dbsession, client):
routes = [
Expand Down

0 comments on commit 5ddf705

Please sign in to comment.