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 | 76dd6e9 | 2021-05-23 16:19:48 +0530 | [diff] [blame] | 4 | import copy |
| 5 | from contextlib import contextmanager |
Ankush Menat | 70c203d | 2021-09-15 19:24:35 +0530 | [diff] [blame] | 6 | from typing import Any, Dict, NewType, Optional |
Rushabh Mehta | 982be9f | 2017-01-17 17:57:19 +0530 | [diff] [blame] | 7 | |
| 8 | import frappe |
Ankush Menat | 70c203d | 2021-09-15 19:24:35 +0530 | [diff] [blame] | 9 | from frappe.core.doctype.report.report import get_report_module_dotted_path |
| 10 | |
| 11 | ReportFilters = Dict[str, Any] |
| 12 | ReportName = NewType("ReportName", str) |
Rushabh Mehta | 982be9f | 2017-01-17 17:57:19 +0530 | [diff] [blame] | 13 | |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 14 | |
Rushabh Mehta | 982be9f | 2017-01-17 17:57:19 +0530 | [diff] [blame] | 15 | def create_test_contact_and_address(): |
Rushabh Mehta | a0c41b7 | 2017-01-18 14:14:20 +0530 | [diff] [blame] | 16 | frappe.db.sql('delete from tabContact') |
Nabin Hait | 1aa8c2e | 2020-03-26 13:15:31 +0530 | [diff] [blame] | 17 | frappe.db.sql('delete from `tabContact Email`') |
| 18 | frappe.db.sql('delete from `tabContact Phone`') |
Rushabh Mehta | a0c41b7 | 2017-01-18 14:14:20 +0530 | [diff] [blame] | 19 | frappe.db.sql('delete from tabAddress') |
| 20 | frappe.db.sql('delete from `tabDynamic Link`') |
Rushabh Mehta | 982be9f | 2017-01-17 17:57:19 +0530 | [diff] [blame] | 21 | |
Himanshu | 25ab1e4 | 2019-09-30 10:08:15 +0530 | [diff] [blame] | 22 | 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 Mehta | a0c41b7 | 2017-01-18 14:14:20 +0530 | [diff] [blame] | 37 | |
Himanshu | 25ab1e4 | 2019-09-30 10:08:15 +0530 | [diff] [blame] | 38 | 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 Menat | 76dd6e9 | 2021-05-23 16:19:48 +0530 | [diff] [blame] | 51 | |
| 52 | |
| 53 | @contextmanager |
| 54 | def 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 Menat | 70c203d | 2021-09-15 19:24:35 +0530 | [diff] [blame] | 86 | |
| 87 | |
| 88 | def 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 |