def test_addInstrumentToCraft_page(self): with self.app.app_context(): testuser1 = User('test_add_craft_user_1', 'testpassword') instrument1_id = self.generate_random_instrument_id() instrument1 = Instrument(instrument1_id, '测试商品1', 100, 200, '测试描述', 100, 'www.test.com') instrument1.saveToDb() instrument2_id = self.generate_random_instrument_id() instrument2 = Instrument(instrument2_id, '测试商品2', 100, 200, '测试描述', 100, 'www.test.com') instrument2.saveToDb() # 测试能否正确加入购物车 c = self.app.test_client() with self.app.app_context(): c.post('/login', data=dict(username='******', password='******')) url = '/addInstrumentToCraft/' + str(instrument1_id) response = c.get(url, follow_redirects=True) assert '成功添加至购物车'.encode('utf-8') in response.data url = '/addInstrumentToCraft/' + str(instrument2_id) response = c.get(url, follow_redirects=True) assert '成功添加至购物车'.encode('utf-8') in response.data response = c.get('/addInstrumentToCraft/999', follow_redirects=True) assert '404'.encode('utf-8') in response.data self.curr.execute('SELECT id FROM shopping_craft WHERE user_id=%s', testuser1.getId()) craft_id = self.curr.fetchone()[0] self.curr.execute( 'SELECT instrument_id FROM shoppingcraft_instrument WHERE shoppingcraft_id=%s', craft_id) instrumens_id = [id[0] for id in self.curr.fetchall()] assert instrument1_id in instrumens_id assert instrument2_id in instrumens_id
def add_instrument(): """Renders the page for adding an instrument, as well as handling the addition of an instrument into the database.""" error = False status = None form = AddInstrumentForm() if form.validate_on_submit(): try: name = form.name.data info = form.info.data range = form.range.data image = form.image.data instrument = Instrument(name=name, info=info, range=range, image=image) db.session.add(instrument) db.session.commit() flash("Instrument added successfully.") except exc.IntegrityError as err: error = True print('/admin/instrument/:', err) flash( "Something went wrong with adding the instrument. Please try again." ) except Exception as err: error = True print("Unexpected error:", err) flash( "Something went wrong with adding the instrument. Please try again." ) return render_template('admin_instrument.html', title="Admin", error=error, form=form)
demo_profile_4.user = demo_4 demo_profile_5.user = demo_5 demo_profile_6.user = demo_6 demo_profile_7.user = demo_7 db.session.add(demo) db.session.add(demo_2) db.session.add(demo_3) db.session.add(demo_4) db.session.add(demo_5) db.session.add(demo_6) db.session.add(demo_7) db.session.commit() # Instrument / Style seed data alto_sax = Instrument(name="Alto Saxophone") tenor_sax = Instrument(name="Tenor Saxophone") drum_kit = Instrument(name="Drum Kit") e_bass = Instrument(name="Electric Bass") keyboard = Instrument(name="Keyboard / Piano") e_guitar = Instrument(name="Electric Guitar") tenor_sax = Instrument(name="Tenor Sax") vocals_and_rap = Instrument(name="Vocals / Rap") db.session.add(tenor_sax) db.session.add(e_bass) db.session.add(drum_kit) jazz = Style(name="Jazz") funk = Style(name="Funk")
def test_orderDetail_page(self): with self.app.app_context(): testuser1 = User('orderDetail_testuser_1', 'testpassword', id=66) instrument1 = Instrument(self.generate_random_instrument_id(), '测试乐器1', 100, 200, '测试描述1', 300, 'imagepath') instrument1.saveToDb() instrument2 = Instrument(self.generate_random_instrument_id(), '测试乐器2', 200, 200, '测试描述2', 300, 'imagepath') instrument2.saveToDb() instrument3_id = self.generate_random_instrument_id() instrument3 = Instrument(instrument3_id, '测试乐器3', 300, 200, '测试描述3', 300, 'imagepath') instrument3.saveToDb() instruments = [instrument1, instrument3] testuser1.addInstrumentToShoppingCraft(instruments) orderId = testuser1.payAllShoppingCraft() Instrument.removeFromDb(instrument3_id) now = datetime.now() c = self.app.test_client() with self.app.app_context(): c.post('/adminLogin', data=dict(username=environ.get('weborder_admin_username'), password=environ.get('weborder_admin_password'))) orderDetail_url = '/orderDetail/' + str(orderId) response = c.get(orderDetail_url) assert str(orderId).encode('utf-8') in response.data assert str(now.year).encode('utf-8') in response.data assert str(now.day).encode('utf-8') in response.data assert 'orderDetail_testuser_1'.encode('utf-8') in response.data assert '400'.encode('utf-8') in response.data assert '测试乐器1'.encode('utf-8') in response.data assert (str(instrument3_id) + '(此商品已下架)').encode('utf-8') in response.data assert '测试乐器2'.encode('utf-8') not in response.data self.curr.execute('SET FOREIGN_KEY_CHECKS = 0;') self.curr.execute('truncate table user;') self.curr.execute('truncate table instrument;') self.curr.execute('truncate table `order`;') self.curr.execute('truncate table shopping_craft;') self.curr.execute('truncate table shoppingcraft_instrument;') self.curr.execute('SET FOREIGN_KEY_CHECKS = 1;')
def test_allOrder_page(self): with self.app.app_context(): testuser1 = User('historyOrder_testuser_1', 'testpassword', id=66) testuser2 = User('historyOrder_testuser_2', 'testpassword', id=666) instrument1 = Instrument(self.generate_random_instrument_id(), '测试乐器1', 100, 200, '测试描述1', 300, 'imagepath') instrument1.saveToDb() instrument2 = Instrument(self.generate_random_instrument_id(), '测试乐器2', 200, 200, '测试描述2', 300, 'imagepath') instrument2.saveToDb() instrument3 = Instrument(self.generate_random_instrument_id(), '测试乐器3', 300, 200, '测试描述3', 300, 'imagepath') instrument3.saveToDb() instruments = [instrument1, instrument2] testuser1.addInstrumentToShoppingCraft(instruments) order1_id = testuser1.payAllShoppingCraft() instruments = [instrument1, instrument3] testuser2.addInstrumentToShoppingCraft(instruments) order2_id = testuser2.payAllShoppingCraft() self.curr.execute("select totalprice from `order` where id=%s", order1_id) order1_totalprice = self.curr.fetchone()[0] self.curr.execute("select totalprice from `order` where id=%s", order2_id) order2_totalprice = self.curr.fetchone()[0] # 测试能否正确显示所有订单 c = self.app.test_client() with self.app.app_context(): c.post('/adminLogin', data=dict(username=environ.get('weborder_admin_username'), password=environ.get('weborder_admin_password'))) response = c.get('/allOrder') assert 'historyOrder_testuser_1'.encode('utf-8') in response.data and \ 'historyOrder_testuser_2'.encode('utf-8') in response.data and \ str(order1_id).encode('utf-8') in response.data and \ str(order2_id).encode('utf-8') in response.data and \ '300'.encode('utf-8') in response.data and \ '400'.encode('utf-8') in response.data self.curr.execute('SET FOREIGN_KEY_CHECKS = 0;') self.curr.execute('truncate table user;') self.curr.execute('truncate table instrument;') self.curr.execute('truncate table `order`;') self.curr.execute('truncate table shopping_craft;') self.curr.execute('truncate table shoppingcraft_instrument;') self.curr.execute('SET FOREIGN_KEY_CHECKS = 1;')
def test_instrumentEdit_page(self): with self.app.app_context(): testInstrument1 = Instrument(111, "测试乐器111", 100, 200, "测试描述", 10, "测试图片") testInstrument1.saveToDb() testInstrument2 = Instrument(222, "测试乐器222", 100, 200, "测试描述", 10, "测试图片") testInstrument2.saveToDb() # 测试在没有管理员权限时访问该页面 c = self.app.test_client() with self.app.app_context(): response = c.get('/instrumentEdit/111', follow_redirects=True) assert '管理员登录'.encode('utf-8') in response.data # 测试有管理员权限时访问不存在的商品页面 c = self.app.test_client() with self.app.app_context(): c.post('/adminLogin', data=dict(username=environ.get('weborder_admin_username'), password=environ.get('weborder_admin_password'))) response = c.get('/instrumentEdit/100') assert '404'.encode('utf-8') in response.data # 测试有管理员权限时访问存在的商品页面 c = self.app.test_client() with self.app.app_context(): c.post('/adminLogin', data=dict(username=environ.get('weborder_admin_username'), password=environ.get('weborder_admin_password'))) response = c.get('/instrumentEdit/111') assert '乐器信息编辑'.encode('utf-8') in response.data # 测试想要编辑的乐器是否能正确的在页面上显示 c = self.app.test_client() with self.app.app_context(): c.post('/adminLogin', data=dict(username=environ.get('weborder_admin_username'), password=environ.get('weborder_admin_password'))) response = c.get('/instrumentEdit/111') assert '测试乐器111'.encode('utf-8') in response.data response = c.get('/instrumentEdit/222') assert '测试乐器222'.encode('utf-8') in response.data # 测试能否正确修改乐器信息 c = self.app.test_client() with self.app.app_context(): c.post('/adminLogin', data=dict(username=environ.get('weborder_admin_username'), password=environ.get('weborder_admin_password'))) response = c.post('/instrumentEdit/111', data=dict(name='测试乐器333', price=100, weight=200, description='测试描述', transport_cost=300, image='测试图片'), follow_redirects=True) self.curr.execute('SELECT name FROM instrument WHERE id = 111') instrument_edited_name = self.curr.fetchone()[0] assert instrument_edited_name == '测试乐器333' and '后台管理'.encode( 'utf-8') in response.data # 测试能否访问已被删除的商品编辑页面 c = self.app.test_client() with self.app.app_context(): Instrument.removeFromDb(222) c.post('/adminLogin', data=dict(username=environ.get('weborder_admin_username'), password=environ.get('weborder_admin_password'))) response = c.get('/instrumentEdit/222', follow_redirects=True) assert '404'.encode('utf-8') in response.data self.curr.execute('SET FOREIGN_KEY_CHECKS = 0;') self.curr.execute('truncate table instrument;') self.curr.execute('SET FOREIGN_KEY_CHECKS = 1;')