File: test_expr.py

package info (click to toggle)
python-evalidate 2.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 144 kB
  • sloc: python: 500; makefile: 3
file content (127 lines) | stat: -rw-r--r-- 3,351 bytes parent folder | download
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
from evalidate import Expr, EvalException, ValidationException, base_eval_model, EvalModel
import pytest

class TestExpr():
    def test_basic(self):
        r = Expr('1+2').eval()
        assert r == 3

    def test_mult(self):
        with pytest.raises(ValidationException):
            r = Expr('42 * 42').eval()

        mult_model = base_eval_model.clone()
        mult_model.nodes.append('Mult')

        r = Expr('42 * 42', mult_model).eval()
        assert r == 1764 

    def test_blank(self):
        with pytest.raises(ValidationException):
            model = EvalModel(nodes=[])
            r = Expr('1 + 2', model=model).eval()

        model = EvalModel(nodes=['Expression', 'BinOp', 'Constant', 'Add'])
            
        r = Expr('1 + 2', model=model).eval()
        assert r == 3

    def test_cleanup(self):
        ctx=dict()
        Expr('None').eval(ctx)
        assert '__builtins__' not in ctx

    def test_context(self):
        e = Expr('a + b')
        assert e.eval({'a': 1, 'b': 2}) == 3
        assert e.eval({'a': 0, 'b': 2}) == 2        
    
    def test_functions(self):
        with pytest.raises(ValidationException):
            r = Expr('int(x)').eval({'x': 1.3})

        model = base_eval_model.clone()
        model.nodes.append('Call')
        model.allowed_functions.append('int')

        r = Expr('int(x)', model=model).eval({ "x": 1.3 })
        assert r == 1
    
    def test_attributes(self):
        
        class Person:
            pass

        class Movie:
            pass

        person = Person()

        person.name = 'Audrey Hepburn'
        person.birth = 1929


        movie1 = Movie()
        movie1.title = "Roman Holiday"
        movie1.year = 1953

        movie2 = Movie()
        movie2.title = "Breakfast at Tiffany's"
        movie2.year = 1961

        movies = [movie1, movie2]


        with pytest.raises(ValidationException):
            r = Expr('movie.year - person.birth').eval({"person": person, "movie": movie1})


        m = base_eval_model.clone()
        m.nodes.append('Attribute')
        m.attributes.extend(['year', 'birth'])
        e = Expr('movie.year - person.birth', model=m)

        age = e.eval({"person":person, "movie": movie1})
        assert age == 24

        age = e.eval({"person": person, "movie": movie2})
        assert age == 32

        ages = [ e.eval({"person": person, "movie": movie}) for movie in movies ]
        assert ages == [ 24, 32 ]


    def test_dicts(self):
        person = {
            'name': 'Audrey Hepburn',
            'birth': 1929
        }
        movies = [
            {
                'title': "Roman Holiday",
                'year': 1953
            },
            {
                'title': "Breakfast at Tiffany's",
                'year': 1961
            }
        ]

        e = Expr('movie["year"] - person["birth"]')

        ages = [ e.eval({"movie": movie, "person": person}) for movie in movies ]
        assert ages == [24,32]
    
    def test_my_funcs(self):
        def double(x):
            return x * 2

        ctx = {'a': 123.45}

        m = base_eval_model.clone()
        m.nodes.append('Call')
        m.allowed_functions.append('int')
        m.imported_functions['double'] = double
        e = Expr('int(a) + double(a)', model=m)
        r = e.eval(ctx)
        assert r == 369.9