Skip to content

Commit

Permalink
[IMP] pos_customer_wallet: Slightly refactor tests to use the same se…
Browse files Browse the repository at this point in the history
…ssion

Signed-off-by: Carmen Bianca BAKKER <[email protected]>
  • Loading branch information
carmenbianca committed May 17, 2024
1 parent 8772350 commit 9a7c707
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 32 deletions.
65 changes: 42 additions & 23 deletions pos_customer_wallet/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,47 @@ def setUpClass(cls, *args, **kwargs):
cls.customer_wallet_payment_method = cls.env.ref(
"pos_customer_wallet.customer_wallet_payment_method"
)
cls.cash_payment_method = cls.env["pos.payment.method"].search(
[("is_cash_count", "=", True)], limit=1
)
cls.pos_session = cls._create_session(cls)

def create_wallet_pos_payment(
self,
pos_session=None,
product=None,
payment_method=None,
amount=0,
partner=None,
):
if pos_session is None:
pos_session = self.pos_session
if product is None:
product = self.env["product.product"].create(
{
"name": "Foo Product",
"available_in_pos": True,
"list_price": amount,
"taxes_id": False,
}
)
if payment_method is None:
payment_method = self.customer_wallet_payment_method
if partner is None:
partner = self.partner
self._create_pos_order(
pos_session,
product,
payment_method,
amount,
partner,
)

def _create_random_uid(self):
return "%05d-%03d-%04d" % (randint(1, 99999), randint(1, 999), randint(1, 9999))

# TODO: maybe also pass a pos_config or pos_session as parameter. not
# necessary yet
def _create_pos_order(self, product, payment_method, amount, partner):
# TODO: maybe pass a pos_config as parameter. not necessary yet
def _create_session(self):
pricelist = self.env["product.pricelist"].create(
{
"name": "Test pricelist",
Expand All @@ -40,8 +74,6 @@ def _create_pos_order(self, product, payment_method, amount, partner):
],
}
)
uid = self._create_random_uid()

# Create a new pos config and open it
pos_config = self.env.ref("point_of_sale.pos_config_main").copy(
{
Expand All @@ -55,6 +87,10 @@ def _create_pos_order(self, product, payment_method, amount, partner):
pos_session.action_pos_session_open()
# Bypass cash control
pos_session.state = "opened"
return pos_session

def _create_pos_order(self, pos_session, product, payment_method, amount, partner):
uid = self._create_random_uid()

order_data = {
"data": {
Expand Down Expand Up @@ -95,7 +131,7 @@ def _create_pos_order(self, product, payment_method, amount, partner):
]
],
"pos_session_id": pos_session.id,
"pricelist_id": pricelist.id,
"pricelist_id": pos_session.config_id.pricelist_id.id,
"partner_id": partner.id,
"user_id": self.env.user.id,
"uid": uid,
Expand All @@ -109,20 +145,3 @@ def _create_pos_order(self, product, payment_method, amount, partner):
}

return self.env["pos.order"].create_from_ui([order_data])

def _create_wallet_pos_payment(self, amount=0, partner=None):
if partner is None:
partner = self.partner
self._create_pos_order(
self.env["product.product"].create(
{
"name": "Foo Product",
"available_in_pos": True,
"list_price": amount,
"taxes_id": False,
}
),
self.customer_wallet_payment_method,
amount,
partner,
)
15 changes: 6 additions & 9 deletions pos_customer_wallet/tests/test_balance.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class TestPosBalance(TestBalance):
def test_with_pos_payment(self):
"""Pos payments in open POS sessions affect balance."""
self._create_move(credit=100)
self._create_wallet_pos_payment(amount=40)
self.create_wallet_pos_payment(amount=40)

self.assertEqual(self.partner.customer_wallet_balance, 60)

Expand All @@ -18,7 +18,7 @@ def test_with_pos_payment_different_partner(self):
clients.
"""
other_partner = self.env.ref("base.res_partner_address_31")
self._create_wallet_pos_payment(amount=100, partner=other_partner)
self.create_wallet_pos_payment(amount=100, partner=other_partner)

self.assertEqual(self.partner.customer_wallet_balance, 0)
self.assertEqual(other_partner.customer_wallet_balance, -100)
Expand All @@ -27,12 +27,9 @@ def test_credit_by_product(self):
# Intialize wallet with 500
self._create_move(credit=500)

self._create_pos_order(
self.wallet_product,
self.env["pos.payment.method"].search(
[("is_cash_count", "=", True)], limit=1
),
1000,
self.partner,
self.create_wallet_pos_payment(
product=self.wallet_product,
payment_method=self.cash_payment_method,
amount=1000,
)
self.assertEqual(self.partner.customer_wallet_balance, 1500)

0 comments on commit 9a7c707

Please sign in to comment.