Skip to content

Commit c7de096

Browse files
committed
fix: naming and precise dates to test edge cases
1 parent 2f6d1f4 commit c7de096

File tree

2 files changed

+52
-32
lines changed

2 files changed

+52
-32
lines changed

check_run/tests/setup.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -286,12 +286,12 @@ def create_payment_terms_templates(settings):
286286
)
287287
doc.save()
288288

289-
if not frappe.db.exists("Payment Terms Template", "3% 10 Net 30"):
289+
if not frappe.db.exists("Payment Terms Template", "$20 10 Net 30"):
290290
doc = frappe.new_doc("Payment Terms Template")
291-
doc.template_name = "3% 10 Net 30"
291+
doc.template_name = "$20 10 Net 30"
292292

293293
pt = frappe.new_doc("Payment Term")
294-
pt.payment_term_name = "3% 10 Net 30"
294+
pt.payment_term_name = "$20 10 Net 30"
295295
pt.due_date_based_on = "Day(s) after the end of the invoice month"
296296
pt.discount_validity_based_on = "Day(s) after the end of the invoice month"
297297
pt.invoice_portion = 100
@@ -338,12 +338,12 @@ def create_payment_terms_templates(settings):
338338
doc.save()
339339

340340
# Payment term with "Month(s) after the end of the invoice month" for discount validity
341-
if not frappe.db.exists("Payment Terms Template", "3% 10 Net 30 - Month End"):
341+
if not frappe.db.exists("Payment Terms Template", "3% Net 30"):
342342
doc = frappe.new_doc("Payment Terms Template")
343-
doc.template_name = "3% 10 Net 30 - Month End"
343+
doc.template_name = "3% Net 30"
344344

