Skip to content

Commit 95ef631

Browse files
committed
MPT-14899 E2E tests for catalog pricing-policies
1 parent 2824ad0 commit 95ef631

9 files changed

Lines changed: 221 additions & 20 deletions

File tree

mpt_api_client/resources/catalog/pricing_policies.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44
)
55
from mpt_api_client.http.mixins import (
66
AsyncCollectionMixin,
7-
AsyncCreateMixin,
8-
AsyncDeleteMixin,
7+
AsyncManagedResourceMixin,
98
CollectionMixin,
10-
CreateMixin,
11-
DeleteMixin,
9+
ManagedResourceMixin,
1210
)
1311
from mpt_api_client.models import Model, ResourceData
1412
from mpt_api_client.resources.catalog.pricing_policy_attachments import (
@@ -30,8 +28,7 @@ class PricingPoliciesServiceConfig:
3028

3129

3230
class PricingPoliciesService( # noqa: WPS215
33-
CreateMixin[PricingPolicy],
34-
DeleteMixin,
31+
ManagedResourceMixin[PricingPolicy],
3532
CollectionMixin[PricingPolicy],
3633
Service[PricingPolicy],
3734
PricingPoliciesServiceConfig,
@@ -45,7 +42,9 @@ def attachments(self, pricing_policy_id: str) -> PricingPolicyAttachmentsService
4542
endpoint_params={"pricing_policy_id": pricing_policy_id},
4643
)
4744

48-
def activate(self, resource_id: str, resource_data: ResourceData) -> PricingPolicy:
45+
def activate(
46+
self, resource_id: str, resource_data: ResourceData | None = None
47+
) -> PricingPolicy:
4948
"""Activate pricing policy.
5049
5150
Args:
@@ -57,7 +56,7 @@ def activate(self, resource_id: str, resource_data: ResourceData) -> PricingPoli
5756
"""
5857
return self._resource_action(resource_id, "POST", "activate", json=resource_data)
5958

60-
def disable(self, resource_id: str, resource_data: ResourceData) -> PricingPolicy:
59+
def disable(self, resource_id: str, resource_data: ResourceData | None = None) -> PricingPolicy:
6160
"""Disable pricing policy.
6261
6362
Args:
@@ -71,8 +70,7 @@ def disable(self, resource_id: str, resource_data: ResourceData) -> PricingPolic
7170

7271

7372
class AsyncPricingPoliciesService(
74-
AsyncCreateMixin[PricingPolicy],
75-
AsyncDeleteMixin,
73+
AsyncManagedResourceMixin[PricingPolicy],
7674
AsyncCollectionMixin[PricingPolicy],
7775
AsyncService[PricingPolicy],
7876
PricingPoliciesServiceConfig,
@@ -86,7 +84,9 @@ def attachments(self, pricing_policy_id: str) -> AsyncPricingPolicyAttachmentsSe
8684
endpoint_params={"pricing_policy_id": pricing_policy_id},
8785
)
8886

89-
async def activate(self, resource_id: str, resource_data: ResourceData) -> PricingPolicy:
87+
async def activate(
88+
self, resource_id: str, resource_data: ResourceData | None = None
89+
) -> PricingPolicy:
9090
"""Activate pricing policy.
9191
9292
Args:
@@ -98,7 +98,9 @@ async def activate(self, resource_id: str, resource_data: ResourceData) -> Prici
9898
"""
9999
return await self._resource_action(resource_id, "POST", "activate", json=resource_data)
100100

101-
async def disable(self, resource_id: str, resource_data: ResourceData) -> PricingPolicy:
101+
async def disable(
102+
self, resource_id: str, resource_data: ResourceData | None = None
103+
) -> PricingPolicy:
102104
"""Disable pricing policy.
103105
104106
Args:

tests/e2e/accounts/account/test_async_account.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ async def async_created_account(logger, async_mpt_ops, account_factory, account_
1515
yield res_account
1616

1717
try:
18-
await async_mpt_ops.accounts.accounts.deactivate(res_account.id)
18+
await async_mpt_ops.accounts.accounts.disable(res_account.id)
1919
except MPTAPIError as error:
2020
print("TEARDOWN - Unable to deactivate account: %s", error.title) # noqa: WPS421
2121

tests/e2e/accounts/account/test_sync_account.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def created_account(logger, mpt_ops, account_factory, account_icon):
1515
yield res_account
1616

1717
try:
18-
mpt_ops.accounts.accounts.deactivate(res_account.id)
18+
mpt_ops.accounts.accounts.disable(res_account.id)
1919
except MPTAPIError as error:
2020
print("TEARDOWN - Unable to deactivate account: %s", error.title) # noqa: WPS421
2121

tests/e2e/accounts/sellers/test_async_sellers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ async def test_update_seller_mpt_error(async_mpt_ops, seller_factory, timestamp,
9797

9898
async def test_activate_seller(async_mpt_ops, async_created_seller, timestamp):
9999
seller_data = await async_created_seller(external_id=f"Async Activate E2E Seller - {timestamp}")
100-
await async_mpt_ops.accounts.sellers.deactivate(seller_data.id)
100+
await async_mpt_ops.accounts.sellers.disable(seller_data.id)
101101
activated_seller = await async_mpt_ops.accounts.sellers.activate(seller_data.id)
102102

103103
assert activated_seller is not None
@@ -112,14 +112,14 @@ async def test_deactivate_seller(async_mpt_ops, async_created_seller, timestamp)
112112
seller_data = await async_created_seller(
113113
external_id=f"Async Deactivate E2E Seller - {timestamp}"
114114
)
115-
deactivated_seller = await async_mpt_ops.accounts.sellers.deactivate(seller_data.id)
115+
deactivated_seller = await async_mpt_ops.accounts.sellers.disable(seller_data.id)
116116

117117
assert deactivated_seller is not None
118118

119119

120120
async def test_deactivate_seller_mpt_error(async_mpt_ops, invalid_seller_id):
121121
with pytest.raises(MPTAPIError):
122-
await async_mpt_ops.accounts.sellers.deactivate(invalid_seller_id)
122+
await async_mpt_ops.accounts.sellers.disable(invalid_seller_id)
123123

124124

125125
async def test_disable_seller(async_mpt_ops, async_created_seller, timestamp):

tests/e2e/accounts/sellers/test_sync_sellers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def test_update_seller_mpt_error(mpt_ops, seller_factory, timestamp, invalid_sel
9696

9797
def test_activate_seller(mpt_ops, created_seller, timestamp):
9898
seller_data = created_seller(external_id=f"Activate E2E Seller - {timestamp}")
99-
mpt_ops.accounts.sellers.deactivate(seller_data.id)
99+
mpt_ops.accounts.sellers.disable(seller_data.id)
100100
activated_seller = mpt_ops.accounts.sellers.activate(seller_data.id)
101101

102102
assert activated_seller is not None
@@ -109,14 +109,14 @@ def test_activate_seller_mpt_error(mpt_ops, invalid_seller_id):
109109

110110
def test_deactivate_seller(mpt_ops, created_seller, timestamp):
111111
seller_data = created_seller(external_id=f"Deactivate E2E Seller - {timestamp}")
112-
deactivated_seller = mpt_ops.accounts.sellers.deactivate(seller_data.id)
112+
deactivated_seller = mpt_ops.accounts.sellers.disable(seller_data.id)
113113

114114
assert deactivated_seller is not None
115115

116116

117117
def test_deactivate_seller_mpt_error(mpt_ops, invalid_seller_id):
118118
with pytest.raises(MPTAPIError):
119-
mpt_ops.accounts.sellers.deactivate(invalid_seller_id)
119+
mpt_ops.accounts.sellers.disable(invalid_seller_id)
120120

121121

122122
def test_disable_seller(mpt_ops, created_seller, timestamp):
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import pytest
2+
3+
4+
@pytest.fixture
5+
def buyer_id(e2e_config):
6+
return e2e_config["accounts.buyer.account.id"]
7+
8+
9+
@pytest.fixture
10+
def pricing_policy_data(buyer_id, product_id):
11+
return {
12+
"name": "e2e - pricing policy please delete",
13+
"description": "Test pricing policy description",
14+
"client": {"id": buyer_id},
15+
"product": {"id": product_id},
16+
"eligibility": {"client": True, "partner": False},
17+
"margin": "0.20",
18+
}
19+
20+
21+
@pytest.fixture
22+
def pricing_policy_id(e2e_config):
23+
return e2e_config.get("catalog.pricing_policy.id")
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import pytest
2+
3+
from mpt_api_client.exceptions import MPTAPIError
4+
from mpt_api_client.rql.query_builder import RQLQuery
5+
6+
pytestmark = [pytest.mark.flaky]
7+
8+
9+
@pytest.fixture
10+
def async_pricing_policies_service(async_mpt_ops):
11+
return async_mpt_ops.catalog.pricing_policies
12+
13+
14+
@pytest.fixture
15+
async def async_created_pricing_policy(async_pricing_policies_service, pricing_policy_data):
16+
policy = await async_pricing_policies_service.create(pricing_policy_data)
17+
18+
yield policy
19+
20+
try:
21+
await async_pricing_policies_service.delete(policy.id)
22+
except MPTAPIError as error:
23+
print(f"TEARDOWN - Unable to delete pricing policy {policy.id}: {error.title}")
24+
25+
26+
def test_create_pricing_policy(async_created_pricing_policy, pricing_policy_data):
27+
assert async_created_pricing_policy.name == pricing_policy_data["name"]
28+
29+
30+
async def test_get_pricing_policy(async_pricing_policies_service, async_created_pricing_policy):
31+
fetched = await async_pricing_policies_service.get(async_created_pricing_policy.id)
32+
assert fetched.id == async_created_pricing_policy.id
33+
34+
35+
async def test_get_pricing_policy_by_id(
36+
async_pricing_policies_service, async_created_pricing_policy
37+
):
38+
fetched = await async_pricing_policies_service.get(async_created_pricing_policy.id)
39+
assert fetched.id == async_created_pricing_policy.id
40+
41+
42+
async def test_iterate_pricing_policies(
43+
async_pricing_policies_service, async_created_pricing_policy
44+
):
45+
policies = [policy async for policy in async_pricing_policies_service.iterate()]
46+
assert any(policy.id == async_created_pricing_policy.id for policy in policies)
47+
48+
49+
async def test_filter_pricing_policies(
50+
async_pricing_policies_service, async_created_pricing_policy
51+
):
52+
target_id = async_created_pricing_policy.id
53+
select_fields = ["-description"]
54+
filtered = async_pricing_policies_service.filter(RQLQuery(id=target_id)).select(*select_fields)
55+
policies = [policy async for policy in filtered.iterate()]
56+
assert len(policies) == 1
57+
assert policies[0].id == target_id
58+
59+
60+
async def test_activate_deactivate_pricing_policy(
61+
async_pricing_policies_service, async_created_pricing_policy
62+
):
63+
deactivate = await async_pricing_policies_service.disable(async_created_pricing_policy.id)
64+
assert deactivate.id == async_created_pricing_policy.id
65+
66+
activated = await async_pricing_policies_service.activate(async_created_pricing_policy.id)
67+
assert activated.id == async_created_pricing_policy.id
68+
69+
70+
async def test_delete_pricing_policy(async_pricing_policies_service, async_created_pricing_policy):
71+
await async_pricing_policies_service.delete(async_created_pricing_policy.id) # act
72+
73+
74+
async def test_get_pricing_policy_not_found(async_pricing_policies_service):
75+
bogus_id = "PPY-0000-NOTFOUND"
76+
with pytest.raises(MPTAPIError, match=r"404 Not Found"):
77+
await async_pricing_policies_service.get(bogus_id)
78+
79+
80+
async def test_create_pricing_policy_invalid_data(async_pricing_policies_service):
81+
invalid_data = {"name": "e2e - delete me", "description": "invalid data"}
82+
with pytest.raises(MPTAPIError, match=r"400 One or more validation errors occurred"):
83+
await async_pricing_policies_service.create(invalid_data)
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import pytest
2+
3+
from mpt_api_client.exceptions import MPTAPIError
4+
from mpt_api_client.rql.query_builder import RQLQuery
5+
6+
pytestmark = [pytest.mark.flaky]
7+
8+
9+
@pytest.fixture
10+
def pricing_policies_service(mpt_ops):
11+
return mpt_ops.catalog.pricing_policies
12+
13+
14+
@pytest.fixture
15+
def created_pricing_policy(pricing_policies_service, pricing_policy_data):
16+
policy = pricing_policies_service.create(pricing_policy_data)
17+
18+
yield policy
19+
20+
try:
21+
pricing_policies_service.delete(policy.id)
22+
except MPTAPIError as error:
23+
print(f"TEARDOWN - Unable to delete pricing policy {policy.id}: {error.title}")
24+
25+
26+
def test_create_pricing_policy(created_pricing_policy, pricing_policy_data):
27+
assert created_pricing_policy.name == pricing_policy_data["name"]
28+
29+
30+
def test_get_pricing_policy(pricing_policies_service, created_pricing_policy):
31+
fetched = pricing_policies_service.get(created_pricing_policy.id)
32+
assert fetched.id == created_pricing_policy.id
33+
34+
35+
def test_get_pricing_policy_by_id(pricing_policies_service, pricing_policy_id):
36+
if not pricing_policy_id:
37+
pytest.skip("No pricing_policy_id configured")
38+
fetched = pricing_policies_service.get(pricing_policy_id)
39+
assert fetched.id == pricing_policy_id
40+
41+
42+
def test_iterate_pricing_policies(pricing_policies_service, created_pricing_policy):
43+
policies = list(pricing_policies_service.iterate())
44+
assert any(policy.id == created_pricing_policy.id for policy in policies)
45+
46+
47+
def test_filter_pricing_policies(pricing_policies_service, created_pricing_policy):
48+
target_id = created_pricing_policy.id
49+
select_fields = ["-description"]
50+
filtered = pricing_policies_service.filter(RQLQuery(id=target_id)).select(*select_fields)
51+
policies = list(filtered.iterate())
52+
assert len(policies) == 1
53+
assert policies[0].id == target_id
54+
55+
56+
def test_activate_deactivate_pricing_policy(pricing_policies_service, created_pricing_policy):
57+
deactivate = pricing_policies_service.disable(created_pricing_policy.id)
58+
assert deactivate.id == created_pricing_policy.id
59+
60+
activated = pricing_policies_service.activate(deactivate.id)
61+
assert activated.id == created_pricing_policy.id
62+
63+
64+
def test_delete_pricing_policy(pricing_policies_service, created_pricing_policy):
65+
pricing_policies_service.delete(created_pricing_policy.id) # act
66+
67+
68+
def test_get_pricing_policy_not_found(pricing_policies_service):
69+
bogus_id = "PPY-0000-NOTFOUND"
70+
with pytest.raises(MPTAPIError, match=r"404 Not Found"):
71+
pricing_policies_service.get(bogus_id)
72+
73+
74+
def test_create_pricing_policy_invalid_data(pricing_policies_service):
75+
invalid_data = {"name": "e2e - delete me", "description": "invalid data"}
76+
with pytest.raises(MPTAPIError, match=r"400 One or more validation errors occurred"):
77+
pricing_policies_service.create(invalid_data)

tests/unit/resources/catalog/test_pricing_policies.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,19 @@ def test_async_property_services(
140140

141141
assert isinstance(property_service, expected_service_class)
142142
assert property_service.endpoint_params == {"pricing_policy_id": "PRP-0000-0001"}
143+
144+
145+
@pytest.mark.parametrize(
146+
"method",
147+
["get", "create", "update", "delete", "activate", "disable"],
148+
)
149+
def test_mixins_present(pricing_policies_service, method):
150+
assert hasattr(pricing_policies_service, method)
151+
152+
153+
@pytest.mark.parametrize(
154+
"method",
155+
["get", "create", "update", "delete", "activate", "disable"],
156+
)
157+
def test_async_mixins_present(async_pricing_policies_service, method):
158+
assert hasattr(async_pricing_policies_service, method)

0 commit comments

Comments
 (0)