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_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)