blob: 159ce7078e391f898eaec7320bf3db34723a381e [file] [log] [blame]
Ankush Menat76dd6e92021-05-23 16:19:48 +05301# Copyright (c) 2021, Frappe Technologies Pvt. Ltd. and Contributors
Rushabh Mehta982be9f2017-01-17 17:57:19 +05302# License: GNU General Public License v3. See license.txt
3
Ankush Menat70c203d2021-09-15 19:24:35 +05304from typing import Any, Dict, NewType, Optional
Rushabh Mehta982be9f2017-01-17 17:57:19 +05305
6import frappe
Ankush Menat70c203d2021-09-15 19:24:35 +05307from frappe.core.doctype.report.report import get_report_module_dotted_path
8
9ReportFilters = Dict[str, Any]
10ReportName = NewType("ReportName", str)
Rushabh Mehta982be9f2017-01-17 17:57:19 +053011
Ankush Menat494bd9e2022-03-28 18:52:46 +053012
Rushabh Mehta982be9f2017-01-17 17:57:19 +053013def create_test_contact_and_address():
Ankush Menat494bd9e2022-03-28 18:52:46 +053014 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 Mehta982be9f2017-01-17 17:57:19 +053019
Ankush Menat494bd9e2022-03-28 18:52:46 +053020 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 Mehtaa0c41b72017-01-18 14:14:20 +053032
Ankush Menat494bd9e2022-03-28 18:52:46 +053033 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 )
Himanshu25ab1e42019-09-30 10:08:15 +053040 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 Menat76dd6e92021-05-23 16:19:48 +053043
Ankush Menat494bd9e2022-03-28 18:52:46 +053044 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 Slauenwhited636c3f2022-02-09 10:52:38 -050051 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 Menat76dd6e92021-05-23 16:19:48 +053055
Ankush Menat70c203d2021-09-15 19:24:35 +053056def execute_script_report(
Ankush Menat494bd9e2022-03-28 18:52:46 +053057 report_name: ReportName,
58 module: str,
59 filters: ReportFilters,
60 default_filters: Optional[ReportFilters] = None,
61 optional_filters: Optional[ReportFilters] = None,
62):
Ankush Menat70c203d2021-09-15 19:24:35 +053063 """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 Menat494bd9e2022-03-28 18:52:46 +053069 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 Menat70c203d2021-09-15 19:24:35 +053074 """
75
76 if default_filters is None:
77 default_filters = {}
78
Ankush Menatf195f802022-01-09 19:23:27 +053079 test_filters = []
Ankush Menat494bd9e2022-03-28 18:52:46 +053080 report_execute_fn = frappe.get_attr(
81 get_report_module_dotted_path(module, report_name) + ".execute"
82 )
Ankush Menat70c203d2021-09-15 19:24:35 +053083 report_filters = frappe._dict(default_filters).copy().update(filters)
84
Ankush Menatf195f802022-01-09 19:23:27 +053085 test_filters.append(report_filters)
Ankush Menat70c203d2021-09-15 19:24:35 +053086
87 if optional_filters:
88 for key, value in optional_filters.items():
Ankush Menatf195f802022-01-09 19:23:27 +053089 test_filters.append(report_filters.copy().update({key: value}))
Ankush Menat70c203d2021-09-15 19:24:35 +053090
Ankush Menatf195f802022-01-09 19:23:27 +053091 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