TransWikia.com

A task to store user's favorited projects in postgres using python

Code Review Asked by technazi on December 6, 2021

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)

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP