blob: d795253665d8154fe9c237c0318d2b86eea6c762 [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
12def create_test_contact_and_address():
Rushabh Mehtaa0c41b72017-01-18 14:14:20 +053013 frappe.db.sql('delete from tabContact')
Nabin Hait1aa8c2e2020-03-26 13:15:31 +053014 frappe.db.sql('delete from `tabContact Email`')
15 frappe.db.sql('delete from `tabContact Phone`')
Rushabh Mehtaa0c41b72017-01-18 14:14:20 +053016 frappe.db.sql('delete from tabAddress')
17 frappe.db.sql('delete from `tabDynamic Link`')
Rushabh Mehta982be9f2017-01-17 17:57:19 +053018
Himanshu25ab1e42019-09-30 10:08:15 +053019 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 Mehtaa0c41b72017-01-18 14:14:20 +053034
Himanshu25ab1e42019-09-30 10:08:15 +053035 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 Menat76dd6e92021-05-23 16:19:48 +053048
Devin Slauenwhited636c3f2022-02-09 10:52:38 -050049 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 Menat76dd6e92021-05-23 16:19:48 +053063
Ankush Menat70c203d2021-09-15 19:24:35 +053064def 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 Menatf195f802022-01-09 19:23:27 +053087 test_filters = []
Ankush Menat70c203d2021-09-15 19:24:35 +053088 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 Menatf195f802022-01-09 19:23:27 +053091 test_filters.append(report_filters)
Ankush Menat70c203d2021-09-15 19:24:35 +053092
93 if optional_filters:
94 for key, value in optional_filters.items():
Ankush Menatf195f802022-01-09 19:23:27 +053095 test_filters.append(report_filters.copy().update({key: value}))
Ankush Menat70c203d2021-09-15 19:24:35 +053096
Ankush Menatf195f802022-01-09 19:23:27 +053097 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