Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions transaction_parser/tests/test_sales_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,63 @@ def test_item_processing(self, mock_parse):
self.assertEqual(item.doctype, "Sales Order Item")
self.assertEqual(item.parentfield, "items")

# ----------------------
# Terms and Conditions Tests
# ----------------------

def test_get_terms_with_local_terms(self):
"""Test terms generation from local_terms data."""
controller = SalesOrder()
controller.data = self.sample_data

terms = controller.get_terms()

self.assertIn("Standard terms and conditions", terms)
self.assertIn("Incoterms: EXW", terms)

def test_get_terms_with_missing_local_terms(self):
"""Test terms generation does not fail when local_terms is None.

The AI response may omit `local_terms` or return it as null, so
get_terms must handle a None value gracefully instead of raising
AttributeError.
"""
modified_data = copy.deepcopy(self.sample_data)
modified_data.local_terms = None

controller = SalesOrder()
controller.data = modified_data

self.assertEqual(controller.get_terms(), "")

def test_get_terms_with_partial_local_terms(self):
"""Test terms generation when local_terms has only a description."""
modified_data = copy.deepcopy(self.sample_data)
modified_data.local_terms = frappe._dict(
{"description": "Only description", "incoterms": None}
)

controller = SalesOrder()
controller.data = modified_data

self.assertEqual(controller.get_terms(), "Only description")
Comment thread
coderabbitai[bot] marked this conversation as resolved.

def test_get_terms_with_missing_local_terms_key(self):
"""Test terms generation when the local_terms key is completely absent.

Unlike an explicit None value, this verifies get_terms relies on
frappe._dict returning None for a missing attribute rather than the
key merely being present with a null value.
"""
modified_data = copy.deepcopy(self.sample_data)
if hasattr(modified_data, "local_terms"):
delattr(modified_data, "local_terms")

controller = SalesOrder()
controller.data = modified_data

self.assertEqual(controller.get_terms(), "")

# ----------------------
# Integration Tests
# ----------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -496,11 +496,11 @@ def get_payment_schedule_doc(self, term):
### Terms and Conditions

def get_terms(self) -> str:
terms = (
description if (description := self.data.local_terms.description) else ""
)
local_terms = self.data.local_terms or frappe._dict()

terms = local_terms.description or ""

if incoterms := self.data.local_terms.incoterms:
if incoterms := local_terms.incoterms:
terms += f"\nIncoterms: {incoterms}"

return terms
Expand Down
Loading