From c914ea4adc2a4d66c1091b4bb3579d03cf05164d Mon Sep 17 00:00:00 2001 From: David Date: Thu, 5 Sep 2024 12:32:27 +0200 Subject: [PATCH] test: add tests for loyalty tier selection --- .../loyalty_program/loyalty_program.py | 5 +- .../loyalty_program/test_loyalty_program.py | 65 +++++++++++++++++++ 2 files changed, 68 insertions(+), 2 deletions(-) diff --git a/erpnext/accounts/doctype/loyalty_program/loyalty_program.py b/erpnext/accounts/doctype/loyalty_program/loyalty_program.py index 9a2b1c113a6..240866df8de 100644 --- a/erpnext/accounts/doctype/loyalty_program/loyalty_program.py +++ b/erpnext/accounts/doctype/loyalty_program/loyalty_program.py @@ -79,9 +79,10 @@ def get_loyalty_program_details_with_points( ): lp_details = get_loyalty_program_details(customer, loyalty_program, company=company, silent=silent) loyalty_program = frappe.get_doc("Loyalty Program", loyalty_program) - lp_details.update( - get_loyalty_details(customer, loyalty_program.name, expiry_date, company, include_expired_entry) + loyalty_details = get_loyalty_details( + customer, loyalty_program.name, expiry_date, company, include_expired_entry ) + lp_details.update(loyalty_details) tier_spent_level = sorted( [d.as_dict() for d in loyalty_program.collection_rules], diff --git a/erpnext/accounts/doctype/loyalty_program/test_loyalty_program.py b/erpnext/accounts/doctype/loyalty_program/test_loyalty_program.py index 4d21fb69806..a79109bcd2b 100644 --- a/erpnext/accounts/doctype/loyalty_program/test_loyalty_program.py +++ b/erpnext/accounts/doctype/loyalty_program/test_loyalty_program.py @@ -7,6 +7,7 @@ import frappe from frappe.utils import cint, flt, getdate, today from erpnext.accounts.doctype.loyalty_program.loyalty_program import ( + get_loyalty_details, get_loyalty_program_details_with_points, ) from erpnext.accounts.party import get_dashboard_info @@ -197,6 +198,70 @@ class TestLoyaltyProgram(unittest.TestCase): for d in company_wise_info: self.assertTrue(d.get("loyalty_points")) + @unittest.mock.patch("erpnext.accounts.doctype.loyalty_program.loyalty_program.get_loyalty_details") + def test_tier_selection(self, mock_get_loyalty_details): + # Create a new loyalty program with multiple tiers + loyalty_program = frappe.get_doc( + { + "doctype": "Loyalty Program", + "loyalty_program_name": "Test Tier Selection", + "auto_opt_in": 1, + "from_date": today(), + "loyalty_program_type": "Multiple Tier Program", + "conversion_factor": 1, + "expiry_duration": 10, + "company": "_Test Company", + "cost_center": "Main - _TC", + "expense_account": "Loyalty - _TC", + "collection_rules": [ + {"tier_name": "Gold", "collection_factor": 1000, "min_spent": 20000}, + {"tier_name": "Silver", "collection_factor": 1000, "min_spent": 10000}, + {"tier_name": "Bronze", "collection_factor": 1000, "min_spent": 5000}, + ], + } + ) + loyalty_program.insert() + + # Test cases with different total_spent and current_transaction_amount combinations + test_cases = [ + (0, 6000, "Bronze"), + (0, 15000, "Silver"), + (0, 25000, "Gold"), + (4000, 500, "Bronze"), + (8000, 3000, "Silver"), + (18000, 3000, "Gold"), + (22000, 5000, "Gold"), + ] + + for total_spent, current_transaction_amount, expected_tier in test_cases: + with self.subTest(total_spent=total_spent, current_transaction_amount=current_transaction_amount): + # Mock the get_loyalty_details function to update the total_spent + def side_effect(*args, **kwargs): + result = get_loyalty_details(*args, **kwargs) + result.update({"total_spent": total_spent}) + return result + + mock_get_loyalty_details.side_effect = side_effect + + lp_details = get_loyalty_program_details_with_points( + "Test Loyalty Customer", + loyalty_program=loyalty_program.name, + company="_Test Company", + current_transaction_amount=current_transaction_amount, + ) + + # Get the selected tier based on the current implementation + selected_tier = lp_details.tier_name + + self.assertEqual( + selected_tier, + expected_tier, + f"Expected tier {expected_tier} for total_spent {total_spent} and current_transaction_amount {current_transaction_amount}, but got {selected_tier}", + ) + + # Clean up + loyalty_program.delete() + def get_points_earned(self): def get_returned_amount():