def setUp(self): """Initiates variables to be used in most tests""" self.db = Database() self.user_data = { 'key': 1, 'first_name': 'John', 'last_name': 'Doe', 'email': '*****@*****.**', 'password': '******', } self.user = User(**self.user_data) self.db = Database() self.user.save(self.db) self.category_data = { 'key': 1, 'name': 'cakes', 'description': 'all orders cake!', 'user': self.user.key, } self.category = orderCategory(**self.category_data) self.category.save(self.db) self.order_data = { 'key': 1, 'name': 'Banana cake', 'description': 'yummy!', 'category': self.category.key } self.order = order(**self.order_data) self.order.save(self.db) self.order_step_data = { 'key': 1, 'text_content': "Don't do anything", 'order': self.order.key } self.order_step = orderStep(**self.order_step_data)
def test_save_method(self): """ The save() method should be able to update the parent category's list of orders as well as that of the database """ self.assertIsInstance(self.order, order) self.order.save(self.db) length_of_db_order_keys = len(self.db.order_keys) length_of_category_orders = len(self.category.orders) self.assertIn(self.order.key, self.db.order_keys) self.assertEqual(self.order, self.db.orders[self.order.key]) self.assertIn(self.order.key, self.category.orders) self.assertIn(self.order.name, self.db.order_name_key_map.keys()) self.assertEqual(self.order.key, self.db.order_name_key_map[self.order.name]) # the category should exist in database invalid_data = utilities.replace_value_in_dict(self.order_data, 'category', 78) new_order = order(**invalid_data) self.assertRaises(KeyError, new_order.save, self.db) # database parameter should be of type Database self.assertRaises(TypeError, self.order.save, 'string instead of Database object') # calling save more than once does not increase size of self.db.order_keys self.order.save(self.db) self.assertEqual(len(self.db.order_keys), length_of_db_order_keys) # calling save more than once does not increase size of self.category.orders self.assertEqual(len(self.category.orders), length_of_category_orders)
def setUp(self): """Initiates variables to be used in most tests""" self.db = Database() self.user_data = { 'key': 1, 'first_name': 'John', 'last_name': 'Doe', 'email': '*****@*****.**', 'password': '******', } self.user = User(**self.user_data) self.db = Database() self.user.save(self.db) self.category_data = { 'key': 1, 'name': 'Chicken – Combo', 'description': '2 Pc. Chicken – Combo-20,000Ugshs', 'user': self.user.key, } self.category = orderCategory(**self.category_data) self.category.save(self.db) self.order_data = { 'key': 1, 'name': 'Roast Pork', 'description': 'Roast Pork-30,000Ugshs', 'category': self.category.key } self.order = order(**self.order_data) self.order_step_data = { 'text_content': "Waiting for your order ....", }
def test_delete_object_for_orders(self): """delete_object should be able to remove the object passed to it from the database""" # setup self.user.save(self.db) category = orderCategory(**self.category_data) category.save(self.db) order = order(**self.order_data) order.save(self.db) # create step as child of order order_step = orderStep(**self.order_step_data) order_step.save(self.db) # delete order self.db.delete_object(order) # assert that the order step object is not in self.db.order_steps self.assertRaises(KeyError, utilities.return_value_from_dict, self.db.order_steps, order_step.key) # assert that the order object is not in self.db.orders self.assertRaises(KeyError, utilities.return_value_from_dict, self.db.orders, order.key) # assert that the order step key is not in self.db.order_step_keys self.assertNotIn(order_step.key, self.db.order_step_keys) # assert that the order key is not in self.db.order_keys self.assertNotIn(order.key, self.db.order_keys) # assert that the order name is not in self.db.order_name_key_map self.assertNotIn(order.name, self.db.order_name_key_map.keys()) # try to delete a non existent object by deleting category again self.assertRaises(KeyError, self.db.delete_object, order)
def test_delete_object_for_category(self): """delete_object should be able to remove the object passed to it from the database""" # setup self.user.save(self.db) # create category category = orderCategory(**self.category_data) # save category category.save(self.db) # create order order = order(**self.order_data) # save order as child of category due to key set in setUp order.save(self.db) # delete category ##################Test deleting categories#################### self.db.delete_object(category) # assert that the category object is not in self.db.order_categories self.assertRaises(KeyError, utilities.return_value_from_dict, self.db.order_categories, category.key) # assert that the order object is not in self.db.orders self.assertRaises(KeyError, utilities.return_value_from_dict, self.db.orders, order.key) # assert that the category key is not in self.db.order_category_keys self.assertNotIn(category.key, self.db.order_category_keys) # assert that the order key is not in self.db.order_keys self.assertNotIn(order.key, self.db.order_keys) # assert that the category name is not in self.db.order_categories_name_key_map self.assertNotIn(category.name, self.db.order_category_name_key_map.keys()) # assert that the order name is not in self.db.order_name_key_map self.assertNotIn(order.name, self.db.order_name_key_map.keys()) # try to delete a non existent object by deleting category again self.assertRaises(KeyError, self.db.delete_object, category) # try to delete an object of a type that does not exist in database self.assertRaises(TypeError, self.db.delete_object, 2)
def test_get_order(self): """A order can be retrieved by key""" # setup self.user.save(self.db) category = orderCategory(**self.category_data) category.save(self.db) order = order(**self.order_data) order.save(self.db) # try retrieving the order order_from_db = self.db.get_order(order.key) self.assertEqual(order, order_from_db) # try retrieving a non-existent order self.assertIsNone(self.db.get_order(4)) # try using a non-int key self.assertRaises(TypeError, self.db.get_order, 'string instead of int')
def neworder(): form = NewOrderForm() form.driver.choices = [(driver.driverID, driver.name) for driver in driver.query.all()] if form.validate_on_submit(): Customer = customer(name=form.custname.data, phone=form.phone.data, address=form.address.data, suburb=form.suburb.data) Order = order(orderID=form.ordernum.data, driverID=form.driver.data) Customer.orders.append(Order) db.session.add(Customer) db.session.add(Order) db.session.commit() flash('Congratulations, you have created a new order!') return redirect('/orders') return render_template('neworder.html', title='New Order', form=form)
def test_get_all_orders(self): """The get_all_orders function should be able to retrieve all orders""" names = ('Banana cake', 'fruit cake', 'icy cake') # create three orders created_orders = [] # incase a order is ever created in the Setup key = 2 # save category in db self.category.save(self.db) for name in names: new_data = utilities.replace_value_in_dict(self.order_data, 'name', name) new_order = order(**new_data, key=key, category=self.category.key) new_order.save(self.db) created_orders.append(new_order) key += 1 orders = self.category.get_all_orders(self.db) self.assertIsInstance(orders, list) self.assertEqual(len(self.category.orders), len(orders)) self.assertListEqual(created_orders, orders) self.assertRaises(TypeError, self.category.get_all_orders, 'expected Database object not string')
try: _p = goods.query.filter(goods.id.in_(session.keys())).all() except Exception, e: abort(404) #获取 商品总额 ,计算运费 total_price = 0 for i in _p: total_price += i.special_price*int(session[str(i.id)]) #配送信息 yunfei = shipping.query.all() #运费计算 jisuanyunfei = 0 for i in yunfei: if total_price<i.max_price: jisuanyunfei = i.freight map_order = order() #收件人信息 map_order.end_name = address_result.username map_order.end_phone = address_result.phone map_order.end_address = address_result.address #发货人 map_order.start_name = address_start_result.username map_order.start_phone = address_start_result.phone map_order.start_address = address_start_result.address #订单号 map_order.number = create_order_number() #下单时间 map_order.number_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") #运费 map_order.freight = jisuanyunfei