def test_rpc_add_to_inventory(): db._reset_db() db.add_bottle_type("Marco Botros","vodka","like the moon") #db.load_db('Database') #making an empty environ dictionary environ = {} environ['PATH_INFO'] = '/rpc' d = dict(method= 'add_to_inventory',params=[("Marco Botros","vodka","3 oz")], id=1) encoded = simplejson.dumps(d) environ['wsgi.input'] = StringIO(encoded) environ['CONTENT_LENGTH'] = len(encoded) environ['REQUEST_METHOD'] = 'POST' #making a start_response function d = {} def my_start_response(s, h, return_in=d): d['status'] = s d['headers'] = h app_obj = app.SimpleApp() results = app_obj(environ, my_start_response) text = "".join(results) status, headers = d['status'], d['headers'] assert db.check_inventory('Marco Botros', 'vodka'),db._inventory_db assert ('Content-Type', 'application/json') in headers assert status == '200 OK'
def recv2(self, environ, start_response): formdata = environ['QUERY_STRING'] results = urlparse.parse_qs(formdata) liquorMfg = results['liquorMfg'][0] liquorName = results['liquorName'][0] liquorType = results['liquorType'][0] db.add_bottle_type(liquorMfg, liquorName, liquorType) content_type = 'text/html' data = """ <!DOCTYPE HTML> <html> <head> <title>Liquor Type Added</title> <style type='text/css'> h1 {text-decoration:underline; text-align:center; color:red;} body { font-size:14px; } </style> </head> <body> <h1>Liquor Type Successfully Added!</h1><br/> <a href='./'>Return to index</a> <p> """ start_response('200 OK', list(html_headers)) return [data]
def test_rpc_add_to_inventory(): db._reset_db() db.add_bottle_type("Abe Lincoln","Freedom","punch") environ = {} environ['PATH_INFO'] = '/rpc' d = dict(method= 'add_to_inventory',params=[("Abe Lincoln","Freedom","3 oz")], id=1) encoded = simplejson.dumps(d) environ['wsgi.input'] = StringIO(encoded) environ['CONTENT_LENGTH'] = len(encoded) environ['REQUEST_METHOD'] = 'POST' #making a start_response function d = {} def my_start_response(s, h, return_in=d): d['status'] = s d['headers'] = h app_obj = app.SimpleApp() results = app_obj(environ, my_start_response) text = "".join(results) status, headers = d['status'], d['headers'] assert db.check_inventory('Abe Lincoln', 'Freedom'),db._inventory_db assert ('Content-Type', 'application/json') in headers assert status == '200 OK'
def add_liquor_type_recv(self, environ, start_response): formdata = environ['QUERY_STRING'] results = urlparse.parse_qs(formdata) mfg = results['mfg'][0] liquor = results['liquor'][0] typ = results['typ'][0] db.add_bottle_type(mfg, liquor, typ) db.save_db('bin/sample_database') taste_of_success = db._check_bottle_type_exists(mfg, liquor) if taste_of_success == True: data = generate_html.generate_liquor_types_html() else: content_type = 'text/html' data = """ <html> <head> <title>Failure to Add Liquor!</title> <style type ="text/css"> h1{color:red;} </style> </head> <body>""" data += """Failed to add Liquor type, please try again!""" data += generate_html.generate_menu() data += """ </body> </html> """ start_response('200 OK', list(html_headers)) return [data]
def recv_addliqtyp(self,environ,start_response): formdata = environ['QUERY_STRING'] results = urlparse.parse_qs(formdata) mfg = results['mfg'][0] liquor = results['liquor'][0] typ = results['typ'][0] #print "FOMRDATA - mfg: %s Liq: %s Type: %s" % (mfg,liquor,typ) try: db.add_bottle_type(mfg,liquor,typ) vars = dict(title='Alll Right! Moar Booze!', content="Added %s, %s, %s to bottle type inventory" % (mfg, liquor, typ)) data = JinjaLoader('blank.html',vars) start_response('200 OK',html_headers) return data except Exception, e: error_message = "An error occured adding %s, %s, %s to bottle type inventory" % (mfg, liquor, typ) data = error(error_message + "<br/>" + e.message) start_response('500 INTERNAL SERVER ERROR', list(html_headers)) return data
def test_get_mixable_recipes(): db._reset_db() db.add_bottle_type('Johnnie Walker', 'Black Label', 'blended scotch') db.add_to_inventory('Johnnie Walker', 'Black Label', '1000 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('Meijer', 'pulp free orange juice', 'orange juice') db.add_to_inventory('Meijer', 'pulp free orange juice', '24 oz') r = recipes.Recipe('scotch on the rocks', [('blended scotch', '4 oz')]) db.add_recipe(r) r = recipes.Recipe('vodka gimlit', [('unflavored vodka', '4 oz'), ('lime juice', '1 oz')]) db.add_recipe(r) r = recipes.Recipe('vomit inducing martini', [('orange juice', '6 oz'), ('vermouth', '1.5 oz')]) db.add_recipe(r) mixable_recs = db.get_mixable_recipes() for rec in mixable_recs: print rec.name
def test_add_bottle_type_1(): print 'Note that output from failing tests is printed out!' db._reset_db() db.add_bottle_type('Johnnie Walker', 'Black Label', 'blended scotch') assert db._check_bottle_type_exists('Johnnie Walker', 'Black Label')
def recv_liquor_types(self, environ, start_response): formdata = environ['QUERY_STRING'] results = urlparse.parse_qs(formdata) mfg = results['manufacturer'][0] liquor = results['liquor'][0] type = results['type'][0] db.add_bottle_type(mfg, liquor, type) content_type = 'text/html' data = """\ Added Liquor Type. <p> """ data += "Manufacturer: %s" % mfg data += "<p>" data += "Liquor: %s" % liquor data += "<p>" data += "Type: %s" % type data += """ <p> <a href='./'>return to index</a> """ start_response('200 OK', list(html_headers)) return [data]
def test_get_liquor_amount_1(): db._reset_db() db.add_bottle_type('Johnnie Walker', 'Black Label', 'blended scotch') db.add_to_inventory('Johnnie Walker', 'Black Label', '1000 ml') amount = db.get_liquor_amount('Johnnie Walker', 'Black Label') assert amount == 1000.0, amount
def addbottletype(self, environ, start_response): formdata = environ['QUERY_STRING'] results = urlparse.parse_qs(formdata) try: mfg = results['mfg'][0] liquor = results['liquor'][0] type = results['type'][0] type = type.strip() mfg = mfg.strip() liquor = liquor.strip() if not db._check_bottle_type_exists(mfg, liquor): db.add_bottle_type(mfg,liquor,type) addin = "<p>Succesfully added.</p>" else: addin= "<p> This bottle type already exists.</p>" except (AssertionError, KeyError, IndexError) : addin = """\ <p> Incorrect format or incomplete. Please try again.</p> """ vars = dict(form=addin) template = env.get_template("add.html") data = template.render(vars) content_type = 'text/html' start_response('200 OK', list(html_headers)) return [str(data)]
def type_add(self, environ, start_response): formdata = environ['QUERY_STRING'] results = urlparse.parse_qs(formdata) mfg = (results['mfg'][0]) liq = (results['liquor_name'][0]) typ = (results['liquor_type'][0]) db.add_bottle_type(mfg, liq, typ) content_type = 'text/html' data = "Bottle Type Entered: " + mfg+' :' + liq +' :'+ typ data = data + """</br> </br> <h2> Menu </h2> <a href='/'>Home</a></br> <a href='recipes'>Recipes</a></br> <a href='inventory'>Inventory</a></br> <a href='liquor_types'>Liquor Types</a></br> <a href='convert_to_ml'>Convert Amount</a></br> <a href='recipe_input'>Add Recipe</a></br> <a href='type_form'>Add Bottle Type</a></br> <a href='inv_form'>Add to Inventory</a></br> """ data = self.buildHtmlPage("Added Bottle Type!","",data) start_response('200 OK', list(html_headers)) return [data]
def create_data(filename): try: db.load_db(filename) except Exception: db._reset_db() db.add_bottle_type('Johnnie Walker', 'black label', 'blended scotch') db.add_to_inventory('Johnnie Walker', 'black label', '1 gallon') 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('Jose Cuervo', 'Silver', 'tequila') db.add_to_inventory('Jose Cuervo', 'Silver', '1 liter') 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) r = recipes.Recipe('whiskey bath', [('blended scotch', '2 liter')]) db.add_recipe(r)
def rpc_add_bottle_type(self,mfg,liquor,typ): returnVal = False try: db.add_bottle_type(mfg, liquor, typ) returnVal = True; except Exception: returnVal = False return returnVal
def test_rpc_addtype_2(): db._reset_db() db.add_bottle_type('Johnnie Walker', 'black label', 'blended scotch') text = call_remote(jsonrpc='1.0', method='addtype', params = ["Johnnie Walker", "black label", "blended scotch"], id='1') rpc_request = simplejson.loads(text) result = rpc_request['result'] print result assert "This bottle type already exists." in result
def load_bottle_types(fp): new_reader = data_reader(fp) #1 n = 0 #2 for mfg, name, typ in new_reader: #3 n += 1 #3,a db.add_bottle_type(mfg, name, typ) #3,b return n #4
def rpc_get_liquor_inventory(self): db.add_bottle_type('Johnnie Walker', 'Black Label', 'blended scotch') db.add_to_inventory('Johnnie Walker', 'Black Label', '1000 ml') inv = [] for mfg,lqr in db.get_liquor_inventory(): inv.append((mfg,lqr)) return inv
def test_bulk_load_inventory_3(): db._reset_db() db.add_bottle_type('Johnnie Walker', 'Black Label', 'blended scotch') data = "Johnnie Walker,Black Label,1000 ml\na,b\n" fp = StringIO(data) # make this look like a file handle n = load_bulk_data.load_inventory(fp) assert db.check_inventory('Johnnie Walker', 'Black Label') assert n == 1, n
def test_get_liquor_amount_3(): db._reset_db() db.add_bottle_type('Johnnie Walker', 'Black Label', 'blended scotch') db.add_to_inventory('Johnnie Walker', 'Black Label', '1000 ml') db.add_to_inventory('Johnnie Walker', 'Black Label', '750 g') db.add_to_inventory('Johnnie Walker', 'Black Label', '250 ml') amount = db.get_liquor_amount('Johnnie Walker', 'Black Label') print "\n%s" % amount
def test_get_liquor_inventory(): db._reset_db() db.add_bottle_type('Johnnie Walker', 'Black Label', 'blended scotch') db.add_to_inventory('Johnnie Walker', 'Black Label', '1000 ml') x = [] for mfg, liquor in db.get_liquor_inventory(): x.append((mfg, liquor)) assert x == [('Johnnie Walker', 'Black Label')], x
def test_get_liquor_amount_2(): db._reset_db() db.add_bottle_type('Johnnie Walker', 'Black Label', 'blended scotch') data = "Johnnie Walker,Black Label,1000 ml" fp = StringIO(data) # make this look like a file handle n = load_bulk_data.load_inventory(fp) amount = db.get_liquor_amount('Johnnie Walker', 'Black Label') assert amount == 1000.0, amount
def test_json_get_liquor_inventory(): 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('Gray Goose', 'vodka', 'unflavored vodka') db.add_to_inventory('Gray Goose', 'vodka', '1 liter') results = call_remote(method='get_liquor_inventory', params=[], id=1) assert ['Johnnie Walker', 'black label'] in results['result'], results['result']
def recv_add_liquor_types(self, environ, start_response): formdata = environ['QUERY_STRING'] results = urlparse.parse_qs(formdata) msg = "No values were modified" #Check if values have data if ( ('mfg' in results) and ('liquor' in results) and ('typ' in results)): #Get manufacturer mfg = results['mfg'][0] #Get liquor name liquor = results['liquor'][0] #Get type typ = results['typ'][0] if (db._check_bottle_type_exists(mfg,liquor)): message = "Ooops Manufacturer and Liquor information was already there" else: #Add bottle type db.add_bottle_type(mfg,liquor,typ) dynamic_web.save_database('/../bin/drinkz_database') message = "Liquor type has been added successfully" msg = "Liquor type has been added" #At least one of the fields is empty else: message = "Ooops at least one of the fields was empty" #Generate results in html format content_type = 'text/html' data= """ <html> <head> <title>Updated Liquor Type</title> <style type='text/css'> h1 {color:red;} body{ font-size:14px; } </style> </head> <body> """ data = data + "<h1>" + message + "</h1>" data = data + msg tmp = dynamic_web.generate_liquor_type_table() data = data + tmp data = data + "<p><a href='./add_liquor_types.html'>add another liquor type</a></p>" data = data + "<p><a href='./'>return to index</a></p>" data = data + """ </body> <html> """ start_response('200 OK', list(html_headers)) return [data]
def initialize_db(): 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') r = recipes.Recipe('scotch on the rocks', [('blended scotch','4 oz')]) db.add_recipe(r) r = recipes.Recipe('vodka martini', [('unflavored vodka', '7 oz'),('vermouth', '1.5 oz')]) db.add_recipe(r) r = recipes.Recipe('kunamatata', [('orange juice', '6 oz'),('vermouth', '1.5 oz')]) db.add_recipe(r)
def addType(self, environ, start_response): formdata = environ['QUERY_STRING'] results = urlparse.parse_qs(formdata) mfg = results['mfg'][0] liquor = results['liquor'][0] typ = results['typ'][0] db.add_bottle_type(mfg, liquor, typ) content_type = 'text/html' data = liqourTypesList(); start_response('200 OK', list(html_headers)) return [data]
def add_items(): #Reset database 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') #Add recipes 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) r = recipes.Recipe('whiskey bath', [('blended scotch', '2 liter')]) db.add_recipe(r)
def liquor_types_add(self, environ, start_response): formdata = environ['QUERY_STRING'] results = urlparse.parse_qs(formdata) mfg = results['mfg'][0] liquor = results['liquor'][0] typ = results['typ'][0] db.add_bottle_type(mfg, liquor, typ) headers = list(html_headers) headers.append(('Location', '/liquor_types')) start_response('302 Found', headers) return ["Redirect to /recipes..."]
def test_getinventory(): db._reset_db() #Reset database 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') text = call_remote(jsonrpc='1.0', method='get_liquor_inventory', params = [], id='1') rpc_request = simplejson.loads(text) print rpc_request result = rpc_request['result'] assert result[0][0] == 'Johnnie Walker' assert result[0][1] == 'black label' assert result[2][1] == 'moonshine' assert result[3][0] == 'Gray Goose' assert result[1][1] == 'extra dry vermouth'
def test_rpc_addinventory_2(): db._reset_db() db.add_bottle_type('Johnnie Walker', 'black label', 'blended scotch') text = call_remote(jsonrpc='1.0', method='addinventory', params = ["Johnnie Walker", "black label", "25 gallon"], id='1') rpc_request = simplejson.loads(text) #print rpc_request result = rpc_request['result'] print result check = db.check_inventory("Johnnie Walker", "black label") assert "Succesfully added." in result assert check
def test_get_liquor_amount_5(): db._reset_db() db.add_bottle_type('Johnnie Walker', 'Black Label', 'blended scotch') data = "Johnnie Walker,Black Label,1000 ml\nJohnnie Walker,Black Label,50 gallons" fp = StringIO(data) # make this look like a file handle n = load_bulk_data.load_inventory(fp) try: amount = db.get_liquor_amount('Johnnie Walker', 'Black Label') assert 0 except: pass # we expect to see an error about gallons
def test_json_liquor_inventory(): 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') liquor_inventory = call_remote(method='get_liquor_inventory', params = [], id='1') rpc_request = simplejson.loads(liquor_inventory) result = rpc_request['result'] print result assert ['Johnnie Walker', 'black label'] in result assert ['Rossi', 'extra dry vermouth'] in result assert ["Uncle Herman's", 'moonshine'] in result assert ['Gray Goose', 'vodka'] in result