def test_get_with_no_session_and_init_failure(self):
        """
        get() will call _init_session() if the session for the given
        database user is not in the cache already.
        When _init_session() fails to add a session to the cache an
        `AssertionError` is raised.
        """
        sc = SessionCache()

        # Prepare mock.
        mock_method = mock.Mock()

        # Replace the original mathod with the mock.
        sc._init_session = mock_method

        # We do not have a session in the cache for the user at hand.
        self.assertTrue(sc.__sessions__.get("usr3") is None)

        # The _init_session() mock will get called but fail to add a session to
        # the cache,
        self.assertRaises(AssertionError, sc.get, "usr3", "")

        # The method under test did call the mock..
        self.assertEqual(1, mock_method.call_count)
        (user, passwd), kwargs = mock_method.call_args
        self.assertEqual("usr3", user)
        self.assertEqual("", passwd)
        # ..but no session was added to the cache.
        self.assertTrue(sc.__sessions__.get("usr3") is None)
    def test_get_with_no_session_for_user(self):
        """
        get() will call _init_session() if this is the first time a session
        is requested for the given database user.
        """
        sc = SessionCache()
        expected_session = object()

        def fake_init_session(user, password):
            """
            When the mock method is called this will set the session for the
            given user to `expected_session`.
            """
            sc.__sessions__[user] = expected_session

        # Prepare mock.
        mock_method = mock.Mock()
        mock_method.side_effect = fake_init_session

        # Replace the original mathod with the mock.
        sc._init_session = mock_method

        # We don't have a session in the cache for the user at hand.
        self.assertTrue(sc.__sessions__.get("usr1") is None)

        # The actual method under test is called.
        self.assertEqual(expected_session, sc.get("usr1", ""))

        # The method under test called the mock once and with the parameters
        # we passed.
        self.assertEqual(1, mock_method.call_count)
        (user, passwd), kwargs = mock_method.call_args
        self.assertEqual("usr1", user)
        self.assertEqual("", passwd)
 def test_init_session_with_repeated_initialization(self):
     """
     _init_session() raises an `AssertionError` upon repeated
     initialization.
     """
     sc = SessionCache()
     sc.__sessions__["oq_uiapi_writer"] = object()
     self.assertRaises(AssertionError, SessionCache()._init_session,
                       "oq_uiapi_writer", "")
 def test_init_session_updates_internal_dict(self):
     """
     _init_session() will add newly created sessions to the internal
     `__sessions__` dictionary.
     """
     session = object()
     sc = SessionCache()
     with mock.patch('sqlalchemy.create_engine') as ce_mock:
         with mock.patch('sqlalchemy.orm.sessionmaker') as sm_mock:
             sm_mock.return_value = lambda: session
             self.assertTrue(sc.__sessions__.get("usr8") is None)
             sc._init_session("usr8", "t0ps3cr3t")
             self.assertEqual(session, sc.__sessions__.get("usr8"))
    def test_get_with_cached_session(self):
        """
        get() will not call _init_session() if the session for the
        given database user is in the cache already.
        """
        sc = SessionCache()
        expected_session = object()
        sc.__sessions__["usr2"] = expected_session

        # Prepare mock.
        mock_method = mock.Mock()

        # Replace the original mathod with the mock.
        sc._init_session = mock_method

        # We do have a session in the cache for the user at hand.
        self.assertTrue(sc.__sessions__.get("usr2") is expected_session)

        # The actual method under test is called.
        self.assertTrue(sc.get("usr2", "") is expected_session)

        # The method under test did *not* call the mock.
        self.assertEqual(0, mock_method.call_count)