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 | |
| 12 | def create_test_contact_and_address(): |
Rushabh Mehta | a0c41b7 | 2017-01-18 14:14:20 +0530 | [diff] [blame] | 13 | frappe.db.sql('delete from tabContact') |
Nabin Hait | 1aa8c2e | 2020-03-26 13:15:31 +0530 | [diff] [blame] | 14 | frappe.db.sql('delete from `tabContact Email`') |
| 15 | frappe.db.sql('delete from `tabContact Phone`') |
Rushabh Mehta | a0c41b7 | 2017-01-18 14:14:20 +0530 | [diff] [blame] | 16 | frappe.db.sql('delete from tabAddress') |
| 17 | frappe.db.sql('delete from `tabDynamic Link`') |
Rushabh Mehta | 982be9f | 2017-01-17 17:57:19 +0530 | [diff] [blame] | 18 | |
Himanshu | 25ab1e4 | 2019-09-30 10:08:15 +0530 | [diff] [blame] | 19 | frappe.get_doc({ |
| 20 | "doctype": "Address", |
| 21 | "address_title": "_Test Address for Customer", |
| 22 | "address_type": "Office", |
| 23 | "address_line1": "Station Road", |
| 24 | "city": "_Test City", |
| 25 | "state": "Test State", |
| 26 | "country": "India", |
| 27 | "links": [ |
| 28 | { |
| 29 | "link_doctype": "Customer", |
| 30 | "link_name": "_Test Customer" |
| 31 | } |
| 32 | ] |
| 33 | }).insert() |
Rushabh Mehta | a0c41b7 | 2017-01-18 14:14:20 +0530 | [diff] [blame] | 34 | |
Himanshu | 25ab1e4 | 2019-09-30 10:08:15 +0530 | [diff] [blame] | 35 | contact = frappe.get_doc({ |
| 36 | "doctype": 'Contact', |
| 37 | "first_name": "_Test Contact for _Test Customer", |
| 38 | "links": [ |
| 39 | { |
| 40 | "link_doctype": "Customer", |
| 41 | "link_name": "_Test Customer" |
| 42 | } |
| 43 | ] |
| 44 | }) |
| 45 | contact.add_email("test_contact_customer@example.com", is_primary=True) |
| 46 | contact.add_phone("+91 0000000000", is_primary_phone=True) |
| 47 | contact.insert() |
Ankush Menat | 76dd6e9 | 2021-05-23 16:19:48 +0530 | [diff] [blame] | 48 | |
Devin Slauenwhite | d636c3f | 2022-02-09 10:52:38 -0500 | [diff] [blame] | 49 | contact_two = frappe.get_doc({ |
| 50 | "doctype": 'Contact', |
| 51 | "first_name": "_Test Contact 2 for _Test Customer", |
| 52 | "links": [ |
| 53 | { |
| 54 | "link_doctype": "Customer", |
| 55 | "link_name": "_Test Customer" |
| 56 | } |
| 57 | ] |
| 58 | }) |
| 59 | contact_two.add_email("test_contact_two_customer@example.com", is_primary=True) |
| 60 | contact_two.add_phone("+92 0000000000", is_primary_phone=True) |
| 61 | contact_two.insert() |
| 62 | |
Ankush Menat | 76dd6e9 | 2021-05-23 16:19:48 +0530 | [diff] [blame] | 63 | |
Ankush Menat | 70c203d | 2021-09-15 19:24:35 +0530 | [diff] [blame] | 64 | def execute_script_report( |
| 65 | report_name: ReportName, |
| 66 | module: str, |
| 67 | filters: ReportFilters, |
| 68 | default_filters: Optional[ReportFilters] = None, |
| 69 | optional_filters: Optional[ReportFilters] = None |
| 70 | ): |
| 71 | """Util for testing execution of a report with specified filters. |
| 72 | |
| 73 | Tests the execution of report with default_filters + filters. |
| 74 | Tests the execution using optional_filters one at a time. |
| 75 | |
| 76 | Args: |
| 77 | report_name: Human readable name of report (unscrubbed) |
| 78 | module: module to which report belongs to |
| 79 | filters: specific values for filters |
| 80 | default_filters: default values for filters such as company name. |
| 81 | optional_filters: filters which should be tested one at a time in addition to default filters. |
| 82 | """ |
| 83 | |
| 84 | if default_filters is None: |
| 85 | default_filters = {} |
| 86 | |
Ankush Menat | f195f80 | 2022-01-09 19:23:27 +0530 | [diff] [blame] | 87 | test_filters = [] |
Ankush Menat | 70c203d | 2021-09-15 19:24:35 +0530 | [diff] [blame] | 88 | report_execute_fn = frappe.get_attr(get_report_module_dotted_path(module, report_name) + ".execute") |
| 89 | report_filters = frappe._dict(default_filters).copy().update(filters) |
| 90 | |
Ankush Menat | f195f80 | 2022-01-09 19:23:27 +0530 | [diff] [blame] | 91 | test_filters.append(report_filters) |
Ankush Menat | 70c203d | 2021-09-15 19:24:35 +0530 | [diff] [blame] | 92 | |
| 93 | if optional_filters: |
| 94 | for key, value in optional_filters.items(): |
Ankush Menat | f195f80 | 2022-01-09 19:23:27 +0530 | [diff] [blame] | 95 | test_filters.append(report_filters.copy().update({key: value})) |
Ankush Menat | 70c203d | 2021-09-15 19:24:35 +0530 | [diff] [blame] | 96 | |
Ankush Menat | f195f80 | 2022-01-09 19:23:27 +0530 | [diff] [blame] | 97 | for test_filter in test_filters: |
| 98 | try: |
| 99 | report_execute_fn(test_filter) |
| 100 | except Exception: |
| 101 | print(f"Report failed to execute with filters: {test_filter}") |
| 102 | raise |