From 3bd8f5cb40237a75078ce235e28ac8d44c27f07e Mon Sep 17 00:00:00 2001 From: collerek Date: Wed, 23 Sep 2020 11:52:47 +0200 Subject: [PATCH 1/2] added startswith, istartswith and same for endswith, add test for those --- .coverage | Bin 53248 -> 53248 bytes ormar/queryset/clause.py | 17 +++++++++++++++-- tests/test_models.py | 30 ++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/.coverage b/.coverage index 550b22b2efd8e90ea4e14326f3bf49df1cbfc4df..17ef238363b58076687acc5da6744c6af16314dd 100644 GIT binary patch delta 371 zcmV-(0gV2DpaX!Q1F!}l3LO9sSPw@JD-RvB5fBXzlN2u_A2B*LF*-Ch4*~-l7j|WG zb7^mGH7+tPF@Yrk1Ra7ulU6TBv!*X#PyxJ?2Y@CkmkxvuYz|`%R}MxFHx48Y6AlIq z?G4lo%niN`hz)2BN(~SU`V8a@*9^R~5fEey8yP4B0SPoHy3IG=`R04R=kw?P{4I~+ zuk-f-lirLV1N`0xlL3tue>fxr0SR&>?%jOz&G$Lq9iKmcp5OEP_MP9|`P=T>w#~Qs z`SUk_`3t}KeP$lk^Z9&+Go0c0-?nWWBLo2nIwN+M_y7ElH~%>Qw>jtLIeXcgz3gQ# ze;y(P0SP`Lx|{EO^ZB07&p$t9bEmb>mj6BH{I6~04>TbJ0SRg$laY-a6Lsg@xwqZ( ze|OH$-|OUWcfNi8lfRAGA|Mk40SQ7A+U5V3x82qEzJKPn{`Rjs$M@^s^L;z@_3r|c RxsG`SfByJ=v$Hpk$w0RLtONi6 delta 386 zcmV-|0e${}paX!Q1F!}l3KRejPY*s1A`cX^5fB6qlN~Q3A22#FH##*j4*~-l7j|WG zb7^mGH7+tPF@Y`t1Ra7=lV2}K1$1R|baS(!FJw>w#FGbrCM%c@g$`{FWDZykM-Dg+ zB@Pr02Mz8G)eX%JzYU5FY7I>d7YzXn>kQru#Iq3)Yz!J0CVVLhMEXE?(d4jUr`0SP!Gc9-}6 z{Es(}^M9LjZl3eYZ+`jZmp>jN1OW*?BD$OJeDnF9&(A+UWfOC!wa?~%&pH2VTloVs zAp`*lXp@eO9TN4 Tuple[str, bool]: has_escaped_character = False - if op not in ["contains", "icontains"]: + if op not in [ + "contains", + "icontains", + "startswith", + "istartswith", + "endswith", + "iendswith", + ]: return value, has_escaped_character if isinstance(value, ormar.Model): @@ -183,7 +194,9 @@ class QueryClause: # enable escape modifier for char in ESCAPE_CHARACTERS: value = value.replace(char, f"\\{char}") - value = f"%{value}%" + prefix = "%" if "start" not in op else "" + sufix = "%" if "end" not in op else "" + value = f"{prefix}{value}{sufix}" return value, has_escaped_character diff --git a/tests/test_models.py b/tests/test_models.py index e402c86..79ca0ed 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -374,3 +374,33 @@ async def test_model_choices(): await Country.objects.create( name=name, taxed=taxed, country_code=country_code ) + + +@pytest.mark.asyncio +async def test_start_and_end_filters(): + async with database: + async with database.transaction(force_rollback=True): + await User.objects.create(name="Markos Uj") + await User.objects.create(name="Maqua Bigo") + await User.objects.create(name="maqo quidid") + await User.objects.create(name="Louis Figo") + await User.objects.create(name="Loordi Kami") + await User.objects.create(name="Yuuki Sami") + + users = await User.objects.filter(name__startswith="Mar").all() + assert len(users) == 1 + + users = await User.objects.filter(name__istartswith="ma").all() + assert len(users) == 3 + + users = await User.objects.filter(name__istartswith="Maq").all() + assert len(users) == 2 + + users = await User.objects.filter(name__iendswith="AMI").all() + assert len(users) == 2 + + users = await User.objects.filter(name__endswith="Uj").all() + assert len(users) == 1 + + users = await User.objects.filter(name__endswith="igo").all() + assert len(users) == 2 From cc1d66001fb2668b6cf7690df1650acaaabb9bd4 Mon Sep 17 00:00:00 2001 From: collerek Date: Wed, 23 Sep 2020 11:58:15 +0200 Subject: [PATCH 2/2] bump version, update readme --- README.md | 5 +++++ ormar/__init__.py | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 730444c..2c81815 100644 --- a/README.md +++ b/README.md @@ -86,6 +86,11 @@ notes = await Note.objects.filter(text__icontains="mum").all() # exclude - from ormar >= 0.3.1 notes = await Note.objects.exclude(text__icontains="mum").all() +# startswith, istartswith, endswith, iendswith - from ormar >= 0.3.3 +notes = await Note.objects.filter(text__iendswith="mum.").all() +notes = await Note.objects.filter(text__istartswith="call").all() +notes = await Note.objects.filter(text__startswith="Buy").all() + # .get() note = await Note.objects.get(id=1) diff --git a/ormar/__init__.py b/ormar/__init__.py index aea7bfd..d77460a 100644 --- a/ormar/__init__.py +++ b/ormar/__init__.py @@ -26,7 +26,7 @@ class UndefinedType: # pragma no cover Undefined = UndefinedType() -__version__ = "0.3.2" +__version__ = "0.3.3" __all__ = [ "Integer", "BigInteger",