test(refactor): use FrappeTestCase
(cherry picked from commit b0d1e6db54)
This commit is contained in:
committed by
Ankush Menat
parent
2e79c4fd59
commit
1fd96f379f
@@ -1,10 +1,6 @@
|
||||
# Copyright (c) 2021, Frappe Technologies Pvt. Ltd. and Contributors
|
||||
# License: GNU General Public License v3. See license.txt
|
||||
|
||||
import copy
|
||||
import signal
|
||||
import unittest
|
||||
from contextlib import contextmanager
|
||||
from typing import Any, Dict, NewType, Optional
|
||||
|
||||
import frappe
|
||||
@@ -13,22 +9,6 @@ from frappe.core.doctype.report.report import get_report_module_dotted_path
|
||||
ReportFilters = Dict[str, Any]
|
||||
ReportName = NewType("ReportName", str)
|
||||
|
||||
|
||||
class ERPNextTestCase(unittest.TestCase):
|
||||
"""A sane default test class for ERPNext tests."""
|
||||
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls) -> None:
|
||||
frappe.db.commit()
|
||||
return super().setUpClass()
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls) -> None:
|
||||
frappe.db.rollback()
|
||||
return super().tearDownClass()
|
||||
|
||||
|
||||
def create_test_contact_and_address():
|
||||
frappe.db.sql('delete from tabContact')
|
||||
frappe.db.sql('delete from `tabContact Email`')
|
||||
@@ -81,43 +61,6 @@ def create_test_contact_and_address():
|
||||
contact_two.insert()
|
||||
|
||||
|
||||
@contextmanager
|
||||
def change_settings(doctype, settings_dict):
|
||||
""" A context manager to ensure that settings are changed before running
|
||||
function and restored after running it regardless of exceptions occured.
|
||||
This is useful in tests where you want to make changes in a function but
|
||||
don't retain those changes.
|
||||
import and use as decorator to cover full function or using `with` statement.
|
||||
|
||||
example:
|
||||
@change_settings("Stock Settings", {"item_naming_by": "Naming Series"})
|
||||
def test_case(self):
|
||||
...
|
||||
"""
|
||||
|
||||
try:
|
||||
settings = frappe.get_doc(doctype)
|
||||
# remember setting
|
||||
previous_settings = copy.deepcopy(settings_dict)
|
||||
for key in previous_settings:
|
||||
previous_settings[key] = getattr(settings, key)
|
||||
|
||||
# change setting
|
||||
for key, value in settings_dict.items():
|
||||
setattr(settings, key, value)
|
||||
settings.save()
|
||||
# singles are cached by default, clear to avoid flake
|
||||
frappe.db.value_cache[settings] = {}
|
||||
yield # yield control to calling function
|
||||
|
||||
finally:
|
||||
# restore settings
|
||||
settings = frappe.get_doc(doctype)
|
||||
for key, value in previous_settings.items():
|
||||
setattr(settings, key, value)
|
||||
settings.save()
|
||||
|
||||
|
||||
def execute_script_report(
|
||||
report_name: ReportName,
|
||||
module: str,
|
||||
@@ -152,23 +95,3 @@ def execute_script_report(
|
||||
report_execute_fn(filter_with_optional_param)
|
||||
|
||||
return report_data
|
||||
|
||||
|
||||
def timeout(seconds=30, error_message="Test timed out."):
|
||||
""" Timeout decorator to ensure a test doesn't run for too long.
|
||||
|
||||
adapted from https://stackoverflow.com/a/2282656"""
|
||||
def decorator(func):
|
||||
def _handle_timeout(signum, frame):
|
||||
raise Exception(error_message)
|
||||
|
||||
def wrapper(*args, **kwargs):
|
||||
signal.signal(signal.SIGALRM, _handle_timeout)
|
||||
signal.alarm(seconds)
|
||||
try:
|
||||
result = func(*args, **kwargs)
|
||||
finally:
|
||||
signal.alarm(0)
|
||||
return result
|
||||
return wrapper
|
||||
return decorator
|
||||
|
||||
Reference in New Issue
Block a user