@db.register_function(deterministic=True), closes #191

This commit is contained in:
Simon Willison 2020-10-28 14:24:03 -07:00
commit 0789bad8f7
3 changed files with 55 additions and 4 deletions

View file

@ -1,3 +1,8 @@
import pytest
import sys
from unittest.mock import MagicMock
def test_register_function(fresh_db):
@fresh_db.register_function
def reverse_string(s):
@ -14,3 +19,28 @@ def test_register_function_multiple_arguments(fresh_db):
result = fresh_db.execute("select a_times_b_plus_c(2, 3, 4)").fetchone()[0]
assert result == 10
def test_register_function_deterministic(fresh_db):
@fresh_db.register_function(deterministic=True)
def to_lower(s):
return s.lower()
result = fresh_db.execute("select to_lower('BOB')").fetchone()[0]
assert result == "bob"
@pytest.mark.skipif(
sys.version_info < (3, 8), reason="deterministic=True was added in Python 3.8"
)
def test_register_function_deterministic_registered(fresh_db):
fresh_db.conn = MagicMock()
fresh_db.conn.create_function = MagicMock()
@fresh_db.register_function(deterministic=True)
def to_lower_2(s):
return s.lower()
fresh_db.conn.create_function.assert_called_with(
"to_lower_2", 1, to_lower_2, deterministic=True
)