blob: a3cab4b59da7657116fa4b22ffd726d93e96e14c [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 Menat76dd6e92021-05-23 16:19:48 +05304import copy
5from contextlib import contextmanager
Ankush Menat70c203d2021-09-15 19:24:35 +05306from typing import Any, Dict, NewType, Optional
Rushabh Mehta982be9f2017-01-17 17:57:19 +05307
8import frappe
Ankush Menat70c203d2021-09-15 19:24:35 +05309from frappe.core.doctype.report.report import get_report_module_dotted_path
10
11ReportFilters = Dict[str, Any]
12ReportName = NewType("ReportName", str)
Rushabh Mehta982be9f2017-01-17 17:57:19 +053013
Chillar Anand915b3432021-09-02 16:44:59 +053014
Rushabh Mehta982be9f2017-01-17 17:57:19 +053015def create_test_contact_and_address():
Rushabh Mehtaa0c41b72017-01-18 14:14:20 +053016 frappe.db.sql('delete from tabContact')
Nabin Hait1aa8c2e2020-03-26 13:15:31 +053017 frappe.db.sql('delete from `tabContact Email`')
18 frappe.db.sql('delete from `tabContact Phone`')
Rushabh Mehtaa0c41b72017-01-18 14:14:20 +053019 frappe.db.sql('delete from tabAddress')
20 frappe.db.sql('delete from `tabDynamic Link`')
Rushabh Mehta982be9f2017-01-17 17:57:19 +053021
Himanshu25ab1e42019-09-30 10:08:15 +053022 frappe.get_doc({
23 "doctype": "Address",
24 "address_title": "_Test Address for Customer",
25 "address_type": "Office",
26 "address_line1": "Station Road",
27 "city": "_Test City",
28 "state": "Test State",
29 "country": "India",
30 "links": [
31 {
32 "link_doctype": "Customer",
33 "link_name": "_Test Customer"
34 }
35 ]
36 }).insert()
Rushabh Mehtaa0c41b72017-01-18 14:14:20 +053037
Himanshu25ab1e42019-09-30 10:08:15 +053038 contact = frappe.get_doc({
39 "doctype": 'Contact',
40 "first_name": "_Test Contact for _Test Customer",
41 "links": [
42 {
43 "link_doctype": "Customer",
44 "link_name": "_Test Customer"
45 }
46 ]
47 })
48 contact.add_email("test_contact_customer@example.com", is_primary=True)
49 contact.add_phone("+91 0000000000", is_primary_phone=True)
50 contact.insert()
Ankush Menat76dd6e92021-05-23 16:19:48 +053051
52
53@contextmanager
54def change_settings(doctype, settings_dict):
55 """ A context manager to ensure that settings are changed before running
56 function and restored after running it regardless of exceptions occured.
57 This is useful in tests where you want to make changes in a function but
58 don't retain those changes.
59 import and use as decorator to cover full function or using `with` statement.
60
61 example:
62 @change_settings("Stock Settings", {"item_naming_by": "Naming Series"})
63 def test_case(self):
64 ...
65 """
66
67 try:
68 settings = frappe.get_doc(doctype)
69 # remember setting
70 previous_settings = copy.deepcopy(settings_dict)
71 for key in previous_settings:
72 previous_settings[key] = getattr(settings, key)
73
74 # change setting
75 for key, value in settings_dict.items():
76 setattr(settings, key, value)
77 settings.save()
78 yield # yield control to calling function
79
80 finally:
81 # restore settings
82 settings = frappe.get_doc(doctype)
83 for key, value in previous_settings.items():
84 setattr(settings, key, value)
85 settings.save()
Ankush Menat70c203d2021-09-15 19:24:35 +053086
87
88def execute_script_report(
89 report_name: ReportName,
90 module: str,
91 filters: ReportFilters,
92 default_filters: Optional[ReportFilters] = None,
93 optional_filters: Optional[ReportFilters] = None
94 ):
95 """Util for testing execution of a report with specified filters.
96
97 Tests the execution of report with default_filters + filters.
98 Tests the execution using optional_filters one at a time.
99
100 Args:
101 report_name: Human readable name of report (unscrubbed)
102 module: module to which report belongs to
103 filters: specific values for filters
104 default_filters: default values for filters such as company name.
105 optional_filters: filters which should be tested one at a time in addition to default filters.
106 """
107
108 if default_filters is None:
109 default_filters = {}
110
111 report_execute_fn = frappe.get_attr(get_report_module_dotted_path(module, report_name) + ".execute")
112 report_filters = frappe._dict(default_filters).copy().update(filters)
113
114 report_data = report_execute_fn(report_filters)
115
116 if optional_filters:
117 for key, value in optional_filters.items():
118 filter_with_optional_param = report_filters.copy().update({key: value})
119 report_execute_fn(filter_with_optional_param)
120
121 return report_data