Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Unexpected behaviour of BlockingClient as a context manager #21

Closed
peterbe opened this Issue Jul 5, 2012 · 2 comments

Comments

Projects
None yet
2 participants
Contributor

peterbe commented Jul 5, 2012

For my unit tests I have another db that I use to stuff in fixture data. Unlike the app, I want this one to be blocking since I don't want additional hassle as I'm setting up the test. Here's what I tried:

#tests/base.py
    @property
    def db(self):
        # Create a database connection when a request handler is called
        # and store the connection in the application object.
        if not hasattr(self._app, 'db'):
            from socorro.unittest.config.commonconfig import (
              databaseHost, databaseName, databaseUserName, databasePassword)

            self._app.db = momoko.BlockingClient({
                'host': databaseHost.default,
                'database': databaseName.default,
                'user': databaseUserName.default,
                'password': databasePassword.default,
                'min_conn': 1,
                'max_conn': 20,
                'cleanup_timeout': 10
            })
        return self._app.db
#sometest.py
    def setUp(self):
        ...
        with self.db.connection() as conn:
            cursor = conn.cursor()
            cursor.execute("""
                INSERT INTO users ....

But unfortunately I'm getting this:

======================================================================
ERROR: test_get_no_data (tests.test_handlers.CrashesCommentsTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/peterb.../middleware2/tests/test_handlers.py", line 20, in setUp
    with self.db.connection() as conn:
TypeError: 'GeneratorContextManager' object is not callable
Contributor

peterbe commented Jul 5, 2012

I guess it's just a documentation issue.

with self.db.connection as conn:
     ....

works like a charm.

I also changed my code so that it's not using the same connection in the fixture set up as the actual handler.

Owner

FSX commented Jul 5, 2012

Ok. I'll leave this ticket open as a reminder to add documentation about making tests.

@FSX FSX closed this Jul 5, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment