From ab8f48f619eb79e3937634f4df780a66211e8d02 Mon Sep 17 00:00:00 2001 From: Luke Kearney Date: Tue, 21 May 2024 17:59:58 +0100 Subject: [PATCH 1/4] BSC-19829: correctly handle boolean values in predicate --- pybsn/__init__.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/pybsn/__init__.py b/pybsn/__init__.py index 5fd441a..4ff40cc 100644 --- a/pybsn/__init__.py +++ b/pybsn/__init__.py @@ -189,7 +189,8 @@ def match(self, **kwargs): .../node[mac-address="14:18:77:96:8b:d6"] """ for k, v in kwargs.items(): - self = self.filter("%s=$x" % k.replace('_', '-'), x=v) + self = self.filter("%s=$x" % k.replace('_', '-'), x=_normalize(v)) + return self def filter(self, template, *args, **kwargs): @@ -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) @@ -467,6 +468,16 @@ def __repr__(self): return "BigDbClient(%s)" % self.url +def _normalize(self, v): + """ Helper method to normalize query values """ + if type(v) == bool: + # replace to use JSON type booleans, not Python + 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) From 01c5b7e5da02bb47b4889bf17d3cb8bf5c8c97f7 Mon Sep 17 00:00:00 2001 From: Luke Kearney Date: Thu, 30 May 2024 12:07:24 +0100 Subject: [PATCH 2/4] BSC-19829: fix normalize --- pybsn/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pybsn/__init__.py b/pybsn/__init__.py index 4ff40cc..6c133d5 100644 --- a/pybsn/__init__.py +++ b/pybsn/__init__.py @@ -189,7 +189,7 @@ def match(self, **kwargs): .../node[mac-address="14:18:77:96:8b:d6"] """ for k, v in kwargs.items(): - self = self.filter("%s=$x" % k.replace('_', '-'), x=_normalize(v)) + self = self.filter("%s=$x" % k.replace('_', '-'), x=v) return self @@ -468,7 +468,7 @@ def __repr__(self): return "BigDbClient(%s)" % self.url -def _normalize(self, v): +def _normalize(v): """ Helper method to normalize query values """ if type(v) == bool: # replace to use JSON type booleans, not Python From 09dc07f27b1246ffcfe505034c4b0f3edcc3d861 Mon Sep 17 00:00:00 2001 From: Luke Kearney Date: Fri, 31 May 2024 15:30:59 +0100 Subject: [PATCH 3/4] BSC-19829: normalize booleans to strings and add test cases --- pybsn/__init__.py | 4 ++-- test/test_node.py | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/pybsn/__init__.py b/pybsn/__init__.py index 6c133d5..97b8773 100644 --- a/pybsn/__init__.py +++ b/pybsn/__init__.py @@ -473,9 +473,9 @@ def _normalize(v): if type(v) == bool: # replace to use JSON type booleans, not Python if v: - return "true" + return "'true'" else: - return "false" + return "'false'" return repr(v) def logged_request(session, request, timeout): diff --git a/test/test_node.py b/test/test_node.py index 2dab817..35e32c9 100644 --- a/test/test_node.py +++ b/test/test_node.py @@ -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]", @@ -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") From 9e205698d4e2ae25099c4f68d38ec10553038f2c Mon Sep 17 00:00:00 2001 From: Luke Kearney Date: Fri, 31 May 2024 15:44:17 +0100 Subject: [PATCH 4/4] BSC-17926: fix comment --- pybsn/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pybsn/__init__.py b/pybsn/__init__.py index 97b8773..c90132d 100644 --- a/pybsn/__init__.py +++ b/pybsn/__init__.py @@ -471,7 +471,7 @@ def __repr__(self): def _normalize(v): """ Helper method to normalize query values """ if type(v) == bool: - # replace to use JSON type booleans, not Python + # replace to use booleans to use strings in JSON-boolean style if v: return "'true'" else: