def test_activity_create(self): self.authenticate() poster = open(os.path.join(settings.BASE_DIR, 'media', 'tests', 'test.png')) response = self.client.post(reverse('activity:create'), data=dict( name='test activity', description='description', max_attend=10, start_at=time_to_string(timezone.now() + datetime.timedelta(days=10)), end_at=time_to_string(timezone.now() + datetime.timedelta(days=12)), location=json.dumps(dict(lat=30, lon=120, description='test location')), poster=poster )) response_data = json.loads(response.content) self.assertTrue(response_data['success'])
def dict_description(self): return dict( car=self.car.dict_description(), signature=self.signature, identified=self.identified, identified_date=time_to_string(self.created_at) )
def test_status_list_access_with_avatar_club(self): """ 在setUp创建的数据集里面,没有引入签名俱乐部,这个测试给默认用户加入签名俱乐部 """ # 首先创建一个俱乐部 club = Club.objects.create(name='test club', host=self.default_user, logo=os.path.join(settings.MEDIA_ROOT, 'tests', 'test.png'), description='test club') # 将这个俱乐部设置为default_user的avatar_club self.default_user.profile.avatar_club = club self.default_user.profile.save() # 然后进行访问 self.authenticate() request_time = timezone.now() + datetime.timedelta(seconds=60) response = self.client.get(reverse('status:status_list'), data=dict( date_threshold=time_to_string(request_time), op_type='more', limit='10' )) response_data = json.loads(response.content) self.maxDiff = None self.assertEqual(len(response_data["data"]), 1) status = self.default_status status_dict = status.dict_description() status_dict["comment_num"] = 0 status_dict["like_num"] = 0 status_dict["liked"] = False self.assertEqual(response_data["data"][0], status_dict)
def dict_description(self, show_liked=False, user=None): """ 获取字典形式的数据描述,字典形式的注释参见view.py中的status_list的注释 但是这里没有生成comment_num和like_num这两个字段 """ result = dict( statusID=self.id, images=self.images, content=self.content, user=self.user.dict_description(detail=True), created_at=time_to_string(self.created_at), ) if self.car is not None: try: own = SportCarOwnership.objects.get(user=self.user, car=self.car) result["car"] = own.dict_description() except ObjectDoesNotExist: result["car"] = self.car.dict_description() if self.recent_like_user: result["recent_like_user"] = self.recent_like_user.nick_name if self.location is not None: result["location"] = self.location.dict_description() if hasattr(self, "comment_num"): result["comment_num"] = self.comment_num if hasattr(self, "like_num"): result["like_num"] = self.like_num if show_liked: result["liked"] = StatusLikeThrough.objects.filter(status=self, user=user).exists() return result
def dict_description(self): return dict( inviter=self.inviter.dict_description(), target=self.target.dict_description(), activity=self.target.dict_description(), created_at=time_to_string(self.created_at), responsed=self.responsed, agree=self.agree )
def test_activity_create_with_club_limit(self): self.authenticate() club = Club.objects.create(name='test_club', host=self.user, logo='meida/tests/test.png', description='test') poster = open(os.path.join(settings.BASE_DIR, 'media', 'tests', 'test.png')) response = self.client.post(reverse('activity:create'), data=dict( name='test activity', description='description', max_attend=10, start_at=time_to_string(timezone.now() + datetime.timedelta(days=10)), end_at=time_to_string(timezone.now() + datetime.timedelta(days=12)), location=json.dumps(dict(lat=30, lon=120, description='test location')), club_limit=club.id, poster=poster )) response_data = json.loads(response.content) self.assertTrue(response_data['success']) act = Activity.objects.order_by('-created_at').first() self.assertEqual(act.allowed_club.id, club.id)
def dict_description(self): result = dict( id=self.id, cover=self.cover.url, created_at=time_to_string(self.created_at), content=self.content, title=self.title, is_video=self.is_video, ) return result
def dict_description_without_user(self): result = dict( actID=self.id, name=self.name, description=self.description, max_attend=self.max_attend, start_at=time_to_string(self.start_at), end_at=time_to_string(self.end_at) if not self.closed \ else time_to_string(self.closed_at), poster=self.poster.url, location=self.location.dict_description(), created_at=time_to_string(self.created_at), authed_user_only=self.authed_user_only ) if self.recent_like_user: result["recent_like_user"] = self.recent_like_user.nick_name if self.allowed_club is not None: result.update(allowed_club=self.allowed_club.dict_description()) return result
def test_news_list_latest(self): self.authenticate() request_time = timezone.now() - datetime.timedelta(seconds=60) response = self.client.get(reverse('news:news_list'), data=dict( date_threshold=time_to_string(request_time), op_type='latest', limit='10' )) response_data = json.loads(response.content) self.assertTrue(response_data["success"]) self.assertEqual(len(response_data["news"]), 1)
def dict_description(self): result = dict( user=self.user.dict_description(), created_at=time_to_string(self.created_at), image=self.image.url if self.image else None, content=self.content, commentID=self.id, ) if self.response_to is not None: result.update(response_to=self.response_to.dict_description()) return result
def test_news_without_login(self): self.create_comments(20) comment = self.create_comments(num=1)[0] request_time = timezone.now() - datetime.timedelta(seconds=60) response = self.client.get(reverse('news:comments_list', args=(self.news1.id, )), data=dict( date_threshold=time_to_string(request_time), op_type='latest', limit='10' )) response_data = json.loads(response.content) self.assertEqual(response_data, dict(success=False, message='You need to login first', code='1402'))
def test_news_get_comments_with_limit(self): self.authenticate() self.create_comments(20) comment = self.create_comments(num=1)[0] request_time = timezone.now() - datetime.timedelta(seconds=60) response = self.client.get(reverse('news:comments_list', args=(self.news1.id, )), data=dict( date_threshold=time_to_string(request_time), op_type='latest', limit='10' )) response_data = json.loads(response.content) self.assertEqual(len(response_data['comments']), 10)
def test_status_comments_list_with_limit(self): self.authenticate() another_user = get_user_model().objects.create(username='******') self.create_comments(num=20, user=another_user) request_time = timezone.now() + datetime.timedelta(seconds=60) response = self.client.get(reverse('status:status_comments', args=(self.default_status.id, )), data=dict( date_threshold=time_to_string(request_time), op_type='more', limit='10' )) response_data = json.loads(response.content) self.assertEqual(len(response_data['comments']), 10)
def test_news_list_with_invalid_operation_type(self): self.authenticate() request_time = timezone.now() - datetime.timedelta(seconds=60) response = self.client.get(reverse('news:news_list'), data=dict( date_threshold=time_to_string(request_time), op_type='invalid', limit='10' )) response_data = json.loads(response.content) self.assertEqual(response_data, dict( success=False, code='3400', message='invalid op_type' ))
def test_activity_create_with_inform_of(self): self.authenticate() users = [] user_num = random.randint(1, 20) for i in range(user_num): users.append(get_user_model().objects.create(username='******' % i)) poster = open(os.path.join(settings.BASE_DIR, 'media', 'tests', 'test.png')) response = self.client.post(reverse('activity:create'), data=dict( name='test activity', description='description', max_attend=10, start_at=time_to_string(timezone.now() + datetime.timedelta(days=10)), end_at=time_to_string(timezone.now() + datetime.timedelta(days=12)), location=json.dumps(dict(lat=30, lon=120, description='test location')), inform_of=json.dumps(map(lambda x: x.id, users)), poster=poster )) response_data = json.loads(response.content) self.assertTrue(response_data['success']) act = Activity.objects.order_by('-created_at').first() self.assertEqual(len(act.inform_of.all()), user_num)
def test_news_get_comments_more(self): self.authenticate() comment = self.create_comments(num=1)[0] request_time = timezone.now() + datetime.timedelta(seconds=60) response = self.client.get(reverse('news:comments_list', args=(self.news1.id, )), data=dict( date_threshold=time_to_string(request_time), op_type='more', limit='10' )) response_data = json.loads(response.content) self.maxDiff = None self.assertTrue(response_data["success"])
def dict_description_simple(self): """ 相比之下,这个函数返回的字典不包含activity的信息 """ result = dict( user=self.user.dict_description(), created_at=time_to_string(self.created_at), image=self.image.url if self.image else None, content=self.content, commentID=self.id ) if self.response_to is not None: result.update(response_to=self.response_to.dict_description_simple()) return result
def test_news_list_limit(self): new_news = self.news1 for _ in range(20): new_news.id = None new_news.save() self.authenticate() request_time = timezone.now() - datetime.timedelta(seconds=60) response = self.client.get(reverse('news:news_list'), data=dict( date_threshold=time_to_string(request_time), op_type='latest', limit='10' )) response_data = json.loads(response.content) self.assertEqual(len(response_data['news']), 10)
def dict_description(self): result = dict( ssid=self.id, unread_num=self.unread_num, recent_chat=self.recent_chat, always_on_top=self.always_on_top, no_disturbing=self.no_disturbing, created_at=time_to_string(self.created_at), updated_at=time_to_string(self.updated_at), ) if self.user is not None: user_json = self.user.dict_description() user_json.update(note_name=ChatRemarkNameStorage.get_nick_name(self.host, self.user)) result.update( user=user_json, entity_type="user" ) if self.club is not None: result.update( club=self.club.dict_description(show_members_num=True), entity_type='club' ) return result
def test_stauts_list_access_latest(self): """ 测试状态列表首页获取是否正常 """ self.authenticate() request_time = timezone.now() - datetime.timedelta(seconds=60) response = self.client.get(reverse('status:status_list'), data=dict( date_threshold=time_to_string(request_time), op_type='latest', limit='10' )) response_data = json.loads(response.content) self.maxDiff = None self.assertTrue(response_data["success"]) self.assertEqual(len(response_data["data"]), 1)
def test_activity_check_detail_with_appliers(self): another_user = get_user_model().objects.create(username='******') join = ActivityJoin.objects.create(user=another_user, activity=self.activity) self.authenticate() response = self.client.get(reverse('activity:detail', args=(self.activity.id, ))) response_data = json.loads(response.content) setattr(self.activity, "like_num", 0) setattr(self.activity, "comment_num", 0) act_dict = self.activity.dict_description_with_aggregation(with_user_info=True) act_dict.update(dict(apply_list=[dict( approved=join.approved, like_at=time_to_string(join.created_at), user=join.user.profile.complete_dict_description() )], liked=False)) self.maxDiff = None self.assertEqual(response_data['data'], act_dict)
def test_status_comment_list_latest(self): self.authenticate() another_user = get_user_model().objects.create(username='******') comment = self.create_comments(num=1, user=another_user) request_time = timezone.now() - datetime.timedelta(seconds=60) response = self.client.get(reverse('status:status_comments', args=(self.default_status.id, )), data=dict( date_threshold=time_to_string(request_time), op_type='latest', limit='10' )) response_data = json.loads(response.content) self.maxDiff = None self.assertEqual( response_data, dict( success=True, comments=[comment.dict_description()] ) )
def activity_detail(request, act_id): """ 活动详情, 注意这里只显示活动相关的内容, 评论列表从单独的接口获取 返回的信息和创建时提交的信息基本一致,但是不用显示inform_of, 回复的club_limit要显示这个俱乐部的标识和id, 另外要返回报名参加者的列表 """ try: act = Activity.objects.get(id=act_id) except ObjectDoesNotExist: return JsonResponse(dict(success=False, code='7000', message='Activity not found.')) data = act.dict_description_with_aggregation(with_user_info=True) apply_list = ActivityJoin.objects.select_related('user__avatar_club').filter(activity=act, approved=True) data['apply_list'] = map(lambda x: dict(approved=x.approved, like_at=time_to_string(x.created_at), user=x.user.dict_description()), apply_list) data["liked"] = act.liked_by.filter(id=request.user.id).exists() data["applied"] = ActivityJoin.objects.filter(user=request.user, activity=act, approved=True).exists() return JsonResponse(dict(success=True, data=data))
def test_status_list_access_more(self): """ 测试状态列表首页获取是否正常 """ self.authenticate() request_time = timezone.now() + datetime.timedelta(seconds=60) response = self.client.get(reverse('status:status_list'), data=dict( date_threshold=time_to_string(request_time), op_type='more', limit='10' )) response_data = json.loads(response.content) self.maxDiff = None self.assertTrue(response_data["success"]) self.assertEqual(len(response_data["data"]), 1) status = self.default_status status_dict = status.dict_description() status_dict["comment_num"] = 0 status_dict["like_num"] = 0 status_dict["liked"] = False self.assertEqual(response_data["data"][0], status_dict)
def dict_description(self, host): """ :param host: :return: """ if isinstance(host, ChatEntity): host = host.user data = self.sender.dict_description() data.update(note_name=ChatRemarkNameStorage.get_nick_name(host, self.sender)) result = dict( chatID=self.id, sender=data, chat_type=self.chat_type, message_type=self.message_type, created_at=time_to_string(self.created_at) ) if self.image is not None and self.image: result.update( image=self.image.url, image_width=self.image.width, image_height=self.image.height ) if self.text is not None: result.update(text=self.text) if self.audio is not None and self.audio: result.update( audio=self.audio.url, audio_wave_data=self.audio_wave_data, audio_length=self.audio_length ) if self.target_user is not None: data = self.target_user.dict_description() data.update(note_name=ChatRemarkNameStorage.get_nick_name(host, self.target_user)) result.update( target_user=data ) if self.target_club is not None: result.update( target_club=self.target_club.dict_description() ) return result
def dict_description(self): """ 获取字典表示形式 """ result = dict( notification_id=self.id, target=self.target.dict_description(), message_type=self.message_type, read=self.read, created_at=time_to_string(self.created_at), flag=self.flag, checked=self.checked ) def set_related(attribute_name): attribute = getattr(self, attribute_name) if attribute is not None: if isinstance(attribute, Club): result[attribute_name] = attribute.dict_description( show_attended=True, target_user=self.target ) else: result[attribute_name] = attribute.dict_description() # if attribute is not None: # if attribute_name == "related_user": # result[attribute_name] = attribute.profile.simple_dict_description() # else: set_related("related_user") set_related("related_act") set_related("related_act_invite") set_related("related_act_join") set_related("related_act_comment") set_related("related_club") set_related("related_status") set_related("related_status_comment") set_related("related_news") set_related("related_news_comment") set_related("related_own") return result
def test_status_list_like_num(self): """ 测试点赞人数是否正确 """ random_like_num = random.randint(0, 20) for i in range(random_like_num): user = get_user_model().objects.create(username=str(i)) StatusLikeThrough.objects.create(user=user, status=self.default_status) self.authenticate() request_time = timezone.now() + datetime.timedelta(seconds=60) response = self.client.get(reverse('status:status_list'), data=dict( date_threshold=time_to_string(request_time), op_type='more', limit='10' )) response_data = json.loads(response.content) self.maxDiff = None self.assertEqual(len(response_data["data"]), 1) status = self.default_status status_dict = status.dict_description() status_dict["comment_num"] = 0 status_dict["like_num"] = random_like_num status_dict["liked"] = False self.assertEqual(response_data["data"][0], status_dict)
def test_status_list_comment_num(self): """ 测试返回的评论数量是否正确 """ random_comment_num = random.randint(1, 20) for _ in range(random_comment_num): StatusComment.objects.create(status=self.default_status, user=self.guest_user, image='/media/tests/test.png', content='content') self.authenticate() request_time = timezone.now() + datetime.timedelta(seconds=60) response = self.client.get(reverse('status:status_list'), data=dict( date_threshold=time_to_string(request_time), op_type='more', limit='10' )) response_data = json.loads(response.content) self.assertEqual(len(response_data["data"]), 1) status = self.default_status status_dict = status.dict_description() status_dict["comment_num"] = random_comment_num status_dict["like_num"] = 0 status_dict["liked"] = False self.assertEqual(response_data["data"][0], status_dict)
def result_format(like): result = like.user.dict_description() result["like_at"] = time_to_string(like.like_at) return result