mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-09-17 05:54:23 +02:00
Raise exception on registering identical function names
This commit is contained in:
parent
a158445db5
commit
486e0cc1cd
2 changed files with 20 additions and 6 deletions
|
|
@ -219,6 +219,11 @@ class AlterError(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class FunctionAlreadyRegistered(Exception):
|
||||||
|
"A function with this name and arity was already registered"
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class NoObviousTable(Exception):
|
class NoObviousTable(Exception):
|
||||||
"Could not tell which table this operation refers to"
|
"Could not tell which table this operation refers to"
|
||||||
pass
|
pass
|
||||||
|
|
@ -409,7 +414,7 @@ class Database:
|
||||||
fn_name = name or fn.__name__
|
fn_name = name or fn.__name__
|
||||||
arity = len(inspect.signature(fn).parameters)
|
arity = len(inspect.signature(fn).parameters)
|
||||||
if not replace and (fn_name, arity) in self._registered_functions:
|
if not replace and (fn_name, arity) in self._registered_functions:
|
||||||
return fn
|
raise FunctionAlreadyRegistered(f'Already registered function with name "{fn_name}" and identical arity')
|
||||||
kwargs = {}
|
kwargs = {}
|
||||||
registered = False
|
registered = False
|
||||||
if deterministic:
|
if deterministic:
|
||||||
|
|
@ -434,7 +439,7 @@ class Database:
|
||||||
|
|
||||||
def register_fts4_bm25(self):
|
def register_fts4_bm25(self):
|
||||||
"Register the ``rank_bm25(match_info)`` function used for calculating relevance with SQLite FTS4."
|
"Register the ``rank_bm25(match_info)`` function used for calculating relevance with SQLite FTS4."
|
||||||
self.register_function(rank_bm25, deterministic=True)
|
self.register_function(rank_bm25, deterministic=True, replace=True)
|
||||||
|
|
||||||
def attach(self, alias: str, filepath: Union[str, pathlib.Path]):
|
def attach(self, alias: str, filepath: Union[str, pathlib.Path]):
|
||||||
"""
|
"""
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ import pytest
|
||||||
import sys
|
import sys
|
||||||
from unittest.mock import MagicMock, call
|
from unittest.mock import MagicMock, call
|
||||||
from sqlite_utils.utils import sqlite3
|
from sqlite_utils.utils import sqlite3
|
||||||
|
from sqlite_utils.db import FunctionAlreadyRegistered
|
||||||
|
|
||||||
def test_register_function(fresh_db):
|
def test_register_function(fresh_db):
|
||||||
@fresh_db.register_function
|
@fresh_db.register_function
|
||||||
|
|
@ -85,9 +85,10 @@ def test_register_function_replace(fresh_db):
|
||||||
assert "one" == fresh_db.execute("select one()").fetchone()[0]
|
assert "one" == fresh_db.execute("select one()").fetchone()[0]
|
||||||
|
|
||||||
# This will fail to replace the function:
|
# This will fail to replace the function:
|
||||||
@fresh_db.register_function()
|
with pytest.raises(FunctionAlreadyRegistered):
|
||||||
def one(): # noqa
|
@fresh_db.register_function()
|
||||||
return "two"
|
def one(): # noqa
|
||||||
|
return "two"
|
||||||
|
|
||||||
assert "one" == fresh_db.execute("select one()").fetchone()[0]
|
assert "one" == fresh_db.execute("select one()").fetchone()[0]
|
||||||
|
|
||||||
|
|
@ -97,3 +98,11 @@ def test_register_function_replace(fresh_db):
|
||||||
return "two"
|
return "two"
|
||||||
|
|
||||||
assert "two" == fresh_db.execute("select one()").fetchone()[0]
|
assert "two" == fresh_db.execute("select one()").fetchone()[0]
|
||||||
|
|
||||||
|
|
||||||
|
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