2
\$\begingroup\$

I am not happy with two functions calling get_user() since while testing I mock get_user and to test with get_user_returns_null case, it returns Null for both function calls naturally which hints that the structure is not correct. I am using flask-sqlalchemy with postgres models ORM.

 def insert_user(user_details):
    user_record = None
    try:
        user = User(name = user_details['name'], email = user_details['email'])
        db.session().add(user)   
        db.session().commit()
        user_record = get_user(user_details)
    except SQLAlchemyError as e:
        db.session().rollback()
    finally:
        db.session().close()
    return user_record


def add_user_favorite(user_details, project_code):
    user_id = None
    user_record = get_user(user_details)

    if user_record is None:
        user_record = insert_user(user_details)

    user_id = user_record['user_id']
    try:
        user_fav = UserFavoriteProject(user_id = user_id, project_code = project_code)
        db.session().add(user_fav)
        db.session().commit()
    except SQLAlchemyError as e:
        db.session().rollback()
    finally:
        db.session().close()

    return is_user_favorite(user_details, project_code)
\$\endgroup\$
  • \$\begingroup\$ Please show all of your code, including import statements. \$\endgroup\$ – Reinderien Jul 24 at 0:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.