345345
pt = frappe.new_doc("Payment Term")
346-
pt.payment_term_name = "3% 10 Net 30 - Month End"
346+
pt.payment_term_name = "3% Net 30"
347347
pt.due_date_based_on = "Month(s) after the end of the invoice month"
348348
pt.discount_validity_based_on = "Month(s) after the end of the invoice month"
349349
pt.invoice_portion = 100
@@ -613,7 +613,7 @@ def create_invoices(settings):
613613
pi.set_posting_time = 1
614614
pi.posting_date = datetime.date.today() - datetime.timedelta(days=5)
615615
pi.supplier = suppliers[3][0]
616-
pi.payment_terms_template = "3% 10 Net 30"
616+
pi.payment_terms_template = "$20 10 Net 30"
617617
pi.append(
618618
"items",
619619
{
@@ -686,7 +686,7 @@ def create_invoices(settings):
686686
pi.set_posting_time = 1
687687
pi.posting_date = first_of_month - datetime.timedelta(days=5) # Previous month
688688
pi.supplier = suppliers[1][0]
689-
pi.payment_terms_template = "3% 10 Net 30 - Month End"
689+
pi.payment_terms_template = "3% Net 30"
690690
pi.append(
691691
"items",
692692
{
@@ -704,7 +704,7 @@ def create_invoices(settings):
704704
pi.set_posting_time = 1
705705
pi.posting_date = datetime.date.today() - datetime.timedelta(days=5)
706706
pi.supplier = suppliers[0][0]
707-
pi.payment_terms_template = "3% 10 Net 30"
707+
pi.payment_terms_template = "$20 10 Net 30"
708708
pi.append(
709709
"items",
710710
{

check_run/tests/test_check_run.py

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -219,67 +219,87 @@ def test_use_cr_posting_date_config(cr):
219219

220220
@pytest.mark.order(18)
221221
def test_calculate_payment_term_discount_function():
222+
# Create precise dates to test edge cases
223+
today = datetime.date.today()
224+
first_of_current_month = today.replace(day=1)
225+
last_month = first_of_current_month - datetime.timedelta(days=1)
226+
first_of_last_month = last_month.replace(day=1)
227+
last_day_of_last_month = last_month
228+
229+
# Simulate being on the 9th day of current month (within 10-day discount period)
230+
simulated_today_within = first_of_current_month + datetime.timedelta(days=8) # day 9
231+
# Simulate being on the 12th day of current month (outside 10-day discount period)
232+
simulated_today_outside = first_of_current_month + datetime.timedelta(days=11) # day 12
233+
222234
cases = [
223235
{
224236
"desc": "2% 10 Net 30 - within the discount period",
225237
"payment_term": "2% 10 Net 30",
226-
"posting_date": datetime.date.today() - datetime.timedelta(days=5),
238+
"posting_date": first_of_last_month, # Invoice on 1st of previous month
239+
"payment_date": simulated_today_within, # Payment on 9th of current month
227240
"amount": 1000.0,
228241
"expected_discount": 1000.0 * 0.02,
229242
"should_have_discount": True,
230243
},
231244
{
232245
"desc": "2% 10 Net 30 - outside the discount period",
233246
"payment_term": "2% 10 Net 30",
234-
"posting_date": datetime.date.today() - datetime.timedelta(days=20),
247+
"posting_date": first_of_last_month, # Invoice on 1st of previous month
248+
"payment_date": simulated_today_outside, # Payment on 12th of current month (outside period)
235249
"amount": 1000.0,
236250
"expected_discount": 0.0,
237251
"should_have_discount": False,
238252
},
239253
{
240-
"desc": "3% 10 Net 30 - within the discount period",
241-
"payment_term": "3% 10 Net 30",
242-
"posting_date": datetime.date.today() - datetime.timedelta(days=8),
254+
"desc": "$20 10 Net 30 - within the discount period",
255+
"payment_term": "$20 10 Net 30",
256+
"posting_date": last_day_of_last_month, # Invoice on last day of previous month
257+
"payment_date": simulated_today_within, # Payment on 9th of current month
243258
"amount": 500.0,
244259
"expected_discount": min(20.0, 500.0),
245260
"should_have_discount": True,
246261
},
247262
{
248-
"desc": "3% 10 Net 30 - outside the discount period",
249-
"payment_term": "3% 10 Net 30",
250-
"posting_date": datetime.date.today() - datetime.timedelta(days=25),
263+
"desc": "$20 10 Net 30 - outside the discount period",
264+
"payment_term": "$20 10 Net 30",
265+
"posting_date": last_day_of_last_month, # Invoice on last day of previous month
266+
"payment_date": simulated_today_outside, # Payment on 12th of current month (outside period)
251267
"amount": 500.0,
252268
"expected_discount": 0.0,
253269
"should_have_discount": False,
254270
},
255271
{
256272
"desc": "2% 10 Net 30 - Invoice Date - last valid day",
257273
"payment_term": "2% 10 Net 30 - Invoice Date",
258-
"posting_date": datetime.date.today() - datetime.timedelta(days=10),
274+
"posting_date": today - datetime.timedelta(days=10), # Exactly 10 days ago
275+
"payment_date": today,
259276
"amount": 800.0,
260277
"expected_discount": 800.0 * 0.02,
261278
"should_have_discount": True,
262279
},
263280
{
264281
"desc": "2% 10 Net 30 - Invoice Date - outside the discount period",
265282
"payment_term": "2% 10 Net 30 - Invoice Date",
266-
"posting_date": datetime.date.today() - datetime.timedelta(days=11),
283+
"posting_date": today - datetime.timedelta(days=11), # 11 days ago (outside period)
284+
"payment_date": today,
267285
"amount": 800.0,
268286
"expected_discount": 0.0,
269287
"should_have_discount": False,
270288
},
271289
{
272-
"desc": "3% 10 Net 30 - Month End - within valid month",
273-
"payment_term": "3% 10 Net 30 - Month End",
274-
"posting_date": (datetime.date.today().replace(day=1) - datetime.timedelta(days=5)),
290+
"desc": "3% Net 30 - within valid month",
291+
"payment_term": "3% Net 30",
292+
"posting_date": first_of_last_month, # Invoice from previous month
293+
"payment_date": today, # Payment today (within validity month)
275294
"amount": 600.0,
276295
"expected_discount": 600.0 * 0.03,
277296
"should_have_discount": True,
278297
},
279298
{
280-
"desc": "3% 10 Net 30 - Month End - outside valid month",
281-
"payment_term": "3% 10 Net 30 - Month End",
282-
"posting_date": (datetime.date.today().replace(day=1) - datetime.timedelta(days=40)),
299+
"desc": "3% Net 30 - outside valid month",
300+
"payment_term": "3% Net 30",
301+
"posting_date": first_of_last_month - datetime.timedelta(days=32), # Invoice from 2 months ago
302+
"payment_date": today, # Payment today (outside validity month)
283303
"amount": 600.0,
284304
"expected_discount": 0.0,
285305
"should_have_discount": False,
@@ -295,7 +315,7 @@ def test_calculate_payment_term_discount_function():
295315
"posting_date": case["posting_date"],
296316
}
297317
)
298-
payment_date = datetime.date.today()
318+
payment_date = case["payment_date"]
299319
discount_amount, has_discount = calculate_payment_term_discount(transaction, payment_date)
300320

301321
assert (
@@ -351,9 +371,9 @@ def test_cr_payment_term_discounts(cr):
351371

352372
if payment_term in [
353373
"2% 10 Net 30",
354-
"3% 10 Net 30",
374+
"$20 10 Net 30",
355375
"2% 10 Net 30 - Invoice Date",
356-
"3% 10 Net 30 - Month End",
376+
"3% Net 30",
357377
]:
358378
row["pay"] = True
359379
row["mode_of_payment"] = "Credit Card"
@@ -372,10 +392,10 @@ def test_cr_payment_term_discounts(cr):
372392
), f"Percentage discount mismatch for {case['invoice']}: expected {expected_discount}, got {case['discount_amount']}"
373393

374394
# Test Case 2: Invoices with amount-based discounts (fixed amount)
375-
amount_discount_cases = [tc for tc in test_cases if "3% 10 Net 30" in tc["payment_term"]]
395+
amount_discount_cases = [tc for tc in test_cases if "$20 10 Net 30" in tc["payment_term"]]
376396
for case in amount_discount_cases:
377397
if case["has_discount"]:
378-
if case["payment_term"] == "3% 10 Net 30 - Month End":
398+
if case["payment_term"] == "3% Net 30":
379399
expected_discount = case["amount"] * 0.03
380400
else:
381401
expected_discount = min(20.0, case["amount"])
@@ -403,8 +423,8 @@ def test_cr_process_with_discounts(cr):
403423
for row in cr.transactions:
404424
if row.get("doctype") == "Purchase Invoice" and row.get("payment_term") in [
405425
"2% 10 Net 30",
406-
"3% 10 Net 30",
407-
"3% 10 Net 30 - Month End",
426+
"$20 10 Net 30",
427+
"3% Net 30",
408428
"2% 10 Net 30 - Invoice Date",
409429
]:
410430
row["pay"] = True

0 commit comments

Comments
 (0)