|
5 | 5 | from mpt_api_client.http.async_service import AsyncService |
6 | 6 | from mpt_api_client.http.service import Service |
7 | 7 | from mpt_api_client.resources.billing.mixins import ( |
| 8 | + AcceptableMixin, |
| 9 | + AsyncAcceptableMixin, |
8 | 10 | AsyncIssuableMixin, |
9 | 11 | AsyncRecalculatableMixin, |
10 | 12 | AsyncRegeneratableMixin, |
@@ -69,6 +71,24 @@ class DummyAsyncIssuableService( |
69 | 71 | _collection_key = "data" |
70 | 72 |
|
71 | 73 |
|
| 74 | +class DummyAcceptableService( |
| 75 | + AcceptableMixin[DummyModel], |
| 76 | + Service[DummyModel], |
| 77 | +): |
| 78 | + _endpoint = "/public/v1/dummy/acceptable/" |
| 79 | + _model_class = DummyModel |
| 80 | + _collection_key = "data" |
| 81 | + |
| 82 | + |
| 83 | +class DummyAsyncAcceptableService( |
| 84 | + AsyncAcceptableMixin[DummyModel], |
| 85 | + AsyncService[DummyModel], |
| 86 | +): |
| 87 | + _endpoint = "/public/v1/dummy/acceptable/" |
| 88 | + _model_class = DummyModel |
| 89 | + _collection_key = "data" |
| 90 | + |
| 91 | + |
72 | 92 | @pytest.fixture |
73 | 93 | def regeneratable_service(http_client): |
74 | 94 | return DummyRegeneratableService(http_client=http_client) |
@@ -99,6 +119,16 @@ def async_issuable_service(async_http_client): |
99 | 119 | return DummyAsyncIssuableService(http_client=async_http_client) |
100 | 120 |
|
101 | 121 |
|
| 122 | +@pytest.fixture |
| 123 | +def acceptable_service(http_client): |
| 124 | + return DummyAcceptableService(http_client=http_client) |
| 125 | + |
| 126 | + |
| 127 | +@pytest.fixture |
| 128 | +def async_acceptable_service(async_http_client): |
| 129 | + return DummyAsyncAcceptableService(http_client=async_http_client) |
| 130 | + |
| 131 | + |
102 | 132 | @pytest.mark.parametrize( |
103 | 133 | ("action", "input_status"), |
104 | 134 | [ |
@@ -493,3 +523,119 @@ async def test_async_issuable_resource_actions_no_data( |
493 | 523 | assert request.content == request_expected_content |
494 | 524 | assert issuable_obj.to_dict() == response_expected_data |
495 | 525 | assert isinstance(issuable_obj, DummyModel) |
| 526 | + |
| 527 | + |
| 528 | +@pytest.mark.parametrize( |
| 529 | + ("action", "input_status"), |
| 530 | + [ |
| 531 | + ("accept", {"id": "OBJ-0000-0001", "status": "update"}), |
| 532 | + ("queue", {"id": "OBJ-0000-0001", "status": "update"}), |
| 533 | + ], |
| 534 | +) |
| 535 | +def test_acceptable_resource_actions(acceptable_service, action, input_status): |
| 536 | + request_expected_content = b'{"id":"OBJ-0000-0001","status":"update"}' |
| 537 | + response_expected_data = {"id": "OBJ-0000-0001", "status": "new_status"} |
| 538 | + with respx.mock: |
| 539 | + mock_route = respx.post( |
| 540 | + f"https://api.example.com/public/v1/dummy/acceptable/OBJ-0000-0001/{action}" |
| 541 | + ).mock( |
| 542 | + return_value=httpx.Response( |
| 543 | + status_code=httpx.codes.OK, |
| 544 | + headers={"content-type": "application/json"}, |
| 545 | + json=response_expected_data, |
| 546 | + ) |
| 547 | + ) |
| 548 | + accept_obj = getattr(acceptable_service, action)("OBJ-0000-0001", input_status) |
| 549 | + |
| 550 | + assert mock_route.call_count == 1 |
| 551 | + request = mock_route.calls[0].request |
| 552 | + |
| 553 | + assert request.content == request_expected_content |
| 554 | + assert accept_obj.to_dict() == response_expected_data |
| 555 | + assert isinstance(accept_obj, DummyModel) |
| 556 | + |
| 557 | + |
| 558 | +@pytest.mark.parametrize( |
| 559 | + ("action", "input_status"), |
| 560 | + [("accept", None), ("queue", None)], |
| 561 | +) |
| 562 | +def test_acceptable_resource_actions_no_data(acceptable_service, action, input_status): |
| 563 | + request_expected_content = b"" |
| 564 | + response_expected_data = {"id": "OBJ-0000-0001", "status": "new_status"} |
| 565 | + with respx.mock: |
| 566 | + mock_route = respx.post( |
| 567 | + f"https://api.example.com/public/v1/dummy/acceptable/OBJ-0000-0001/{action}" |
| 568 | + ).mock( |
| 569 | + return_value=httpx.Response( |
| 570 | + status_code=httpx.codes.OK, |
| 571 | + headers={"content-type": "application/json"}, |
| 572 | + json=response_expected_data, |
| 573 | + ) |
| 574 | + ) |
| 575 | + accept_obj = getattr(acceptable_service, action)("OBJ-0000-0001", input_status) |
| 576 | + |
| 577 | + assert mock_route.call_count == 1 |
| 578 | + request = mock_route.calls[0].request |
| 579 | + |
| 580 | + assert request.content == request_expected_content |
| 581 | + assert accept_obj.to_dict() == response_expected_data |
| 582 | + assert isinstance(accept_obj, DummyModel) |
| 583 | + |
| 584 | + |
| 585 | +@pytest.mark.parametrize( |
| 586 | + ("action", "input_status"), |
| 587 | + [ |
| 588 | + ("accept", {"id": "OBJ-0000-0001", "status": "update"}), |
| 589 | + ("queue", {"id": "OBJ-0000-0001", "status": "update"}), |
| 590 | + ], |
| 591 | +) |
| 592 | +async def test_async_acceptable_resource_actions(async_acceptable_service, action, input_status): |
| 593 | + request_expected_content = b'{"id":"OBJ-0000-0001","status":"update"}' |
| 594 | + response_expected_data = {"id": "OBJ-0000-0001", "status": "new_status"} |
| 595 | + with respx.mock: |
| 596 | + mock_route = respx.post( |
| 597 | + f"https://api.example.com/public/v1/dummy/acceptable/OBJ-0000-0001/{action}" |
| 598 | + ).mock( |
| 599 | + return_value=httpx.Response( |
| 600 | + status_code=httpx.codes.OK, |
| 601 | + headers={"content-type": "application/json"}, |
| 602 | + json=response_expected_data, |
| 603 | + ) |
| 604 | + ) |
| 605 | + accept_obj = await getattr(async_acceptable_service, action)("OBJ-0000-0001", input_status) |
| 606 | + |
| 607 | + assert mock_route.call_count == 1 |
| 608 | + request = mock_route.calls[0].request |
| 609 | + |
| 610 | + assert request.content == request_expected_content |
| 611 | + assert accept_obj.to_dict() == response_expected_data |
| 612 | + assert isinstance(accept_obj, DummyModel) |
| 613 | + |
| 614 | + |
| 615 | +@pytest.mark.parametrize( |
| 616 | + ("action", "input_status"), |
| 617 | + [("accept", None), ("queue", None)], |
| 618 | +) |
| 619 | +async def test_async_acceptable_resource_actions_no_data( |
| 620 | + async_acceptable_service, action, input_status |
| 621 | +): |
| 622 | + request_expected_content = b"" |
| 623 | + response_expected_data = {"id": "OBJ-0000-0001", "status": "new_status"} |
| 624 | + with respx.mock: |
| 625 | + mock_route = respx.post( |
| 626 | + f"https://api.example.com/public/v1/dummy/acceptable/OBJ-0000-0001/{action}" |
| 627 | + ).mock( |
| 628 | + return_value=httpx.Response( |
| 629 | + status_code=httpx.codes.OK, |
| 630 | + headers={"content-type": "application/json"}, |
| 631 | + json=response_expected_data, |
| 632 | + ) |
| 633 | + ) |
| 634 | + accept_obj = await getattr(async_acceptable_service, action)("OBJ-0000-0001", input_status) |
| 635 | + |
| 636 | + assert mock_route.call_count == 1 |
| 637 | + request = mock_route.calls[0].request |
| 638 | + |
| 639 | + assert request.content == request_expected_content |
| 640 | + assert accept_obj.to_dict() == response_expected_data |
| 641 | + assert isinstance(accept_obj, DummyModel) |
0 commit comments