Skip to content

Commit

Permalink
IMP sh_api_cart: tests for options
Browse files Browse the repository at this point in the history
  • Loading branch information
bealdav committed Jun 6, 2024
1 parent e732b56 commit d829f82
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 1 deletion.
1 change: 1 addition & 0 deletions shopinvader_api_cart/routers/cart.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ def _group_transactions_by_product_and_option(
transactions_by_product_and_option = OrderedDict()
for trans in transactions:
product_id = trans.product_id
# TODO test to remove json
options = (
json.dumps(trans.options.model_dump(exclude_unset=True), sort_keys=True)
if trans.options
Expand Down
24 changes: 24 additions & 0 deletions shopinvader_api_cart/tests/fake_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright 2024 Akretion
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import api, fields, models


class SaleOrderLine(models.Model):
_inherit = 'sale.order.line'

note = fields.Char()


class ShopinvaderApiCartRouterHelper(models.AbstractModel):
_inherit = "shopinvader_api_cart.cart_router.helper"

@api.model
def _prepare_line_from_transactions(self, cart, transactions):
vals = super()._prepare_line_from_transactions(cart, transactions)
# if the qty delta (add and remove of a product) is 0 then the vals is None
if vals:
options = transactions[0].options
if options and options.note:
vals["note"] = options.note
return vals
66 changes: 66 additions & 0 deletions shopinvader_api_cart/tests/test_shopinvader_api_cart_option.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Copyright 2022 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

import json
import random

from odoo_test_helper import FakeModelLoader


from ..routers.cart import cart_router

from .common import CommonSaleCart
from odoo.addons.shopinvader_schema_sale.schemas import sale_line_option

from odoo import api, models

NUMBER = range(1, 3)


class SaleLineOption(sale_line_option.SaleLineOption, extends=True):
note: str | None = None

@classmethod
def _prepare_from_sale_order_line(cls, line):
vals = super()._prepare_from_sale_order_line(line)
if hasattr(line, "note") and line.note:
vals["note"] = line.note
return vals


class TestSaleCartOption(CommonSaleCart):
@classmethod
def setUpClass(cls):
super(TestSaleCartOption, cls).setUpClass()
cls.loader = FakeModelLoader(cls.env, cls.__module__)
cls.loader.backup_registry()
from .fake_model import ShopinvaderApiCartRouterHelper
from .fake_model import SaleOrderLine
cls.loader.update_registry((SaleOrderLine,))
# cls.loader.update_registry((SaleOrderLine, ShopinvaderApiCartRouterHelper))
cls.loader.update_registry((ShopinvaderApiCartRouterHelper,))

@classmethod
def tearDownClass(cls):
cls.loader.restore_registry()
super(TestSaleCartOption, cls).tearDownClass()

def test_transactions(self) -> None:
so = self.env["sale.order"]._create_empty_cart(
self.default_fastapi_authenticated_partner.id
)
data = {
"transactions": [
{
"uuid": self.trans_uuid_1,
"product_id": self.product_1.id,
"qty": 1,
"options": {"note": str(random.Random(NUMBER))},
}
]
}
with self._create_test_client(router=cart_router) as test_client:
test_client.post(f"/{so.uuid}/sync", content=json.dumps(data))
line = so.order_line
self.assertEqual(1, line.product_uom_qty)
self.assertIn(line.note, ("1", "2"), "Note field should be 1 or 2")
2 changes: 1 addition & 1 deletion shopinvader_schema_sale/schemas/sale_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ def from_sale_order_line(cls, odoo_rec):
qty=json_float_round(
odoo_rec.product_uom_qty,
precision_digits=len(str(odoo_rec.product_uom.rounding).split(".")[1]),
options=SaleLineOption.from_sale_order_line(odoo_rec),
),
options=SaleLineOption.from_sale_order_line(odoo_rec),
)

0 comments on commit d829f82

Please sign in to comment.