diff --git a/transaction_parser/tests/test_sales_order.py b/transaction_parser/tests/test_sales_order.py index 1f419a6..72e156f 100644 --- a/transaction_parser/tests/test_sales_order.py +++ b/transaction_parser/tests/test_sales_order.py @@ -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") + + 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 # ---------------------- diff --git a/transaction_parser/transaction_parser/controllers/transaction.py b/transaction_parser/transaction_parser/controllers/transaction.py index eca493e..dcf5545 100644 --- a/transaction_parser/transaction_parser/controllers/transaction.py +++ b/transaction_parser/transaction_parser/controllers/transaction.py @@ -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