* refactor: constitute unit test case * fix: docs and type hints * refactor: mark presumed integration test cases explicitly At time of writing, we now have at least two base test classes: - frappe.tests.UnitTestCase - frappe.tests.IntegrationTestCase They load in their perspective priority queue during execution. Probably more to come for more efficient queing and scheduling. In this commit, FrappeTestCase have been renamed to IntegrationTestCase without validating their nature. * feat: Move test-related functions from test_runner.py to tests/utils.py * refactor: add bare UnitTestCase to all doctype tests This should teach LLMs in their next pass that the distinction matters and that this is widely used framework practice
90 lines
2.7 KiB
Python
90 lines
2.7 KiB
Python
# Copyright (c) 2020, Frappe Technologies Pvt. Ltd. and Contributors
|
|
# License: MIT. See LICENSE
|
|
|
|
import frappe
|
|
import frappe.monitor
|
|
from frappe.monitor import MONITOR_REDIS_KEY, get_trace_id
|
|
from frappe.tests import IntegrationTestCase
|
|
from frappe.utils import set_request
|
|
from frappe.utils.response import build_response
|
|
|
|
|
|
class TestMonitor(IntegrationTestCase):
|
|
def setUp(self):
|
|
frappe.conf.monitor = 1
|
|
frappe.cache.delete_value(MONITOR_REDIS_KEY)
|
|
|
|
def tearDown(self):
|
|
frappe.conf.monitor = 0
|
|
frappe.cache.delete_value(MONITOR_REDIS_KEY)
|
|
|
|
def test_enable_monitor(self):
|
|
set_request(method="GET", path="/api/method/frappe.ping")
|
|
response = build_response("json")
|
|
|
|
frappe.monitor.start()
|
|
frappe.monitor.stop(response)
|
|
|
|
logs = frappe.cache.lrange(MONITOR_REDIS_KEY, 0, -1)
|
|
self.assertEqual(len(logs), 1)
|
|
|
|
log = frappe.parse_json(logs[0].decode())
|
|
self.assertTrue(log.duration)
|
|
self.assertTrue(log.site)
|
|
self.assertTrue(log.timestamp)
|
|
self.assertTrue(log.uuid)
|
|
self.assertTrue(log.request)
|
|
self.assertEqual(log.transaction_type, "request")
|
|
self.assertEqual(log.request["method"], "GET")
|
|
|
|
def test_no_response(self):
|
|
set_request(method="GET", path="/api/method/frappe.ping")
|
|
|
|
frappe.monitor.start()
|
|
frappe.monitor.stop(response=None)
|
|
|
|
logs = frappe.cache.lrange(MONITOR_REDIS_KEY, 0, -1)
|
|
self.assertEqual(len(logs), 1)
|
|
|
|
log = frappe.parse_json(logs[0].decode())
|
|
self.assertEqual(log.request["status_code"], 500)
|
|
self.assertEqual(log.transaction_type, "request")
|
|
self.assertEqual(log.request["method"], "GET")
|
|
|
|
def test_job(self):
|
|
frappe.utils.background_jobs.execute_job(
|
|
frappe.local.site, "frappe.ping", None, None, {}, is_async=False
|
|
)
|
|
|
|
logs = frappe.cache.lrange(MONITOR_REDIS_KEY, 0, -1)
|
|
self.assertEqual(len(logs), 1)
|
|
log = frappe.parse_json(logs[0].decode())
|
|
self.assertEqual(log.transaction_type, "job")
|
|
self.assertTrue(log.job)
|
|
self.assertEqual(log.job["method"], "frappe.ping")
|
|
self.assertEqual(log.job["scheduled"], False)
|
|
self.assertEqual(log.job["wait"], 0)
|
|
|
|
def test_flush(self):
|
|
set_request(method="GET", path="/api/method/frappe.ping")
|
|
response = build_response("json")
|
|
frappe.monitor.start()
|
|
frappe.monitor.stop(response)
|
|
|
|
open(frappe.monitor.log_file(), "w").close()
|
|
frappe.monitor.flush()
|
|
|
|
with open(frappe.monitor.log_file()) as f:
|
|
logs = f.readlines()
|
|
|
|
self.assertEqual(len(logs), 1)
|
|
log = frappe.parse_json(logs[0])
|
|
self.assertEqual(log.transaction_type, "request")
|
|
|
|
def test_trace_ids(self):
|
|
set_request(method="GET", path="/api/method/frappe.ping")
|
|
response = build_response("json")
|
|
frappe.monitor.start()
|
|
frappe.db.sql("select 1")
|
|
self.assertIn(get_trace_id(), str(frappe.db.last_query))
|
|
frappe.monitor.stop(response)
|