|
| 1 | +import pytest |
| 2 | + |
1 | 3 | from postgrest import CountMethod |
| 4 | +from postgrest.exceptions import APIError |
2 | 5 |
|
3 | 6 | from .client import rest_client, rest_client_httpx |
4 | 7 |
|
@@ -76,6 +79,23 @@ async def test_no_match_maybe_single(): |
76 | 79 | assert res is None |
77 | 80 |
|
78 | 81 |
|
| 82 | +async def test_maybe_single_multiple_rows(): |
| 83 | + with pytest.raises(APIError) as exc_info: |
| 84 | + await ( |
| 85 | + rest_client() |
| 86 | + .from_("countries") |
| 87 | + .select("country_name, iso") |
| 88 | + .lte("numcode", 8) |
| 89 | + .gte("numcode", 4) |
| 90 | + .maybe_single() |
| 91 | + .execute() |
| 92 | + ) |
| 93 | + |
| 94 | + assert exc_info.value.code == "406" |
| 95 | + assert exc_info.value.message == "Cannot coerce the result to a single JSON object" |
| 96 | + assert exc_info.value.details == "The result contains more than one row." |
| 97 | + |
| 98 | + |
79 | 99 | async def test_equals(): |
80 | 100 | res = ( |
81 | 101 | await rest_client() |
@@ -500,6 +520,47 @@ async def test_rpc_with_single(): |
500 | 520 | assert res.data == {"nicename": "Albania", "country_name": "ALBANIA", "iso": "AL"} |
501 | 521 |
|
502 | 522 |
|
| 523 | +async def test_rpc_with_maybe_single(): |
| 524 | + res = ( |
| 525 | + await rest_client() |
| 526 | + .rpc("list_stored_countries", {}) |
| 527 | + .select("nicename, country_name, iso") |
| 528 | + .eq("nicename", "Albania") |
| 529 | + .maybe_single() |
| 530 | + .execute() |
| 531 | + ) |
| 532 | + |
| 533 | + assert res.data == {"nicename": "Albania", "country_name": "ALBANIA", "iso": "AL"} |
| 534 | + |
| 535 | + |
| 536 | +async def test_rpc_with_maybe_single_no_match(): |
| 537 | + res = ( |
| 538 | + await rest_client() |
| 539 | + .rpc("list_stored_countries", {}) |
| 540 | + .select("nicename, country_name, iso") |
| 541 | + .eq("nicename", "Wonderland") |
| 542 | + .maybe_single() |
| 543 | + .execute() |
| 544 | + ) |
| 545 | + |
| 546 | + assert res is None |
| 547 | + |
| 548 | + |
| 549 | +async def test_rpc_with_maybe_single_multiple_rows(): |
| 550 | + with pytest.raises(APIError) as exc_info: |
| 551 | + await ( |
| 552 | + rest_client() |
| 553 | + .rpc("list_stored_countries", {}) |
| 554 | + .select("nicename, country_name, iso") |
| 555 | + .maybe_single() |
| 556 | + .execute() |
| 557 | + ) |
| 558 | + |
| 559 | + assert exc_info.value.code == "406" |
| 560 | + assert exc_info.value.message == "Cannot coerce the result to a single JSON object" |
| 561 | + assert exc_info.value.details == "The result contains more than one row." |
| 562 | + |
| 563 | + |
503 | 564 | async def test_rpc_with_limit(): |
504 | 565 | res = ( |
505 | 566 | await rest_client() |
|
0 commit comments