Пример #1
0
def test_order_desc():
    expected = Query(_stmt='select',
                     _table='test',
                     _order=Order(cols=['created_at', 'updated_at'],
                                  dir='desc'))
    query = Query.select('test').order_desc('created_at', 'updated_at')
    assert query == expected
Пример #2
0
def test_full_outer_join():
    expected = Query(_stmt='select', _table='test')
    expected._joins = [
        Join(_kind='full_outer',
             _table='test2',
             _ons=[On(left='test.id', op='=', right='test2.id')])
    ]
    query = Query.select('test')
    query.full_outer_join('test2').on('test.id', '=', 'test2.id')
    assert query == expected
Пример #3
0
def test_multiple_joins():
    expected = Query(_stmt='select', _table='test')
    expected._joins = [
        Join(_kind='inner',
             _table='test2',
             _ons=[On(left='test.id', op='=', right='test2.tid')]),
        Join(_kind='inner',
             _table='test3',
             _ons=[
                 On(left='test.id', op='=', right='test3.tid'),
                 On(left='test.date', op='=', right='test3.tdate'),
             ]),
    ]
    query = Query.select('test')
    query.join('test2').on('test.id', '=', 'test2.tid')
    query.join('test3').on('test.id', '=',
                           'test3.tid').on('test.date', '=', 'test3.tdate')
    assert query == expected
Пример #4
0
import pytest

from skeptic.query import Query, Where


# where_tests defines a list of tuples for a table-test of the queries where capabilities. All this does is allows us to
# write these tests without redoing all of the boiler plate. Unfortunately, this also means that we have less
# descriptive names for our tests when they do fail. However, to combat that, we pass in an argvalue with a name.
where_tests = [
    (
        'where_eq',
        Query.delete('test').where('email', '*****@*****.**'),
        Query(_stmt='delete', _table='test', _wheres=[Where(col="email", op="=")], _args=['*****@*****.**'])
    ),
    (
        'where_eq_raw',
        Query.delete('test').where_raw('email', '*****@*****.**'),
        Query(_stmt='delete', _table='test', _wheres=[Where(col="email", op="=", val='*****@*****.**')])
    ),
    (
        'where_ne',
        Query.delete('test').where('email', '!=', '*****@*****.**'),
        Query(_stmt='delete', _table='test', _wheres=[Where(col="email", op="!=")], _args=['*****@*****.**'])
    ),
    (
        'where_ne_raw',
        Query.delete('test').where_raw('email', '!=', '*****@*****.**'),
        Query(_stmt='delete', _table='test', _wheres=[Where(col="email", op="!=", val='*****@*****.**')])
    ),
    (
Пример #5
0
import pytest

from skeptic.query import Query, Having

# having_tests defines a list of tuples for a table-test of the queries where capabilities. All this does is allows us
# to write these tests without redoing all of the boiler plate. Unfortunately, this also means that we have less
# descriptive names for our tests when they do fail. However, to combat that, we pass in an argvalue with a name.
having_tests = [
    ('having_eq', Query.select('test').having('email', '*****@*****.**'),
     Query(_stmt='select',
           _table='test',
           _havings=[Having(col="email", op="=")],
           _args=['*****@*****.**'])),
    ('having_eq_raw', Query.select('test').having_raw('email',
                                                      '*****@*****.**'),
     Query(_stmt='select',
           _table='test',
           _havings=[Having(col="email", op="=", val='*****@*****.**')])),
    ('having_ne', Query.select('test').having('email', '!=', '*****@*****.**'),
     Query(_stmt='select',
           _table='test',
           _havings=[Having(col="email", op="!=")],
           _args=['*****@*****.**'])),
    ('having_ne_raw', Query.select('test').having_raw('email', '!=',
                                                      '*****@*****.**'),
     Query(_stmt='select',
           _table='test',
           _havings=[Having(col="email", op="!=", val='*****@*****.**')])),
    ('having_gt', Query.select('test').having('len', '>', 10),
     Query(_stmt='select',
           _table='test',
Пример #6
0
def test_set():
    expected = Query(_stmt='update', _table='test', _sets=[Set(col='username', val=None)], _args=['me'])
    query = Query.update('test').set('username', 'me')
    assert query == expected
Пример #7
0
def test_set_raw_not_update():
    expected = Query(_stmt='select', _table='test')
    query = Query.select('test').set_raw('username', 'me')
    assert query == expected
Пример #8
0
def test_set_raw_multiple():
    expected = Query(_stmt='update', _table='test',
                     _sets=[Set(col='username', val='me'), Set(col='password', val='you')])
    query = Query.update('test').set_raw('username', 'me').set_raw('password', 'you')
    assert query == expected
Пример #9
0
def test_set_raw():
    expected = Query(_stmt='update', _table='test', _sets=[Set(col='username', val='me')])
    query = Query.update('test').set_raw('username', 'me')
    assert query == expected
Пример #10
0
def test_group_by():
    expected = Query(_stmt='select', _table='test', _group_by=['name', 'city'])
    query = Query.select('test').group_by('name', 'city')
    assert query == expected
Пример #11
0
def test_offset():
    expected = Query(_stmt='select', _table='test', _offset=10)
    query = Query.select('test').offset(10)
    assert query == expected
Пример #12
0
def test_limit():
    expected = Query(_stmt='select', _table='test', _limit=10)
    query = Query.select('test').limit(10)
    assert query == expected
Пример #13
0
def test_returning(statement_func, returning, stmt, ret):
    expected = Query(_stmt=stmt, _table='test', _ret=ret)
    query = statement_func('test').returning(*returning)
    assert query == expected
Пример #14
0
def test_columns(input, expected):
    expected = Query(_stmt='select', _table='test', _columns=expected)
    query = Query.select('test').columns(*input)
    assert expected == query
Пример #15
0
columns_tests = [
    (['*'], ['*']),
    (['id', 'email', 'password'], ['id', 'email', 'password']),
]


@pytest.mark.parametrize('input,expected', columns_tests)
def test_columns(input, expected):
    expected = Query(_stmt='select', _table='test', _columns=expected)
    query = Query.select('test').columns(*input)
    assert expected == query


values_tests = [
    (Query.insert('test'),
     Query(_stmt='insert', _table='test', _args=['*****@*****.**', 23])),
    (Query.select('test'), Query(_stmt='select', _table='test')),
    (Query.update('test'), Query(_stmt='update', _table='test')),
    (Query.delete('test'), Query(_stmt='delete', _table='test')),
]


@pytest.mark.parametrize('query,expected', values_tests)
def test_values(query, expected):
    query = query.values('*****@*****.**', 23)
    assert query == expected


returning_tests = [
    (Query.insert, ['id', 'created_at'], 'insert', ['id', 'created_at']),