def test_update_club(self): create_res = self.client.post('/club', self.data) self.data['longitude'] = 113.287131 self.data['latitude'] = 23.138592 update_res = self.client.put(f'/club/{create_res.data["id"]}', json.dumps(self.data), content_type='application/json') self.assertEqual(update_res.status_code, status.HTTP_200_OK) # 判断拳馆数据已更新 for key in self.data.keys(): if key in ("longitude", "latitude"): self.assertEqual(update_res.data[key], str(self.data.get(key))) else: self.assertEqual(update_res.data[key], self.data.get(key)) # 判断更新后的位置信息,已知(116.317457,39.999664)province、city、address分别为广东省、广州市、广东省广州市越秀区建设大马路2号 self.assertEqual(update_res.data['province'], '广东省') self.assertEqual(update_res.data['city'], '广州市') club = BoxingClub.objects.get(id=update_res.data['id']) redis_location_record = redis_client.get_object_location(club) # 判断redis中位置记录已更新 self.assertAlmostEqual(self.data['longitude'], redis_location_record[0][0], delta=0.00001) self.assertAlmostEqual(self.data['latitude'], redis_location_record[0][1], delta=0.00001)
def test_close_and_open_club(self): boxer = BoxerIdentification.objects.create(**self.boxer_data) create_res = self.client.post('/club', self.data) club = BoxingClub.objects.get(id=create_res.data['id']) redis_client.record_object_location(boxer, club.longitude, club.latitude) self.assertAlmostEqual(redis_client.get_object_location(club)[0][0], float(club.longitude), delta=0.00001) self.assertAlmostEqual(redis_client.get_object_location(club)[0][1], float(club.latitude), delta=0.00001) self.assertAlmostEqual(redis_client.get_object_location(boxer)[0][0], float(club.longitude), delta=0.00001) self.assertAlmostEqual(redis_client.get_object_location(boxer)[0][1], float(club.latitude), delta=0.00001) course_data = { "boxer": boxer, "course_name": constants.BOXER_ALLOWED_COURSES_MMA, "price": 100, "duration": 120, "validity": "2018-08-25", "club": club, "is_open": True } course = Course.objects.create(**course_data) # 关闭拳馆 close_res = self.client.post(f'/club/{create_res.data["id"]}/close') self.assertEqual(close_res.status_code, status.HTTP_204_NO_CONTENT) # 判断拳馆已关闭,并且课程已关闭 self.assertFalse( BoxingClub.objects.all().filter(id=create_res.data['id']).exists()) self.assertTrue(BoxingClub.all_objects.all().filter( id=create_res.data['id']).exists()) self.assertFalse(Course.objects.get(id=course.id).is_open) # 拳馆位置、拳手位置已删除 self.assertIn(redis_client.get_object_location(club), ([], [None])) self.assertIn(redis_client.get_object_location(boxer), ([], [None])) # 开启拳馆 open_res = self.client.post(f'/club/{create_res.data["id"]}/open') self.assertEqual(open_res.status_code, status.HTTP_204_NO_CONTENT) self.assertTrue( BoxingClub.objects.all().filter(id=create_res.data['id']).exists())
def test_create_club(self): res = self.client.post('/club', self.data) self.assertEqual(res.status_code, status.HTTP_201_CREATED) # 判断拳馆已正确创建 for key in self.data.keys(): if key in ("longitude", "latitude"): self.assertEqual(res.data[key], str(self.data.get(key))) else: self.assertEqual(res.data[key], self.data.get(key)) # 判断redis中已记录位置信息 club = BoxingClub.objects.get(id=res.data['id']) redis_location_record = redis_client.get_object_location(club) self.assertAlmostEqual(self.data['longitude'], redis_location_record[0][0], delta=0.00001) self.assertAlmostEqual(self.data['latitude'], redis_location_record[0][1], delta=0.00001) # 已知通过百度api实际请求到(116.405994, 39.916042)的province、city、address信息分别为北京市、北京市、北京市东城区东长安街 self.assertEqual(res.data['province'], '北京市') self.assertEqual(res.data['city'], '北京市') self.assertEqual(res.data['city_index_letter'], 'B')
def test_update_boxer_course(self): # 创建拳手,并根据拳手allowed_course为拳手创建课程 identification = BoxerIdentification.objects.create(**self.boxer_data) for course_name in self.boxer_data['allowed_course']: Course.objects.create(boxer=identification, course_name=course_name) # 获取拳手课程初始列表,判断列表数量是否与拳手可开课程数量一致 course_list_res = self.client1.get('/boxer/course') self.assertEqual(len(course_list_res.data['results']), len(self.boxer_data['allowed_course'])) # 构造修改课程列表数据,并修改课程(is_open为False,不校验数据合法性) my_club = BoxingClub.objects.create(**self.club_data) course1_data = { 'is_open': False, 'course_id': course_list_res.data['results'][0].get('id'), 'price': 120, 'duration': 365, } course2_data = { 'is_open': False, 'course_id': course_list_res.data['results'][1].get('id'), 'price': 121, 'duration': 366, } course3_data = { 'course_id': course_list_res.data['results'][2].get('id'), 'is_open': True, 'price': 122, 'duration': 367, } update_course_data = { "validity": "2088-04-25", "club": my_club.pk, "course_list": [{ **course1_data }, { **course2_data }] } # 修改未开通课程,不需要校验数据 update_res = self.client1.post('/boxer/course', data=json.dumps(update_course_data), content_type='application/json') self.assertEqual(update_res.status_code, status.HTTP_204_NO_CONTENT) # 修改开通课程,需要校验数据(价格为空,数据不合法,校验失败) course3_data['price'] = '' update_course_data["course_list"] = [{ **course1_data }, { **course2_data }, { **course3_data }] update_res = self.client1.post('/boxer/course', data=json.dumps(update_course_data), content_type='application/json') self.assertEqual(update_res.status_code, status.HTTP_400_BAD_REQUEST) # 修改开通课程,需要校验数据(有效期小于今天,数据不合法,校验失败) update_course_data['validity'] = "2008-04-25" update_course_data["course_list"] = [{ **course1_data }, { **course2_data }, { **course3_data }] update_res = self.client1.post('/boxer/course', data=json.dumps(update_course_data), content_type='application/json') self.assertEqual(update_res.status_code, status.HTTP_400_BAD_REQUEST) # 修改开通课程,需要校验数据(数据合法,校验成功) course3_data['price'] = 120 update_course_data['validity'] = "2088-04-25" update_course_data["course_list"] = [{ **course1_data }, { **course2_data }, { **course3_data }] update_res = self.client1.post('/boxer/course', data=json.dumps(update_course_data), content_type='application/json') self.assertEqual(update_res.status_code, status.HTTP_204_NO_CONTENT) # 比较修改后的课程数据与修改数据是否一致 course_list_res = self.client1.get('/boxer/course') # 没有评论时,comments_count为0,avg_score为None self.assertEqual(course_list_res.data['comments_count'], 0) self.assertIsNone(course_list_res.data['avg_score']) # 比较课程数据 course_result = course_list_res.data['results'] self.assertEqual(len(course_list_res.data['results']), len(self.boxer_data['allowed_course'])) self.assertTrue(course_result[2]['is_open']) self.assertEqual(course_result[2]['id'], course3_data['course_id']) self.assertEqual(course_result[2]['price'], course3_data['price']) self.assertEqual(course_result[2]['duration'], course3_data['duration']) self.assertEqual(course_result[2]['validity'], update_course_data['validity']) self.assertEqual(course_result[2]['club'], update_course_data['club']) self.assertEqual(course_result[2]['validity'], update_course_data['validity']) self.assertEqual(course_result[2]['club_name'], self.club_data['name']) self.assertEqual(course_result[2]['club_address'], self.club_data['address']) self.assertEqual(str(course_result[2]['club_longitude']), str(self.club_data['longitude'])) self.assertEqual(str(course_result[2]['club_latitude']), str(self.club_data['latitude'])) self.assertEqual(course_list_res.data['validity'].strftime("%Y-%m-%d"), update_course_data['validity']) self.assertEqual(course_list_res.data['club_id'], update_course_data['club']) self.assertEqual(course_list_res.data['club_name'], self.club_data['name']) self.assertEqual(course_list_res.data['club_address'], self.club_data['address']) self.assertEqual(str(course_list_res.data['club_longitude']), str(self.club_data['longitude'])) self.assertEqual(str(course_list_res.data['club_latitude']), str(self.club_data['latitude'])) # 判断是否将拳手位置存入了redis,并校验正确性 boxer = BoxerIdentification.objects.get(user=self.user1) boxer_location = redis_client.get_object_location(boxer) self.assertAlmostEqual(boxer_location[0][0], self.club_data['longitude'], delta=0.00001) self.assertAlmostEqual(boxer_location[0][1], self.club_data['latitude'], delta=0.00001)
def get_boxer_loacation(obj): return get_object_location(obj)[0]