Ejemplo n.º 1
0
    def insert_user(cls, name: str, password: str) -> Users:
        """
        Insert data in user entity
        :param  - name: person name
                - password: user password
        :return - tuple with new user inserted informations
        """

        # Creating a Return Tuple With Informations

        with DBConnectionHandler() as db_connection:
            try:
                new_user = UsersModel(name=name, password=password)
                db_connection.session.add(new_user)
                db_connection.session.commit()

                return Users(id=new_user.id,
                             name=new_user.name,
                             password=new_user.password)

            except Exception as ex:
                db_connection.session.rollback()
                print(ex)
                raise
            finally:
                db_connection.session.close()

        return None
    def insert_user(cls, name: str, password: str) -> Users:
        """insert data in user entity
        :param - name: person name
               - password: user pasword
        :return - Users tuple with new user inserted
        """

        with DBConnectionHandler() as db_connection:
            try:
                new_user = UsersModel(name=name, password=password)
                db_connection.session.add(new_user)
                db_connection.session.commit()
                return Users(id=new_user.id,
                             name=new_user.name,
                             password=new_user.password)
            except:
                db_connection.session.rollback()
                raise
            finally:
                db_connection.session.close()
        return None
def test_select_user():
    """ Should select a user in Users table and compare it """
    user_id = faker.random_number(digits=5)
    name = faker.name()
    password = faker.word()
    data = UsersModel(id=user_id, name=name, password=password)

    engine = db_connection_handler.get_engine()

    engine.execute(
        "INSERT INTO users (id, name, password) VALUES ('{}', '{}', '{}');".
        format(user_id, name, password))
    query_user1 = user_repository.select_user(user_id=user_id)
    query_user2 = user_repository.select_user(name=name)
    query_user3 = user_repository.select_user(user_id=user_id, name=name)

    assert data in query_user1
    assert data in query_user2
    assert data in query_user3

    engine.execute("DELETE FROM users WHERE id='{}';".format(user_id))
    def insert_user(cls, name: str, password: str) -> Users:
        """Insert data in user tables
        :return - tuple with new user
        :params - name, password

        """
        with DBConnectionHandler() as db_conn:
            try:
                new_user = UsersModel(name=name, password=password)
                db_conn.session.add(new_user)
                db_conn.session.commit()

                return Users(id=new_user.id,
                             name=new_user.name,
                             password=new_user.password)

            except:
                db_conn.session.rollback()
                raise
            finally:
                db_conn.session.close()