test: Add tests for typing validations

This commit is contained in:
Gavin D'souza 2022-11-30 17:00:37 +05:30
parent 2327a56abc
commit 73b0971a26
3 changed files with 28 additions and 2 deletions

View file

@ -2,6 +2,7 @@
# License: MIT. See LICENSE
import datetime
import json
from typing import Union
import frappe
import frappe.desk.query_report
@ -326,7 +327,7 @@ class Report(Document):
return data
@frappe.whitelist()
def toggle_disable(self, disable):
def toggle_disable(self, disable: bool | str | int):
if not self.has_permission("write"):
frappe.throw(_("You are not allowed to edit the report."))

View file

@ -919,3 +919,28 @@ class TestMiscUtils(FrappeTestCase):
self.assertEqual(safe_json_loads("{}"), {})
self.assertEqual(safe_json_loads("{ /}"), "{ /}")
self.assertEqual(safe_json_loads("12"), 12) # this is a quirk
class TestTypingValidations(FrappeTestCase):
ERR_REGEX = "^type of .* must be .*; got (object|list) instead$"
def test_validate_whitelisted_api(self):
from inspect import signature
whitelisted_fn = next(x for x in frappe.whitelisted if x.__annotations__)
bad_params = (object(),) * len(signature(whitelisted_fn).parameters)
with self.assertRaisesRegex(TypeError, self.ERR_REGEX):
whitelisted_fn(*bad_params)
def test_validate_whitelisted_doc_method(self):
report = frappe.get_last_doc("Report")
with self.assertRaisesRegex(TypeError, self.ERR_REGEX):
report.toggle_disable(["disable"])
current_value = report.disabled
changed_value = not current_value
report.toggle_disable(changed_value)
report.toggle_disable(current_value)

View file

@ -1,4 +1,4 @@
from inspect import _empty, isclass, signature
from inspect import _empty, signature
from typing import Callable, ForwardRef, Union
from typeguard import check_type