Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BSC-19829: correctly handle boolean values in predicate #71

Merged
merged 4 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion pybsn/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ def match(self, **kwargs):
"""
for k, v in kwargs.items():
self = self.filter("%s=$x" % k.replace('_', '-'), x=v)

return self

def filter(self, template, *args, **kwargs):
Expand All @@ -209,7 +210,7 @@ def filter(self, template, *args, **kwargs):
.../segment[member-vlan<1000]
"""

kwargs = {k: repr(v) for k, v in kwargs.items()}
kwargs = {k: _normalize(v) for k, v in kwargs.items()}
predicate = '[' + Template(template).substitute(**kwargs) + ']'
return Node(self._path + predicate, self._connection)

Expand Down Expand Up @@ -467,6 +468,16 @@ def __repr__(self):
return "BigDbClient(%s)" % self.url


def _normalize(v):
""" Helper method to normalize query values """
if type(v) == bool:
# replace to use booleans to use strings in JSON-boolean style
if v:
return "'true'"
else:
return "'false'"
return repr(v)

def logged_request(session, request, timeout):
""" Helper method that logs HTTP requests made by this library, if configured. """
prepared = session.prepare_request(request)
Expand Down
2 changes: 2 additions & 0 deletions test/test_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ def test_match(self):
node = self.root.node
self.assertEqual(node.match()._path, "controller/node")
self.assertEqual(node.match(a="foo")._path, "controller/node[a='foo']")
self.assertEqual(node.match(a=True)._path, "controller/node[a='true']")
self.assertIn(
node.match(a="foo", b=2)._path,
("controller/node[a='foo'][b=2]",
Expand All @@ -144,6 +145,7 @@ def test_filter(self):
self.assertEqual(node.filter("a='foo'")._path, "controller/node[a='foo']")
self.assertEqual(node.filter("a=$x", x="foo")._path, "controller/node[a='foo']")
self.assertEqual(node.filter("b=$x", x=1)._path, "controller/node[b=1]")
self.assertEqual(node.filter("a=$x", x=True)._path, "controller/node[a='true']")

def test_root_call(self):
self.client.get.return_value = dict(foo="bar")
Expand Down