Skip to content

Commit

Permalink
fixes #132
Browse files Browse the repository at this point in the history
  • Loading branch information
WolfgangFahl committed Sep 11, 2024
1 parent 3bd5437 commit e40aae6
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lodstorage/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.12.2"
__version__ = "0.12.3"
4 changes: 2 additions & 2 deletions lodstorage/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ def apply_parameters_with_check(self, param_dict: dict = None) -> str:
query = self.query
if self.has_params:
if not param_dict:
param_names = self.params
param_names = list(dict.fromkeys(self.params)) # remove duplicates while preserving order
if len(param_names) > 3:
displayed_params = ", ".join(param_names[:3]) + ", ..."
else:
displayed_params = ", ".join(param_names)
plural_suffix = "s" if len(param_names) > 1 else ""
msg = f"Query needs {len(self.params)} parameter{plural_suffix}: {displayed_params}"
msg = f"Query needs {len(param_names)} parameter{plural_suffix}: {displayed_params}"
raise Exception(msg)
else:
self.set(param_dict)
Expand Down
2 changes: 1 addition & 1 deletion lodstorage/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ class Version(object):
name = "pylodstorage"
version = lodstorage.__version__
date = "2020-09-10"
updated = "2024-08-21"
updated = "2024-09-11"
description = "python List of Dict (Table) Storage library"
37 changes: 37 additions & 0 deletions tests/test_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,40 @@ def test_jinja_params(self):
self.assertEqual(
"PREFIX target: <http://www.wikidata.org/entity/Q80>", query
)

def test_duplicate_params(self):
"""
Test handling of duplicate parameters
"""
sample = "Hello {{name}}, welcome to {{place}}! Nice to meet you, {{name}}."
params_dict = {"name": "Alice", "place": "Wonderland"}

params = Params(sample)
if self.debug:
print(params.params)

self.assertEqual(["name", "place", "name"], params.params)
self.assertTrue("name" in params.params_dict)
self.assertTrue("place" in params.params_dict)

params.params_dict = params_dict
query = params.apply_parameters()

self.assertEqual(
"Hello Alice, welcome to Wonderland! Nice to meet you, Alice.",
query
)

# Test apply_parameters_with_check
result = params.apply_parameters_with_check(params_dict)
self.assertEqual(
"Hello Alice, welcome to Wonderland! Nice to meet you, Alice.",
result
)

# Test error message for missing parameters
with self.assertRaises(Exception) as context:
params.apply_parameters_with_check()

msg=str(context.exception)
self.assertTrue("Query needs 2 parameters: name, place" in msg,msg)

0 comments on commit e40aae6

Please sign in to comment.