def list(self, cur_p=''): ''' List the replies. ''' if cur_p == '': current_page_number = 1 else: current_page_number = int(cur_p) current_page_number = 1 if current_page_number < 1 else current_page_number pager_num = int(MReply.total_number() / CMS_CFG['list_num']) kwd = { 'pager': '', 'current_page': current_page_number, 'title': '单页列表', 'num_of_reply': MReply.total_number() } self.render( 'admin/reply_ajax/reply_list.html', kwd=kwd, view_all=MReply.query_all(), infos=MReply.query_pager(current_page_num=current_page_number), userinfo=self.userinfo)
def list(self): ''' List the replies. ''' kwd = {'pager': '', 'title': '单页列表'} self.render('admin/reply_ajax/reply_list.html', kwd=kwd, view_all=MReply.query_all(), userinfo=self.userinfo)
def test_query_all(self): self.add_message() aa=MReply.query_all() tf = False for i in aa: if i.uid == self.reply_uid: tf = True self.tearDown() assert tf
def list(self): kwd = { 'pager': '', 'unescape': tornado.escape.xhtml_unescape, 'title': '单页列表' } self.render('admin/reply_ajax/reply_list.html', kwd=kwd, view_all=MReply.query_all(), userinfo=self.userinfo)
def test_update_vote(self): self.add_message() MReply.update_vote(self.reply_uid, 8) aa = MReply.query_all() tf = False for i in aa: if i.uid == self.reply_uid: tf = True assert i.vote==8 self.tearDown() assert tf
def test_delete_by_uid(self): self.add_message() aa = MReply.query_all() tf = False for i in aa: if i.uid == self.reply_uid: tf = True assert tf MReply.delete_by_uid(self.reply_uid) aa = MReply.get_by_uid(self.reply_uid) assert aa == None self.tearDown()
def test_query_all(self): MReply.query_all() assert True
class TestMReply(): def setup(self): print('setup 方法执行于本类中每条用例之前') self.post = MPost() self.user = MUser() self.reply = MReply() self.r2u = MReply2User() self.post_title = 'fwwgccc' self.username = '******' self.user_uid = '' self.reply_uid = '' self.post_uid = '998h' self.password = '******' def add_user(self, **kwargs): name = kwargs.get('user_name', self.username) post_data = { 'user_name': name, 'user_pass': kwargs.get('user_pass', self.password), 'user_email': kwargs.get('user_email', '*****@*****.**'), } self.user.create_user(post_data) aa = self.user.get_by_name(name) self.user_uid = aa.uid def add_post(self, **kwargs): p_d = { 'title': kwargs.get('title', self.post_title), 'cnt_md': kwargs.get('cnt_md', 'grgr'), 'time_create': kwargs.get('time_create', '1992'), 'time_update': kwargs.get('time_update', '1996070600'), 'user_name': self.username, 'view_count': kwargs.get('view_count', 1), 'logo': kwargs.get('logo', 'prprprprpr'), 'memo': kwargs.get('memo', ''), 'order': kwargs.get('order', '1'), 'keywords': kwargs.get('keywords', 'sd,as'), 'extinfo': kwargs.get('extinfo', {}), 'kind': kwargs.get('kind', '1'), 'valid': kwargs.get('valid', 1), } MPost.create_post(self.post_uid, p_d) def test_insert_post(self): # raw_count = self.post.get_counts() self.add_post() tt = self.post.get_by_uid(self.post_uid) assert tt.title == self.post_title self.tearDown() def test_insert_user(self): self.add_user() tt = self.user.get_by_uid(self.user_uid) assert tt.user_name == self.username self.tearDown() def add_reply(self, **kwargs): p_d = { 'post_id': self.post_uid, 'user_name': self.username, 'user_id': self.user_uid, 'category': '0', 'cnt_reply': kwargs.get('cnt_reply', 'f4klkj进口国海关好姐姐4'), } uid = self.reply.create_reply(p_d) self.reply_uid = uid self.r2u.create_reply(self.user_uid, uid) def test_insert_reply(self): self.add_user() self.add_post() self.add_reply() aa = self.reply.get_by_uid(self.reply_uid) assert aa.user_name == self.username assert aa.post_id == self.post_uid assert aa.user_id == self.user_uid self.tearDown() def test_update_vote(self): self.add_user() self.add_post() self.add_reply() before = self.reply.get_by_uid(self.reply_uid) self.reply.update_vote(self.reply_uid, 10) after = self.reply.get_by_uid(self.reply_uid) assert after.vote == 10 assert before.vote < after.vote self.tearDown() def test_delete_by_uid(self): self.add_user() self.add_post() self.add_reply() yesrep = self.reply.get_by_uid(self.reply_uid) assert yesrep.post_id == self.post_uid aa = self.reply.delete_by_uid(self.reply_uid) assert aa nosrep = self.reply.get_by_uid(self.reply_uid) assert nosrep == None self.tearDown() def test_modify_by_uid(self): self.add_user() self.add_post() self.add_reply() p_d = { 'user_name': self.username, 'user_id': self.user_uid, 'category': '1', 'cnt_reply': '一二三四', } aa = self.reply.modify_by_uid(self.reply_uid, p_d) assert aa == self.reply_uid tt = self.reply.get_by_uid(self.reply_uid) assert tt.category == p_d['category'] assert tt.cnt_md == p_d['cnt_reply'] self.tearDown() def test_query_pager(self): self.add_user() self.add_post() self.add_reply() aa = self.reply.total_number() a = int(aa / 10) tf = False for i in range(a + 3): list = self.reply.query_pager(current_page_num=i) for x in list: if x.uid == self.reply_uid: tf = True break self.tearDown() assert tf def test_total_number(self): aa = self.reply.total_number() self.add_user() self.add_post() self.add_reply() bb = self.reply.total_number() assert aa + 1 <= bb self.tearDown() def test_count_of_certain(self): aa = self.reply.count_of_certain() self.add_user() self.add_post() self.add_reply() bb = self.reply.count_of_certain() assert aa + 1 <= bb self.tearDown() # def test_delete(self): # self.tearDown() # bb = self.reply.count_of_certain() # print(bb) # self.add_user() # self.add_post() # self.add_reply() # bb = self.reply.count_of_certain() # print(bb) # aa=self.reply.get_by_uid(self.reply_uid) # assert aa.post_id==self.post_uid # aa = self.reply.query_by_post(self.post_uid) # tf = False # for i in aa: # if i.uid == self.reply_uid: # tf = True # break # assert tf # vv=self.reply.delete(self.post_uid) # print('LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLl') # print(vv) # s = self.reply.count_of_certain() # print(s) # # assert vv # aa = self.reply.get_by_uid(self.reply_uid) # self.tearDown() # assert aa==None def test_query_all(self): self.tearDown() aa = self.reply.query_all() tf = True for i in aa: if i.uid == self.reply_uid: tf = False break assert tf self.add_user() self.add_post() self.add_reply() bb = self.reply.query_all() tf = False for i in bb: if i.uid == self.reply_uid: tf = True assert i.post_id == self.post_uid break assert tf self.tearDown() def test_get_by_zan(self): self.add_user() self.add_post() self.add_reply() aa = self.reply.get_by_zan(self.reply_uid) assert aa >= 1 self.tearDown() def test_query_by_post(self): self.add_user() self.add_post() self.add_reply() aa = self.reply.query_by_post(self.post_uid) tf = False for i in aa: if i.uid == self.reply_uid: tf = True break assert tf self.tearDown() def test_get_by_uid(self): self.add_user() self.add_post() self.add_reply() aa = self.reply.get_by_uid(self.reply_uid) assert aa.user_id == self.user_uid self.tearDown() def tearDown(self): print("function teardown") tt = self.post.get_by_uid(self.post_uid) if tt: self.post.delete(tt.uid) self.user.delete_by_user_name(self.username) tt = self.user.get_by_uid(self.user_uid) if tt: self.user.delete(tt.uid) tt = self.reply.get_by_uid(self.reply_uid) if tt: self.reply.delete_by_uid(tt.uid) self.r2u.delete(self.reply_uid)
class ReplyHandler(BaseHandler): def initialize(self): self.init() self.tab = MReply() self.mreply2user = MReply2User() def get(self, url_str=''): url_arr = self.parse_url(url_str) if url_arr[0] == 'get': self.get_by_id(url_arr[1]) elif url_arr[0] == 'list': self.list() elif url_arr[0] == 'delete': self.delete(url_arr[1]) elif url_arr[0] == 'zan': self.zan(url_arr[1]) def post(self, url_str=''): url_arr = self.parse_url(url_str) if url_arr[0] == 'add': self.add(url_arr[1]) def list(self): kwd = { 'pager': '', 'unescape': tornado.escape.xhtml_unescape, 'title': '单页列表', } self.render( 'admin/reply_ajax/reply_list.html', kwd=kwd, view_all=self.tab.query_all(), userinfo=self.userinfo, ) def get_by_id(self, reply_id): reply = self.tab.get_by_uid(reply_id) logger.info('get_reply: {0}'.format(reply_id)) self.render( 'reply/show_reply.html', reply=reply, username=reply.user_name, date=reply.date, vote=reply.vote, uid=reply.uid, userinfo=self.userinfo, unescape=tornado.escape.xhtml_unescape, ) def add(self, post_id): post_data = self.get_post_data() post_data['user_name'] = self.userinfo.user_name post_data['user_id'] = self.userinfo.uid post_data['post_id'] = post_id replyid = self.tab.insert_data(post_data) if replyid: out_dic = {'pinglun': post_data['cnt_reply'], 'uid': replyid} logger.info('add reply result dic: {0}'.format(out_dic)) return json.dump(out_dic, self) # @tornado.web.authenticated def zan(self, id_reply): logger.info('zan: {0}'.format(id_reply)) # 先在外部表中更新,然后更新内部表字段的值。 # 有冗余,但是查看的时候避免了联合查询 self.mreply2user.insert_data(self.userinfo.uid, id_reply) cur_count = self.mreply2user.get_voter_count(id_reply) if cur_count: self.tab.update_vote(id_reply, cur_count) output = { 'text_zan': cur_count, } else: output = { 'text_zan': 0, } logger.info('zan dic: {0}'.format(cur_count)) return json.dump(output, self) def delete(self, del_id): if self.mreply2user.delete(del_id): output = {'del_zan': 1} else: output = { 'del_zan': 0, } return json.dump(output, self)