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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
import operator
import pytest
from altair import expr
from altair import datum
from altair import ExprRef
from jsonschema.exceptions import ValidationError
def test_unary_operations():
OP_MAP = {"-": operator.neg, "+": operator.pos}
for op, func in OP_MAP.items():
z = func(datum.xxx)
assert repr(z) == "({}datum.xxx)".format(op)
def test_binary_operations():
OP_MAP = {
"+": operator.add,
"-": operator.sub,
"*": operator.mul,
"/": operator.truediv,
"%": operator.mod,
"===": operator.eq,
"<": operator.lt,
"<=": operator.le,
">": operator.gt,
">=": operator.ge,
"!==": operator.ne,
"&&": operator.and_,
"||": operator.or_,
}
# When these are on the RHS, the opposite is evaluated instead.
INEQ_REVERSE = {
">": "<",
"<": ">",
"<=": ">=",
">=": "<=",
"===": "===",
"!==": "!==",
}
for op, func in OP_MAP.items():
z1 = func(datum.xxx, 2)
assert repr(z1) == "(datum.xxx {} 2)".format(op)
z2 = func(2, datum.xxx)
if op in INEQ_REVERSE:
assert repr(z2) == "(datum.xxx {} 2)".format(INEQ_REVERSE[op])
else:
assert repr(z2) == "(2 {} datum.xxx)".format(op)
z3 = func(datum.xxx, datum.yyy)
assert repr(z3) == "(datum.xxx {} datum.yyy)".format(op)
def test_abs():
z = abs(datum.xxx)
assert repr(z) == "abs(datum.xxx)"
def test_expr_funcs():
"""test all functions defined in expr.funcs"""
name_map = {val: key for key, val in expr.funcs.NAME_MAP.items()}
for funcname in expr.funcs.__all__:
func = getattr(expr, funcname)
z = func(datum.xxx)
assert repr(z) == "{}(datum.xxx)".format(name_map.get(funcname, funcname))
def test_expr_consts():
"""Test all constants defined in expr.consts"""
name_map = {val: key for key, val in expr.consts.NAME_MAP.items()}
for constname in expr.consts.__all__:
const = getattr(expr, constname)
z = const * datum.xxx
assert repr(z) == "({} * datum.xxx)".format(name_map.get(constname, constname))
def test_json_reprs():
"""Test JSON representations of special values"""
assert repr(datum.xxx == None) == "(datum.xxx === null)" # noqa: E711
assert repr(datum.xxx == False) == "(datum.xxx === false)" # noqa: E712
assert repr(datum.xxx == True) == "(datum.xxx === true)" # noqa: E712
def test_to_dict():
ex = datum.xxx * 2 > datum.yyy
assert ex.to_dict() == repr(ex)
def test_copy():
ex = datum.xxx * 2 > abs(datum.yyy)
ex_copy = ex.copy()
assert ex.to_dict() == ex_copy.to_dict()
def test_datum_getattr():
x = datum["foo"]
assert repr(x) == "datum['foo']"
magic_attr = "__magic__"
with pytest.raises(AttributeError):
getattr(datum, magic_attr)
def test_expression_getitem():
x = datum.foo[0]
assert repr(x) == "datum.foo[0]"
def test_expression_function_expr():
# test including a expr.<CONSTANT> should return an ExprRef
er = expr(expr.PI * 2)
assert isinstance(er, ExprRef)
assert repr(er) == "ExprRef({\n expr: (PI * 2)\n})"
def test_expression_function_string():
# expr() can only work with str
er = expr("2 * 2")
assert isinstance(er, ExprRef)
assert repr(er) == "ExprRef({\n expr: '2 * 2'\n})"
def test_expression_function_nostring():
# expr() can only work with str otherwise
# should raise a SchemaValidationError
with pytest.raises(ValidationError):
expr(2 * 2)
with pytest.raises(ValidationError):
expr(["foo", "bah"])
|