forked from ctb/cse491-drinkz
/
test_jsonrpc.py
255 lines (190 loc) · 7.19 KB
/
test_jsonrpc.py
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import sys
sys.path.insert(0, 'bin/') # allow _mypath to be loaded;
#jonesd52
import os
import ast
from drinkz import db
from drinkz import recipes
from drinkz import app
import urllib
import simplejson
from StringIO import StringIO
def db_Init():
db._reset_db()
db.add_bottle_type('Johnnie Walker', 'black label', 'blended scotch')
db.add_to_inventory('Johnnie Walker', 'black label', '500 ml')
db.add_bottle_type('Uncle Herman\'s', 'moonshine', 'blended scotch')
db.add_to_inventory('Uncle Herman\'s', 'moonshine', '5 liter')
db.add_bottle_type('Gray Goose', 'vodka', 'unflavored vodka')
db.add_to_inventory('Gray Goose', 'vodka', '1 liter')
db.add_bottle_type('Rossi', 'extra dry vermouth', 'vermouth')
db.add_to_inventory('Rossi', 'extra dry vermouth', '24 oz')
db.add_bottle_type('Freedom Walker', 'blue label', 'unblended scotch')
r = recipes.Recipe('scotch on the rocks', [('blended scotch','4 oz')])
db.add_recipe(r)
r = recipes.Recipe('vodka martini', [('unflavored vodka', '6 oz'),('vermouth', '1.5 oz')])
db.add_recipe(r)
r = recipes.Recipe('vomit inducing martini', [('orange juice','6 oz'),('vermouth','1.5 oz')])
db.add_recipe(r)
def call_rpc():
db_Init()
theApp = app.SimpleApp()
environ = {}
environ['REQUEST_METHOD'] = 'POST'
environ['PATH_INFO'] = '/rpc'
d = dict(method='hello', params=[] ,id=1)
encoded = simplejson.dumps(d)
environ['wsgi.input'] = StringIO(encoded)
environ['CONTENT_LENGTH'] = 1000
def my_start_response(s, h, return_in=d):
d['status'] = s
d['headers'] = h
results = theApp.__call__(environ,my_start_response)
text = "".join(results)
assert text.find("world") != -1, text
def test_rpc_convert_ml_to_ml():
db_Init()
theApp = app.SimpleApp()
environ = {}
environ['REQUEST_METHOD'] = 'POST'
environ['PATH_INFO'] = '/rpc'
d = dict(method='convert_units_to_ml', params=['100 ml'] ,id=1)
encoded = simplejson.dumps(d)
environ['wsgi.input'] = StringIO(encoded)
environ['CONTENT_LENGTH'] = 1000
def my_start_response(s, h, return_in=d):
d['status'] = s
d['headers'] = h
results = theApp.__call__(environ,my_start_response)
text = "".join(results)
assert text.find("100.0") != -1, text
def test_rpc_convert_gallon_to_ml():
db_Init()
theApp = app.SimpleApp()
environ = {}
environ['REQUEST_METHOD'] = 'POST'
environ['PATH_INFO'] = '/rpc'
d = dict(method='convert_units_to_ml', params=['40 gallon'] ,id=1)
encoded = simplejson.dumps(d)
environ['wsgi.input'] = StringIO(encoded)
environ['CONTENT_LENGTH'] = 1000
def my_start_response(s, h, return_in=d):
d['status'] = s
d['headers'] = h
results = theApp.__call__(environ,my_start_response)
text = "".join(results)
assert text.find("151416.4") != -1, text
def test_rpc_convert_oz_to_ml():
db_Init()
theApp = app.SimpleApp()
environ = {}
environ['REQUEST_METHOD'] = 'POST'
environ['PATH_INFO'] = '/rpc'
d = dict(method='convert_units_to_ml', params=['40 oz'] ,id=1)
encoded = simplejson.dumps(d)
environ['wsgi.input'] = StringIO(encoded)
environ['CONTENT_LENGTH'] = 1000
def my_start_response(s, h, return_in=d):
d['status'] = s
d['headers'] = h
results = theApp.__call__(environ,my_start_response)
text = "".join(results)
assert text.find("1182.94") != -1, text
def test_rpc_convert_liters_to_ml():
db_Init()
theApp = app.SimpleApp()
environ = {}
environ['REQUEST_METHOD'] = 'POST'
environ['PATH_INFO'] = '/rpc'
d = dict(method='convert_units_to_ml', params=['40 liter'] ,id=1)
encoded = simplejson.dumps(d)
environ['wsgi.input'] = StringIO(encoded)
environ['CONTENT_LENGTH'] = 1000
def my_start_response(s, h, return_in=d):
d['status'] = s
d['headers'] = h
results = theApp.__call__(environ,my_start_response)
text = "".join(results)
assert text.find("40000.0") != -1, text
def test_rpc_get_recipe_names():
db_Init()
theApp = app.SimpleApp()
environ = {}
environ['REQUEST_METHOD'] = 'POST'
environ['PATH_INFO'] = '/rpc'
d = dict(method='get_recipe_names', params=[] ,id=1)
encoded = simplejson.dumps(d)
environ['wsgi.input'] = StringIO(encoded)
environ['CONTENT_LENGTH'] = 1000
def my_start_response(s, h, return_in=d):
d['status'] = s
d['headers'] = h
results = theApp.__call__(environ,my_start_response)
text = "".join(results)
assert text.find("scotch on the rocks") != -1, text
assert text.find("vodka martini") != -1, text
assert text.find("vomit inducing martini") != -1, text
def test_rpc_get_liqour_inventory():
db_Init()
theApp = app.SimpleApp()
environ = {}
environ['REQUEST_METHOD'] = 'POST'
environ['PATH_INFO'] = '/rpc'
d = dict(method='get_liqour_inventory', params=[] ,id=1)
encoded = simplejson.dumps(d)
environ['wsgi.input'] = StringIO(encoded)
environ['CONTENT_LENGTH'] = 1000
def my_start_response(s, h, return_in=d):
d['status'] = s
d['headers'] = h
results = theApp.__call__(environ,my_start_response)
text = "".join(results)
assert text.find("Johnnie Walker\", \"black label") != -1, text
assert text.find("Uncle Herman's\", \"moonshine") != -1, text
def test_rpc_add_liqour():
db_Init()
theApp = app.SimpleApp()
environ = {}
environ['REQUEST_METHOD'] = 'POST'
environ['PATH_INFO'] = '/rpc'
d = dict(method='add_liquor', params=['Brother David', 'shinemoon', 'volka'] ,id=1)
encoded = simplejson.dumps(d)
environ['wsgi.input'] = StringIO(encoded)
environ['CONTENT_LENGTH'] = 1000
def my_start_response(s, h, return_in=d):
d['status'] = s
d['headers'] = h
results = theApp.__call__(environ,my_start_response)
assert True, results
def test_rpc_add_inventory():
db_Init()
theApp = app.SimpleApp()
environ = {}
environ['REQUEST_METHOD'] = 'POST'
environ['PATH_INFO'] = '/rpc'
d = dict(method='add_inventory', params=['Freedom Walker', 'blue label', '100 ml'] ,id=1)
encoded = simplejson.dumps(d)
environ['wsgi.input'] = StringIO(encoded)
environ['CONTENT_LENGTH'] = 1000
def my_start_response(s, h, return_in=d):
d['status'] = s
d['headers'] = h
results = theApp.__call__(environ,my_start_response)
assert True, results
def test_rpc_add_recipe():
db_Init()
theApp = app.SimpleApp()
environ = {}
environ['REQUEST_METHOD'] = 'POST'
environ['PATH_INFO'] = '/rpc'
name = "vongola pure"
ing = [('blended water','20 oz')]
d = dict(method='add_recipe', params=[name, ing] ,id=1)
encoded = simplejson.dumps(d)
environ['wsgi.input'] = StringIO(encoded)
environ['CONTENT_LENGTH'] = 1000
def my_start_response(s, h, return_in=d):
d['status'] = s
d['headers'] = h
results = theApp.__call__(environ,my_start_response)
assert True, results