blob: 0ef108e5f7bbfc117a5481d43e0e048ee9b138e5 [file] [log] [blame]
Ankush Menat62fc5442021-09-10 12:46:35 +05301import unittest
2from functools import partial
3
Ankush Menat33aad4b2022-01-11 17:06:31 +05304import frappe
5
Ankush Menat62fc5442021-09-10 12:46:35 +05306from erpnext.controllers import queries
7
8
9def add_default_params(func, doctype):
Akhil Narang3effaf22024-03-27 11:37:26 +053010 return partial(func, doctype=doctype, txt="", searchfield="name", start=0, page_len=20, filters=None)
Ankush Menat62fc5442021-09-10 12:46:35 +053011
12
13class TestQueries(unittest.TestCase):
Ankush Menat62fc5442021-09-10 12:46:35 +053014 # All tests are based on doctype/test_records.json
15
16 def assert_nested_in(self, item, container):
17 self.assertIn(item, [vals for tuples in container for vals in tuples])
18
19 def test_employee_query(self):
20 query = add_default_params(queries.employee_query, "Employee")
21
22 self.assertGreaterEqual(len(query(txt="_Test Employee")), 3)
23 self.assertGreaterEqual(len(query(txt="_Test Employee 1")), 1)
24
25 def test_lead_query(self):
26 query = add_default_params(queries.lead_query, "Lead")
27
28 self.assertGreaterEqual(len(query(txt="_Test Lead")), 4)
29 self.assertEqual(len(query(txt="_Test Lead 4")), 1)
30
Ankush Menat62fc5442021-09-10 12:46:35 +053031 def test_item_query(self):
32 query = add_default_params(queries.item_query, "Item")
33
34 self.assertGreaterEqual(len(query(txt="_Test Item")), 7)
35 self.assertEqual(len(query(txt="_Test Item Home Desktop 100 3")), 1)
36
37 fg_item = "_Test FG Item"
38 stock_items = query(txt=fg_item, filters={"is_stock_item": 1})
39 self.assert_nested_in("_Test FG Item", stock_items)
40
41 bundled_stock_items = query(txt="_test product bundle item 5", filters={"is_stock_item": 1})
42 self.assertEqual(len(bundled_stock_items), 0)
43
Ankush Menat41a95e52022-02-03 13:02:13 +053044 # empty customer/supplier should be stripped of instead of failure
45 query(txt="", filters={"customer": None})
46 query(txt="", filters={"customer": ""})
47 query(txt="", filters={"supplier": None})
48 query(txt="", filters={"supplier": ""})
49
Ankush Menat62fc5442021-09-10 12:46:35 +053050 def test_bom_qury(self):
51 query = add_default_params(queries.bom, "BOM")
52
53 self.assertGreaterEqual(len(query(txt="_Test Item Home Desktop Manufactured")), 1)
54
55 def test_project_query(self):
ruthra kumar3349dde2024-01-16 14:28:09 +053056 query = add_default_params(queries.get_project_name, "Project")
Ankush Menat62fc5442021-09-10 12:46:35 +053057
58 self.assertGreaterEqual(len(query(txt="_Test Project")), 1)
59
60 def test_account_query(self):
61 query = add_default_params(queries.get_account_list, "Account")
62
63 debtor_accounts = query(txt="Debtors", filters={"company": "_Test Company"})
64 self.assert_nested_in("Debtors - _TC", debtor_accounts)
65
66 def test_income_account_query(self):
67 query = add_default_params(queries.get_income_account, "Account")
68
69 self.assertGreaterEqual(len(query(filters={"company": "_Test Company"})), 1)
70
71 def test_expense_account_query(self):
72 query = add_default_params(queries.get_expense_account, "Account")
73
74 self.assertGreaterEqual(len(query(filters={"company": "_Test Company"})), 1)
75
76 def test_warehouse_query(self):
77 query = add_default_params(queries.warehouse_query, "Account")
78
79 wh = query(filters=[["Bin", "item_code", "=", "_Test Item"]])
80 self.assertGreaterEqual(len(wh), 1)
Ankush Menat33aad4b2022-01-11 17:06:31 +053081
82 def test_default_uoms(self):
83 self.assertGreaterEqual(frappe.db.count("UOM", {"enabled": 1}), 10)