def check_url_args(ref_url): PAGINATE_BY = 10 DEFAULT_PAGE = 1 RANGE_LIMIT = 25 # # Request would not work without setting a request context in this # function's local scope # # app.test_request_context/push will set context to the referring url. ### ctx = app.test_request_context(ref_url) ctx.push if request.args.get('page'): if request.args.get('page').isdigit(): page = int(request.args.get('page')) if page < 1: page = DEFAULT_PAGE else: page = 1 try: limit = int(request.args.get('limit')) if limit in range(1,RANGE_LIMIT): limit = limit else: limit = PAGINATE_BY except: limit = PAGINATE_BY return page, limit
def test_displays_only_items_for_that_list(self): correct_list = List() db_session.add(correct_list) db_session.commit() item_01 = Item(text='itemey 1', list=correct_list.id) item_02 = Item(text='itemey 2', list=correct_list.id) db_session.add(item_01) db_session.add(item_02) db_session.commit() other_list = List() db_session.add(other_list) db_session.commit() other_item_01 = Item(text='다른 목록 아이템 1', list=other_list.id) other_item_02 = Item(text='다른 목록 아이템 2', list=other_list.id) with app.test_request_context('/lists/%d' % correct_list.id): res = view_list(list_id=correct_list.id) assert item_01.text in res assert item_02.text in res assert other_item_01.text not in res assert other_item_02.text not in res Item.query.filter(Item.list == correct_list.id).delete() Item.query.filter(Item.list == other_list.id).delete() db_session.commit() List.query.filter(List.id == correct_list.id).delete() List.query.filter(List.id == other_list.id).delete() db_session.commit()
def test_redirect_nonwww(self): app.config['SERVER_NAME'] = "arsakian.com" with app.test_request_context(): with app.test_client() as c: response = c.get(url_for('index', _external=True)) self.assertEqual(response.location, "http://www.arsakian.com/") response = c.get(url_for('archives', _external=True)) self.assertEqual(response.location, "http://www.arsakian.com/archives") app.config['SERVER_NAME'] = None
def test_welcome_page_returns_correct_html(self): with app.test_request_context('/', method='GET'): res = index() # type 'str', rendered html expected_html = render_template( 'home.html') # type 'str', rendered html assert res.startswith('<!DOCTYPE html>') assert '<title>To-Do lists</title>' in res assert res.endswith('</html>') # table에 데이터베이스의 값이 들어오면서 index()의 반환값과 index.html의 값이 달라지게 됩니다. assert res == expected_html
def test_redirect_nonwww(self): app.config['SERVER_NAME'] = "arsakian.com" with app.test_request_context(): with app.test_client() as c: response = c.get(url_for('index', _external=True)) self.assertEqual(response.location, "http://www.arsakian.com/") response = c.get(url_for('archives', _external=True)) self.assertEqual(response.location, "http://www.arsakian.com/archives") app.config['SERVER_NAME'] = None # def test_upload(self): # output = StringIO() # output.write('hello there') # # response = self.client.post(url_for('upload'), buffered=True, # content_type='multipart/form-data', # data={'file_field': (output, 'hello there')}) # # # self.assertEqual('ok',response.data)
#!/usr/bin/env python from blog import app from blog.search import update_documentation_index with app.test_request_context(): update_documentation_index()
def test_root_url_resolves_to_welcome_page_view(self): with app.test_request_context('/', method='GET'): module = index.__module__.split('.')[ 1] # index의 모듈 blog.base.views func = index.__name__ # index assert request.endpoint in module + '.' + func