Ankush Menat | 76dd6e9 | 2021-05-23 16:19:48 +0530 | [diff] [blame] | 1 | # Copyright (c) 2021, Frappe Technologies Pvt. Ltd. and Contributors |
Rushabh Mehta | 982be9f | 2017-01-17 17:57:19 +0530 | [diff] [blame] | 2 | # License: GNU General Public License v3. See license.txt |
| 3 | |
Ankush Menat | 70c203d | 2021-09-15 19:24:35 +0530 | [diff] [blame] | 4 | from typing import Any, Dict, NewType, Optional |
Rushabh Mehta | 982be9f | 2017-01-17 17:57:19 +0530 | [diff] [blame] | 5 | |
| 6 | import frappe |
Ankush Menat | 70c203d | 2021-09-15 19:24:35 +0530 | [diff] [blame] | 7 | from frappe.core.doctype.report.report import get_report_module_dotted_path |
| 8 | |
| 9 | ReportFilters = Dict[str, Any] |
| 10 | ReportName = NewType("ReportName", str) |
Rushabh Mehta | 982be9f | 2017-01-17 17:57:19 +0530 | [diff] [blame] | 11 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 12 | |
Rushabh Mehta | 982be9f | 2017-01-17 17:57:19 +0530 | [diff] [blame] | 13 | def create_test_contact_and_address(): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 14 | frappe.db.sql("delete from tabContact") |
| 15 | frappe.db.sql("delete from `tabContact Email`") |
| 16 | frappe.db.sql("delete from `tabContact Phone`") |
| 17 | frappe.db.sql("delete from tabAddress") |
| 18 | frappe.db.sql("delete from `tabDynamic Link`") |
Rushabh Mehta | 982be9f | 2017-01-17 17:57:19 +0530 | [diff] [blame] | 19 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 20 | frappe.get_doc( |
| 21 | { |
| 22 | "doctype": "Address", |
| 23 | "address_title": "_Test Address for Customer", |
| 24 | "address_type": "Office", |
| 25 | "address_line1": "Station Road", |
| 26 | "city": "_Test City", |
| 27 | "state": "Test State", |
| 28 | "country": "India", |
| 29 | "links": [{"link_doctype": "Customer", "link_name": "_Test Customer"}], |
| 30 | } |
| 31 | ).insert() |
Rushabh Mehta | a0c41b7 | 2017-01-18 14:14:20 +0530 | [diff] [blame] | 32 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 33 | contact = frappe.get_doc( |
| 34 | { |
| 35 | "doctype": "Contact", |
| 36 | "first_name": "_Test Contact for _Test Customer", |
| 37 | "links": [{"link_doctype": "Customer", "link_name": "_Test Customer"}], |
| 38 | } |
| 39 | ) |
Himanshu | 25ab1e4 | 2019-09-30 10:08:15 +0530 | [diff] [blame] | 40 | contact.add_email("test_contact_customer@example.com", is_primary=True) |
| 41 | contact.add_phone("+91 0000000000", is_primary_phone=True) |
| 42 | contact.insert() |
Ankush Menat | 76dd6e9 | 2021-05-23 16:19:48 +0530 | [diff] [blame] | 43 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 44 | contact_two = frappe.get_doc( |
| 45 | { |
| 46 | "doctype": "Contact", |
| 47 | "first_name": "_Test Contact 2 for _Test Customer", |
| 48 | "links": [{"link_doctype": "Customer", "link_name": "_Test Customer"}], |
| 49 | } |
| 50 | ) |
Devin Slauenwhite | d636c3f | 2022-02-09 10:52:38 -0500 | [diff] [blame] | 51 | contact_two.add_email("test_contact_two_customer@example.com", is_primary=True) |
| 52 | contact_two.add_phone("+92 0000000000", is_primary_phone=True) |
| 53 | contact_two.insert() |
| 54 | |
Ankush Menat | 76dd6e9 | 2021-05-23 16:19:48 +0530 | [diff] [blame] | 55 | |
Ankush Menat | 70c203d | 2021-09-15 19:24:35 +0530 | [diff] [blame] | 56 | def execute_script_report( |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 57 | report_name: ReportName, |
| 58 | module: str, |
| 59 | filters: ReportFilters, |
| 60 | default_filters: Optional[ReportFilters] = None, |
| 61 | optional_filters: Optional[ReportFilters] = None, |
| 62 | ): |
Ankush Menat | 70c203d | 2021-09-15 19:24:35 +0530 | [diff] [blame] | 63 | """Util for testing execution of a report with specified filters. |
| 64 | |
| 65 | Tests the execution of report with default_filters + filters. |
| 66 | Tests the execution using optional_filters one at a time. |
| 67 | |
| 68 | Args: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 69 | report_name: Human readable name of report (unscrubbed) |
| 70 | module: module to which report belongs to |
| 71 | filters: specific values for filters |
| 72 | default_filters: default values for filters such as company name. |
| 73 | optional_filters: filters which should be tested one at a time in addition to default filters. |
Ankush Menat | 70c203d | 2021-09-15 19:24:35 +0530 | [diff] [blame] | 74 | """ |
| 75 | |
| 76 | if default_filters is None: |
| 77 | default_filters = {} |
| 78 | |
Ankush Menat | f195f80 | 2022-01-09 19:23:27 +0530 | [diff] [blame] | 79 | test_filters = [] |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 80 | report_execute_fn = frappe.get_attr( |
| 81 | get_report_module_dotted_path(module, report_name) + ".execute" |
| 82 | ) |
Ankush Menat | 70c203d | 2021-09-15 19:24:35 +0530 | [diff] [blame] | 83 | report_filters = frappe._dict(default_filters).copy().update(filters) |
| 84 | |
Ankush Menat | f195f80 | 2022-01-09 19:23:27 +0530 | [diff] [blame] | 85 | test_filters.append(report_filters) |
Ankush Menat | 70c203d | 2021-09-15 19:24:35 +0530 | [diff] [blame] | 86 | |
| 87 | if optional_filters: |
| 88 | for key, value in optional_filters.items(): |
Ankush Menat | f195f80 | 2022-01-09 19:23:27 +0530 | [diff] [blame] | 89 | test_filters.append(report_filters.copy().update({key: value})) |
Ankush Menat | 70c203d | 2021-09-15 19:24:35 +0530 | [diff] [blame] | 90 | |
Ankush Menat | f195f80 | 2022-01-09 19:23:27 +0530 | [diff] [blame] | 91 | for test_filter in test_filters: |
| 92 | try: |
| 93 | report_execute_fn(test_filter) |
| 94 | except Exception: |
| 95 | print(f"Report failed to execute with filters: {test_filter}") |
| 96 | raise |
Anand Baburajan | 988d755 | 2023-06-30 11:02:49 +0530 | [diff] [blame^] | 97 | |
| 98 | |
| 99 | def if_lending_app_installed(function): |
| 100 | """Decorator to check if lending app is installed""" |
| 101 | |
| 102 | def wrapper(*args, **kwargs): |
| 103 | if "lending" in frappe.get_installed_apps(): |
| 104 | return function(*args, **kwargs) |
| 105 | return |
| 106 | |
| 107 | return wrapper |
| 108 | |
| 109 | |
| 110 | def if_lending_app_not_installed(function): |
| 111 | """Decorator to check if lending app is not installed""" |
| 112 | |
| 113 | def wrapper(*args, **kwargs): |
| 114 | if "lending" not in frappe.get_installed_apps(): |
| 115 | return function(*args, **kwargs) |
| 116 | return |
| 117 | |
| 118 | return wrapper |