class PizzaTests(unittest.TestCase): def setUp(self): self.pizza = Pizza() def test_take_order(self): self.pizza.take("take rado 5.0") self.assertEqual(self.pizza.order, {"rado": 5.0}) self.pizza.take("take toni 6") self.assertEqual(self.pizza.order, {"rado": 5.0, "toni": 6.0}) def test_save_order(self): self.pizza.take("take rado 5") self.pizza.save() self.assertEqual(self.pizza.loadOrder(self.pizza.orderNames[-1]), {"rado": 5.0}) remove(self.pizza.orderNames[-1]) def test_load_order(self): self.pizza.take("take rado 5") self.pizza.save() self.pizza.take("take toni 60") self.pizza.isListCalled = True # in order to bypass user input self.pizza.isLastOrderSaved = True # in order to bypass user input self.pizza.load("load 1") self.assertEqual(self.pizza.order, {"rado": 5.0}) remove(self.pizza.orderNames[-1])
def get_pizza(self, id): try: id = int(id) except ValueError: return None raw = self.db.pizzas.find_one({'id': int(id)}, {'_id': 0}) if raw is None: return None pizza = Pizza.load(raw) return pizza
def order_price(): raw_pizza = request.json.get('pizza') raw_extras = request.json.get('extras') pizza = Pizza.load(raw_pizza) extras = [Extra.load(e) for e in raw_extras] order = Order(pizza=pizza, extras=extras) price = order.get_total_price() return Response(json.dumps({"price": price}), status=200, mimetype='application/json')
def register_order(): raw_pizza = request.json.get('pizza') raw_extras = request.json.get('extras') name = request.json.get('name') address = request.json.get('address') if raw_pizza is None: return Response(json.dumps({'error': '"pizza" field missing'}), status=400, mimetype='application/json') if raw_extras is None: return Response(json.dumps({'error': '"extras" field missing'}), status=400, mimetype='application/json') if name is None: return Response(json.dumps({'error': '"name" field missing'}), status=400, mimetype='application/json') if address is None: return Response(json.dumps({'error': '"address" field missing'}), status=400, mimetype='application/json') try: pizza = Pizza.load(raw_pizza) except KeyError: return Response(json.dumps( {'error': 'Keyerror: Check "pizza" attributes'}), status=400, mimetype='application/json') try: extras = [Extra.load(e) for e in raw_extras] except KeyError: return Response(json.dumps( {'error': 'Keyerror: Check "extras" attributes'}), status=400, mimetype='application/json') order = Order(pizza=pizza, extras=extras, name=name, address=address) db.insert_order(order) return Response(json.dumps(order.serialize()), status=200, mimetype='application/json')
def get_pizzas(self): raw_pizzas = self.__get_all_items('pizzas') pizzas = [Pizza.load(p) for p in raw_pizzas] return pizzas