blob: 93d1b321b4c18f0461c03f30bb9b33ba7edc8887 [file] [log] [blame]
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +05301from __future__ import unicode_literals
2import unittest
3import frappe
4
5import random, json
6import frappe.utils
KanchanChauhan58c99342017-07-26 18:17:11 +05307from frappe.utils import nowdate, add_months
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +05308from frappe.model import mapper
9from frappe.test_runner import make_test_records
10
11class 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")
16 items = frappe.get_all("Item", fields = ["name", "item_code"], filters = {'is_sales_item': 1, 'has_variants': 0})
17 customers = frappe.get_all("Customer")
18 if items and customers:
19 # Make source docs (quotations) and a target doc (sales order)
20 customer = random.choice(customers).name
21 qtn1, item_list_1 = self.make_quotation(items, customer)
22 qtn2, item_list_2 = self.make_quotation(items, customer)
23 so, item_list_3 = self.make_sales_order()
24
25 # Map source docs to target with corresponding mapper method
26 method = "erpnext.selling.doctype.quotation.quotation.make_sales_order"
27 updated_so = mapper.map_docs(method, json.dumps([qtn1.name, qtn2.name]), so)
28
29 # Assert that all inserted items are present in updated sales order
30 src_items = item_list_1 + item_list_2 + item_list_3
31 self.assertEqual(set([d.item_code for d in src_items]),
32 set([d.item_code for d in updated_so.items]))
33
34 def get_random_items(self, items, limit):
35 '''Get a number of random items from a list of given items'''
36 random_items = []
37 for i in range(0, limit):
38 random_items.append(random.choice(items))
39 return random_items
40
41 def make_quotation(self, items, customer):
42 item_list = self.get_random_items(items, 3)
43 qtn = frappe.get_doc({
44 "doctype": "Quotation",
45 "quotation_to": "Customer",
Saqib Ansari1f20c992020-04-14 19:40:25 +053046 "company": "_Test Company",
Nabin Hait34c551d2019-07-03 10:34:31 +053047 "party_name": customer,
KanchanChauhan58c99342017-07-26 18:17:11 +053048 "order_type": "Sales",
49 "transaction_date" : nowdate(),
50 "valid_till" : add_months(nowdate(), 1)
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +053051 })
52 for item in item_list:
53 qtn.append("items", {"qty": "2", "item_code": item.item_code})
54
55 qtn.submit()
56 return qtn, item_list
57
58 def make_sales_order(self):
59 item = frappe.get_doc({
60 "base_amount": 1000.0,
61 "base_rate": 100.0,
62 "description": "CPU",
Saqib Ansari1f20c992020-04-14 19:40:25 +053063 "company": "_Test Company",
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +053064 "doctype": "Sales Order Item",
65 "item_code": "_Test Item Home Desktop 100",
66 "item_name": "CPU",
67 "parentfield": "items",
68 "qty": 10.0,
69 "rate": 100.0,
70 "warehouse": "_Test Warehouse - _TC",
71 "stock_uom": "_Test UOM",
72 "conversion_factor": 1.0,
73 "uom": "_Test UOM"
74 })
75 so = frappe.get_doc(frappe.get_test_records('Sales Order')[0])
76 so.insert(ignore_permissions=True)
77 return so, [item]