mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-10-23 17:57:03 +02:00
Merge cab088f765 into 9c66bafa52
This commit is contained in:
commit
3775d98747
2 changed files with 23 additions and 1 deletions
|
|
@ -4235,7 +4235,10 @@ class Table(Queryable):
|
|||
fn_name = getattr(fn, "__name__", "fn")
|
||||
if fn_name == "<lambda>":
|
||||
fn_name = f"lambda_{abs(hash(fn))}"
|
||||
self.db.register_function(convert_value, name=fn_name)
|
||||
# replace=True: a previous .convert() may have registered a
|
||||
# different callable under the same name, e.g. two bound methods
|
||||
# called "get" or two functools.partial objects
|
||||
self.db.register_function(convert_value, name=fn_name, replace=True)
|
||||
sql = "update {} set {sets}{where};".format(
|
||||
quote_identifier(self.name),
|
||||
sets=", ".join(
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
import functools
|
||||
|
||||
import pytest
|
||||
|
||||
from sqlite_utils.db import BadMultiValues
|
||||
|
|
@ -158,3 +160,20 @@ def test_convert_repeated(fresh_db):
|
|||
table.convert(col, lambda x: x * 2)
|
||||
table.convert(col, lambda _x: 0)
|
||||
assert table.get(1) == {col: 0}
|
||||
|
||||
|
||||
def test_convert_repeated_callables_with_same_name(fresh_db):
|
||||
# Different callables can share a name: bound methods such as dict.get,
|
||||
# or functools.partial objects, which have no __name__ at all
|
||||
table = fresh_db.table("places")
|
||||
table.insert({"id": 1, "state": "CA", "country": "US"}, pk="id")
|
||||
table.convert("state", {"CA": "California"}.get)
|
||||
table.convert("country", {"US": "United States"}.get)
|
||||
assert table.get(1) == {"id": 1, "state": "California", "country": "United States"}
|
||||
table.convert("state", functools.partial("{}!".format))
|
||||
table.convert("country", functools.partial("{}?".format))
|
||||
assert table.get(1) == {
|
||||
"id": 1,
|
||||
"state": "California!",
|
||||
"country": "United States?",
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue