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 |
Rushabh Mehta | 982be9f | 2017-01-17 17:57:19 +0530 | [diff] [blame] | 6 | |
| 7 | import frappe |
| 8 | |
| 9 | def create_test_contact_and_address(): |
Rushabh Mehta | a0c41b7 | 2017-01-18 14:14:20 +0530 | [diff] [blame] | 10 | frappe.db.sql('delete from tabContact') |
Nabin Hait | 1aa8c2e | 2020-03-26 13:15:31 +0530 | [diff] [blame] | 11 | frappe.db.sql('delete from `tabContact Email`') |
| 12 | frappe.db.sql('delete from `tabContact Phone`') |
Rushabh Mehta | a0c41b7 | 2017-01-18 14:14:20 +0530 | [diff] [blame] | 13 | frappe.db.sql('delete from tabAddress') |
| 14 | frappe.db.sql('delete from `tabDynamic Link`') |
Rushabh Mehta | 982be9f | 2017-01-17 17:57:19 +0530 | [diff] [blame] | 15 | |
Himanshu | 25ab1e4 | 2019-09-30 10:08:15 +0530 | [diff] [blame] | 16 | frappe.get_doc({ |
| 17 | "doctype": "Address", |
| 18 | "address_title": "_Test Address for Customer", |
| 19 | "address_type": "Office", |
| 20 | "address_line1": "Station Road", |
| 21 | "city": "_Test City", |
| 22 | "state": "Test State", |
| 23 | "country": "India", |
| 24 | "links": [ |
| 25 | { |
| 26 | "link_doctype": "Customer", |
| 27 | "link_name": "_Test Customer" |
| 28 | } |
| 29 | ] |
| 30 | }).insert() |
Rushabh Mehta | a0c41b7 | 2017-01-18 14:14:20 +0530 | [diff] [blame] | 31 | |
Himanshu | 25ab1e4 | 2019-09-30 10:08:15 +0530 | [diff] [blame] | 32 | contact = frappe.get_doc({ |
| 33 | "doctype": 'Contact', |
| 34 | "first_name": "_Test Contact for _Test Customer", |
| 35 | "links": [ |
| 36 | { |
| 37 | "link_doctype": "Customer", |
| 38 | "link_name": "_Test Customer" |
| 39 | } |
| 40 | ] |
| 41 | }) |
| 42 | contact.add_email("test_contact_customer@example.com", is_primary=True) |
| 43 | contact.add_phone("+91 0000000000", is_primary_phone=True) |
| 44 | contact.insert() |
Ankush Menat | 76dd6e9 | 2021-05-23 16:19:48 +0530 | [diff] [blame] | 45 | |
| 46 | |
| 47 | @contextmanager |
| 48 | def change_settings(doctype, settings_dict): |
| 49 | """ A context manager to ensure that settings are changed before running |
| 50 | function and restored after running it regardless of exceptions occured. |
| 51 | This is useful in tests where you want to make changes in a function but |
| 52 | don't retain those changes. |
| 53 | import and use as decorator to cover full function or using `with` statement. |
| 54 | |
| 55 | example: |
| 56 | @change_settings("Stock Settings", {"item_naming_by": "Naming Series"}) |
| 57 | def test_case(self): |
| 58 | ... |
| 59 | """ |
| 60 | |
| 61 | try: |
| 62 | settings = frappe.get_doc(doctype) |
| 63 | # remember setting |
| 64 | previous_settings = copy.deepcopy(settings_dict) |
| 65 | for key in previous_settings: |
| 66 | previous_settings[key] = getattr(settings, key) |
| 67 | |
| 68 | # change setting |
| 69 | for key, value in settings_dict.items(): |
| 70 | setattr(settings, key, value) |
| 71 | settings.save() |
| 72 | yield # yield control to calling function |
| 73 | |
| 74 | finally: |
| 75 | # restore settings |
| 76 | settings = frappe.get_doc(doctype) |
| 77 | for key, value in previous_settings.items(): |
| 78 | setattr(settings, key, value) |
| 79 | settings.save() |