def other_request(): with app.app_context(): store.execute("SELECT 'other'") # Ensure main thread does not leak into this one queries = get_debug_queries() assert len(queries) == 1 assert queries[0].statement == "SELECT 'other'"
def test_shell_tracer_failure(): output = StringIO() with ShellTracer(file=output, fancy=False), pytest.raises(Exception): store.execute("SELECT !") assert "FAILURE" in output.getvalue() output.close()
def test_shell_tracer_success(): output = StringIO() with ShellTracer(file=output, fancy=False): store.execute("SELECT 1") assert "SUCCESS" in output.getvalue() output.close()
def test_query_error(): try: with DebugTracer(): store.execute("SELECT !") except Exception: pass queries = get_debug_queries() assert len(queries) == 1 assert queries[0].statement == "SELECT !"
def test_query_without_context(app): fs = FlaskStorm(app) try: store = fs.connect() with DebugTracer(): store.execute("SELECT 1") # Since no application context is available queries are not saved # anywhere assert get_debug_queries() == [] finally: store.close()
def test_get_debug_queries(): with DebugTracer(): store.execute("SELECT 1") store.execute("SELECT ? + ?", [1, 2]) queries = get_debug_queries() assert len(queries) == 2 first, second = queries assert first.statement == "SELECT 1" assert first.params == () assert isinstance(first.start_time, datetime) assert isinstance(first.end_time, datetime) assert isinstance(first.duration, timedelta) assert second.statement == "SELECT ? + ?" assert second.params == [1, 2]
def initdb(): """Create schema and fill database with 15 sample posts by random authors""" store.execute("""DROP TABLE IF EXISTS posts""") store.execute(""" CREATE TABLE posts( id INTEGER PRIMARY KEY, name VARCHAR, text VARCHAR ) """) names = [ u"Alice", u"Bob", u"Eve", ] for i in range(1, 16): store.add(Post(choice(names), u"Post #{}".format(i))) store.commit()
def test_tracer_thread_isolation(app, app_context): with DebugTracer(): store.execute("SELECT 'this'") # Spawn a separate thread that also executes a query def other_request(): with app.app_context(): store.execute("SELECT 'other'") # Ensure main thread does not leak into this one queries = get_debug_queries() assert len(queries) == 1 assert queries[0].statement == "SELECT 'other'" t = Thread(target=other_request) t.start() t.join() # Ensure query log was not polluted by other thread queries = get_debug_queries() assert len(queries) == 1 assert queries[0].statement == "SELECT 'this'"
def test_shell_tracer(): # Alias helper functions remove_whitespace = pytest.helpers.remove_whitespace remove_ansi = pytest.helpers.remove_ansi sql = "SELECT 1 + 1 FROM (SELECT 2 + 2)" output = StringIO() output.isatty = MagicMock(return_value=True) with patch("sys.platform", "linux2"), ShellTracer(file=output, fancy=True): store.execute(sql) color_output = output.getvalue() output.close() assert color_output != sql assert \ remove_whitespace(remove_ansi(color_output)).rsplit(";", 1)[0] == \ remove_whitespace(sql) output = StringIO() output.isatty = MagicMock(return_value=False) with ShellTracer(file=output, fancy=False): store.execute(sql) colorless_output = output.getvalue() output.close() assert color_output != colorless_output assert \ remove_ansi(color_output).rsplit(";", 1)[0] == \ colorless_output.rsplit(";", 1)[0] assert \ remove_whitespace(remove_ansi(color_output)).rsplit(";", 1)[0] == \ remove_whitespace(sql)