test: add tests for loyalty tier selection

This commit is contained in:
David
2024-09-05 12:32:27 +02:00
parent 81dc5872c4
commit c914ea4adc
2 changed files with 68 additions and 2 deletions

View File

@@ -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],

View File

@@ -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():