blob: 9dbcf6cafd2189c14eebff33a372d2bfc46dd7dd [file] [log] [blame]
Chillar Anand915b3432021-09-02 16:44:59 +05301import json
2import unittest
3
4import frappe
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +05305import frappe.utils
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +05306from frappe.model import mapper
7from frappe.test_runner import make_test_records
Chillar Anand915b3432021-09-02 16:44:59 +05308from frappe.utils import add_months, nowdate
9
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +053010
11class TestMapper(unittest.TestCase):
12 def test_map_docs(self):
Ankush Menat494bd9e2022-03-28 18:52:46 +053013 """Test mapping of multiple source docs on a single target doc"""
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +053014
15 make_test_records("Item")
Ankush Menat494bd9e2022-03-28 18:52:46 +053016 items = ["_Test Item", "_Test Item 2", "_Test FG Item"]
Deepesh Garga9820cd2020-07-11 19:38:51 +053017
18 # Make source docs (quotations) and a target doc (sales order)
Ankush Menat494bd9e2022-03-28 18:52:46 +053019 qtn1, item_list_1 = self.make_quotation(items, "_Test Customer")
20 qtn2, item_list_2 = self.make_quotation(items, "_Test Customer")
Deepesh Garga9820cd2020-07-11 19:38:51 +053021 so, item_list_3 = self.make_sales_order()
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +053022
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 Menat494bd9e2022-03-28 18:52:46 +053029 self.assertEqual(set(d for d in src_items), set(d.item_code for d in updated_so.items))
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +053030
Deepesh Garga9820cd2020-07-11 19:38:51 +053031 def make_quotation(self, item_list, customer):
Ankush Menat494bd9e2022-03-28 18:52:46 +053032 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 Singhedeb4dc2017-05-15 11:32:06 +053042 for item in item_list:
Deepesh Garga9820cd2020-07-11 19:38:51 +053043 qtn.append("items", {"qty": "2", "item_code": item})
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +053044
45 qtn.submit()
46 return qtn, item_list
47
48 def make_sales_order(self):
Ankush Menat494bd9e2022-03-28 18:52:46 +053049 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 Singhedeb4dc2017-05-15 11:32:06 +053067 so.insert(ignore_permissions=True)
Deepesh Garga9820cd2020-07-11 19:38:51 +053068 return so, [item.item_code]