Skip to content

Commit

Permalink
Merge pull request #131 from plone/fix-int-ops
Browse files Browse the repository at this point in the history
Fix int operations to actually accept integers
  • Loading branch information
davisagli authored Jun 15, 2023
2 parents 54a485e + 1c34acb commit 11fc4cd
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 22 deletions.
2 changes: 2 additions & 0 deletions news/131.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix integer operations to accept integers. @davisagli
Fix integer operations to avoid adding None to the query when the input is not valid. @davisagli
31 changes: 13 additions & 18 deletions plone/app/querystring/queryparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,13 @@ def _intEqual(context, row):
values = [int(v) for v in row.values]
except (ValueError, TypeError, AttributeError):
pass
elif not isinstance(row.values, int):
else:
try:
values = int(row.values)
except (ValueError, TypeError, AttributeError):
pass
if values is None:
return {}
return {row.index: {"query": values}}


Expand Down Expand Up @@ -154,19 +156,16 @@ def _largerThan(context, row):


def _intLargerThan(context, row):
value = None
if not isinstance(row.values, int):
try:
value = int(row.values)
except (ValueError, TypeError, AttributeError):
pass
tmp = {
try:
value = int(row.values)
except (ValueError, TypeError, AttributeError):
return {}
return {
row.index: {
"query": value,
"range": "min",
},
}
return tmp


def _lessThan(context, row):
Expand All @@ -180,20 +179,16 @@ def _lessThan(context, row):


def _intLessThan(context, row):
value = None
if not isinstance(row.values, int):
try:
value = int(row.values)
except (ValueError, TypeError, AttributeError):
# value = 0
pass
tmp = {
try:
value = int(row.values)
except (ValueError, TypeError, AttributeError):
return {}
return {
row.index: {
"query": value,
"range": "max",
},
}
return tmp


def _currentUser(context, row):
Expand Down
32 changes: 28 additions & 4 deletions plone/app/querystring/tests/testQueryParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,18 @@ def test__equal(self):
self.assertEqual(parsed, expected)

def test__intEqual(self):
# int
data = Row(index="modified", operator="_intEqual", values=20)
parsed = queryparser._intEqual(MockSite(), data)
expected = {"modified": {"query": 20}}
self.assertEqual(parsed, expected)

# list of ints
data = Row(index="modified", operator="_intEqual", values=[20, 21])
parsed = queryparser._intEqual(MockSite(), data)
expected = {"modified": {"query": [20, 21]}}
self.assertEqual(parsed, expected)

# bytes
data = Row(index="modified", operator="_intEqual", values=b"20")
parsed = queryparser._intEqual(MockSite(), data)
Expand Down Expand Up @@ -354,15 +366,15 @@ def test__intEqual(self):
# bad text
data = Row(index="modified", operator="_intEqual", values="bad")
parsed = queryparser._intEqual(MockSite(), data)
expected = {"modified": {"query": None}}
expected = {}
self.assertEqual(parsed, expected)

# list of bad text
data = Row(
index="modified", operator="_intEqual", values=[b"bad", "text", "values"]
)
parsed = queryparser._intEqual(MockSite(), data)
expected = {"modified": {"query": None}}
expected = {}
self.assertEqual(parsed, expected)

def test__lessThan(self):
Expand All @@ -372,6 +384,12 @@ def test__lessThan(self):
self.assertEqual(parsed, expected)

def test__intLessThan(self):
# int
data = Row(index="modified", operator="_intLessThan", values=20)
parsed = queryparser._intLessThan(MockSite(), data)
expected = {"modified": {"query": 20, "range": "max"}}
self.assertEqual(parsed, expected)

# bytes
data = Row(index="modified", operator="_intLessThan", values=b"20")
parsed = queryparser._intLessThan(MockSite(), data)
Expand All @@ -387,7 +405,7 @@ def test__intLessThan(self):
# bad value
data = Row(index="modified", operator="_intLessThan", values="bad")
parsed = queryparser._intLessThan(MockSite(), data)
expected = {"modified": {"query": None, "range": "max"}}
expected = {}
self.assertEqual(parsed, expected)

def test__largerThan(self):
Expand All @@ -397,6 +415,12 @@ def test__largerThan(self):
self.assertEqual(parsed, expected)

def test__intLargerThan(self):
# int
data = Row(index="modified", operator="_intLargerThan", values=20)
parsed = queryparser._intLargerThan(MockSite(), data)
expected = {"modified": {"query": 20, "range": "min"}}
self.assertEqual(parsed, expected)

# bytes
data = Row(index="modified", operator="_intLargerThan", values=b"20")
parsed = queryparser._intLargerThan(MockSite(), data)
Expand All @@ -412,7 +436,7 @@ def test__intLargerThan(self):
# bad value
data = Row(index="modified", operator="_intLargerThan", values="bad")
parsed = queryparser._intLargerThan(MockSite(), data)
expected = {"modified": {"query": None, "range": "min"}}
expected = {}
self.assertEqual(parsed, expected)

def test__currentUser(self):
Expand Down

0 comments on commit 11fc4cd

Please sign in to comment.