|
1 | 1 | import json |
2 | 2 |
|
3 | 3 | import httpx |
| 4 | +import pytest |
4 | 5 | import respx |
5 | 6 | from pydantic import Field |
6 | 7 |
|
7 | 8 | from bubble_data_api_client.client.orm import BubbleBaseModel |
| 9 | +from bubble_data_api_client.exceptions import UnknownFieldError |
8 | 10 |
|
9 | 11 |
|
10 | 12 | def test_model_instantiation(): |
@@ -36,3 +38,47 @@ class Order(BubbleBaseModel, typename="order"): |
36 | 38 | assert route.call_count == 1 |
37 | 39 | request_body = json.loads(route.calls[0].request.content) |
38 | 40 | assert request_body == {"Buying company": "Acme Corp"} |
| 41 | + |
| 42 | + |
| 43 | +@respx.mock |
| 44 | +async def test_update_single_field(configured_client: None) -> None: |
| 45 | + """Verify update() sends only the specified field.""" |
| 46 | + |
| 47 | + class User(BubbleBaseModel, typename="user"): |
| 48 | + name: str |
| 49 | + email: str |
| 50 | + |
| 51 | + route = respx.patch("https://example.com/user/abc123").mock(return_value=httpx.Response(204)) |
| 52 | + |
| 53 | + await User.update(uid="abc123", name="New Name") |
| 54 | + |
| 55 | + assert route.call_count == 1 |
| 56 | + request_body = json.loads(route.calls[0].request.content) |
| 57 | + assert request_body == {"name": "New Name"} |
| 58 | + |
| 59 | + |
| 60 | +@respx.mock |
| 61 | +async def test_update_translates_field_aliases(configured_client: None) -> None: |
| 62 | + """Verify update() translates Python field names to Bubble aliases.""" |
| 63 | + |
| 64 | + class Order(BubbleBaseModel, typename="order"): |
| 65 | + company: str = Field(alias="Buying company") |
| 66 | + status: str |
| 67 | + |
| 68 | + route = respx.patch("https://example.com/order/xyz789").mock(return_value=httpx.Response(204)) |
| 69 | + |
| 70 | + await Order.update(uid="xyz789", company="Acme Corp", status="active") |
| 71 | + |
| 72 | + assert route.call_count == 1 |
| 73 | + request_body = json.loads(route.calls[0].request.content) |
| 74 | + assert request_body == {"Buying company": "Acme Corp", "status": "active"} |
| 75 | + |
| 76 | + |
| 77 | +async def test_update_raises_for_unknown_field() -> None: |
| 78 | + """Verify update() raises UnknownFieldError for fields not in the model.""" |
| 79 | + |
| 80 | + class User(BubbleBaseModel, typename="user"): |
| 81 | + name: str |
| 82 | + |
| 83 | + with pytest.raises(UnknownFieldError, match="unknown field: nonexistent"): |
| 84 | + await User.update(uid="abc123", nonexistent="value") |
0 commit comments