Пример #1
0
def create_thread():
    json_data = json.loads(request.data)
    current_app.logger.info('Create_thread called with payload: %s', json_data)
    # Consider creating a consolidated/central error handling strategy to handle malformed requests, db connection
    # failure, etc...
    thread_id = ThreadService.create_thread(json_data)
    if thread_id is None:  # The variable
        current_app.logger.debug('Create_thread failed to create thread.')
        return {'error': 'malformed request', 'data': json_data}, 400
    else:
        data = {'thread_id': str(thread_id)}
        current_app.logger.debug('Create_thread returning: %s', data)
        return data, 201
 def create_message(json_data, thread_id, username) -> Any:
     json_data['thread_id'] = thread_id
     json_data['username'] = username
     # this section of code is a candidate for refactoring/consolidation
     is_valid = MessageEntity.validate_json(json_data)
     if is_valid:
         current_app.logger.debug('Json is valid.')
         message_thread: Optional[
             Any] = ThreadService.get_thread_with_id_and_username(
                 thread_id, username)
         if message_thread is not None:
             current_app.logger.debug(
                 'Found corresponding thread for this message. Will save thread in DB.'
             )
             result = messagesTable.insert_one(json_data)
             current_app.logger.debug("Inserted Id: %s" +
                                      str(result.inserted_id))
             return result.inserted_id
     else:
         current_app.logger.error("JSON WAS INVALID. Will... idk yet.")
     return None  # No message was created
def test_new_message_missing_users(test_client):
    mock_thread_json = {"userss": ["quinn", "jeff_goldblum"]}
    thread_id = ThreadService.create_thread(mock_thread_json)
    assert not thread_id
def test_new_message_exceeding_max_users(test_client):
    a_list = list(range(0, 101))
    max_number_of_users = [f'User{str(entry)}' for entry in a_list]
    mock_thread_json = {"users": max_number_of_users}
    thread_id = ThreadService.create_thread(mock_thread_json)
    assert not thread_id
def test_valid_new_message(test_client):
    mock_thread_json = {"users": ["quinn", "jeff_goldblum"]}
    thread_id = ThreadService.create_thread(mock_thread_json)
    assert thread_id
def test_new_message_one_user(test_client):
    mock_thread_json = {"users": ["User1"]}
    thread_id = ThreadService.create_thread(mock_thread_json)
    assert not thread_id
def test_new_message_empty_users(test_client):
    mock_thread_json = {"users": []}
    thread_id = ThreadService.create_thread(mock_thread_json)
    assert not thread_id