Skip to content

Commit 20a0120

Browse files
committed
Fix PostgrestClient.schema() not work. Add tests for PostgrestClient
1 parent d8d9e2c commit 20a0120

File tree

7 files changed

+109
-6
lines changed

7 files changed

+109
-6
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111

1212
- Remove `RequestBuilder.filter_in()` and `RequestBuilder.filter_out()`
1313

14+
#### Fixed
15+
16+
- Fix `PostgrestClient.schema()` not actually work
17+
1418
### v0.2.0
1519

1620
#### Added

README.md

+21-1
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,18 @@ Status: **Unstable**
1414

1515
### Instructions
1616

17-
#### With Poetry
17+
#### With Poetry (recommended)
1818

1919
```sh
2020
$ poetry add postgrest-py
2121
```
2222

23+
#### With Pip
24+
25+
```sh
26+
$ pip install postgrest-py
27+
```
28+
2329
## USAGE
2430

2531
### Getting started
@@ -65,6 +71,20 @@ await client.from_("countries").eq("name", "Việt Nam").delete().execute()
6571

6672
### Stored procedures (RPC)
6773

74+
## DEVELOPMENT
75+
76+
```sh
77+
$ git clone https://github.com/lqmanh/postgrest-py.git
78+
$ cd postgrest-py
79+
$ poetry install
80+
```
81+
82+
### Testing
83+
84+
```sh
85+
$ poetry run pytest
86+
```
87+
6888
## CHANGELOG
6989

7090
Read more [here](https://github.com/lqmanh/postgrest-py/blob/master/CHANGELOG.md).

TODO.md

-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@
33
- [ ] Exact match filtering
44
- [ ] AND and OR filters
55
- [ ] Counting
6-
- [ ] Tests
76
- [ ] CI/CD

poetry.lock

+19-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

postgrest_py/client.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Union
2+
13
from deprecation import deprecated
24
from httpx import AsyncClient, Response
35

@@ -15,7 +17,7 @@ def __init__(self, base_url: str, *, schema="public") -> None:
1517
"Accept-Profile": schema,
1618
"Content-Profile": schema,
1719
}
18-
self.session = AsyncClient(base_url=base_url, params={}, headers=headers)
20+
self.session = AsyncClient(base_url=base_url, headers=headers)
1921

2022
async def __aenter__(self):
2123
return self
@@ -26,7 +28,13 @@ async def __aexit__(self, exc_type, exc, tb) -> None:
2628
async def aclose(self) -> None:
2729
await self.session.aclose()
2830

29-
def auth(self, token: str, *, username: str = None, password: str = None):
31+
def auth(
32+
self,
33+
token: str,
34+
*,
35+
username: Union[str, bytes] = None,
36+
password: Union[str, bytes] = None,
37+
):
3038
"""Authenticate the client with either bearer token or basic authentication."""
3139
if username:
3240
self.session.auth = (username, password)
@@ -36,7 +44,7 @@ def auth(self, token: str, *, username: str = None, password: str = None):
3644

3745
def schema(self, schema: str):
3846
"""Switch to another schema."""
39-
self.session.merge_headers(
47+
self.session.headers.update(
4048
{"Accept-Profile": schema, "Content-Profile": schema}
4149
)
4250
return self

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ deprecation = "^2.1.0"
1212

1313
[tool.poetry.dev-dependencies]
1414
pytest = "^6.0.1"
15+
pytest-asyncio = "^0.14.0"
1516
black = {version = "^19.10b0", allow-prereleases = true}
1617
pylint = "^2.5.3"
1718

tests/test_client.py

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import pytest
2+
from httpx import AsyncClient, Headers
3+
4+
from postgrest_py import PostgrestClient
5+
6+
7+
@pytest.fixture
8+
async def postgrest_client():
9+
async with PostgrestClient("https://example.com") as client:
10+
yield client
11+
12+
13+
@pytest.mark.asyncio
14+
def test_constructor(postgrest_client):
15+
session = postgrest_client.session
16+
17+
assert session.base_url == "https://example.com"
18+
assert session.headers == Headers(
19+
{
20+
"accept": "application/json",
21+
"content-type": "application/json",
22+
"accept-profile": "public",
23+
"content-profile": "public",
24+
}
25+
)
26+
27+
28+
class TestAuth:
29+
@pytest.mark.asyncio
30+
def test_auth_token(self, postgrest_client):
31+
postgrest_client.auth("s3cr3t")
32+
session = postgrest_client.session
33+
34+
assert session.headers["Authorization"] == "Bearer s3cr3t"
35+
36+
@pytest.mark.asyncio
37+
def test_auth_basic(self, postgrest_client):
38+
postgrest_client.auth(None, username="admin", password="s3cr3t")
39+
session = postgrest_client.session
40+
41+
assert session.auth == ("admin", "s3cr3t")
42+
43+
44+
@pytest.mark.asyncio
45+
def test_schema(postgrest_client: PostgrestClient):
46+
postgrest_client.schema("private")
47+
session = postgrest_client.session
48+
subheaders = {
49+
"accept-profile": "private",
50+
"content-profile": "private",
51+
}
52+
53+
assert subheaders.items() < dict(session.headers).items()

0 commit comments

Comments
 (0)