Skip to content

Commit 0499fa2

Browse files
committed
Testing - support order line items
1 parent 489cdfc commit 0499fa2

File tree

2 files changed

+115
-10
lines changed

2 files changed

+115
-10
lines changed

src/commercetools/testing/orders.py

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from commercetools.testing.utils import (
2121
create_commercetools_response,
2222
create_from_draft,
23+
get_product_from_storage,
2324
money_to_typed,
2425
set_custom_field,
2526
set_line_item_custom_field,
@@ -88,6 +89,9 @@ def _create_from_cart_draft(self, draft: models.OrderFromCartDraft, id: str):
8889
return order
8990

9091
def _create_from_import_draft(self, draft: models.OrderImportDraft, id: str):
92+
line_items = [
93+
self._create_line_item_from_draft(line) for line in draft.line_items
94+
]
9195
custom_line_items = [
9296
self._create_custom_line_item_from_draft(line)
9397
for line in draft.custom_line_items
@@ -98,7 +102,7 @@ def _create_from_import_draft(self, draft: models.OrderImportDraft, id: str):
98102
version=1,
99103
created_at=datetime.datetime.now(datetime.timezone.utc),
100104
last_modified_at=datetime.datetime.now(datetime.timezone.utc),
101-
line_items=[],
105+
line_items=line_items,
102106
custom_line_items=custom_line_items,
103107
total_price=money_to_typed(draft.total_price),
104108
taxed_price=create_from_draft(draft.taxed_price),
@@ -113,13 +117,98 @@ def _create_from_import_draft(self, draft: models.OrderImportDraft, id: str):
113117
)
114118
return order
115119

120+
def _create_line_item_from_draft(self, draft: models.LineItemImportDraft):
121+
tax_rate = 0
122+
tax_included = False
123+
124+
if draft.tax_rate:
125+
tax_rate = draft.tax_rate.amount
126+
tax_included = draft.tax_rate.included_in_price
127+
128+
currency_code = draft.price.value.currency_code
129+
total_net_cents = draft.price.value.cent_amount * draft.quantity
130+
total_gross_cents = total_net_cents
131+
132+
if tax_rate:
133+
if tax_included:
134+
total_net_cents = total_gross_cents / (1 + tax_rate)
135+
else:
136+
total_gross_cents = total_net_cents * (1 + tax_rate)
137+
138+
total_net = money_to_typed(
139+
models.Money(cent_amount=total_net_cents, currency_code=currency_code)
140+
)
141+
total_gross = money_to_typed(
142+
models.Money(cent_amount=total_gross_cents, currency_code=currency_code)
143+
)
144+
taxed_price = None
145+
if tax_rate:
146+
taxed_price = models.TaxedItemPrice(
147+
total_net=total_net,
148+
total_gross=total_gross,
149+
)
150+
151+
product = get_product_from_storage(
152+
self._storage, product_id=draft.product_id, sku=draft.variant.sku
153+
)
154+
155+
current = product.master_data.current
156+
variant = current.master_variant
157+
158+
return models.LineItem(
159+
id=str(uuid.uuid4()),
160+
product_id=draft.product_id,
161+
product_slug=current.slug,
162+
product_type=product.product_type,
163+
variant=variant,
164+
name=current.name,
165+
quantity=draft.quantity,
166+
price_mode=models.LineItemPriceMode.PLATFORM,
167+
line_item_mode=models.LineItemMode.STANDARD,
168+
price=create_from_draft(draft.price),
169+
total_price=total_net,
170+
state=[],
171+
discounted_price_per_quantity=[],
172+
tax_rate=draft.tax_rate,
173+
taxed_price=taxed_price,
174+
custom=create_from_draft(draft.custom),
175+
shipping_details=create_from_draft(draft.shipping_details),
176+
)
177+
116178
def _create_custom_line_item_from_draft(self, draft: models.CustomLineItemDraft):
179+
tax_rate = 0
180+
tax_included = False
181+
182+
if draft.external_tax_rate:
183+
tax_rate = draft.external_tax_rate.amount
184+
tax_included = draft.external_tax_rate.included_in_price
185+
117186
total_net_cents = draft.money.cent_amount * draft.quantity
187+
total_gross_cents = total_net_cents
188+
189+
if tax_rate:
190+
if tax_included:
191+
total_net_cents = total_gross_cents / (1 + tax_rate)
192+
else:
193+
total_gross_cents = total_net_cents * (1 + tax_rate)
194+
118195
total_net = money_to_typed(
119196
models.Money(
120197
cent_amount=total_net_cents, currency_code=draft.money.currency_code
121198
)
122199
)
200+
total_gross = money_to_typed(
201+
models.Money(
202+
cent_amount=total_gross_cents, currency_code=draft.money.currency_code
203+
)
204+
)
205+
taxed_price = None
206+
if tax_rate:
207+
taxed_price = models.TaxedItemPrice(
208+
total_net=total_net,
209+
total_gross=total_gross,
210+
)
211+
123212
return models.CustomLineItem(
124213
id=str(uuid.uuid4()),
125214
name=draft.name,
@@ -131,6 +220,7 @@ def _create_custom_line_item_from_draft(self, draft: models.CustomLineItemDraft)
131220
discounted_price_per_quantity=[],
132221
tax_category=draft.tax_category,
133222
tax_rate=create_from_draft(draft.external_tax_rate),
223+
taxed_price=taxed_price,
134224
custom=create_from_draft(draft.custom),
135225
shipping_details=create_from_draft(draft.shipping_details),
136226
)

src/commercetools/testing/utils.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import copy
2-
import importlib
32
import json
43
import typing
54
import uuid
@@ -17,12 +16,11 @@
1716
)
1817
from commercetools.platform.models._abstract import _BaseType
1918
from commercetools.platform.models._schemas.product import ProductSchema
20-
from commercetools.platform.models.cart import (
21-
Cart,
22-
CartSetLineItemCustomFieldAction,
23-
TaxPortion,
24-
)
25-
from commercetools.platform.models.order import Order, OrderSetLineItemCustomFieldAction
19+
from commercetools.platform.models.cart import CartSetLineItemCustomFieldAction
20+
from commercetools.platform.models.order import OrderSetLineItemCustomFieldAction
21+
22+
if typing.TYPE_CHECKING:
23+
from commercetools.testing import Storage # NOQA
2624

