Prateeksha Singh | edeb4dc | 2017-05-15 11:32:06 +0530 | [diff] [blame] | 1 | from __future__ import unicode_literals |
| 2 | import unittest |
| 3 | import frappe |
| 4 | |
| 5 | import random, json |
| 6 | import frappe.utils |
KanchanChauhan | 58c9934 | 2017-07-26 18:17:11 +0530 | [diff] [blame] | 7 | from frappe.utils import nowdate, add_months |
Prateeksha Singh | edeb4dc | 2017-05-15 11:32:06 +0530 | [diff] [blame] | 8 | from frappe.model import mapper |
| 9 | from frappe.test_runner import make_test_records |
| 10 | |
| 11 | class TestMapper(unittest.TestCase): |
| 12 | def test_map_docs(self): |
| 13 | '''Test mapping of multiple source docs on a single target doc''' |
| 14 | |
| 15 | make_test_records("Item") |
Deepesh Garg | a9820cd | 2020-07-11 19:38:51 +0530 | [diff] [blame] | 16 | items = ['_Test Item', '_Test Item 2', '_Test FG Item'] |
| 17 | |
| 18 | # Make source docs (quotations) and a target doc (sales order) |
| 19 | qtn1, item_list_1 = self.make_quotation(items, '_Test Customer') |
| 20 | qtn2, item_list_2 = self.make_quotation(items, '_Test Customer') |
| 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 | a9c84f7 | 2021-06-11 16:00:48 +0530 | [diff] [blame] | 29 | self.assertEqual(set(d for d in src_items), |
| 30 | set(d.item_code for d in updated_so.items)) |
Prateeksha Singh | edeb4dc | 2017-05-15 11:32:06 +0530 | [diff] [blame] | 31 | |
Prateeksha Singh | edeb4dc | 2017-05-15 11:32:06 +0530 | [diff] [blame] | 32 | |
Deepesh Garg | a9820cd | 2020-07-11 19:38:51 +0530 | [diff] [blame] | 33 | def make_quotation(self, item_list, customer): |
| 34 | |
Prateeksha Singh | edeb4dc | 2017-05-15 11:32:06 +0530 | [diff] [blame] | 35 | qtn = frappe.get_doc({ |
| 36 | "doctype": "Quotation", |
| 37 | "quotation_to": "Customer", |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 38 | "party_name": customer, |
KanchanChauhan | 58c9934 | 2017-07-26 18:17:11 +0530 | [diff] [blame] | 39 | "order_type": "Sales", |
| 40 | "transaction_date" : nowdate(), |
| 41 | "valid_till" : add_months(nowdate(), 1) |
Prateeksha Singh | edeb4dc | 2017-05-15 11:32:06 +0530 | [diff] [blame] | 42 | }) |
| 43 | for item in item_list: |
Deepesh Garg | a9820cd | 2020-07-11 19:38:51 +0530 | [diff] [blame] | 44 | qtn.append("items", {"qty": "2", "item_code": item}) |
Prateeksha Singh | edeb4dc | 2017-05-15 11:32:06 +0530 | [diff] [blame] | 45 | |
| 46 | qtn.submit() |
| 47 | return qtn, item_list |
| 48 | |
| 49 | def make_sales_order(self): |
| 50 | item = frappe.get_doc({ |
| 51 | "base_amount": 1000.0, |
| 52 | "base_rate": 100.0, |
| 53 | "description": "CPU", |
| 54 | "doctype": "Sales Order Item", |
Deepesh Garg | a9820cd | 2020-07-11 19:38:51 +0530 | [diff] [blame] | 55 | "item_code": "_Test Item", |
Prateeksha Singh | edeb4dc | 2017-05-15 11:32:06 +0530 | [diff] [blame] | 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 | so = frappe.get_doc(frappe.get_test_records('Sales Order')[0]) |
| 66 | so.insert(ignore_permissions=True) |
Deepesh Garg | a9820cd | 2020-07-11 19:38:51 +0530 | [diff] [blame] | 67 | return so, [item.item_code] |