mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-07-23 09:24:31 +02:00
parent
02f5c4d69d
commit
fca3ef8cf2
3 changed files with 12 additions and 5 deletions
|
|
@ -414,7 +414,9 @@ class Database:
|
|||
fn_name = name or fn.__name__
|
||||
arity = len(inspect.signature(fn).parameters)
|
||||
if not replace and (fn_name, arity) in self._registered_functions:
|
||||
raise FunctionAlreadyRegistered(f'Already registered function with name "{fn_name}" and identical arity')
|
||||
raise FunctionAlreadyRegistered(
|
||||
f'Already registered function with name "{fn_name}" and identical arity'
|
||||
)
|
||||
kwargs = {}
|
||||
registered = False
|
||||
if deterministic:
|
||||
|
|
@ -2693,15 +2695,17 @@ class Table(Queryable):
|
|||
return jsonify_if_needed(fn(v))
|
||||
|
||||
fn_name = fn.__name__
|
||||
if fn_name == '<lambda>':
|
||||
fn_name = f'lambda_{hash(fn)}'
|
||||
if fn_name == "<lambda>":
|
||||
fn_name = f"lambda_{hash(fn)}"
|
||||
self.db.register_function(convert_value, name=fn_name)
|
||||
sql = "update [{table}] set {sets}{where};".format(
|
||||
table=self.name,
|
||||
sets=", ".join(
|
||||
[
|
||||
"[{output_column}] = {fn_name}([{column}])".format(
|
||||
output_column=output or column, column=column, fn_name=fn_name
|
||||
output_column=output or column,
|
||||
column=column,
|
||||
fn_name=fn_name,
|
||||
)
|
||||
for column in columns
|
||||
]
|
||||
|
|
|
|||
|
|
@ -153,6 +153,6 @@ def test_convert_repeated(fresh_db):
|
|||
table = fresh_db["table"]
|
||||
col = "num"
|
||||
table.insert({col: 1})
|
||||
table.convert(col, lambda x: x*2)
|
||||
table.convert(col, lambda x: x * 2)
|
||||
table.convert(col, lambda _x: 0)
|
||||
assert table.get(1) == {col: 0}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ from unittest.mock import MagicMock, call
|
|||
from sqlite_utils.utils import sqlite3
|
||||
from sqlite_utils.db import FunctionAlreadyRegistered
|
||||
|
||||
|
||||
def test_register_function(fresh_db):
|
||||
@fresh_db.register_function
|
||||
def reverse_string(s):
|
||||
|
|
@ -86,6 +87,7 @@ def test_register_function_replace(fresh_db):
|
|||
|
||||
# This will fail to replace the function:
|
||||
with pytest.raises(FunctionAlreadyRegistered):
|
||||
|
||||
@fresh_db.register_function()
|
||||
def one(): # noqa
|
||||
return "two"
|
||||
|
|
@ -103,6 +105,7 @@ def test_register_function_replace(fresh_db):
|
|||
def test_register_function_duplicate(fresh_db):
|
||||
def to_lower(s):
|
||||
return s.lower()
|
||||
|
||||
fresh_db.register_function(to_lower)
|
||||
with pytest.raises(FunctionAlreadyRegistered):
|
||||
fresh_db.register_function(to_lower)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue