-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_incentives.py
More file actions
106 lines (86 loc) · 4.03 KB
/
test_incentives.py
File metadata and controls
106 lines (86 loc) · 4.03 KB
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
# -*- coding: utf-8 -*-
import json
import unittest
from datetime import datetime
from syncano.exceptions import SyncanoValidationError
from syncano.models import ResponseTemplate, Script, ScriptEndpoint, ScriptEndpointTrace, ScriptTrace
try:
from unittest import mock
except ImportError:
import mock
class ScriptTestCase(unittest.TestCase):
def setUp(self):
self.model = Script()
@mock.patch('syncano.models.Script._get_connection')
def test_run(self, connection_mock):
model = Script(instance_name='test', id=10, links={'run': '/v1.1/instances/test/snippets/scripts/10/run/'})
connection_mock.return_value = connection_mock
connection_mock.request.return_value = {'id': 10}
self.assertFalse(connection_mock.called)
self.assertFalse(connection_mock.request.called)
result = model.run(a=1, b=2)
self.assertTrue(connection_mock.called)
self.assertTrue(connection_mock.request.called)
self.assertIsInstance(result, ScriptTrace)
connection_mock.assert_called_once_with(a=1, b=2)
call_args = connection_mock.request.call_args[0]
call_kwargs = connection_mock.request.call_args[1]
call_kwargs['data']['payload'] = json.loads(call_kwargs['data']['payload'])
self.assertEqual(('POST', '/v1.1/instances/test/snippets/scripts/10/run/'), call_args)
self.assertDictEqual(call_kwargs['data'], {'payload': {"a": 1, "b": 2}})
model = Script()
with self.assertRaises(SyncanoValidationError):
model.run()
class ScriptEndpointTestCase(unittest.TestCase):
def setUp(self):
self.model = ScriptEndpoint()
@mock.patch('syncano.models.ScriptEndpoint._get_connection')
def test_run(self, connection_mock):
model = ScriptEndpoint(instance_name='test', name='name',
links={'run': '/v1.1/instances/test/endpoints/scripts/name/run/'})
connection_mock.return_value = connection_mock
connection_mock.request.return_value = {
'status': 'success',
'duration': 937,
'result': {'stdout': 1, 'stderr': ''},
'executed_at': '2015-03-16T11:52:14.172830Z'
}
self.assertFalse(connection_mock.called)
self.assertFalse(connection_mock.request.called)
result = model.run(x=1, y=2)
self.assertTrue(connection_mock.called)
self.assertTrue(connection_mock.request.called)
self.assertIsInstance(result, ScriptEndpointTrace)
self.assertEqual(result.status, 'success')
self.assertEqual(result.duration, 937)
self.assertEqual(result.result, {'stdout': 1, 'stderr': ''})
self.assertIsInstance(result.executed_at, datetime)
connection_mock.assert_called_once_with(x=1, y=2)
connection_mock.request.assert_called_once_with(
'POST',
'/v1.1/instances/test/endpoints/scripts/name/run/',
data={"y": 2, "x": 1}
)
model = ScriptEndpoint()
with self.assertRaises(SyncanoValidationError):
model.run()
class ResponseTemplateTestCase(unittest.TestCase):
def setUp(self):
self.model = ResponseTemplate
@mock.patch('syncano.models.ResponseTemplate._get_connection')
def test_render(self, connection_mock):
model = self.model(instance_name='test', name='name',
links={'run': '/v1.1/instances/test/snippets/templates/name/render/'})
connection_mock.return_value = connection_mock
connection_mock.request.return_value = '<div>12345</div>'
self.assertFalse(connection_mock.called)
self.assertFalse(connection_mock.request.called)
response = model.render()
self.assertTrue(connection_mock.called)
self.assertTrue(connection_mock.request.called)
self.assertEqual(response, '<div>12345</div>')
connection_mock.request.assert_called_once_with(
'POST',
'/v1.1/instances/test/snippets/templates/name/render/',
data={'context': {}}
)