def to_dict(request: Request) -> Dict: start_time = request.start_time assigned_to = request.assigned_to return { 'request_id': request.id, 'customer': user_converter.to_dict(request.customer), 'start_time': None if start_time is None else int(start_time.timestamp()), 'duration': int(request.duration.total_seconds()), 'latitude': float(request.latitude), 'longitude': float(request.longitude), 'finished': request.finished, 'description': request.description, 'title': request.title, 'assigned_to': None if assigned_to is None else user_converter.to_dict(assigned_to), 'responses': [ response_converter.to_dict(response) for response in request.response_set.all() ] }
def test_to_dict(has_start, has_assign, create_full_user, response_count, finished): request = fake_request(has_start) request.finished = finished worker, _ = create_full_user if has_assign: request.assigned_to = worker responses = [fake_response(request) for _ in range(response_count)] request.save() request_dict = request_converter.to_dict(request) assert request_dict['request_id'] == request.id assert request_dict['customer'] == user_converter.to_dict(request.customer) if has_start: assert request_dict['start_time'] == int( request.start_time.timestamp()) else: assert request_dict['start_time'] is None assert request_dict['duration'] == int(request.duration.total_seconds()) assert abs(request_dict['latitude'] - float(request.latitude)) < 0.0001 assert abs(request_dict['longitude'] - float(request.longitude)) < 0.0001 assert request_dict['finished'] == request.finished assert request_dict['description'] == request.description if has_assign: assert request_dict['assigned_to'] == user_converter.to_dict(worker) else: assert request_dict['assigned_to'] is None assert len(request_dict['responses']) == len(responses) for response_dict in (response_converter.to_dict(resp) for resp in responses): assert response_dict in request_dict['responses']
def test_to_dict(has_start_time): req = fake_request(has_start_time) resp = fake_response(req) resp_dict = response_converter.to_dict(resp) assert resp.id == resp_dict['response_id'] assert user_converter.to_dict(resp.worker) == resp_dict['worker'] assert resp.comment == resp_dict['comment']
def test_success(client, fake_user_dict): resp = client.post('/api/v1/create_user', data=fake_user_dict, content_type='application/json') assert resp.status_code == 200 assert json.loads(resp.content.decode()) == user_converter.to_dict( authenticate(username=fake_user_dict['email'], password=fake_user_dict['password']))
def test_to_dict(): for user, _ in generate_users(10): user_dict = user_converter.to_dict(user) assert user_dict['user_id'] == user.id assert user_dict['first_name'] == user.first_name assert user_dict['last_name'] == user.last_name assert user_dict['bio'] == user.bio assert isinstance(user_dict['bio'], str) for key in user_dict: assert key in ['user_id', 'first_name', 'last_name', 'bio']
def test_success_query(create_full_user, client): my_user, password = create_full_user other_users = list(generate_users(10)) for user, _ in other_users: user.save() for other, _ in other_users: resp = client.get(f'/api/v1/profile?user_id={other.id}', HTTP_AUTHORIZATION=encode_auth( my_user.email, password)) assert resp.status_code == 200 assert resp.content.decode() == json.dumps( user_converter.to_dict(other))
def find_worker(request_id, user: User): parsed_id = validate_int(request_id) if not parsed_id: return Err(HttpError(400, f'{request_id} is not a valid request ID')) request_id = parsed_id.value try: request = Request.objects.get(id=request_id) except (OverflowError, Request.DoesNotExist): return Err(HttpError(404, f'Request with {request_id} does not exist')) if request.customer != user: return Err(HttpError(403, 'You cannot find a worker for another customer\'s request')) worker = Request.objects.find_worker(request) if worker: return Ok(user_converter.to_dict(worker.value)) return Ok(None)
def test_found(create_full_user, client): Request.objects.paired_requests = {} Request.objects.pending_requests.clear() user, password = create_full_user worker, _ = random.choice(list(generate_users(3))) request_dict = fake_request_creation(False) request_dict['customer'] = user request = Request.objects.create_request(**request_dict) assert Request.objects.find_job(worker) resp = client.get( f'/api/v1/find_worker?request_id={request.id}', HTTP_AUTHORIZATION=encode_auth(user.email, password) ) assert resp.status_code == 200 resp_json = json.loads(resp.content) assert resp_json['worker'] == user_converter.to_dict(worker)
def test_success(create_full_user, client, has_start_time): user, password = create_full_user req_dict = fake_request_creation(has_start_time) req = Request.objects.create_request(**req_dict) actual_json = request_converter.to_dict(req) data = {'request_id': req.id, 'comment': fake.text()} resp = client.post('/api/v1/create_response', data=data, content_type='application/json', HTTP_AUTHORIZATION=encode_auth(user.email, password)) assert resp.status_code == 200 resp_json = json.loads(resp.content) actual_json['responses'].append({ 'worker': user_converter.to_dict(user), 'comment': data['comment'] }) for response in resp_json['responses']: del response['response_id'] assert resp_json == actual_json
def profile(user: User, request: HttpRequest): user_id = request.GET.get('user_id') if user_id is None: return JsonResponse(user_converter.to_dict(user)) return controller.get_profile(user_id)
def test_found(create_full_user): user = create_full_user[0] assert loads(get_profile(user.id).content.decode()) == user_converter.to_dict(user)
def to_dict(response: Response) -> Dict: return { 'response_id': response.id, 'worker': user_converter.to_dict(response.worker), 'comment': response.comment }
def test_success(create_full_user, client): user, password = create_full_user resp = client.get('/api/v1/profile', HTTP_AUTHORIZATION=encode_auth(user.email, password)) assert resp.status_code == 200 assert resp.content.decode() == json.dumps(user_converter.to_dict(user))