您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

97 行
2.9 KiB

  1. # Copyright (c) 2021, Frappe Technologies Pvt. Ltd. and Contributors
  2. # License: GNU General Public License v3. See license.txt
  3. from typing import Any, Dict, NewType, Optional
  4. import frappe
  5. from frappe.core.doctype.report.report import get_report_module_dotted_path
  6. ReportFilters = Dict[str, Any]
  7. ReportName = NewType("ReportName", str)
  8. def create_test_contact_and_address():
  9. frappe.db.sql("delete from tabContact")
  10. frappe.db.sql("delete from `tabContact Email`")
  11. frappe.db.sql("delete from `tabContact Phone`")
  12. frappe.db.sql("delete from tabAddress")
  13. frappe.db.sql("delete from `tabDynamic Link`")
  14. frappe.get_doc(
  15. {
  16. "doctype": "Address",
  17. "address_title": "_Test Address for Customer",
  18. "address_type": "Office",
  19. "address_line1": "Station Road",
  20. "city": "_Test City",
  21. "state": "Test State",
  22. "country": "India",
  23. "links": [{"link_doctype": "Customer", "link_name": "_Test Customer"}],
  24. }
  25. ).insert()
  26. contact = frappe.get_doc(
  27. {
  28. "doctype": "Contact",
  29. "first_name": "_Test Contact for _Test Customer",
  30. "links": [{"link_doctype": "Customer", "link_name": "_Test Customer"}],
  31. }
  32. )
  33. contact.add_email("test_contact_customer@example.com", is_primary=True)
  34. contact.add_phone("+91 0000000000", is_primary_phone=True)
  35. contact.insert()
  36. contact_two = frappe.get_doc(
  37. {
  38. "doctype": "Contact",
  39. "first_name": "_Test Contact 2 for _Test Customer",
  40. "links": [{"link_doctype": "Customer", "link_name": "_Test Customer"}],
  41. }
  42. )
  43. contact_two.add_email("test_contact_two_customer@example.com", is_primary=True)
  44. contact_two.add_phone("+92 0000000000", is_primary_phone=True)
  45. contact_two.insert()
  46. def execute_script_report(
  47. report_name: ReportName,
  48. module: str,
  49. filters: ReportFilters,
  50. default_filters: Optional[ReportFilters] = None,
  51. optional_filters: Optional[ReportFilters] = None,
  52. ):
  53. """Util for testing execution of a report with specified filters.
  54. Tests the execution of report with default_filters + filters.
  55. Tests the execution using optional_filters one at a time.
  56. Args:
  57. report_name: Human readable name of report (unscrubbed)
  58. module: module to which report belongs to
  59. filters: specific values for filters
  60. default_filters: default values for filters such as company name.
  61. optional_filters: filters which should be tested one at a time in addition to default filters.
  62. """
  63. if default_filters is None:
  64. default_filters = {}
  65. test_filters = []
  66. report_execute_fn = frappe.get_attr(
  67. get_report_module_dotted_path(module, report_name) + ".execute"
  68. )
  69. report_filters = frappe._dict(default_filters).copy().update(filters)
  70. test_filters.append(report_filters)
  71. if optional_filters:
  72. for key, value in optional_filters.items():
  73. test_filters.append(report_filters.copy().update({key: value}))
  74. for test_filter in test_filters:
  75. try:
  76. report_execute_fn(test_filter)
  77. except Exception:
  78. print(f"Report failed to execute with filters: {test_filter}")
  79. raise