forked from gtalarico/pyairtable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_formulas.py
113 lines (80 loc) · 2.34 KB
/
test_formulas.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import pytest
from pyairtable.formulas import (
AND,
EQUAL,
FIELD,
FIND,
GREATER,
GREATER_EQUAL,
IF,
LESS,
LESS_EQUAL,
LOWER,
NOT_EQUAL,
OR,
STR_VALUE,
escape_quotes,
match,
)
def test_equal():
assert EQUAL("A", "B") == "A=B"
def test_field():
assert FIELD("Name") == "{Name}"
assert FIELD("Guest's Name") == r"{Guest\'s Name}"
def test_and():
assert AND("A", "B", "C") == "AND(A,B,C)"
def test_or():
assert OR("A", "B", "C") == "OR(A,B,C)"
def test_if():
assert IF(1, 0, 1) == "IF(1, 0, 1)"
def test_find():
rv = FIND(STR_VALUE(2021), FIELD("DatetimeCol"))
assert rv == "FIND('2021', {DatetimeCol})"
rv = FIND(STR_VALUE(2021), FIELD("DatetimeCol"), 2)
assert rv == "FIND('2021', {DatetimeCol}, 2)"
def test_string_value():
assert STR_VALUE("A") == "'A'"
def test_combination():
formula = AND(
EQUAL(FIELD("First Name"), STR_VALUE("A")),
EQUAL(FIELD("Last Name"), STR_VALUE("B")),
EQUAL(FIELD("Age"), STR_VALUE(15)),
)
assert formula == ("AND({First Name}='A',{Last Name}='B',{Age}='15')")
@pytest.mark.parametrize(
"dict,kwargs,expected_formula",
[
({"First Name": "John"}, {"match_any": False}, "{First Name}='John'"),
({"First Name": "John"}, {"match_any": True}, "{First Name}='John'"),
({"A": "1", "B": "2"}, {"match_any": False}, "AND({A}='1',{B}='2')"),
({"A": "1", "B": "2"}, {"match_any": True}, "OR({A}='1',{B}='2')"),
({}, {"match_any": False}, ""),
({}, {"match_any": True}, ""),
],
)
def test_match(dict, kwargs, expected_formula):
rv = match(dict, **kwargs)
assert rv == expected_formula
@pytest.mark.parametrize(
"text,escaped",
[
("hello", "hello"),
("player's name", r"player\'s name"),
(r"player\'s name", r"player\'s name"),
],
)
def test_escape_quotes(text, escaped):
rv = escape_quotes(text)
assert rv == escaped
def test_lower():
assert LOWER("TestValue") == "LOWER(TestValue)"
def test_greater_equal():
assert GREATER_EQUAL("A", "B") == "A>=B"
def test_less_equal():
assert LESS_EQUAL("A", "B") == "A<=B"
def test_greater():
assert GREATER("A", "B") == "A>B"
def test_less():
assert LESS("A", "B") == "A<B"
def test_not_equal():
assert NOT_EQUAL("A", "B") == "A!=B"