diff --git a/pos_customer_wallet/tests/common.py b/pos_customer_wallet/tests/common.py index 811625612..b0ab4d7ac 100644 --- a/pos_customer_wallet/tests/common.py +++ b/pos_customer_wallet/tests/common.py @@ -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", @@ -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( { @@ -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": { @@ -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, @@ -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, - ) diff --git a/pos_customer_wallet/tests/test_balance.py b/pos_customer_wallet/tests/test_balance.py index a0034b60c..4d7f1f6ea 100644 --- a/pos_customer_wallet/tests/test_balance.py +++ b/pos_customer_wallet/tests/test_balance.py @@ -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) @@ -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) @@ -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)