Пример #1
0
        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'"
Пример #2
0
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()
Пример #3
0
def test_shell_tracer_success():
    output = StringIO()

    with ShellTracer(file=output, fancy=False):
        store.execute("SELECT 1")

    assert "SUCCESS" in output.getvalue()
    output.close()
Пример #4
0
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 !"
Пример #5
0
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()
Пример #6
0
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]
Пример #7
0
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()
Пример #8
0
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'"
Пример #9
0
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)