Fix PatchStacktraceTest test

After we moved out from the namespaces, tests were moved to
oslo_db/tests. The code in oslo_db.sqlalchemy.session filters out
lines from modules under oslo_db, so the test filename does not
appear in the context for the error message.
Use patched traceback module in tests to fix it.

Closes-Bug: #1405376

Change-Id: Id6022c065dfe13351fca5e54baa343d6c96b0270
This commit is contained in:
Victor Sergeyev 2014-12-25 19:14:11 +02:00 committed by Roman Podoliaka
parent 44c9c574bd
commit 91b0199a4f
2 changed files with 45 additions and 29 deletions
oslo_db/tests
old_import_api/sqlalchemy
sqlalchemy

@ -533,22 +533,31 @@ class MysqlConnectTest(test_base.MySQLOpportunisticTestCase):
log.output)
# NOTE(dhellmann): This test no longer works as written. The code in
# oslo_db.sqlalchemy.session filters out lines from modules under
# oslo_db, and now this test is under oslo_db, so the test filename
# does not appear in the context for the error message. LP #1405376
class PatchStacktraceTest(test_base.DbTestCase):
# class PatchStacktraceTest(test_base.DbTestCase):
def test_trace(self):
engine = self.engine
# def test_trace(self):
# engine = self.engine
# private_session._add_trace_comments(engine)
# conn = engine.connect()
# with mock.patch.object(engine.dialect, "do_execute") as mock_exec:
# NOTE(viktors): The code in oslo_db.sqlalchemy.session filters out
# lines from modules under oslo_db, so we should remove
# "oslo_db/" from file path in traceback.
import traceback
orig_extract_stack = traceback.extract_stack
# conn.execute("select * from table")
def extract_stack():
return [(row[0].replace("oslo_db/", ""), row[1], row[2], row[3])
for row in orig_extract_stack()]
# call = mock_exec.mock_calls[0]
with mock.patch("traceback.extract_stack", side_effect=extract_stack):
# # we're the caller, see that we're in there
# self.assertTrue("tests/sqlalchemy/test_sqlalchemy.py" in call[1][1])
private_session._add_trace_comments(engine)
conn = engine.connect()
with mock.patch.object(engine.dialect, "do_execute") as mock_exec:
conn.execute("select * from table")
call = mock_exec.mock_calls[0]
# we're the caller, see that we're in there
self.assertIn("tests/old_import_api/sqlalchemy/test_sqlalchemy.py",
call[1][1])

@ -633,23 +633,30 @@ class CreateEngineTest(oslo_test.BaseTestCase):
)
# NOTE(dhellmann): This test no longer works as written. The code in
# oslo_db.sqlalchemy.session filters out lines from modules under
# oslo_db, and now this test is under oslo_db, so the test filename
# does not appear in the context for the error message. LP #1405376
class PatchStacktraceTest(test_base.DbTestCase):
# class PatchStacktraceTest(test_base.DbTestCase):
def test_trace(self):
engine = self.engine
# def test_trace(self):
# engine = self.engine
# session._add_trace_comments(engine)
# conn = engine.connect()
# with mock.patch.object(engine.dialect, "do_execute") as mock_exec:
# NOTE(viktors): The code in oslo_db.sqlalchemy.session filters out
# lines from modules under oslo_db, so we should remove
# "oslo_db/" from file path in traceback.
import traceback
orig_extract_stack = traceback.extract_stack
# conn.execute("select * from table")
def extract_stack():
return [(row[0].replace("oslo_db/", ""), row[1], row[2], row[3])
for row in orig_extract_stack()]
# call = mock_exec.mock_calls[0]
with mock.patch("traceback.extract_stack", side_effect=extract_stack):
# # we're the caller, see that we're in there
# self.assertIn("oslo_db/tests/sqlalchemy/test_sqlalchemy.py",
# call[1][1])
session._add_trace_comments(engine)
conn = engine.connect()
with mock.patch.object(engine.dialect, "do_execute") as mock_exec:
conn.execute("select * from table")
call = mock_exec.mock_calls[0]
# we're the caller, see that we're in there
self.assertIn("tests/sqlalchemy/test_sqlalchemy.py", call[1][1])