2725

2826
class InternalUpdateError(ValueError):
@@ -99,6 +97,21 @@ def create_from_draft(draft: typing.Optional[_BaseType]):
9997
state=draft.state,
10098
sub_rates=draft.sub_rates,
10199
)
100+
if isinstance(draft, models.PriceDraft):
101+
return models.Price(
102+
id=str(uuid.uuid4()),
103+
value=money_to_typed(draft.value),
104+
country=draft.country,
105+
customer_group=draft.customer_group,
106+
channel=draft.channel,
107+
valid_from=draft.valid_from,
108+
valid_until=draft.valid_until,
109+
discounted=draft.discounted,
110+
custom=create_from_draft(draft.custom),
111+
tiers=None
112+
if draft.tiers is None
113+
else [create_from_draft(t) for t in draft.tiers],
114+
)
102115

103116
raise ValueError(f"Unsupported type {draft.__class__}")
104117

@@ -303,14 +316,16 @@ def updater(
303316

304317

305318
def get_product_from_storage(
306-
storage: "commercetools.testing.Storage",
307-
product_id: uuid.UUID = None,
319+
storage: "Storage",
320+
product_id: typing.Union[str, uuid.UUID] = None,
308321
sku: str = None,
309322
) -> typing.Optional[models.Product]:
310323
product = None
311324
product_store = storage._stores["product"]
312325

313326
if product_id:
327+
if isinstance(product_id, str):
328+
product_id = uuid.UUID(product_id)
314329
product = ProductSchema().load(product_store[product_id])
315330
elif sku:
316331
for product_data in product_store.values():

0 commit comments

Comments
 (0)