From b019a1163c19fd54f19e5fe0dc767485cfef6806 Mon Sep 17 00:00:00 2001 From: Gursheen Anand Date: Fri, 11 Aug 2023 15:34:07 +0530 Subject: [PATCH] feat: implement like & not like in compare util --- frappe/tests/test_utils.py | 28 ++++++++++++++++++++++++++++ frappe/utils/data.py | 2 ++ 2 files changed, 30 insertions(+) diff --git a/frappe/tests/test_utils.py b/frappe/tests/test_utils.py index 59e942ef42..a10978d72b 100644 --- a/frappe/tests/test_utils.py +++ b/frappe/tests/test_utils.py @@ -173,6 +173,34 @@ class TestFilters(FrappeTestCase): ) ) + def test_like_not_like(self): + doc = {"doctype": "User", "username": "test_abc"} + self.assertTrue( + evaluate_filters( + doc, + [["username", "like", "test"]], + ) + ) + self.assertFalse( + evaluate_filters( + doc, + [["username", "like", "user1"]], + ) + ) + + self.assertFalse( + evaluate_filters( + doc, + [["username", "not like", "test"]], + ) + ) + self.assertTrue( + evaluate_filters( + doc, + [["username", "not like", "user1"]], + ) + ) + class TestMoney(FrappeTestCase): def test_money_in_words(self): diff --git a/frappe/utils/data.py b/frappe/utils/data.py index 6974d6d636..43eca61893 100644 --- a/frappe/utils/data.py +++ b/frappe/utils/data.py @@ -1707,6 +1707,8 @@ operator_map = { "<=": operator.le, "not None": lambda a, b: a is not None, "None": lambda a, b: a is None, + "like": lambda a, b: operator.contains(a.strip("%"), b.strip("%")), + "not like": lambda a, b: not operator.contains(a.strip("%"), b.strip("%")), }