File size: 2,880 Bytes
ba2f5d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import operator

import pytest

from ... import expr
from .. import datum


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']"

    with pytest.raises(AttributeError):
        datum.__magic__


def test_expression_getitem():
    x = datum.foo[0]
    assert repr(x) == "datum.foo[0]"