示例#1
0
def test_query_no_results(query, variable_expression, play):
    import os
    db_path = os.path.join(os.path.abspath(os.path.dirname(__file__)),
                           'database.db')
    database_url = 'sqlite:///{0}'.format(db_path)
    from play_sql import providers
    sql_provider = providers.SQLProvider(play)
    assert sql_provider.engine is play
    sql_provider.command_sql({
        'provider': 'play_sql',
        'type': 'sql',
        'database_url': database_url,
        'variable': 'invoice_id',
        'variable_expression': variable_expression,
        'query': query
    })
    play.variables['invoice_id'] is None
示例#2
0
def test_commands_len(play):
    query1 = 'select * from invoices;'
    import os
    db_path = os.path.join(os.path.abspath(os.path.dirname(__file__)),
                           'database.db')
    database_url = 'sqlite:///{0}'.format(db_path)
    from play_sql import providers
    sql_provider = providers.SQLProvider(play)
    assert sql_provider.engine is play
    sql_provider.command_sql({
        'provider': 'play_sql',
        'type': 'sql',
        'database_url': database_url,
        'variable': 'invoices_len',
        'variable_expression': 'len(results.fetchall())',
        'assertion': '$invoices_len == 4',
        'query': query1
    })
示例#3
0
def test_insert_drop(play, tmpdir):
    query2 = 'select * from contacts;'
    import os
    copy_database = tmpdir.join("copy_database.db")
    db_path2 = os.path.join(os.path.abspath(os.path.dirname(__file__)),
                            'database2.db')
    with open(db_path2, 'rb') as db_path2_file:
        copy_database.write_binary(db_path2_file.read())
    database_url2 = 'sqlite:///{0}'.format(copy_database.strpath)
    from play_sql import providers
    sql_provider = providers.SQLProvider(play)
    assert sql_provider.engine is play
    sql_provider.command_sql({
        'provider': 'play_sql',
        'type': 'sql',
        'database_url': database_url2,
        'variable': 'all_contacts',
        'variable_expression': 'results.fetchall()',
        'assertion': 'len(variables["all_contacts"]) == 1',
        'query': query2
    })
    with pytest.raises(Exception):
        sql_provider.command_sql({
            'provider':
            'play_sql',
            'type':
            'sql',
            'database_url':
            database_url2,
            'query':
            'INSERT INTO contacts VALUES '
            '(2, "John", "Doe", "*****@*****.**", "+01"); '
            'DROP TABLE contacts;'
        })
    sql_provider.command_sql({
        'provider': 'play_sql',
        'type': 'sql',
        'database_url': database_url2,
        'variable': 'all_contacts',
        'variable_expression': 'results.fetchall()',
        'assertion': 'len(variables["all_contacts"]) == 1',
        'query': query2
    })
示例#4
0
def test_multiple_databases(play):
    query1 = 'select * from invoices;'
    query2 = 'select * from contacts;'
    import os
    db_path = os.path.join(os.path.abspath(os.path.dirname(__file__)),
                           'database.db')
    db_path2 = os.path.join(os.path.abspath(os.path.dirname(__file__)),
                            'database2.db')
    database_url = 'sqlite:///{0}'.format(db_path)
    database_url2 = 'sqlite:///{0}'.format(db_path2)
    from play_sql import providers
    sql_provider = providers.SQLProvider(play)
    assert sql_provider.engine is play
    sql_provider.command_sql({
        'provider':
        'play_sql',
        'type':
        'sql',
        'database_url':
        database_url,
        'variable':
        'all_invoices',
        'variable_expression':
        'results.fetchall()',
        'assertion':
        'len(variables["all_invoices"]) == 4 and '
        'variables["all_invoices"][0][1] == "invoice 1"',
        'query':
        query1
    })

    sql_provider.command_sql({
        'provider': 'play_sql',
        'type': 'sql',
        'database_url': database_url2,
        'variable': 'all_contacts',
        'variable_expression': 'results.fetchall()',
        'assertion': 'len(variables["all_contacts"]) == 1',
        'query': query2
    })