Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 1 | import json |
| 2 | import unittest |
| 3 | |
| 4 | import frappe |
Prateeksha Singh | edeb4dc | 2017-05-15 11:32:06 +0530 | [diff] [blame] | 5 | import frappe.utils |
Prateeksha Singh | edeb4dc | 2017-05-15 11:32:06 +0530 | [diff] [blame] | 6 | from frappe.model import mapper |
| 7 | from frappe.test_runner import make_test_records |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 8 | from frappe.utils import add_months, nowdate |
| 9 | |
Prateeksha Singh | edeb4dc | 2017-05-15 11:32:06 +0530 | [diff] [blame] | 10 | |
| 11 | class TestMapper(unittest.TestCase): |
| 12 | def test_map_docs(self): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 13 | """Test mapping of multiple source docs on a single target doc""" |
Prateeksha Singh | edeb4dc | 2017-05-15 11:32:06 +0530 | [diff] [blame] | 14 | |
| 15 | make_test_records("Item") |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 16 | items = ["_Test Item", "_Test Item 2", "_Test FG Item"] |
Deepesh Garg | a9820cd | 2020-07-11 19:38:51 +0530 | [diff] [blame] | 17 | |
| 18 | # Make source docs (quotations) and a target doc (sales order) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 19 | qtn1, item_list_1 = self.make_quotation(items, "_Test Customer") |
| 20 | qtn2, item_list_2 = self.make_quotation(items, "_Test Customer") |
Deepesh Garg | a9820cd | 2020-07-11 19:38:51 +0530 | [diff] [blame] | 21 | so, item_list_3 = self.make_sales_order() |
Prateeksha Singh | edeb4dc | 2017-05-15 11:32:06 +0530 | [diff] [blame] | 22 | |
| 23 | # Map source docs to target with corresponding mapper method |
| 24 | method = "erpnext.selling.doctype.quotation.quotation.make_sales_order" |
| 25 | updated_so = mapper.map_docs(method, json.dumps([qtn1.name, qtn2.name]), so) |
| 26 | |
| 27 | # Assert that all inserted items are present in updated sales order |
| 28 | src_items = item_list_1 + item_list_2 + item_list_3 |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 29 | self.assertEqual(set(d for d in src_items), set(d.item_code for d in updated_so.items)) |
Prateeksha Singh | edeb4dc | 2017-05-15 11:32:06 +0530 | [diff] [blame] | 30 | |
Deepesh Garg | a9820cd | 2020-07-11 19:38:51 +0530 | [diff] [blame] | 31 | def make_quotation(self, item_list, customer): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 32 | qtn = frappe.get_doc( |
| 33 | { |
| 34 | "doctype": "Quotation", |
| 35 | "quotation_to": "Customer", |
| 36 | "party_name": customer, |
| 37 | "order_type": "Sales", |
| 38 | "transaction_date": nowdate(), |
| 39 | "valid_till": add_months(nowdate(), 1), |
| 40 | } |
| 41 | ) |
Prateeksha Singh | edeb4dc | 2017-05-15 11:32:06 +0530 | [diff] [blame] | 42 | for item in item_list: |
Deepesh Garg | a9820cd | 2020-07-11 19:38:51 +0530 | [diff] [blame] | 43 | qtn.append("items", {"qty": "2", "item_code": item}) |
Prateeksha Singh | edeb4dc | 2017-05-15 11:32:06 +0530 | [diff] [blame] | 44 | |
| 45 | qtn.submit() |
| 46 | return qtn, item_list |
| 47 | |
| 48 | def make_sales_order(self): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 49 | item = frappe.get_doc( |
| 50 | { |
| 51 | "base_amount": 1000.0, |
| 52 | "base_rate": 100.0, |
| 53 | "description": "CPU", |
| 54 | "doctype": "Sales Order Item", |
| 55 | "item_code": "_Test Item", |
| 56 | "item_name": "CPU", |
| 57 | "parentfield": "items", |
| 58 | "qty": 10.0, |
| 59 | "rate": 100.0, |
| 60 | "warehouse": "_Test Warehouse - _TC", |
| 61 | "stock_uom": "_Test UOM", |
| 62 | "conversion_factor": 1.0, |
| 63 | "uom": "_Test UOM", |
| 64 | } |
| 65 | ) |
| 66 | so = frappe.get_doc(frappe.get_test_records("Sales Order")[0]) |
Prateeksha Singh | edeb4dc | 2017-05-15 11:32:06 +0530 | [diff] [blame] | 67 | so.insert(ignore_permissions=True) |
Deepesh Garg | a9820cd | 2020-07-11 19:38:51 +0530 | [diff] [blame] | 68 | return so, [item.item_code] |