4

What is the best way to test code like this (the one below obviously fails while object is created in different block every time):

def get_session(db_name, verbose, test):
"""Returns current DB session from SQLAlchemy pool.

>>> get_session('Mmusc20090126', False, True)
<sqlalchemy.orm.session.Session object at 0xfb5ff0>

"""
if test:
    engine = create_engine('sqlite:///:memory:', echo=verbose)
    log_load.debug('DB in RAM.')
else:
    engine = create_engine('sqlite:///' + 'DB/' + db_name + '.db', echo=verbose)
    log_load.debug('DB stored in file: %s' % 'DB/' + db_name + '.db')

# Create TABLES: Structures, Interactions, Interactors, PDB_UniProt, UniProtSeq
meta.create_all(engine)

Session = sessionmaker(bind=engine)
session = Session()

return session
Piotr Byzia
  • 3,363
  • 7
  • 42
  • 62

1 Answers1

10

I think you want to use ellipsis, like this:

>>> get_session('Mmusc20090126', False, True) #doctest: +ELLIPSIS
<sqlalchemy.orm.session.Session object at 0x...>

See here for more info.

DNS
  • 37,249
  • 18
  • 95
  • 132
  • Thanks for the update of your answer, I was digging online for where I should put this #doctest: +ELLIPSIS thingy (I thought that using nose makes it different from unitest) ;) – Piotr Byzia Mar 24 '09 at 20:26