Passed
Push — master ( 53016e...38fde4 )
by Oleksandr
02:52
created

test_evaluate   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 55
dl 0
loc 110
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A TestEvaluate.test_single_value_returned() 0 19 1
A TestEvaluate.test_nothing_returned() 0 19 1
A TestEvaluate.test_syntax_error() 0 20 1
A TestEvaluate.test_none_returned() 0 19 1
A TestEvaluate.test_collection_returned() 0 19 1
1
"""
2
Script evaluation tests.
3
"""
4
5
import integ_test_base
6
import json
7
8
9
class TestEvaluate(integ_test_base.IntegTestBase):
10
    def test_single_value_returned(self):
11
        payload = """
12
            {
13
                "data": { "_arg1": 2, "_arg2": 40 },
14
                "script":
15
                "return _arg1 + _arg2"
16
            }
17
            """
18
        headers = {
19
            "Content-Type": "application/json",
20
        }
21
22
        conn = self._get_connection()
23
        conn.request("POST", "/evaluate", payload, headers)
24
        response = conn.getresponse()
25
        result = response.read().decode("utf-8")
26
27
        self.assertEqual(200, response.status)
28
        self.assertEqual("42", result)
29
30
    def test_collection_returned(self):
31
        payload = """
32
            {
33
                "data": { "_arg1": [2, 3], "_arg2": [40, 0.1415926] },
34
                "script":
35
                "return [x + y for x, y in zip(_arg1, _arg2)]"
36
            }
37
            """
38
        headers = {
39
            "Content-Type": "application/json",
40
        }
41
42
        conn = self._get_connection()
43
        conn.request("POST", "/evaluate", payload, headers)
44
        response = conn.getresponse()
45
        result = response.read().decode("utf-8")
46
47
        self.assertEqual(200, response.status)
48
        self.assertEqual("[42, 3.1415926]", result)
49
50
    def test_none_returned(self):
51
        payload = """
52
            {
53
                "data": { "_arg1": 2, "_arg2": 40 },
54
                "script":
55
                "return None"
56
            }
57
            """
58
        headers = {
59
            "Content-Type": "application/json",
60
        }
61
62
        conn = self._get_connection()
63
        conn.request("POST", "/evaluate", payload, headers)
64
        response = conn.getresponse()
65
        result = response.read().decode("utf-8")
66
67
        self.assertEqual(200, response.status)
68
        self.assertEqual("null", result)
69
70
    def test_nothing_returned(self):
71
        payload = """
72
            {
73
                "data": { "_arg1": [2], "_arg2": [40] },
74
                "script":
75
                "res = [x + y for x, y in zip(_arg1, _arg2)]"
76
            }
77
            """
78
        headers = {
79
            "Content-Type": "application/json",
80
        }
81
82
        conn = self._get_connection()
83
        conn.request("POST", "/evaluate", payload, headers)
84
        response = conn.getresponse()
85
        result = response.read().decode("utf-8")
86
87
        self.assertEqual(200, response.status)
88
        self.assertEqual("null", result)
89
90
    def test_syntax_error(self):
91
        payload = """
92
            {
93
                "data": { "_arg1": [2], "_arg2": [40] },
94
                "script":
95
                "% ^ !! return Nothing"
96
            }
97
            """
98
        headers = {
99
            "Content-Type": "application/json",
100
        }
101
102
        conn = self._get_connection()
103
        conn.request("POST", "/evaluate", payload, headers)
104
        response = conn.getresponse()
105
        result = json.loads(response.read().decode("utf-8"))
106
107
        self.assertEqual(500, response.status)
108
        self.assertEqual("Error processing script", result["message"])
109
        self.assertTrue(result["info"].startswith("SyntaxError"))
110