Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/postgrest/src/postgrest/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ def __init__(
self.base_url = base_url
self.default_headers = (
supabase_headers.update(default_headers)
.set("Accept-Profile", schema)
.set("Content-Profile", schema)
.override("Accept-Profile", schema)
.override("Content-Profile", schema)
)

def set_auth(
Expand Down
79 changes: 79 additions & 0 deletions src/postgrest/tests/test_schema_switch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
from collections.abc import Callable

from httpx import AsyncClient, Client, MockTransport, Request, Response
from pydantic import BaseModel
from supabase_utils.http.adapters.httpx import AsyncHttpxSession, HttpxSession

from postgrest import AsyncPostgrestClient, SyncPostgrestClient, Table


class ItemRow(BaseModel):
id: int


ITEMS = Table("items", ItemRow)


HEADERS = {"apikey": "key"}
EXPECTED = [
(["public"], ["public"], ["key"]),
(["other"], ["other"], ["key"]),
(["other"], ["other"], ["key"]),
(["other"], ["other"], ["key"]),
(["other"], ["other"], ["key"]),
]


def sent_headers(request: Request) -> tuple[list[str], list[str], list[str]]:
return (
request.headers.get_list("Accept-Profile"),
request.headers.get_list("Content-Profile"),
request.headers.get_list("apikey"),
)


def recording_handler(seen: list[Request]) -> Callable[[Request], Response]:
def handler(request: Request) -> Response:
seen.append(request)
return Response(200, json=[])

return handler


def test_sync_schema_switches_replace_the_profile_headers() -> None:
seen: list[Request] = []
client = SyncPostgrestClient(
"https://example.com",
http_session=HttpxSession(
client=Client(transport=MockTransport(recording_handler(seen)))
),
headers=HEADERS,
)

other = client.schema("private").schema("other")
client.from_("items").select("*").execute()
other.from_("items").select("*").execute()
other.rpc("hello", {}).execute()
other.table(ITEMS).select().execute()
other.from_("items").insert({"id": 1}).execute()

assert [sent_headers(request) for request in seen] == EXPECTED


async def test_async_schema_switches_replace_the_profile_headers() -> None:
seen: list[Request] = []
async with AsyncClient(transport=MockTransport(recording_handler(seen))) as http:
client = AsyncPostgrestClient(
"https://example.com",
http_session=AsyncHttpxSession(client=http),
headers=HEADERS,
)

other = client.schema("private").schema("other")
await client.from_("items").select("*").execute()
await other.from_("items").select("*").execute()
await other.rpc("hello", {}).execute()
await other.table(ITEMS).select().execute()
await other.from_("items").insert({"id": 1}).execute()

assert [sent_headers(request) for request in seen] == EXPECTED
Loading