File: test_exc.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 (40 lines) | stat: -rw-r--r-- 1,173 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
from evalidate import Expr, ValidationException, ExecutionException, CompilationException, base_eval_model
import pytest

class TestExceptions():
    def test_exec(self):
        with pytest.raises(ExecutionException) as e:
            r = Expr('1/0').eval()

        with pytest.raises(ExecutionException) as e:
            r = Expr('1+x').eval()

    def test_badcode(self):

        with pytest.raises(CompilationException) as e: 
            r = Expr('')

        with pytest.raises(CompilationException) as e: 
            r = Expr(';')

        with pytest.raises(CompilationException) as e: 
            r = Expr("""
            1+1
            2+2""")

    def test_call(self):
        ctx = {'a':36.6}
        with pytest.raises(ValidationException) as e:
            r = Expr('int(a)').eval(ctx)

    def test_return(self):
        with pytest.raises(CompilationException) as e: 
            Expr('return 42').eval()
    
    def test_startswith(self):
        with pytest.raises(ValidationException) as e:
            src = '"abcdef".startswith("abc")'
            m = base_eval_model.clone()
            m.nodes.append('Call')
            r = Expr(src).eval()