Ankush Menat | 62fc544 | 2021-09-10 12:46:35 +0530 | [diff] [blame] | 1 | import unittest |
| 2 | from functools import partial |
| 3 | |
Ankush Menat | 33aad4b | 2022-01-11 17:06:31 +0530 | [diff] [blame] | 4 | import frappe |
| 5 | |
Ankush Menat | 62fc544 | 2021-09-10 12:46:35 +0530 | [diff] [blame] | 6 | from erpnext.controllers import queries |
| 7 | |
| 8 | |
| 9 | def add_default_params(func, doctype): |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 10 | return partial(func, doctype=doctype, txt="", searchfield="name", start=0, page_len=20, filters=None) |
Ankush Menat | 62fc544 | 2021-09-10 12:46:35 +0530 | [diff] [blame] | 11 | |
| 12 | |
| 13 | class TestQueries(unittest.TestCase): |
Ankush Menat | 62fc544 | 2021-09-10 12:46:35 +0530 | [diff] [blame] | 14 | # 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 Menat | 62fc544 | 2021-09-10 12:46:35 +0530 | [diff] [blame] | 31 | 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 Menat | 41a95e5 | 2022-02-03 13:02:13 +0530 | [diff] [blame] | 44 | # 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 Menat | 62fc544 | 2021-09-10 12:46:35 +0530 | [diff] [blame] | 50 | 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 kumar | 3349dde | 2024-01-16 14:28:09 +0530 | [diff] [blame] | 56 | query = add_default_params(queries.get_project_name, "Project") |
Ankush Menat | 62fc544 | 2021-09-10 12:46:35 +0530 | [diff] [blame] | 57 | |
| 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 Menat | 33aad4b | 2022-01-11 17:06:31 +0530 | [diff] [blame] | 81 | |
| 82 | def test_default_uoms(self): |
| 83 | self.assertGreaterEqual(frappe.db.count("UOM", {"enabled": 1}), 10) |