-
-
Notifications
You must be signed in to change notification settings - Fork 111
/
test_register_function.py
69 lines (49 loc) · 1.82 KB
/
test_register_function.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# flake8: noqa
import pytest
import sys
from unittest.mock import MagicMock
def test_register_function(fresh_db):
@fresh_db.register_function
def reverse_string(s):
return "".join(reversed(list(s)))
result = fresh_db.execute('select reverse_string("hello")').fetchone()[0]
assert result == "olleh"
def test_register_function_multiple_arguments(fresh_db):
@fresh_db.register_function
def a_times_b_plus_c(a, b, c):
return a * b + c
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
)
def test_register_function_replace(fresh_db):
@fresh_db.register_function()
def one():
return "one"
assert "one" == fresh_db.execute("select one()").fetchone()[0]
# This will fail to replace the function:
@fresh_db.register_function()
def one(): # noqa
return "two"
assert "one" == fresh_db.execute("select one()").fetchone()[0]
# This will replace it
@fresh_db.register_function(replace=True)
def one(): # noqa
return "two"
assert "two" == fresh_db.execute("select one()").fetchone()[0]