Grechka Vassili
Grechka Vassili

Reputation: 883

Examining SQLAlchemy query results in Pyramid

I'm trying to add a method to an (quite big) existing project writtent in python with pyramid framework and sqlalchemy ORM. I've wanted to execute an sql query with sqlalchemy but I've never developped with pyramid or sqlalchemy before. So I would like to test it and see if the query returns what I'm expecting but I don't want to add useless code to test my query ( like a new template, a view etc). My SQL query is :

select a.account_type, u.user_id from accounts a inner join account_users au on a.account_id=au.account_id inner join users u on u.user_id=au.user_id where u.user_id = ?;

And my method is :

def find_account_type_from_user_id(self,user_id):
    '''
    Method that finds the account type (one/several points of sale...)
    from the id of the user who is linked to this account

    :param user_id:
    :return:(string) account_type
    '''

    q = self.query(Account)\
        .join(AccountUser)\
        .join(User)\
        .filter(User.user_id == user_id)\
        .one()

    return q

ps: I've already searched on the internet but I only find things like : unit tests etc and I've never did that. (Noob's sorry).

Upvotes: 1

Views: 247

Answers (2)

Mikko Ohtamaa
Mikko Ohtamaa

Reputation: 83676

Two ways to see SQLAlchemy query content

Upvotes: 0

Antonio Beamud
Antonio Beamud

Reputation: 2341

Unit tests are a must to test new services, fixes, refactoring code, etc, you need a good collection of unit tests. You can start here.

Upvotes: 1

Related Questions