from sqlalchemy import Column, Integer, String, Float, DateTime, create_engine, ForeignKey from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import relationship import cProfile import logging import pstats import unittest2 # Disable all logging since we don't care about its performance logging.disable(logging.CRITICAL) engine = create_engine('sqlite:///:memory:', echo=True) Base = declarative_base(engine) session_handler = ScopedSessionHandler(engine) def profileit(func): """ Decorator straight up stolen from stackoverflow """ def wrapper(*args, **kwargs): datafn = func.__name__ + ".profile" # Name the data file sensibly prof = cProfile.Profile() prof.enable() retval = prof.runcall(func, *args, **kwargs) prof.disable() stats = pstats.Stats(prof) try: stats.sort_stats('cumtime').print_stats()
def test_handle_session(self): engine = create_engine('sqlite:///:memory:') handler = ScopedSessionHandler(engine) session = mock.MagicMock() handler.handle_session(session) self.assertTrue(session.close.called)
def test_handle_session_exception(self): session = mock.MagicMock() handler = ScopedSessionHandler(session) e = Exception() handler.handle_session(session, exc=e) self.assertTrue(session.rollback.called)
def test_get_session(self): engine = create_engine('sqlite:///:memory:') handler = ScopedSessionHandler(engine) session = handler.get_session() self.assertIsInstance(session, Session)