Issue #1494
closedThe streamer unit tests need to set up a database connection
Description
Unit test suites need to set up their own database connection, and it seems that the streamer tests do not do this:
$ nosetests -sx streamer/test/unit/streamer/test_server.py
...............E
======================================================================
ERROR: Assert a deferred download entry is made.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/fedora/.virtualenvs/pulp/lib/python2.7/site-packages/mock.py", line 1193, in patched
arg = patching.__enter__()
File "/home/fedora/.virtualenvs/pulp/lib/python2.7/site-packages/mock.py", line 1349, in __enter__
_name=self.attribute, **kwargs)
File "/home/fedora/.virtualenvs/pulp/lib/python2.7/site-packages/mock.py", line 2200, in create_autospec
_name='()', _parent=mock)
File "/home/fedora/.virtualenvs/pulp/lib/python2.7/site-packages/mock.py", line 2221, in create_autospec
original = getattr(spec, entry)
File "/usr/lib/python2.7/site-packages/mongoengine/queryset/manager.py", line 37, in __get__
queryset = queryset_class(owner, owner._get_collection())
File "/usr/lib/python2.7/site-packages/mongoengine/document.py", line 177, in _get_collection
db = cls._get_db()
File "/usr/lib/python2.7/site-packages/mongoengine/document.py", line 170, in _get_db
return get_db(cls._meta.get("db_alias", DEFAULT_CONNECTION_NAME))
File "/usr/lib/python2.7/site-packages/mongoengine/connection.py", line 145, in get_db
conn = get_connection(alias)
File "/usr/lib/python2.7/site-packages/mongoengine/connection.py", line 101, in get_connection
raise ConnectionError(msg)
ConnectionError: You have not defined a default connection
Here is an example of how this is done for pulp.server tests:
$ cat server/test/unit/__init__.py
from pulp.devel.unit.server import base as devel_base
def setup():
"""
Set up the database connection for the tests to use.
"""
devel_base.start_database_connection()
def teardown():
"""
Drop the test database.
"""
devel_base.drop_database()
Without this, the streamer tests cannot be run on their own.
Updated by mhrivnak about 7 years ago
- Status changed from NEW to ASSIGNED
- Assignee set to jcline@redhat.com
- Triaged changed from No to Yes
- Tags Easy Fix added
Updated by bmbouter about 7 years ago
Consider two things:
1) Unit tests shouldn't need the DB at all. Unit tests should be able to be run without mongodb running. Many unit tests require the db so this is nothing new, but adding in a DB connection would take us further away from this goal.
2) There is guard logic[0] that prevents two connections from being called twice so if you add this here, when you run the whole test suite then that will break.
I also find not being able to run a specific test annoying.
Added by Jeremy Cline about 7 years ago
Added by Jeremy Cline about 7 years ago
Do not use autospec with the mock MongoEngine objects
This was causing code to execute that required a database connection, but the tests themselves never used the database.
closes #1494
Updated by jcline@redhat.com about 7 years ago
- Status changed from ASSIGNED to POST
Updated by Anonymous about 7 years ago
- Status changed from POST to MODIFIED
- % Done changed from 0 to 100
Applied in changeset pulp|77235b586bd812cef9869cb1a6072baf56fea86c.
Updated by semyers almost 7 years ago
- Status changed from MODIFIED to CLOSED - CURRENTRELEASE
- Platform Release set to 2.8.1
This was released in 2.8.1.
Do not use autospec with the mock MongoEngine objects
This was causing code to execute that required a database connection, but the tests themselves never used the database.
closes #1494