title: Using python mocking library on sqlalchemy
body: <p>I'm using sqlalchemy to query my databases for a project. Side-by-side, I'm new to unit testing and I'm trying to learn how can I make unit tests to test my database. I tried to use mocking library to test but so far, it seems to be very difficult.</p>
  
  <p>So I made a piece of code which creates a <code>Session</code> object. This object is used to connect to database.</p>
  
  <pre><code>from sqlalchemy import create_engine
  from sqlalchemy.orm import sessionmaker
  from sqlalchemy.ext.declarative import declarative_base
  from sqlalchemy.exc import OperationalError, ArgumentError
  
  test_db_string = 'postgresql+psycopg2://testdb:hello@localhost/' \
                     'test_databasetable'
  def get_session(database_connection_string):
      try:
          Base = declarative_base()
          engine = create_engine(database_connection_string)
          Base.metadata.bind = engine
          DBSession = sessionmaker(bind=engine)
          session = DBSession()
          connection = session.connection()
          return session
      except OperationalError:
          return None
      except ArgumentError:
          return None
  </code></pre>
  
  <p>So I made a unit test case for this function:</p>
  
  <pre><code>import mock
  import unittest
  from mock import sentinel
  import get_session
  
  class TestUtilMock(unittest.TestCase):
  
      @mock.patch('app.Utilities.util.create_engine')  # mention the whole path
      @mock.patch('app.Utilities.util.sessionmaker')
      @mock.patch('app.Utilities.util.declarative_base')
      def test_get_session1(self, mock_delarative_base, mock_sessionmaker,
                           mock_create_engine):
          mock_create_engine.return_value = sentinel.engine
          get_session('any_path')
          mock_delarative_base.called
          mock_create_engine.assert_called_once_with('any_path')
          mock_sessionmaker.assert_called_once_with(bind=sentinel.engine)
  </code></pre>
  
  <p>As you see in my unit test, I cannot test code in <code>get_session()</code> starting from line <code>session = DBSession()</code>. So far after googling, I cannot find out if mock value returned can be also used to mock function calls - something like, I mock <code>session</code> object and verify if I called <code>session.connection()</code></p>
  
  <p>Is the above method of writing unit test case the right way? Is there a better method to do this?</p>