forked from nathanabay/bespo_notifications
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_error_log.py
More file actions
55 lines (46 loc) · 1.78 KB
/
test_error_log.py
File metadata and controls
55 lines (46 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import sys
sys.path.insert(0, '/home/frappe/frappe-bench/apps/frappe')
import frappe
frappe.init(site='erp.bespo.et', sites_path='/home/frappe/frappe-bench/sites')
frappe.connect()
frappe.flags.in_test = True
from bespo_notifications.bespo_notifications.events import _evaluate_notification_rules_async
# Mock a Telegram Notification Rule with a syntax error in condition
doc_type = "Tender Opportunity"
error_rule_name = "TNR-TEST-ERROR"
# Clean up if exists
if frappe.db.exists("Telegram Notification Rule", error_rule_name):
frappe.delete_doc("Telegram Notification Rule", error_rule_name, force=True)
# Create mock rule with bad python condition
doc = frappe.get_doc({
"doctype": "Telegram Notification Rule",
"name": error_rule_name,
"document_type": doc_type,
"trigger_event": "New",
"condition": "doc.invalid_syntax ===",
"message_destinations": [
{
"destination_chat": "Nate",
"message_template": "Testing bad rule..."
}
]
})
doc.insert(ignore_permissions=True)
# Delete existing log errors for clean slate
frappe.db.delete("Error Log", {"method": "Notification Rule Evaluator Failed"})
frappe.db.commit()
print("Mock Rule created. Executing evaluator...")
# Get a real tender or mock one to pass in
tender = frappe.get_last_doc(doc_type)
_evaluate_notification_rules_async(tender, "after_insert")
# Check if Error Log caught it
logs = frappe.get_all("Error Log", filters={"method": "Notification Rule Evaluator Failed"})
if logs:
error_log = frappe.get_doc("Error Log", logs[0].name)
print("SUCCESS: Evaluator caught exception and logged it.")
print(error_log.error)
else:
print("FAILED: No error log generated.")
# Clean up
frappe.delete_doc("Telegram Notification Rule", error_rule_name, force=True)
frappe.db.commit()